How can I save my secret keys and password securely in my version control system?

前端 未结 17 1702
生来不讨喜
生来不讨喜 2020-11-29 14:49

I keep important settings like the hostnames and ports of development and production servers in my version control system. But I know that it\'s bad practice to kee

相关标签:
17条回答
  • 2020-11-29 15:15

    Usually, i seperate password as a config file. and make them dist.

    /yourapp
        main.py
        default.cfg.dist
    

    And when i run main.py, put the real password in default.cfg that copied.

    ps. when you work with git or hg. you can ignore *.cfg files to make .gitignore or .hgignore

    0 讨论(0)
  • 2020-11-29 15:16

    The cleanest way in my opinion is to use environment variables. You won't have to deal with .dist files for example, and the project state on the production environment would be the same as your local machine's.

    I recommend reading The Twelve-Factor App's config chapter, the others too if you're interested.

    0 讨论(0)
  • 2020-11-29 15:16

    If you need VCS for your secrets you should at least keep them in a second repository seperated from you actual code. So you can give your team members access to the source code repository and they won't see your credentials. Furthermore host this repository somewhere else (eg. on your own server with an encrypted filesystem, not on github) and for checking it out to the production system you could use something like git-submodule.

    0 讨论(0)
  • 2020-11-29 15:17

    This is what I do:

    • Keep all secrets as env vars in $HOME/.secrets (go-r perms) that $HOME/.bashrc sources (this way if you open .bashrc in front of someone, they won't see the secrets)
    • Configuration files are stored in VCS as templates, such as config.properties stored as config.properties.tmpl
    • The template files contain a placeholder for the secret, such as:

      my.password=##MY_PASSWORD##

    • On application deployment, script is ran that transforms the template file into the target file, replacing placeholders with values of environment variables, such as changing ##MY_PASSWORD## to the value of $MY_PASSWORD.

    0 讨论(0)
  • 2020-11-29 15:18

    BlackBox was recently released by StackExchange and while I have yet to use it, it seems to exactly address the problems and support the features requested in this question.

    From the description on https://github.com/StackExchange/blackbox:

    Safely store secrets in a VCS repo (i.e. Git or Mercurial). These commands make it easy for you to GPG encrypt specific files in a repo so they are "encrypted at rest" in your repository. However, the scripts make it easy to decrypt them when you need to view or edit them, and decrypt them for for use in production.

    0 讨论(0)
  • 2020-11-29 15:20

    EDIT: I assume you want to keep track of your previous passwords versions - say, for a script that would prevent password reusing etc.

    I think GnuPG is the best way to go - it's already used in one git-related project (git-annex) to encrypt repository contents stored on cloud services. GnuPG (gnu pgp) provides a very strong key-based encryption.

    1. You keep a key on your local machine.
    2. You add 'mypassword' to ignored files.
    3. On pre-commit hook you encrypt the mypassword file into the mypassword.gpg file tracked by git and add it to the commit.
    4. On post-merge hook you just decrypt mypassword.gpg into mypassword.

    Now if your 'mypassword' file did not change then encrypting it will result with same ciphertext and it won't be added to the index (no redundancy). Slightest modification of mypassword results in radically different ciphertext and mypassword.gpg in staging area differs a lot from the one in repository, thus will be added to the commit. Even if the attacker gets a hold of your gpg key he still needs to bruteforce the password. If the attacker gets an access to remote repository with ciphertext he can compare a bunch of ciphertexts, but their number won't be sufficient to give him any non-negligible advantage.

    Later on you can use .gitattributes to provide an on-the-fly decryption for quit git diff of your password.

    Also you can have separate keys for different types of passwords etc.

    0 讨论(0)
提交回复
热议问题