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

前端 未结 17 1701
生来不讨喜
生来不讨喜 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:22

    Since asking this question I have settled on a solution, which I use when developing small application with a small team of people.

    git-crypt

    git-crypt uses GPG to transparently encrypt files when their names match certain patterns. For intance, if you add to your .gitattributes file...

    *.secret.* filter=git-crypt diff=git-crypt
    

    ...then a file like config.secret.json will always be pushed to remote repos with encryption, but remain unencrypted on your local file system.

    If I want to add a new GPG key (a person) to your repo which can decrypt the protected files then run git-crypt add-gpg-user <gpg_user_key>. This creates a new commit. The new user will be able to decrypt subsequent commits.

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

    An option would be to put project-bound credentials into an encrypted container (TrueCrypt or Keepass) and push it.

    Update as answer from my comment below:

    Interesting question btw. I just found this: github.com/shadowhand/git-encrypt which looks very promising for automatic encryption

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

    Another approach could be to completely avoid saving secrets in version control systems and instead use a tool like vault from hashicorp, a secret storage with key rolling and auditing, with an API and embedded encryption.

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

    Heroku pushes the use of environment variables for settings and secret keys:

    The traditional approach for handling such config vars is to put them under source - in a properties file of some sort. This is an error-prone process, and is especially complicated for open source apps which often have to maintain separate (and private) branches with app-specific configurations.

    A better solution is to use environment variables, and keep the keys out of the code. On a traditional host or working locally you can set environment vars in your bashrc. On Heroku, you use config vars.

    With Foreman and .env files Heroku provide an enviable toolchain to export, import and synchronise environment variables.


    Personally, I believe it's wrong to save secret keys alongside code. It's fundamentally inconsistent with source control, because the keys are for services extrinsic to the the code. The one boon would be that a developer can clone HEAD and run the application without any setup. However, suppose a developer checks out a historic revision of the code. Their copy will include last year's database password, so the application will fail against today's database.

    With the Heroku method above, a developer can checkout last year's app, configure it with today's keys, and run it successfully against today's database.

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

    Provide a way to override the config

    This is the best way to manage a set of sane defaults for the config you checkin without requiring the config be complete, or contain things like hostnames and credentials. There are a few ways to override default configs.

    Environment variables (as others have already mentioned) are one way of doing it.

    The best way is to look for an external config file that overrides the default config values. This allows you to manage the external configs via a configuration management system like Chef, Puppet or Cfengine. Configuration management is the standard answer for the management of configs separate from the codebase so you don't have to do a release to update the config on a single host or a group of hosts.

    FYI: Encrypting creds is not always a best practice, especially in a place with limited resources. It may be the case that encrypting creds will gain you no additional risk mitigation and simply add an unnecessary layer of complexity. Make sure you do the proper analysis before making a decision.

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