-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
400 lines (328 loc) · 12.2 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright © 2018 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"log/slog"
"net"
"os"
"os/exec"
"os/signal"
"slices"
"strings"
"syscall"
"time"
"emperror.dev/errors"
injector "github.com/bank-vaults/vault-sdk/injector/vault"
"github.com/bank-vaults/vault-sdk/vault"
vaultapi "github.com/hashicorp/vault/api"
slogmulti "github.com/samber/slog-multi"
slogsyslog "github.com/samber/slog-syslog"
"github.com/spf13/cast"
)
var Version = "dev"
// The special value for VAULT_ENV which marks that the login token needs to be passed through to the application
// which was acquired during the new Vault client creation
const vaultLogin = "vault:login"
type sanitizedEnviron struct {
env []string
login bool
}
type envType struct {
login bool
}
var sanitizeEnvmap = map[string]envType{
"VAULT_TOKEN": {login: true},
"VAULT_ADDR": {login: true},
"VAULT_AGENT_ADDR": {login: true},
"VAULT_CACERT": {login: true},
"VAULT_CAPATH": {login: true},
"VAULT_CLIENT_CERT": {login: true},
"VAULT_CLIENT_KEY": {login: true},
"VAULT_CLIENT_TIMEOUT": {login: true},
"VAULT_SRV_LOOKUP": {login: true},
"VAULT_SKIP_VERIFY": {login: true},
"VAULT_NAMESPACE": {login: true},
"VAULT_TLS_SERVER_NAME": {login: true},
"VAULT_WRAP_TTL": {login: true},
"VAULT_MFA": {login: true},
"VAULT_MAX_RETRIES": {login: true},
"VAULT_CLUSTER_ADDR": {login: false},
"VAULT_REDIRECT_ADDR": {login: false},
"VAULT_CLI_NO_COLOR": {login: false},
"VAULT_RATE_LIMIT": {login: false},
"VAULT_ROLE": {login: false},
"VAULT_PATH": {login: false},
"VAULT_AUTH_METHOD": {login: false},
"VAULT_TRANSIT_KEY_ID": {login: false},
"VAULT_TRANSIT_PATH": {login: false},
"VAULT_TRANSIT_BATCH_SIZE": {login: false},
"VAULT_IGNORE_MISSING_SECRETS": {login: false},
"VAULT_ENV_PASSTHROUGH": {login: false},
"VAULT_JSON_LOG": {login: false},
"VAULT_LOG_LEVEL": {login: false},
"VAULT_REVOKE_TOKEN": {login: false},
"VAULT_ENV_DAEMON": {login: false},
"VAULT_ENV_FROM_PATH": {login: false},
"VAULT_ENV_DELAY": {login: false},
}
// Appends variable an entry (name=value) into the environ list.
// VAULT_* variables are not populated into this list if this is not a login scenario.
func (e *sanitizedEnviron) append(name string, value string) {
if envType, ok := sanitizeEnvmap[name]; !ok || (e.login && envType.login) {
e.env = append(e.env, fmt.Sprintf("%s=%s", name, value))
}
}
type daemonSecretRenewer struct {
client *vault.Client
sigs chan os.Signal
logger *slog.Logger
}
func (r daemonSecretRenewer) Renew(path string, secret *vaultapi.Secret) error {
watcherInput := vaultapi.LifetimeWatcherInput{Secret: secret}
watcher, err := r.client.RawClient().NewLifetimeWatcher(&watcherInput)
if err != nil {
return errors.Wrap(err, "failed to create secret watcher")
}
go watcher.Start()
go func() {
defer watcher.Stop()
for {
select {
case renewOutput := <-watcher.RenewCh():
r.logger.Info("secret renewed", slog.String("path", path), slog.Duration("lease-duration", time.Duration(renewOutput.Secret.LeaseDuration)*time.Second))
case doneError := <-watcher.DoneCh():
if !secret.Renewable {
leaseDuration := time.Duration(secret.LeaseDuration) * time.Second
time.Sleep(leaseDuration)
r.logger.Info("secret lease has expired", slog.String("path", path), slog.Duration("lease-duration", leaseDuration))
}
r.logger.Info("secret renewal has stopped, sending SIGTERM to process", slog.String("path", path), slog.Any("done-error", doneError))
r.sigs <- syscall.SIGTERM
timeout := <-time.After(10 * time.Second)
r.logger.Info("killing process due to SIGTERM timeout", slog.Time("timeout", timeout))
r.sigs <- syscall.SIGKILL
return
}
}
}()
return nil
}
func main() {
var logger *slog.Logger
{
var level slog.Level
err := level.UnmarshalText([]byte(os.Getenv("VAULT_LOG_LEVEL")))
if err != nil { // Silently fall back to info level
level = slog.LevelInfo
}
levelFilter := func(levels ...slog.Level) func(ctx context.Context, r slog.Record) bool {
return func(_ context.Context, r slog.Record) bool {
return slices.Contains(levels, r.Level)
}
}
router := slogmulti.Router()
if cast.ToBool(os.Getenv("VAULT_JSON_LOG")) {
// Send logs with level higher than warning to stderr
router = router.Add(
slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: level}),
levelFilter(slog.LevelWarn, slog.LevelError),
)
// Send info and debug logs to stdout
router = router.Add(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level}),
levelFilter(slog.LevelDebug, slog.LevelInfo),
)
} else {
// Send logs with level higher than warning to stderr
router = router.Add(
slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level}),
levelFilter(slog.LevelWarn, slog.LevelError),
)
// Send info and debug logs to stdout
router = router.Add(
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level}),
levelFilter(slog.LevelDebug, slog.LevelInfo),
)
}
if logServerAddr := os.Getenv("VAULT_ENV_LOG_SERVER"); logServerAddr != "" {
writer, err := net.Dial("udp", logServerAddr)
// We silently ignore syslog connection errors for the lack of a better solution
if err == nil {
router = router.Add(slogsyslog.Option{Level: slog.LevelInfo, Writer: writer}.NewSyslogHandler())
}
}
// TODO: add level filter handler
logger = slog.New(router.Handler())
logger = logger.With(slog.String("app", "vault-env"))
slog.SetDefault(logger)
}
if len(os.Args) == 1 {
logger.Error("no command is given, vault-env can't determine the entrypoint (command), please specify it explicitly or let the webhook query it (see documentation)")
os.Exit(1)
}
daemonMode := cast.ToBool(os.Getenv("VAULT_ENV_DAEMON"))
delayExec := cast.ToDuration(os.Getenv("VAULT_ENV_DELAY"))
sigs := make(chan os.Signal, 1)
entrypointCmd := os.Args[1:]
binary, err := exec.LookPath(entrypointCmd[0])
if err != nil {
logger.Error("binary not found", slog.String("binary", entrypointCmd[0]))
os.Exit(1)
}
// Used both for reading secrets and transit encryption
ignoreMissingSecrets := cast.ToBool(os.Getenv("VAULT_IGNORE_MISSING_SECRETS"))
clientOptions := []vault.ClientOption{vault.ClientLogger(clientLogger{logger})}
// The login procedure takes the token from a file (if using Vault Agent)
// or requests one for itself (Kubernetes Auth, or GCP, etc...),
// so if we got a VAULT_TOKEN for the special value with "vault:login"
originalVaultTokenEnvVar := os.Getenv("VAULT_TOKEN")
isLogin := originalVaultTokenEnvVar == vaultLogin
if tokenFile := os.Getenv("VAULT_TOKEN_FILE"); tokenFile != "" {
// load token from vault-agent .vault-token or injected webhook
if b, err := os.ReadFile(tokenFile); err == nil {
originalVaultTokenEnvVar = string(b)
} else {
logger.Error("could not read vault token file", slog.String("file", tokenFile))
os.Exit(1)
}
clientOptions = append(clientOptions, vault.ClientToken(originalVaultTokenEnvVar))
} else {
if isLogin {
_ = os.Unsetenv("VAULT_TOKEN")
}
// use role/path based authentication
clientOptions = append(clientOptions,
vault.ClientRole(os.Getenv("VAULT_ROLE")),
vault.ClientAuthPath(os.Getenv("VAULT_PATH")),
vault.ClientAuthMethod(os.Getenv("VAULT_AUTH_METHOD")),
)
}
client, err := vault.NewClientWithOptions(clientOptions...)
if err != nil {
logger.Error(fmt.Errorf("failed to create vault client: %w", err).Error())
os.Exit(1)
}
passthroughEnvVars := strings.Split(os.Getenv("VAULT_ENV_PASSTHROUGH"), ",")
if isLogin {
_ = os.Setenv("VAULT_TOKEN", vaultLogin)
passthroughEnvVars = append(passthroughEnvVars, "VAULT_TOKEN")
}
// do not sanitize env vars specified in VAULT_ENV_PASSTHROUGH
for _, envVar := range passthroughEnvVars {
if trimmed := strings.TrimSpace(envVar); trimmed != "" {
delete(sanitizeEnvmap, trimmed)
}
}
// initial and sanitized environs
environ := make(map[string]string, len(os.Environ()))
sanitized := sanitizedEnviron{login: isLogin}
config := injector.Config{
TransitKeyID: os.Getenv("VAULT_TRANSIT_KEY_ID"),
TransitPath: os.Getenv("VAULT_TRANSIT_PATH"),
TransitBatchSize: cast.ToInt(os.Getenv("VAULT_TRANSIT_BATCH_SIZE")),
DaemonMode: daemonMode,
IgnoreMissingSecrets: ignoreMissingSecrets,
}
var secretRenewer injector.SecretRenewer
if daemonMode {
secretRenewer = daemonSecretRenewer{client: client, sigs: sigs, logger: logger}
}
secretInjector := injector.NewSecretInjector(config, client, secretRenewer, logger)
for _, env := range os.Environ() {
split := strings.SplitN(env, "=", 2)
name := split[0]
value := split[1]
environ[name] = value
}
inject := func(key, value string) {
sanitized.append(key, value)
}
err = secretInjector.InjectSecretsFromVault(environ, inject)
if err != nil {
logger.Error(fmt.Errorf("failed to inject secrets from vault: %w", err).Error())
os.Exit(1)
}
if paths := os.Getenv("VAULT_ENV_FROM_PATH"); paths != "" {
err = secretInjector.InjectSecretsFromVaultPath(paths, inject)
}
if err != nil {
logger.Error(fmt.Errorf("failed to inject secrets from vault path: %w", err).Error())
os.Exit(1)
}
if cast.ToBool(os.Getenv("VAULT_REVOKE_TOKEN")) {
// ref: https://www.vaultproject.io/api/auth/token/index.html#revoke-a-token-self-
err = client.RawClient().Auth().Token().RevokeSelf(client.RawClient().Token())
if err != nil {
// Do not exit on error, token revoking can be denied by policy
logger.Warn("failed to revoke token")
}
client.Close()
}
if delayExec > 0 {
logger.Info(fmt.Sprintf("sleeping for %s...", delayExec))
time.Sleep(delayExec)
}
logger.Info("spawning process", slog.String("entrypoint", fmt.Sprint(entrypointCmd)))
if daemonMode {
logger.Info("in daemon mode...")
cmd := exec.Command(binary, entrypointCmd[1:]...)
cmd.Env = append(os.Environ(), sanitized.env...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
signal.Notify(sigs)
err = cmd.Start()
if err != nil {
logger.Error(fmt.Errorf("failed to start process: %w", err).Error(), slog.String("entrypoint", fmt.Sprint(entrypointCmd)))
os.Exit(1)
}
go func() {
for sig := range sigs {
// We don't want to signal a non-running process.
if cmd.ProcessState != nil && cmd.ProcessState.Exited() {
break
}
err := cmd.Process.Signal(sig)
if err != nil {
logger.Warn(fmt.Errorf("failed to signal process: %w", err).Error(), slog.String("signal", sig.String()))
} else if sig == syscall.SIGURG {
logger.Debug("received signal", slog.String("signal", sig.String()))
} else {
logger.Info("received signal", slog.String("signal", sig.String()))
}
}
}()
err = cmd.Wait()
close(sigs)
if err != nil {
exitCode := -1
// try to get the original exit code if possible
var exitError *exec.ExitError
if errors.As(err, &exitError) {
exitCode = exitError.ExitCode()
}
logger.Error(fmt.Errorf("failed to exec process: %w", err).Error(), slog.String("entrypoint", fmt.Sprint(entrypointCmd)))
os.Exit(exitCode)
}
os.Exit(cmd.ProcessState.ExitCode())
} else { //nolint:revive
err = syscall.Exec(binary, entrypointCmd, sanitized.env)
if err != nil {
logger.Error(fmt.Errorf("failed to exec process: %w", err).Error(), slog.String("entrypoint", fmt.Sprint(entrypointCmd)))
os.Exit(1)
}
}
}