-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
196 lines (172 loc) · 5.1 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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
type stepInput struct {
awsAccessKeyId string
awsSecretAccessKey string
awsDefaultRegion string
awsProfile string
awsIamRoleArn string
secretList string
}
type secretListItem struct {
arn string
key string
envvar string
}
type secretValueJson map[string]string
type secretCacheMap map[string]string
func prepareAwsConfig(sinput stepInput) (awsConfig aws.Config, err error) {
if sinput.awsAccessKeyId != "" && sinput.awsSecretAccessKey != "" && sinput.awsDefaultRegion != "" {
fmt.Println("Loading AWS config using static credentials")
awsConfig, err = config.LoadDefaultConfig(
context.Background(),
config.WithRegion(sinput.awsDefaultRegion),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
sinput.awsAccessKeyId,
sinput.awsSecretAccessKey,
"",
),
),
)
} else if sinput.awsProfile != "" {
fmt.Println("Loading AWS config using named profile")
awsConfig, err = config.LoadDefaultConfig(
context.Background(),
config.WithSharedConfigProfile(sinput.awsProfile),
)
} else {
err = errors.New("Incomplete AWS configuration. Specify AWS static credentials and region, or an AWS named profile for shared configuration, via the Step's input.")
}
return
}
func assumeRole(sinput stepInput, awsConfig *aws.Config) {
stsSvc := sts.NewFromConfig(*awsConfig)
creds := stscreds.NewAssumeRoleProvider(stsSvc, sinput.awsIamRoleArn)
awsConfig.Credentials = aws.NewCredentialsCache(creds)
return
}
func parseSecretList(secretList string) (items []secretListItem) {
for _, secret := range strings.Split(secretList, "\n") {
if strings.TrimSpace(secret) == "" {
continue
}
secretComponents := strings.Split(secret, "#")
items = append(items, secretListItem{
arn: strings.TrimSpace(secretComponents[0]),
key: strings.TrimSpace(secretComponents[1]),
envvar: strings.TrimSpace(secretComponents[2]),
})
}
return
}
func cacher(secretCache secretCacheMap, secretId string, awsConfig aws.Config, fetcher func(string, aws.Config) (string, error)) (secretString string, err error) {
secretString, cached := secretCache[secretId]
if cached {
err = nil
return
}
secretString, err = fetcher(secretId, awsConfig)
if err != nil {
return
}
secretCache[secretId] = secretString
return
}
func fetchSecrets(secretId string, awsConfig aws.Config) (secretString string, err error) {
fmt.Printf("Getting secret for %s\n", secretId)
secretString = ""
smSvc := secretsmanager.NewFromConfig(awsConfig)
secretValue, err := smSvc.GetSecretValue(
context.Background(),
&secretsmanager.GetSecretValueInput{
SecretId: &secretId,
},
)
if err != nil {
return
}
if secretValue.SecretString == nil {
err = errors.New("Missing SecretString on GetSecretValue response")
return
}
secretString = *secretValue.SecretString
return
}
func loadJson(secretString string) (result secretValueJson, err error) {
jsonData := []byte(secretString)
err = json.Unmarshal(jsonData, &result)
return
}
func exportEnvVarJson(data secretValueJson, dataKey string, envVarKey string) (err error) {
dataValue, ok := data[dataKey]
if !ok {
err = errors.New(dataKey + " not found in secret")
return
}
fmt.Printf("Storing secret value for key '%s' into $%s\n", dataKey, envVarKey)
err = exportEnvVar(dataValue, envVarKey)
return
}
func exportEnvVar(value string, envVarKey string) (err error) {
c := exec.Command("envman", "add", "--key", envVarKey, "--value", value, "--sensitive")
err = c.Run()
return
}
func IsJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
func main() {
// Prevent environment variables from interfering with AWS config loading
os.Unsetenv("AWS_ACCESS_KEY_ID")
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
os.Unsetenv("AWS_DEFAULT_REGION")
os.Unsetenv("AWS_REGION")
os.Unsetenv("AWS_PROFILE")
sinput := stepInput{
awsAccessKeyId: os.Getenv("aws_access_key_id"),
awsSecretAccessKey: os.Getenv("aws_secret_access_key"),
awsDefaultRegion: os.Getenv("aws_default_region"),
awsProfile: os.Getenv("aws_profile"),
awsIamRoleArn: os.Getenv("aws_iam_role_arn"),
secretList: os.Getenv("secret_list"),
}
awsConfig, err := prepareAwsConfig(sinput)
if err != nil {
panic(err)
}
if sinput.awsIamRoleArn != "" {
assumeRole(sinput, &awsConfig)
}
secretCache := make(secretCacheMap)
for _, item := range parseSecretList(sinput.secretList) {
secretString, err := cacher(secretCache, item.arn, awsConfig, fetchSecrets)
if err != nil {
panic(err)
}
if IsJSON(secretString) {
secretJson, err := loadJson(secretString)
if err != nil {
panic(err)
}
exportEnvVarJson(secretJson, item.key, item.envvar)
} else {
exportEnvVar(secretString, item.envvar)
}
}
}