-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiggy.go
415 lines (386 loc) · 10.1 KB
/
figgy.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//Package figgy provides tags for loading parameters from AWS Parameter Store
package figgy
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"text/template"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
)
// maxParameters is the maximum number of parameters that can be requested in a single call to GetParameters
const maxParameters = 10
var durationType reflect.Type = reflect.TypeOf(time.Duration(0))
type Unmarshaler interface {
UnmarshalParameter(string) error
}
// InvalidTypeError descibes an invalid argument passed to Load.
type InvalidTypeError struct {
Type reflect.Type
}
func (e *InvalidTypeError) Error() string {
if e.Type == nil {
return "nil type"
}
if e.Type.Kind() != reflect.Ptr {
return "non-pointer Load(" + e.Type.String() + ")"
}
return "invalid type " + e.Type.String()
}
// TagParseError describes an invalid tag
type TagParseError struct {
// Tag that failed to be fully parsed
Tag string
// Field metadata that the tag is parsed from
Field string
}
func (e *TagParseError) Error() string {
return "failed to parse tag [" + e.Tag + "] for field " + e.Field
}
// ConvertTypeError describes a value that failed to be set for a field
type ConvertTypeError struct {
//Field that the value was being assigned to
Field string
// Type of value that couldn't be assigned
Type string
// Value that failed to be converted
Value string
}
func (e *ConvertTypeError) Error() string {
if e.Field != "" || e.Type != "" {
return "failed to convert '" + e.Value + "' to " + e.Type + " for field " + e.Field
}
return "failed to convert '" + e.Value + "'"
}
// field represents parse struct fields tags and the underlying value
type field struct {
key string
decrypt bool
json bool
value reflect.Value
field reflect.StructField
}
func newField(key string, decrypt bool) *field {
return &field{
key: strings.TrimSpace(key),
decrypt: decrypt,
}
}
// P is a convenience alias for passing paramters to LoadWithParameters
type P map[string]interface{}
// Load AWS Parameter Store parameters based on the defined tags.
//
// When a source type is an array, it is assumed the parameter being loaded
// is a comma separated list. The list will be split and converted to
// match the array's typing.
//
// You can ignore a field by using "-" for a fields tag. Unexported fields are also ignored.
func Load(c ssmiface.SSMAPI, v interface{}) error {
return LoadWithParameters(c, v, nil)
}
// LoadWithParameters loads AWS Parameter Store parameters based on the defined tags, performing parameter
// substitution on field tags using data-driven templates from "text/template".
//
// When a source type is an array, it is assumed the parameter being loaded
// is a comma separated list. The list will be split and converted to
// match the array's typing.
//
// You can ignore a field by using "-" for a fields tag. Unexported fields are also ignored.
func LoadWithParameters(c ssmiface.SSMAPI, v interface{}, data interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidTypeError{Type: reflect.TypeOf(v)}
}
t, err := walk(rv.Elem(), data)
if err != nil {
return err
}
return load(c, t)
}
// load fields from AWS Parameter Store
func load(c ssmiface.SSMAPI, f []*field) error {
plain, decrypt := partitionFields(f, func(x *field) bool {
return x.decrypt
})
err := batchIterateFields(plain, maxParameters, func(f []*field) error {
return loadParameters(c, f, false)
})
if err != nil {
return err
}
return batchIterateFields(decrypt, maxParameters, func(f []*field) error {
return loadParameters(c, f, true)
})
}
// in place half stable partition
func partitionFields(f []*field, suffix func(*field) bool) (p1, p2 []*field) {
var i int
for ; i < len(f); i++ {
if suffix(f[i]) {
break
}
}
for j := i + 1; j < len(f); j++ {
if !suffix(f[j]) {
f[i], f[j] = f[j], f[i]
i++
}
}
return f[:i], f[i:]
}
func batchIterateFields(f []*field, batchSize int, g func([]*field) error) error {
for i := 0; i < len(f); {
j := i + batchSize
if j > len(f) {
j = len(f)
}
if err := g(f[i:j]); err != nil {
return err
}
i = j
}
return nil
}
func loadParameters(c ssmiface.SSMAPI, f []*field, decrypt bool) error {
params, err := getParameters(c, f, decrypt)
if err != nil {
return err
}
idx := indexParameters(params)
for _, x := range f {
p, ok := idx[x.key]
if !ok {
return fmt.Errorf("failed to load parameter for key '%s'", x.key)
}
err = set(x, aws.StringValue(p.Value))
if err != nil {
switch err := err.(type) {
case *ConvertTypeError:
//enrich the error with the field
err.Field = x.field.Name
return err
}
return err
}
}
return nil
}
func getParameters(c ssmiface.SSMAPI, f []*field, decrypt bool) ([]*ssm.Parameter, error) {
res, err := c.GetParameters(&ssm.GetParametersInput{
Names: parameterNames(f),
WithDecryption: aws.Bool(decrypt),
})
if err != nil {
return nil, err
}
if len(res.InvalidParameters) != 0 {
return nil, fmt.Errorf("invalid parameters: %s",
strings.Join(aws.StringValueSlice(res.InvalidParameters), ", "),
)
}
return res.Parameters, nil
}
func parameterNames(f []*field) []*string {
names := make([]*string, len(f))
for i := range f {
names[i] = aws.String(f[i].key)
}
return names
}
func indexParameters(params []*ssm.Parameter) map[string]*ssm.Parameter {
idx := make(map[string]*ssm.Parameter, len(params))
for _, p := range params {
idx[aws.StringValue(p.Name)] = p
}
return idx
}
// walk the value recursively to initialize pointers and build a graph of fields and tag options
func walk(v reflect.Value, data interface{}) ([]*field, error) {
p := make([]*field, 0)
t := v.Type()
for i := 0; i < v.NumField(); i++ {
fv := v.Field(i)
ft := t.Field(i)
// ignore unexported field
if ft.PkgPath != "" {
continue
}
// handles initializing a ptr and gets the underlying value to operate on
if fv.Kind() == reflect.Ptr {
fv.Set(reflect.New(fv.Type().Elem()))
fv = reflect.Indirect(fv)
}
pf, err := tag(ft, data)
if err != nil {
return nil, err
}
if pf != nil {
pf.field = ft
pf.value = fv
p = append(p, pf)
} else {
// only walk down embedded structs with no 'ssm' tag
switch fv.Kind() {
case reflect.Struct:
tags, err := walk(fv, data)
if err != nil {
return nil, err
}
p = append(p, tags...)
continue
}
}
}
return p, nil
}
// tag parses the ssm tag from a given field
func tag(f reflect.StructField, data interface{}) (*field, error) {
t := f.Tag.Get("ssm")
if t == "" || t == "-" {
return nil, nil
}
o := strings.Split(t, ",")
fld := newField(strings.TrimSpace(o[0]), false)
if fld.key == "" {
return nil, &TagParseError{Tag: t, Field: f.Name}
}
tpl, err := template.New(fld.key).Parse(fld.key)
if err == nil {
b := &bytes.Buffer{}
err = tpl.Execute(b, data)
if err == nil {
fld.key = b.String()
}
}
for _, option := range o[1:] {
switch strings.TrimSpace(option) {
case "decrypt":
fld.decrypt = true
case "json":
fld.json = true
}
}
return fld, nil
}
// set will attempt to set the underlying value based on the value's type
func set(f *field, s string) error {
v := f.value
if !v.CanSet() {
return errors.New(v.Type().String() + " cannot be set")
}
if u := unmarshaler(v); u != nil {
if f.json {
return fmt.Errorf("cannot use 'json' option on a type with a custom unmarshaller: %s %s", f.field.Name, f.field.Type.String())
}
return u.UnmarshalParameter(s)
}
if f.json {
return setJSON(f, s)
}
// special case with time.Duration and assignable types
if v.Type().AssignableTo(durationType) {
if p, err := time.ParseDuration(s); err == nil {
v.Set(reflect.ValueOf(p))
return nil
}
}
switch v.Kind() {
// handles the case data types are wrapped in other constructs, EG slices
case reflect.Ptr:
// create new pointer to a zero value
new := reflect.New(v.Type().Elem())
set(&field{value: new.Elem()}, s)
// assign new pointer
v.Set(new)
break
case reflect.Slice:
// we assume the list is separated by commas
l := strings.Split(s, ",")
sz := len(l)
v.Set(reflect.MakeSlice(v.Type(), sz, sz))
for i, w := range l {
set(&field{value: v.Index(i)}, w)
}
break
case reflect.String:
v.SetString(s)
break
case reflect.Bool:
n, err := strconv.ParseBool(s)
if err != nil {
return &ConvertTypeError{
Type: v.Type().String(),
Value: s,
}
}
v.SetBool(n)
break
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || v.OverflowInt(n) {
return &ConvertTypeError{
Type: v.Type().String(),
Value: s,
}
}
v.SetInt(n)
break
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || v.OverflowUint(n) {
return &ConvertTypeError{
Type: v.Type().String(),
Value: s,
}
}
v.SetUint(n)
break
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
return &ConvertTypeError{
Type: v.Type().String(),
Value: s,
}
}
v.SetFloat(n)
break
}
return nil
}
func unmarshaler(v reflect.Value) Unmarshaler {
// If v is a named type and is addressable,
// start with its address, so that if the type has pointer methods,
// we find them.
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
v = v.Addr()
}
if v.Type().NumMethod() > 0 && v.CanInterface() {
if u, ok := v.Interface().(Unmarshaler); ok {
return u
}
}
return nil
}
func setJSON(f *field, s string) error {
v := f.value
if v.Kind() != reflect.Ptr {
if !v.CanAddr() {
return fmt.Errorf("%s is not addressable", v.Type().String())
}
v = v.Addr()
}
if !v.CanInterface() {
return fmt.Errorf("%s is not interfaceable", v.Type().String())
}
if err := json.Unmarshal([]byte(s), v.Interface()); err != nil {
return fmt.Errorf("json unmarshal error for field '%s'", f.field.Name)
}
return nil
}