-
Notifications
You must be signed in to change notification settings - Fork 5
/
param.go
259 lines (231 loc) · 6.42 KB
/
param.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
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
type ssmClient struct {
client *ssm.SSM
}
type parameter struct {
Name string
Versions []paramHistory
}
type parameters []parameter
type paramHistory struct {
Value string
Version string
}
func NewClient(region string) *ssm.SSM {
session := session.Must(session.NewSession())
if DebugMode {
session.Config.WithRegion(region).WithLogLevel(aws.LogDebugWithHTTPBody) //.WithMaxRetries(2)
} else {
session.Config.WithRegion(region)
}
return ssm.New(session)
}
var (
CACHEREF = map[string]map[string]string{}
)
func Deserialize(encoded string) (map[string]string, error) {
params := make(map[string]string)
compressed, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
error := fmt.Sprintf("Error decoding value returned by single key param: %s", err.Error())
return params, errors.New(error)
}
gz, err := gzip.NewReader(bytes.NewBuffer(compressed))
defer gz.Close()
if err != nil {
error := fmt.Sprintf("Error decompressing value returned by single key param: %s", err.Error())
return params, errors.New(error)
}
jsonData, err := ioutil.ReadAll(gz)
if err != nil {
error := fmt.Sprintf("Error reading decompressed json stream: %s", err.Error())
return params, errors.New(error)
}
err = json.Unmarshal(jsonData, ¶ms)
if err != nil {
error := fmt.Sprintf("Error unmarshalling JSON to struct: %s", err.Error())
return params, errors.New(error)
}
return params, nil
}
////// SINGLE KEY MODE ONLY
//////
//////
//////
func (s ssmClient) SingleParam(paramName string) map[string]string {
empty := make(map[string]string)
// Get requested parameter
paramData, err := s.CacheRequestedParam(paramName)
if err != nil {
return empty
}
CACHEREF[paramName] = paramData
// Iterate over param data to get ssm:// references
for i := range CACHEREF[paramName] {
if strings.HasPrefix(CACHEREF[paramName][i], "ssm://") {
// Trim the ssm:// off of the key so we know the full name of the param to get
keyName := strings.Trim(CACHEREF[paramName][i], "ssm://")
// Request param store for the key above and store it in the CACHEREF
paramReferenceData, err := s.CacheRequestedParam(keyName)
if err != nil {
return empty
}
CACHEREF[keyName] = paramReferenceData
// If the reference json data has the key required, get the value
if val, ok := CACHEREF[keyName][i]; ok {
CACHEREF[paramName][i] = val
// If the reference json data does not have the key we require, return empty
} else {
log.Printf("Error: Key %s in parameter reference %s was not found.\n", i, keyName)
return empty
}
}
}
return CACHEREF[paramName]
}
func (s ssmClient) CacheRequestedParam(paramName string) (map[string]string, error) {
empty := make(map[string]string)
if _, ok := CACHEREF[paramName]; ok {
if DebugMode {
log.Printf("%s is already cached, skipping\n", paramName)
}
return CACHEREF[paramName], nil
}
pi := &ssm.GetParameterInput{Name: aws.String(paramName),
WithDecryption: aws.Bool(true)}
r, err := s.client.GetParameter(pi)
if err != nil {
log.Printf("%s, parameter name: %s\n", err.Error(), paramName)
return empty, err
}
ret, err := Deserialize(*r.Parameter.Value)
if err != nil {
log.Println(err)
return empty, err
}
return ret, nil
}
////// ANYTHING BEYOND THIS POINT IS FOR NON-SINGLE KEY MODE ONLY
//////
//////
//////
func (s ssmClient) WithPrefix(prefix string) parameters {
var names parameters
resp, err := s.ParamList(prefix)
if err != nil {
log.Println("Encountered an error listing params", err)
return parameters{}
}
for _, param := range resp.Parameters {
names = append(names, parameter{*param.Name, []paramHistory{}})
}
return names
}
func (s ssmClient) ParamList(filter string) (*ssm.DescribeParametersOutput, error) {
//limit of 50 parameters, unless extra logic is added to paginate
params := &ssm.DescribeParametersInput{
MaxResults: aws.Int64(50),
Filters: []*ssm.ParametersFilter{
{
Values: []*string{
aws.String(filter),
},
Key: aws.String("Name"),
},
},
}
return s.client.DescribeParameters(params)
}
func (p parameters) IncludeHistory(s ssmClient) parameters {
var params parameters
for _, param := range p {
param.history(s)
params = append(params, param)
}
return params
}
func (p *parameter) history(s ssmClient) {
//todo, return error
pi := &ssm.GetParametersInput{
Names: []*string{&p.Name},
WithDecryption: aws.Bool(true),
}
hpi := &ssm.GetParameterHistoryInput{
Name: &p.Name,
WithDecryption: aws.Bool(true),
}
resp, err := s.client.GetParameterHistory(hpi)
if err != nil {
fmt.Println(err.Error())
return
}
r, err := s.client.GetParameters(pi)
if err != nil {
fmt.Println(err.Error())
return
}
re, err := s.ParamList(p.Name)
if err != nil {
fmt.Println(err.Error())
return
}
//todo, guard against empty param
//this is being done in order to get the current version description
p.Versions = append(p.Versions, paramHistory{Value: *r.Parameters[0].Value, Version: *re.Parameters[0].Description})
var hist []paramHistory
var des string
for _, param := range resp.Parameters {
if param.Description != nil {
des = *param.Description
}
val := *param.Value
hist = append(hist, paramHistory{Value: val, Version: des})
}
p.Versions = append(p.Versions, hist...)
return
}
func (p parameters) withVersion(version string) map[string]string {
paramsDoc := make(map[string]string)
//todo, deserialize right here
for _, param := range p {
ver, err := param.containsVersion(version)
if err != nil {
log.Printf("Error: could not find version: %v for param %v", version, param.Name)
continue
}
if SingleKeyMode {
decodedData, err := Deserialize(ver.Value)
if err != nil {
log.Printf("Could not retrieve single key param: %s", err.Error())
continue
}
return decodedData
}
ParsedName := strings.Split(param.Name, ".") //todo, check if envName matches ENV VAR regex
envName := ParsedName[len(ParsedName)-1]
paramsDoc[envName] = ver.Value
}
return paramsDoc
}
func (p parameter) containsVersion(version string) (paramHistory, error) {
for _, v := range p.Versions {
if v.Version == version {
return v, nil
}
}
return paramHistory{}, errors.New("could not find version")
}