6
6
"os"
7
7
"os/exec"
8
8
"path/filepath"
9
+ "strconv"
9
10
"strings"
10
11
"sync"
11
12
"syscall"
@@ -108,12 +109,13 @@ func runAction(ac *ActionConfig) error {
108
109
return err
109
110
}
110
111
111
- var env [ ]string
112
+ env := make ( map [ string ]string )
112
113
tempFactory := NewTempFactory ("" )
113
114
defer tempFactory .Cleanup ()
114
115
115
116
type Result struct {
116
- string
117
+ key string
118
+ value string
117
119
error
118
120
}
119
121
@@ -128,7 +130,7 @@ func runAction(ac *ActionConfig) error {
128
130
if spec .IsVar () {
129
131
value , err = prov .Call (ac .Provider , spec .Path )
130
132
if err != nil {
131
- results <- Result {key , err }
133
+ results <- Result {key , "" , err }
132
134
wg .Done ()
133
135
return
134
136
}
@@ -142,8 +144,8 @@ func runAction(ac *ActionConfig) error {
142
144
value = spec .DefaultValue
143
145
}
144
146
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 }
147
149
wg .Done ()
148
150
}(key , spec )
149
151
}
@@ -153,44 +155,54 @@ func runAction(ac *ActionConfig) error {
153
155
EnvLoop:
154
156
for envvar := range results {
155
157
if envvar .error == nil {
156
- env = append ( env , envvar .string )
158
+ env [ envvar . key ] = envvar .value
157
159
} else {
158
160
if ac .IgnoreAll {
159
161
continue EnvLoop
160
162
}
161
163
162
164
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 ) {
164
166
continue EnvLoop
165
167
}
166
168
}
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 ())
168
170
}
169
171
}
170
172
171
173
// Append environment variable if one is specified
172
174
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
174
176
}
175
177
176
178
setupEnvFile (ac .Args , env , & tempFactory )
177
179
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 ... ))
179
186
}
180
187
181
188
// formatForEnv returns a string in %k=%v format, where %k=namespace of the secret and
182
189
// %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 ) {
184
191
if spec .IsFile () {
185
192
fname := tempFactory .Push (value )
186
193
value = fname
187
194
}
188
195
189
- return fmt . Sprintf ( "%s=%s" , key , value )
196
+ return key , value
190
197
}
191
198
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 "
194
206
}
195
207
196
208
// findInParentTree recursively searches for secretsFile starting at leafDir and in the
@@ -234,7 +246,7 @@ func findInParentTree(secretsFile string, leafDir string) (string, error) {
234
246
// creates a tempfile to which all the environment mappings are dumped
235
247
// and replaces the magic string with its path.
236
248
// 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 {
238
250
var envFile = ""
239
251
240
252
for i , arg := range args {
0 commit comments