项目作者: alexandre-normand

项目描述 :
Command-line tool integrating with go:generate to replace substitute tokens with ENV variables value.
高级语言: Go
项目地址: git://github.com/alexandre-normand/safekeeper.git
创建时间: 2015-02-21T04:43:31Z
项目社区:https://github.com/alexandre-normand/safekeeper

开源协议:MIT License

下载


safekeeper

safekeeper is a command-line tool meant to help maintain your secrets (passwords, client ids/secrets) safe from your source.

It does a single thing and this is replacing all occurences of keys by the value mapped to the corresponding environment variable.

It works with go generate to replace tokens in your source code.

Why You Might Use safekeeper

First, if you have a better solution, don’t!

If you can directly use env variables, that might be an easier and simpler solution. Unfortunately, Google App Engine won’t allow this. An alternative for GAE might be to use the datastore to keep your secrets but that’s sometimes cumbersome.

I use it personally because:

  1. I’m running on the Google App Engine which doesn’t allow the use of env variables.
  2. I have many app secrets that would start to be cumbersome to manage via the datastore.
  3. I like a little more formal dev/contribution workflow than having to require contributors to create a file with a set of constants.

Example

Let’s have all of our application secrets in one place. Let’s put this in a secrets package. We’ll need a <yourname>.go.safekeeper file that will serve as the template for the generated go code as well as a matching <yourname>.go file with the go:generate directive.

secrets.go:

  1. package secrets
  2. //go:generate safekeeper --output=appsecrets.go --keys=CLIENT_ID,CLIENT_SECRET $GOFILE

secrets.go.safekeeper:

  1. package secrets
  2. // AppSecrets is the source for all application secrets (client ids/secrets/passwords)
  3. type AppSecrets struct {
  4. ClientId string
  5. ClientSecret string
  6. }
  7. // NewAppSecrets returns the AppSecrets with all values set
  8. func NewAppSecrets() *AppSecrets {
  9. appSecrets := new(AppSecrets)
  10. appSecrets.ClientId = "ENV_CLIENT_ID"
  11. appSecrets.ClientSecret = "ENV_CLIENT_SECRET"
  12. return appSecrets
  13. }

Since you’d want to avoid committing the generate source with the resolved secrets, you’ll want to have the generated file be in your .gitignore. We achieve this here by generating the output to a 3rd file called appsecrets.go (using the --output flag) in the same package.

Once ready, generate the resolved appsecrets by running
go generate <path.to.secrets.package>

I’m currently using this in glukit so have a look there for an example of actual integration.

LICENSE

Licensed under MIT license.