Skip to content

Commit

Permalink
Print env var keys on exec with global -v flag
Browse files Browse the repository at this point in the history
It's useful to know the exact keys that are picked up by Chamber as that
information is not otherwise available anywhere else. A global `verbose`
flag is added, and can be used by other commands to output useful
information.

Care must be taken not to expose any secret values, as `-v` flag may be
used in production to help trace origin of env vars.
  • Loading branch information
YarekTyshchenko committed Sep 10, 2018
1 parent 29a1f2b commit c9f8f68
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func execRun(cmd *cobra.Command, args []string) error {

env := environ(os.Environ())
secretStore := getSecretStore()
envVarKeys := make([]string, 0)
for _, service := range services {
if err := validateService(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
Expand All @@ -52,13 +53,19 @@ func execRun(cmd *cobra.Command, args []string) error {
envVarKey := strings.ToUpper(key(rawSecret.Key))
envVarKey = strings.Replace(envVarKey, "-", "_", -1)

envVarKeys = append(envVarKeys, envVarKey)

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

if verbose {
fmt.Fprintf(os.Stdout, "info: With environment %s\n", strings.Join(envVarKeys, ","))
}

return exec(command, commandArgs, env)
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
validKeyFormat = regexp.MustCompile(`^[A-Za-z0-9-_]+$`)
validServiceFormat = regexp.MustCompile(`^[A-Za-z0-9-_]+$`)

verbose bool
numRetries int
chamberVersion string
)
Expand Down Expand Up @@ -45,6 +46,7 @@ var RootCmd = &cobra.Command{

func init() {
RootCmd.PersistentFlags().IntVarP(&numRetries, "retries", "r", DefaultNumRetries, "For SSM, the number of retries we'll make before giving up")
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print more information to STDOUT")
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand Down

0 comments on commit c9f8f68

Please sign in to comment.