What is the best practice for dealing with passwor

2019-01-03 04:10发布

I've got a little Bash script that I use to access twitter and pop up a Growl notification in certain situations. Whats the best way to handle storing my password with the script?

I would like to commit this script to the git repo and make it available on GitHub, but I'm wondering what the best way to keep my login/password private while doing this is. Currently the password is stored in the script itself. I can't remove it right before I push because all the old commits will contain the password. Developing without the password isn't an option. I imagine that I should be storing the password in an external config file, but I thought I'd check to see if there was an established way to handle this before I tried and put something together.

6条回答
甜甜的少女心
2楼-- · 2019-01-03 04:38

Here is a technique I use:

I create a folder in my home folder called: .config

In that folder I place the config files for any number of things that I want to externalize passwords and keys.

I typically use reverse domain name syntax such as:

com.example.databaseconfig

Then in the bash script I do this:

#!/bin/bash
source $HOME/.config/com.example.databaseconfig ||exit 1

The || exit 1 causes the script to exit if it is not able to load the config file.

I used that technique for bash, python, and ant scripts.

I am pretty paranoid and don't think that a .gitignore file is sufficiently robust to prevent an inadvertent check-in. Plus, there is nothing monitoring it, so if a check-in did happen no one would find out to deal with it.

If a particular application requires more than one file I create subfolder rather than a single file.

查看更多
Root(大扎)
3楼-- · 2019-01-03 04:42

An approach can be to set password (or API key) using an environment variable. So this password is out of revision control.

With Bash, you can set environment variable using

export YOUR_ENV_VARIABLE=your_password

This approach can be use with continuous integration services like Travis, your code (without password) being stored in a GitHub repository can be executed by Travis (with your password being set using environment variable).

With Bash, you can get value of an environment variable using:

echo $YOUR_ENV_VARIABLE

With Python, you can get value of an environment variable using:

import os
print os.environ['YOUR_ENV_VARIABLE']

PS: be aware that it's probably a bit risky (but it'ss a quite common practice) https://www.bleepingcomputer.com/news/security/javascript-packages-caught-stealing-environment-variables/

PS2: this article titled "How to securely store API keys" https://dev.to/bpedro/how-to-securely-store-api-keys-ab6 may be interesting to read

查看更多
来,给爷笑一个
4楼-- · 2019-01-03 04:45

The typical way to do this is to read the password info from a configuration file. If your configuration file is called foobar.config, then you would commit a file called foobar.config.example to the repository, containing sample data. To run your program, you would create a local (not tracked) file called foobar.config with your real password data.

To filter out your existing password from previous commits, see the GitHub help page on Removing sensitive data.

查看更多
Viruses.
5楼-- · 2019-01-03 04:51

What Greg said but I'd add that it's a good idea to check in a file foobar.config-TEMPLATE.

It should contain example names, passwords or other config info. Then it is very obvious what the real foobar.config should contain, without having to look in all the code for which values must be present in foobar.config and what format they should have.

Often config values can be non obvious, like database connection strings and similar things.

查看更多
放荡不羁爱自由
6楼-- · 2019-01-03 05:01

If you're using ruby on rails, the Figaro gem is very good, easy, and reliable. It has a low headache factor with the production environment too.

查看更多
▲ chillily
7楼-- · 2019-01-03 05:02

One can use Vault which secures, stores, and controls access to tokens, passwords, certificates, API keys, etc. For example Ansible uses the Ansible Vault which deals with passwords or certificates used in playbooks

查看更多
登录 后发表回答