Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use map[string]string instead of []string internally #188

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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