/
main.go
202 lines (194 loc) · 5.31 KB
/
main.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
197
198
199
200
201
202
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/stephane-martin/vault-exec/lib"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)
// Version stores the current version of vault-exec. It as populated by Makefile.
var Version string
func main() {
app := cli.NewApp()
app.Name = "vault-exec"
app.Usage = "fetch secrets from vault and inject them as environment variables"
app.UsageText = "vault-exec [options] cmd-to-execute"
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "address,vault-addr,addr,a",
Value: "http://127.0.0.1:8200",
EnvVar: "VAULT_ADDR",
Usage: "Vault address",
},
cli.StringFlag{
Name: "token,t",
Value: "",
EnvVar: "VAULT_TOKEN",
Usage: "Vault authentication token (use with the token auth method)",
},
cli.StringSliceFlag{
Name: "secret,key,s,k",
Usage: "path of a secret to be read from Vault (multiple times)",
},
cli.StringFlag{
Name: "method,m",
Usage: "type of authentication [token,userpass,ldap,approle]",
Value: "token",
EnvVar: "VAULT_AUTH_METHOD",
},
cli.StringFlag{
Name: "path",
Usage: "vault remote path where the auth method is mounted (optional)",
Value: "",
EnvVar: "VAULT_AUTH_PATH",
},
cli.StringFlag{
Name: "username,user,U",
Usage: "Vault username or RoleID",
Value: "",
EnvVar: "VAULT_USERNAME",
},
cli.StringFlag{
Name: "password,pass,P",
Usage: "Vault password or SecretID",
Value: "",
EnvVar: "VAULT_PASSWORD",
},
cli.BoolFlag{
Name: "upcase,up",
Usage: "convert all environment variable keys to uppercase",
EnvVar: "UPCASE",
},
cli.BoolFlag{
Name: "prefix,p",
Usage: "prefix the environment variable keys with names of secrets",
EnvVar: "PREFIX",
},
cli.BoolFlag{
Name: "once",
Usage: "don't try to renew token and secrets, run the command only once",
EnvVar: "ONCE",
},
cli.StringFlag{
Name: "forward,fwd,f",
Usage: "comma separated list of environment variable keys to forward from parent environment",
Value: "*",
},
cli.StringFlag{
Name: "loglevel",
Usage: "logging level",
Value: "info",
},
}
app.Action = func(c *cli.Context) error {
logger, err := lib.Logger(c.GlobalString("loglevel"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
defer func() {
_ = logger.Sync()
}()
authType := strings.ToLower(c.GlobalString("method"))
authPath := strings.TrimSpace(c.GlobalString("path"))
if authPath == "" {
authPath = authType
}
args := c.Args()
if len(args) == 0 {
args = []string{"env"}
}
env := lib.ForwardEnv(strings.TrimSpace(c.GlobalString("forward")))
upcase := c.GlobalBool("upcase")
username := c.GlobalString("username")
password := c.GlobalString("password")
vaultAddress := c.GlobalString("address")
vaultToken := c.GlobalString("token")
prefix := c.GlobalBool("prefix")
once := c.GlobalBool("once")
keys := c.GlobalStringSlice("secret")
_ = os.Unsetenv("VAULT_ADDR")
// TODO: real context
client, err := lib.Auth(context.Background(), authType, vaultAddress, authPath, vaultToken, username, password, logger)
if err != nil {
return cli.NewExitError(fmt.Sprintf("auth failed: %s", err), 1)
}
// TODO: real context
err = lib.CheckHealth(context.Background(), client)
if err != nil {
return cli.NewExitError(fmt.Sprintf("vault health check error: %s", err), 1)
}
ctx, cancel := lib.GlobalContext()
results := make(chan map[string]string)
go func() {
err := lib.GetSecrets(ctx, client, prefix, upcase, once, keys, logger, results)
if e, ok := err.(lib.TokenNotRenewedError); ok {
logger.Errorw("can't renew token: giving up", "error", e.Err)
} else if err != nil && err != context.Canceled {
for _, line := range strings.Split(err.Error(), "\n") {
line = strings.TrimSpace(line)
if line != "" {
logger.Errorw("vault error", "error", line)
}
}
}
cancel()
}()
var cmdCtx context.Context
var cmdCancel context.CancelFunc
var cmdGroup *errgroup.Group
cmdSubCtx := context.Background()
for {
select {
case <-ctx.Done():
// global context is done, means that we received a signal to stop from user
if cmdCancel != nil {
// ask the command to stop
cmdCancel()
_ = cmdGroup.Wait()
}
return nil
case <-cmdSubCtx.Done():
cmdCancel()
err := cmdGroup.Wait()
if err == lib.ErrCmdFinishedNoError {
return nil
}
if err != lib.ErrForceStop {
return cli.NewExitError(err.Error(), 1)
}
case result := <-results:
if cmdCancel != nil {
logger.Info("secrets updated from vault: restarting command")
// ask the command to stop
cmdCancel()
err := cmdGroup.Wait()
if err == lib.ErrCmdFinishedNoError {
return nil
}
if err != lib.ErrForceStop {
return cli.NewExitError(err.Error(), 1)
}
}
cmdCtx, cmdCancel = context.WithCancel(ctx)
cmdGroup, cmdSubCtx = errgroup.WithContext(cmdCtx)
cmdGroup.Go(func() error {
return lib.ExecCmd(cmdCtx, args, result, env, logger)
})
}
}
}
cli.OsExiter = func(code int) {
_ = os.Stdout.Sync()
_ = os.Stderr.Sync()
time.Sleep(200 * time.Millisecond)
os.Exit(code)
}
_ = app.Run(os.Args)
_ = os.Stdout.Sync()
_ = os.Stderr.Sync()
time.Sleep(200 * time.Millisecond)
}