-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
113 lines (100 loc) · 3.29 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
var config struct {
SecretStringsAssignments AssignmentsMap
SecretBinariesAssignments AssignmentsMap
SecretBinaryStringsAssignments AssignmentsMap
SecretJSONKeyStringAssignments AssignmentsMap
SecretJSONKeyAssignments AssignmentsMap
SecretJSONKeyStrings map[string]secretJSONKey
SecretJSONKeys map[string]secretJSONKey
PrintEnvAndExit bool
PrintVersionAndExit bool
Profile string
}
var version string
type secretJSONKey struct {
SecretID string
JSONKey string
}
func init() {
config.SecretJSONKeyStrings = make(map[string]secretJSONKey)
config.SecretJSONKeys = make(map[string]secretJSONKey)
log.SetOutput(os.Stderr)
log.SetFlags(log.LstdFlags | log.Ldate)
log.SetPrefix("[aws-secretsmanager-env] ")
flag.Var(&config.SecretStringsAssignments, "secret-string", "a key/value pair `ENV_VAR=SECRET_ARN` (may be specified repeatedly)")
flag.Var(&config.SecretBinariesAssignments, "secret-binary-base64", "a key/value pair `ENV_VAR=SECRET_ARN` (may be specified repeatedly)")
flag.Var(&config.SecretBinaryStringsAssignments, "secret-binary-string", "a key/value pair `ENV_VAR=SECRET_ARN` (may be specified repeatedly)")
flag.Var(&config.SecretJSONKeyStringAssignments, "secret-json-key-string", "a key/value pair `ENV_VAR=SECRET_ARN#JSON_KEY` (may be specified repeatedly)")
flag.Var(&config.SecretJSONKeyAssignments, "secret-json-key", "a key/value pair `ENV_VAR=SECRET_ARN#JSON_KEY` (may be specified repeatedly)")
flag.StringVar(&config.Profile, "profile", "", "override the current AWS_PROFILE setting")
flag.BoolVar(&config.PrintVersionAndExit, "version", config.PrintVersionAndExit, "print version and exit")
flag.Parse()
config.PrintEnvAndExit = flag.NArg() > 0
for key, value := range config.SecretJSONKeyStringAssignments.Values {
i := strings.IndexRune(value, '#')
if i < 0 {
log.Fatalf(`"%s" must have the form SECRET_ID#JSON_KEY`, value)
}
secretID, jsonKey := value[:i], value[i+1:]
config.SecretJSONKeyStrings[key] = secretJSONKey{
SecretID: secretID,
JSONKey: jsonKey,
}
}
for key, value := range config.SecretJSONKeyAssignments.Values {
i := strings.IndexRune(value, '#')
if i < 0 {
log.Fatalf(`"%s" must have the form SECRET_ID#JSON_KEY`, value)
}
secretID, jsonKey := value[:i], value[i+1:]
config.SecretJSONKeys[key] = secretJSONKey{
SecretID: secretID,
JSONKey: jsonKey,
}
}
}
func main() {
if config.PrintVersionAndExit {
fmt.Println(version)
return
}
awsSession, err := awsSession()
if err != nil {
log.Fatalf("aws: %v", err)
}
secretsEnv, err := awsSecretsEnv(secretsmanager.New(awsSession))
if err != nil {
log.Fatalf("error(s) while reading secrets: %v", err)
}
if !config.PrintEnvAndExit {
for _, assignment := range secretsEnv {
fmt.Println(assignment)
}
return
}
var osEnvAndSecretsEnv = append(os.Environ(), secretsEnv...)
nameAndArgs := flag.Args()
var args []string
name := nameAndArgs[0]
if len(nameAndArgs) > 1 {
args = nameAndArgs[1:]
}
cmd := exec.Command(name, args...)
cmd.Env = osEnvAndSecretsEnv
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("run %v: %v", cmd.Args, err)
}
}