@@ -3,6 +3,7 @@ package command
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
+ "io"
6
7
"os"
7
8
"os/exec"
8
9
"path/filepath"
@@ -12,16 +13,21 @@ import (
12
13
"syscall"
13
14
14
15
"github.com/codegangsta/cli"
16
+
15
17
prov "github.com/cyberark/summon/provider"
16
18
"github.com/cyberark/summon/secretsyml"
17
19
)
18
20
19
21
// ActionConfig is an object that holds all the info needed to run
20
22
// a Summon instance
21
23
type ActionConfig struct {
24
+ StdIn io.Reader
25
+ StdOut io.Writer
26
+ StdErr io.Writer
22
27
Args []string
23
28
Provider string
24
29
Filepath string
30
+ TmpPath string
25
31
YamlInline string
26
32
Subs map [string ]string
27
33
Ignores []string
@@ -31,8 +37,9 @@ type ActionConfig struct {
31
37
ShowProviderVersions bool
32
38
}
33
39
34
- const ENV_FILE_MAGIC = "@SUMMONENVFILE"
35
- const SUMMON_ENV_KEY_NAME = "SUMMON_ENV"
40
+ const EnvFileMagic = "@SUMMONENVFILE"
41
+ const VarNamesMagic = "@SUMMONVARNAMES"
42
+ const SummonEnvKeyName = "SUMMON_ENV"
36
43
37
44
// Action is the runner for the main program logic
38
45
var Action = func (c * cli.Context ) {
@@ -110,7 +117,7 @@ func runAction(ac *ActionConfig) error {
110
117
}
111
118
112
119
env := make (map [string ]string )
113
- tempFactory := NewTempFactory ("" )
120
+ tempFactory := NewTempFactory (ac . TmpPath )
114
121
defer tempFactory .Cleanup ()
115
122
116
123
type Result struct {
@@ -145,6 +152,7 @@ func runAction(ac *ActionConfig) error {
145
152
}
146
153
147
154
k , v := formatForEnv (key , value , spec , & tempFactory )
155
+
148
156
results <- Result {k , v , nil }
149
157
wg .Done ()
150
158
}(key , spec )
@@ -172,17 +180,42 @@ EnvLoop:
172
180
173
181
// Append environment variable if one is specified
174
182
if ac .Environment != "" {
175
- env [SUMMON_ENV_KEY_NAME ] = ac .Environment
183
+ env [SummonEnvKeyName ] = ac .Environment
176
184
}
177
185
178
186
setupEnvFile (ac .Args , env , & tempFactory )
187
+ setupVarNames (ac .Args , secrets )
179
188
180
189
var e []string
181
190
for k , v := range env {
182
191
e = append (e , fmt .Sprintf ("%s=%s" , k , v ))
183
192
}
184
193
185
- return runSubcommand (ac .Args , append (os .Environ (), e ... ))
194
+ return runSubcommand (
195
+ ac .Args ,
196
+ append (os .Environ (), e ... ),
197
+ ac .StdIn ,
198
+ ac .StdOut ,
199
+ ac .StdErr ,
200
+ )
201
+ }
202
+
203
+ func setupVarNames (args []string , secrets secretsyml.SecretsMap ) {
204
+ var varNames []string
205
+ for varName := range secrets {
206
+ varNames = append (varNames , varName )
207
+ }
208
+ sort .Strings (varNames )
209
+
210
+ // Inject @SUMMONVARNAMES
211
+ for idx , arg := range args {
212
+ // Replace argument substring
213
+ if strings .Contains (arg , VarNamesMagic ) {
214
+ // Replace substring in argument with slice of docker options
215
+ args [idx ] = strings .Replace (arg , VarNamesMagic , strings .Join (varNames , " " ), - 1 )
216
+ continue
217
+ }
218
+ }
186
219
}
187
220
188
221
// formatForEnv returns a string in %k=%v format, where %k=namespace of the secret and
@@ -201,7 +234,7 @@ func joinEnv(env map[string]string) string {
201
234
for k , v := range env {
202
235
envs = append (envs , fmt .Sprintf ("%s=%s" , k , v ))
203
236
}
204
-
237
+
205
238
// Sort to ensure predictable results
206
239
sort .Strings (envs )
207
240
@@ -245,20 +278,20 @@ func findInParentTree(secretsFile string, leafDir string) (string, error) {
245
278
}
246
279
}
247
280
248
- // scans arguments for the magic string; if found,
281
+ // scans arguments for the envfile magic string; if found,
249
282
// creates a tempfile to which all the environment mappings are dumped
250
283
// and replaces the magic string with its path.
251
284
// Returns the path if so, returns an empty string otherwise.
252
285
func setupEnvFile (args []string , env map [string ]string , tempFactory * TempFactory ) string {
253
286
var envFile = ""
254
287
255
288
for i , arg := range args {
256
- idx := strings .Index (arg , ENV_FILE_MAGIC )
289
+ idx := strings .Index (arg , EnvFileMagic )
257
290
if idx >= 0 {
258
291
if envFile == "" {
259
292
envFile = tempFactory .Push (joinEnv (env ))
260
293
}
261
- args [i ] = strings .Replace (arg , ENV_FILE_MAGIC , envFile , - 1 )
294
+ args [i ] = strings .Replace (arg , EnvFileMagic , envFile , - 1 )
262
295
}
263
296
}
264
297
0 commit comments