-
Notifications
You must be signed in to change notification settings - Fork 1
/
serializer.go
228 lines (160 loc) · 5.38 KB
/
serializer.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
package pms
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/mariotoffia/ssm/internal/common"
"github.com/mariotoffia/ssm/parser"
"github.com/mariotoffia/ssm/support"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// Serializer handles the parameter store communication
type Serializer struct {
// AWS Config to use when communicating
config aws.Config
// The name of the service using this library
service string
// Default tier if not specified.
tier types.ParameterTier
}
// SeDefaultTier allows for change the tier. By default the
// serializer uses the standard tier.
func (p *Serializer) SeDefaultTier(tier types.ParameterTier) *Serializer {
p.tier = tier
return p
}
// NewFromConfig creates a repository using a existing configuration
func NewFromConfig(config aws.Config, service string) *Serializer {
return &Serializer{config: config, service: service,
tier: types.ParameterTierStandard}
}
// New creates a repository using the default AWS configuration
func New(service string) (*Serializer, error) {
awscfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return &Serializer{}, errors.Wrapf(err, "Failed to load AWS config")
}
return &Serializer{config: awscfg, service: service,
tier: types.ParameterTierStandard}, nil
}
// Get parameters from the parameter store and populates the node graph with values.
// Any fields that was not able to be set is reported in the FullNameField string map.
// FullNameField do not include those fields filtered out in exclusion filter.
func (p *Serializer) Get(node *parser.StructNode,
filter *support.FieldFilters) (map[string]support.FullNameField, error) {
m := map[string]*parser.StructNode{}
parser.NodesToParameterMap(node, m, filter, []string{"pms"})
paths := parser.ExtractPaths(m)
isSecure := isSecure(node)
params := &ssm.GetParametersInput{
Names: paths,
WithDecryption: aws.Bool(isSecure),
}
log.Debug().Str("svc", p.service).
Str("package", "pms").
Str("method", "Get").
Msgf("Fetching: %v", params)
prms, invalid, err := p.getFromAws(params)
if err != nil {
return nil, err
}
im := p.handleInvalidRequestParameters(invalid, m, "find")
err = p.populate(node, prms)
return im, err
}
func isSecure(node *parser.StructNode) bool {
if node.HasChildren() {
for _, n := range node.Childs {
if isSecure(&n) {
return true
}
}
}
if tag, ok := ToPmsTag(node); ok {
return tag.Secure()
}
return false
}
// Upsert stores the node values (after filter is applied). If any
// error occurs it will return that in the support.FullNameField.Error
// field. Thus it is possible to track which fields did not get written
// to the Parameter store and hence needs to be handeled.
func (p *Serializer) Upsert(node *parser.StructNode,
filter *support.FieldFilters) map[string]support.FullNameField {
m := map[string]*parser.StructNode{}
parser.NodesToParameterMap(node, m, filter, []string{"pms"})
im := map[string]support.FullNameField{}
if len(m) == 0 {
return im
}
params := p.toPutParameters(m)
client := ssm.NewFromConfig(p.config)
for _, prm := range params {
tags := prm.Tags
prm.Tags = nil
resp, err := client.PutParameter(context.Background(), &prm)
if err != nil {
im[node.FqName] = p.createFullNameFieldNode(*prm.Name, err, m[*prm.Name])
log.Debug().Str("svc", p.service).Msgf("Failed to write %v error: %v", im[node.FqName], err)
} else {
log.Debug().Str("svc", p.service).Msgf("Successfully wrote %v", resp)
if len(tags) > 0 {
resp, err := client.AddTagsToResource(context.Background(), &ssm.AddTagsToResourceInput{
ResourceId: prm.Name,
ResourceType: types.ResourceTypeForTaggingParameter,
Tags: tags,
})
if err != nil {
im[node.FqName] = p.createFullNameFieldNode(*prm.Name, err, m[*prm.Name])
log.Debug().Str("svc", p.service).Msgf("Failed to write tags on %v error: %v", im[node.FqName], err)
} else {
log.Debug().Str("svc", p.service).Msgf("Successfully wrote tags %v", resp)
}
} else {
log.Debug().Str("svc", p.service).Msgf("No tags to add to %s - skipping", *prm.Name)
}
}
}
return im
}
func (p *Serializer) handleInvalidRequestParameters(
invalid []string,
m map[string]*parser.StructNode,
operation string) map[string]support.FullNameField {
im := map[string]support.FullNameField{}
if len(invalid) > 0 {
for _, name := range invalid {
if val, ok := m[name]; ok {
im[val.FqName] = support.FullNameField{RemoteName: val.Tag["pms"].GetFullName(),
LocalName: val.FqName, Field: val.Field, Value: val.Value}
} else {
log.Warn().Str("service", p.service).Msgf("Could not %s %s in node map", operation, name)
}
}
}
if len(im) > 0 {
for key, val := range im {
log.Debug().Msgf("not %s - %s [%s]", operation, key, val.RemoteName)
}
}
return im
}
func (p *Serializer) populate(node *parser.StructNode, params map[string]types.Parameter) error {
node.EnsureInstance(false)
if tag, ok := node.Tag["pms"]; ok {
if val, ok := params[tag.GetFullName()]; ok {
if tag.GetFullName() != "" {
common.SetStructValueFromString(node, *val.Name, *val.Value)
}
}
}
if node.HasChildren() {
for _, n := range node.Childs {
p.populate(&n, params)
}
}
return nil
}