-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
218 lines (180 loc) · 4.76 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
)
const version = "0.1.2"
func main() {
if !validateArgs(os.Args) {
os.Exit(1)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
var awsRegion *string
if aws.StringValue(sess.Config.Region) == "" {
ec2Session := session.New()
ec2Svc := ec2metadata.New(ec2Session)
ec2Region, err := ec2Svc.Region()
if err == nil {
awsRegion = aws.String(ec2Region)
}
}
svc := ssm.New(sess, &aws.Config{
Region: awsRegion,
Endpoint: aws.String(os.Getenv("SSMPS_AWS_SSM_ENDPOINT")),
})
basePath := os.Getenv("SSMPS_BASE_PATH")
names := os.Args[1:]
nameToPath := makeNameToPathMap(basePath, names)
paths := []string{}
for _, path := range nameToPath {
paths = append(paths, path)
}
pathToValue, err := getMultipleParamValues(svc, paths)
if err != nil {
log.Fatal(err)
}
if len(names) == 1 {
path := nameToPath[names[0]]
fmt.Println(pathToValue[path])
} else {
// Treat as multiple parameters even if the same name is given twice.
nameToValue := map[string]string{}
for name, path := range nameToPath {
value, ok := pathToValue[path]
if ok {
nameToValue[name] = value
} else {
nameToValue[name] = ""
}
}
data, err := json.Marshal(nameToValue)
if err != nil {
log.Fatalf("Error marshalling data: %s", err)
}
fmt.Printf("%s\n", data)
}
os.Exit(0)
}
func validateArgs(args []string) bool {
if len(args) < 2 {
log.Println("Too few arguments")
return false
}
return true
}
func makePath(basePath string, paramName string) string {
if strings.HasPrefix(paramName, "/") {
return paramName
}
return normalizeBasePath(basePath) + normalizeParamName(paramName)
}
func normalizeBasePath(basePath string) string {
if len(basePath) == 0 {
return basePath
}
// Prepend slash
if !strings.HasPrefix(basePath, "/") {
basePath = "/" + basePath
}
// Remove trailing slash
basePath = strings.TrimRight(basePath, "/")
return basePath
}
func normalizeParamName(name string) string {
// Prepend slash
if !strings.HasPrefix(name, "/") {
name = "/" + name
}
return name
}
func makeNameToPathMap(basePath string, names []string) map[string]string {
m := map[string]string{}
for _, name := range names {
path := makePath(basePath, name)
m[name] = path
}
return m
}
func makeBatches(values []string, batchSize int) ([][]string, error) {
batches := [][]string{}
if batchSize < 1 {
return nil, fmt.Errorf("batchSize must be greater than 0")
}
for batchSize < len(values) {
values, batches = values[batchSize:], append(batches, values[0:batchSize:batchSize])
}
batches = append(batches, values)
return batches, nil
}
func getParamValue(svc ssmiface.SSMAPI, name string) (string, error) {
output, err := svc.GetParameter(&ssm.GetParameterInput{
Name: &name,
WithDecryption: aws.Bool(true),
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
switch aerr.Code() {
case ssm.ErrCodeParameterNotFound, ssm.ErrCodeParameterVersionNotFound:
log.Printf("ssmps(%q) returned no data: %s", name, aerr.Code())
return "", nil
default:
return "", fmt.Errorf("ssmps(%q) returned error: %s", name, aerr.Code())
}
} else {
return "", fmt.Errorf("ssmps(%q) returned unknown error: %v", name, err)
}
}
return *output.Parameter.Value, nil
}
func getMultipleParamValues(svc ssmiface.SSMAPI, names []string) (map[string]string, error) {
batches, err := makeBatches(names, 10) // This is the limit of the GetParameters endpoint.
if err != nil {
log.Fatalf("ssmps returned error: %s", err)
}
pathToValue := map[string]string{}
// Defaults to an empty string
for _, name := range names {
pathToValue[name] = ""
}
for _, batch := range batches {
paths := []*string{}
for _, path := range batch {
copy := path
paths = append(paths, ©)
}
output, err := svc.GetParameters(&ssm.GetParametersInput{
Names: paths,
WithDecryption: aws.Bool(true),
})
if err != nil {
aerr, ok := err.(awserr.Error)
if ok {
return nil, fmt.Errorf("ssmps returned error: %s, message: %s", aerr.Code(), aerr.Message())
}
return nil, fmt.Errorf("ssmps returned unknown error: %s", err)
}
for _, p := range output.InvalidParameters {
log.Printf("ssmps(%q) returned no data: Invalid Parameter", *p)
}
for _, p := range output.Parameters {
path := *p.Name
if p.Selector != nil {
path += *p.Selector
}
pathToValue[path] = *p.Value
}
}
return pathToValue, nil
}