Skip to content

Commit

Permalink
Use map[string]string instead of []string internally
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanevet committed Jan 7, 2021
1 parent 45b2dbc commit 1b24840
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
40 changes: 25 additions & 15 deletions internal/command/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ func runAction(ac *ActionConfig) error {
return err
}

var env []string
env := make(map[string]string)
tempFactory := NewTempFactory("")
defer tempFactory.Cleanup()

type Result struct {
string
key string
value string
error
}

Expand All @@ -128,7 +129,7 @@ func runAction(ac *ActionConfig) error {
if spec.IsVar() {
value, err = prov.Call(ac.Provider, spec.Path)
if err != nil {
results <- Result{key, err}
results <- Result{key, "", err}
wg.Done()
return
}
Expand All @@ -142,8 +143,8 @@ func runAction(ac *ActionConfig) error {
value = spec.DefaultValue
}

envvar := formatForEnv(key, value, spec, &tempFactory)
results <- Result{envvar, nil}
k, v := formatForEnv(key, value, spec, &tempFactory)
results <- Result{k, v, nil}
wg.Done()
}(key, spec)
}
Expand All @@ -153,44 +154,53 @@ func runAction(ac *ActionConfig) error {
EnvLoop:
for envvar := range results {
if envvar.error == nil {
env = append(env, envvar.string)
env[envvar.key] = envvar.value
} else {
if ac.IgnoreAll {
continue EnvLoop
}

for i := range ac.Ignores {
if ac.Ignores[i] == envvar.string {
if ac.Ignores[i] == fmt.Sprintf("%s=%s", envvar.key, envvar.value) {
continue EnvLoop
}
}
return fmt.Errorf("Error fetching variable %v: %v", envvar.string, envvar.error.Error())
return fmt.Errorf("Error fetching variable %v: %v", envvar.key, envvar.error.Error())
}
}

// Append environment variable if one is specified
if ac.Environment != "" {
env = append(env, fmt.Sprintf("%s=%s", SUMMON_ENV_KEY_NAME, ac.Environment))
env[SUMMON_ENV_KEY_NAME] = ac.Environment
}

setupEnvFile(ac.Args, env, &tempFactory)

return runSubcommand(ac.Args, append(os.Environ(), env...))
var e []string
for k, v := range env {
e = append(e, fmt.Sprintf("%s=%s", k, v))
}

return runSubcommand(ac.Args, append(os.Environ(), e...))
}

// formatForEnv returns a string in %k=%v format, where %k=namespace of the secret and
// %v=the secret value or path to a temporary file containing the secret
func formatForEnv(key string, value string, spec secretsyml.SecretSpec, tempFactory *TempFactory) string {
func formatForEnv(key string, value string, spec secretsyml.SecretSpec, tempFactory *TempFactory) (string, string) {
if spec.IsFile() {
fname := tempFactory.Push(value)
value = fname
}

return fmt.Sprintf("%s=%s", key, value)
return key, value
}

func joinEnv(env []string) string {
return strings.Join(env, "\n") + "\n"
func joinEnv(env map[string]string) string {
var envs []string
for k, v := range env {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(envs, "\n") + "\n"
}

// findInParentTree recursively searches for secretsFile starting at leafDir and in the
Expand Down Expand Up @@ -234,7 +244,7 @@ func findInParentTree(secretsFile string, leafDir string) (string, error) {
// creates a tempfile to which all the environment mappings are dumped
// and replaces the magic string with its path.
// Returns the path if so, returns an empty string otherwise.
func setupEnvFile(args []string, env []string, tempFactory *TempFactory) string {
func setupEnvFile(args []string, env map[string]string, tempFactory *TempFactory) string {
var envFile = ""

for i, arg := range args {
Expand Down
15 changes: 6 additions & 9 deletions internal/command/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -42,14 +41,15 @@ func TestFormatForEnvString(t *testing.T) {
Path: "mysql1/password",
Tags: []secretsyml.YamlTag{secretsyml.Var},
}
envvar := formatForEnv(
k, v := formatForEnv(
"dbpass",
"mysecretvalue",
spec,
nil,
)

So(envvar, ShouldEqual, "dbpass=mysecretvalue")
So(k, ShouldEqual, "dbpass")
So(v, ShouldEqual, "mysecretvalue")
})
Convey("For files, VALUE should be the path to a tempfile containing the secret", func() {
tempFactory := NewTempFactory("")
Expand All @@ -59,16 +59,13 @@ func TestFormatForEnvString(t *testing.T) {
Path: "certs/webtier1/private-cert",
Tags: []secretsyml.YamlTag{secretsyml.File},
}
envvar := formatForEnv(
key, path := formatForEnv(
"SSL_CERT",
"mysecretvalue",
spec,
&tempFactory,
)

s := strings.Split(envvar, "=")
key, path := s[0], s[1]

So(key, ShouldEqual, "SSL_CERT")

// Temp path should exist
Expand All @@ -84,8 +81,8 @@ func TestFormatForEnvString(t *testing.T) {

func TestJoinEnv(t *testing.T) {
Convey("adds a trailing newline", t, func() {
result := joinEnv([]string{"foo", "bar"})
So(result, ShouldEqual, "foo\nbar\n")
result := joinEnv(map[string]string{"foo": "bar", "baz": "qux"})
So(result, ShouldEqual, "foo=bar\nbaz=qux\n")
})
}

Expand Down

0 comments on commit 1b24840

Please sign in to comment.