Skip to content

Commit 7bb3056

Browse files
committed
Quote only values in SUMMONENVFILE
1 parent 6145b9f commit 7bb3056

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

internal/command/action.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/exec"
88
"path/filepath"
9+
"strconv"
910
"strings"
1011
"sync"
1112
"syscall"
@@ -108,12 +109,13 @@ func runAction(ac *ActionConfig) error {
108109
return err
109110
}
110111

111-
var env []string
112+
env := make(map[string]string)
112113
tempFactory := NewTempFactory("")
113114
defer tempFactory.Cleanup()
114115

115116
type Result struct {
116-
string
117+
key string
118+
value string
117119
error
118120
}
119121

@@ -128,7 +130,7 @@ func runAction(ac *ActionConfig) error {
128130
if spec.IsVar() {
129131
value, err = prov.Call(ac.Provider, spec.Path)
130132
if err != nil {
131-
results <- Result{key, err}
133+
results <- Result{key, "", err}
132134
wg.Done()
133135
return
134136
}
@@ -142,8 +144,8 @@ func runAction(ac *ActionConfig) error {
142144
value = spec.DefaultValue
143145
}
144146

145-
envvar := formatForEnv(key, value, spec, &tempFactory)
146-
results <- Result{envvar, nil}
147+
k, v := formatForEnv(key, value, spec, &tempFactory)
148+
results <- Result{k, v, nil}
147149
wg.Done()
148150
}(key, spec)
149151
}
@@ -153,44 +155,54 @@ func runAction(ac *ActionConfig) error {
153155
EnvLoop:
154156
for envvar := range results {
155157
if envvar.error == nil {
156-
env = append(env, envvar.string)
158+
env[envvar.key] = envvar.value
157159
} else {
158160
if ac.IgnoreAll {
159161
continue EnvLoop
160162
}
161163

162164
for i := range ac.Ignores {
163-
if ac.Ignores[i] == envvar.string {
165+
if ac.Ignores[i] == fmt.Sprintf("%s=%s", envvar.key, envvar.value) {
164166
continue EnvLoop
165167
}
166168
}
167-
return fmt.Errorf("Error fetching variable %v: %v", envvar.string, envvar.error.Error())
169+
return fmt.Errorf("Error fetching variable %v: %v", envvar.key, envvar.error.Error())
168170
}
169171
}
170172

171173
// Append environment variable if one is specified
172174
if ac.Environment != "" {
173-
env = append(env, fmt.Sprintf("%s=%s", SUMMON_ENV_KEY_NAME, ac.Environment))
175+
env[SUMMON_ENV_KEY_NAME] = ac.Environment
174176
}
175177

176178
setupEnvFile(ac.Args, env, &tempFactory)
177179

178-
return runSubcommand(ac.Args, append(os.Environ(), env...))
180+
var e []string
181+
for k, v := range env {
182+
e = append(e, fmt.Sprintf("%s=%s", k, v))
183+
}
184+
185+
return runSubcommand(ac.Args, append(os.Environ(), e...))
179186
}
180187

181188
// formatForEnv returns a string in %k=%v format, where %k=namespace of the secret and
182189
// %v=the secret value or path to a temporary file containing the secret
183-
func formatForEnv(key string, value string, spec secretsyml.SecretSpec, tempFactory *TempFactory) string {
190+
func formatForEnv(key string, value string, spec secretsyml.SecretSpec, tempFactory *TempFactory) (string, string) {
184191
if spec.IsFile() {
185192
fname := tempFactory.Push(value)
186193
value = fname
187194
}
188195

189-
return fmt.Sprintf("%s=%s", key, value)
196+
return key, value
190197
}
191198

192-
func joinEnv(env []string) string {
193-
return strings.Join(env, "\n") + "\n"
199+
func joinEnv(env map[string]string) string {
200+
var envs []string
201+
for k, v := range env {
202+
keyEq := []byte(fmt.Sprintf("%s=", k))
203+
envs = append(envs, string(strconv.AppendQuote(keyEq, v)))
204+
}
205+
return strings.Join(envs, "\n") + "\n"
194206
}
195207

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

240252
for i, arg := range args {

0 commit comments

Comments
 (0)