Skip to content

Commit

Permalink
Merge pull request #11 from segmentio/clarify-conflicting-secrets
Browse files Browse the repository at this point in the history
Clarify conflicting secrets
  • Loading branch information
dfuentes committed Jul 17, 2017
2 parents f517e4f + d4a3418 commit 2ec2115
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ The history command gives a historical view of a given secret. This view is usef
$ chamber exec <service...> -- <your executable>
```

The purpose of this command is to be used inside your service's docker container. Exec sets up environment variables populated with all the latest versions of secrets for the given service and environment.
`exec` populates the environment with the secrets from the specified services and executes the given command. Secret keys are converted to upper case (for example a secret with key `secret_key` will become `SECRET_KEY`).

Secrets from services are loaded in the order specified in the command. For example, if you do `chamber exec app apptwo -- ...` and both apps have a secret named `api_key`, the `api_key` from `apptwo` will be the one set in your environment.

## Releasing

Expand Down
18 changes: 17 additions & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -61,7 +62,12 @@ func execRun(cmd *cobra.Command, args []string) error {
return err
}
for _, secret := range secrets {
env.Set(strings.ToUpper(key(secret.Meta.Key)), *secret.Value)
envVarKey := strings.ToUpper(key(secret.Meta.Key))

if env.IsSet(envVarKey) {
fmt.Fprintf(os.Stderr, "warning: overwriting environment variable %s\n", envVarKey)
}
env.Set(envVarKey, *secret.Value)
}
}

Expand Down Expand Up @@ -109,6 +115,16 @@ func (e *environ) Unset(key string) {
}
}

// IsSet returns whether or not a key is currently set in the environ
func (e *environ) IsSet(key string) bool {
for i := range *e {
if strings.HasPrefix((*e)[i], key+"=") {
return true
}
}
return false
}

// Set adds an environment variable, replacing any existing ones of the same key
func (e *environ) Set(key, val string) {
e.Unset(key)
Expand Down

0 comments on commit 2ec2115

Please sign in to comment.