/
action.go
196 lines (166 loc) · 4.26 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package command
import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"github.com/codegangsta/cli"
prov "github.com/cyberark/summon/provider"
"github.com/cyberark/summon/secretsyml"
)
type ActionConfig struct {
Args []string
Provider string
Filepath string
YamlInline string
Subs map[string]string
Ignores []string
IgnoreAll bool
Environment string
}
const ENV_FILE_MAGIC = "@SUMMONENVFILE"
var Action = func(c *cli.Context) {
if !c.Args().Present() {
fmt.Println("Enter a subprocess to run!")
os.Exit(127)
}
provider, err := prov.Resolve(c.String("provider"))
if err != nil {
fmt.Println(err.Error())
os.Exit(127)
}
err = runAction(&ActionConfig{
Args: c.Args(),
Provider: provider,
Environment: c.String("environment"),
Filepath: c.String("f"),
YamlInline: c.String("yaml"),
Ignores: c.StringSlice("ignore"),
IgnoreAll: c.Bool("ignore-all"),
Subs: convertSubsToMap(c.StringSlice("D")),
})
code, err := returnStatusOfError(err)
if err != nil {
fmt.Println(err.Error())
os.Exit(127)
}
os.Exit(code)
}
// runAction encapsulates the logic of Action without cli Context for easier testing
func runAction(ac *ActionConfig) error {
var (
secrets secretsyml.SecretsMap
err error
)
switch ac.YamlInline {
case "":
secrets, err = secretsyml.ParseFromFile(ac.Filepath, ac.Environment, ac.Subs)
default:
secrets, err = secretsyml.ParseFromString(ac.YamlInline, ac.Environment, ac.Subs)
}
if err != nil {
return err
}
var env []string
tempFactory := NewTempFactory("")
defer tempFactory.Cleanup()
type Result struct {
string
error
}
// Run provider calls concurrently
results := make(chan Result, len(secrets))
var wg sync.WaitGroup
for key, spec := range secrets {
wg.Add(1)
go func(key string, spec secretsyml.SecretSpec) {
var value string
if spec.IsVar() {
value, err = prov.Call(ac.Provider, spec.Path)
if err != nil {
results <- Result{key, err}
wg.Done()
return
}
} else {
// If the spec isn't a variable, use its value as-is
value = spec.Path
}
envvar := formatForEnv(key, value, spec, &tempFactory)
results <- Result{envvar, nil}
wg.Done()
}(key, spec)
}
wg.Wait()
close(results)
EnvLoop:
for envvar := range results {
if envvar.error == nil {
env = append(env, envvar.string)
} else {
if ac.IgnoreAll {
continue EnvLoop
}
for i := range ac.Ignores {
if ac.Ignores[i] == envvar.string {
continue EnvLoop
}
}
return fmt.Errorf("Error fetching variable %v: %v", envvar.string, envvar.error.Error())
}
}
setupEnvFile(ac.Args, env, &tempFactory)
return runSubcommand(ac.Args, append(os.Environ(), env...))
}
// 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 {
if spec.IsFile() {
fname := tempFactory.Push(value)
value = fname
}
return fmt.Sprintf("%s=%s", key, value)
}
func joinEnv(env []string) string {
return strings.Join(env, "\n") + "\n"
}
// scans arguments for the magic string; if found,
// 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 {
var envFile = ""
for i, arg := range args {
idx := strings.Index(arg, ENV_FILE_MAGIC)
if idx >= 0 {
if envFile == "" {
envFile = tempFactory.Push(joinEnv(env))
}
args[i] = strings.Replace(arg, ENV_FILE_MAGIC, envFile, -1)
}
}
return envFile
}
// convertSubsToMap converts the list of substitutions passed in via
// command line to a map
func convertSubsToMap(subs []string) map[string]string {
out := make(map[string]string)
for _, sub := range subs {
s := strings.SplitN(sub, "=", 2)
key, val := s[0], s[1]
out[key] = val
}
return out
}
func returnStatusOfError(err error) (int, error) {
if eerr, ok := err.(*exec.ExitError); ok {
if ws, ok := eerr.Sys().(syscall.WaitStatus); ok {
if ws.Exited() {
return ws.ExitStatus(), nil
}
}
}
return 0, err
}