/
filter.go
341 lines (281 loc) · 7.47 KB
/
filter.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
package resource
import (
"errors"
"fmt"
"io/ioutil"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/apex/log"
"github.com/jckuester/awsls/resource"
"github.com/jckuester/awstools-lib/terraform"
"gopkg.in/yaml.v2"
)
// Filter represents the content of a yaml file that is used to filter resources for deletion.
type Filter map[string][]TypeFilter
// TypeFilter represents an entry in the yaml file to filter the resources of a particular resource type.
type TypeFilter struct {
ID *StringFilter `yaml:",omitempty"`
Tagged *bool `yaml:",omitempty"`
Tags map[string]StringFilter `yaml:",omitempty"`
Created *Created `yaml:",omitempty"`
}
type StringMatcher interface {
matches(string) (bool, error)
}
type StringFilter struct {
Pattern string `yaml:",omitempty"`
Negate bool
}
type CreatedTime struct {
time.Time `yaml:",omitempty"`
}
type Created struct {
Before *CreatedTime `yaml:",omitempty"`
After *CreatedTime `yaml:",omitempty"`
}
// NewFilter creates a resource filter defined via a given path to a yaml file.
func NewFilter(path string) (*Filter, error) {
var cfg Filter
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
err = yaml.UnmarshalStrict(data, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
// Validate checks if all resource types appearing in the config are currently supported.
func (f Filter) Validate() error {
for _, rType := range f.Types() {
if !resource.IsSupportedType(rType) {
return fmt.Errorf("unsupported resource type: %s", rType)
}
// there is currently a bug that getting the state for aws_key_alias kills the Terraform provider process
if rType == "aws_kms_alias" {
return fmt.Errorf("unsupported resource type: %s", rType)
}
}
return nil
}
// Types returns all the resource types in the config in their dependency order.
func (f Filter) Types() []string {
resTypes := make([]string, 0, len(f))
for k := range f {
resTypes = append(resTypes, k)
}
sort.Slice(resTypes, func(i, j int) bool {
return DependencyOrder[resTypes[i]] > DependencyOrder[resTypes[j]]
})
return resTypes
}
// MatchID checks whether a resource ID matches the filter.
func (f TypeFilter) matchID(id string) bool {
if f.ID == nil {
return true
}
if ok, err := f.ID.matches(id); ok {
if err != nil {
log.WithError(err).Fatal("failed to match ID")
}
return true
}
return false
}
// MatchTagged filters resources with a non-empty or empty tag set.
func (f TypeFilter) MatchTagged(tags map[string]string) bool {
if f.Tagged == nil {
return true
}
if *f.Tagged && len(tags) != 0 {
return true
}
if !*f.Tagged && len(tags) == 0 {
return true
}
return false
}
// MatchTags checks whether a resource's tag set matches the filter.
func (f TypeFilter) MatchTags(tags map[string]string) bool {
return f.matchIncludedTags(tags) && f.matchExcludedTags(tags)
}
// matchIncludedTags checks for tags that must be included in a resource's tag set.
func (f TypeFilter) matchIncludedTags(tags map[string]string) bool {
tagFilters := notNegatedTagFilterExpr(f.Tags)
if len(tagFilters) == 0 {
return true
}
for key, valueFilter := range tagFilters {
value, ok := tags[key]
if !ok {
return false
}
if match, err := valueFilter.matches(value); !match {
if err != nil {
log.WithError(err).Fatal("failed to match tags")
}
return false
}
}
return true
}
// matchExcludedTags checks for tags that must not exist in a resource's tag set.
func (f TypeFilter) matchExcludedTags(tags map[string]string) bool {
tagFilters := negatedTagFilterExpr(f.Tags)
if len(tagFilters) == 0 {
return true
}
for key, valueFilter := range tagFilters {
value, ok := tags[key]
if !ok {
return true
}
if match, err := valueFilter.matches(value); !match {
if err != nil {
log.WithError(err).Fatal("failed to match tags")
}
return true
}
}
return false
}
// notNegatedTagFilterExpr returns tag filter expressions where keys are not surrounded by NOT(...).
func notNegatedTagFilterExpr(tags map[string]StringFilter) map[string]StringFilter {
result := map[string]StringFilter{}
for key, value := range tags {
if !isNegatedTagKey(key) {
result[key] = value
}
}
return result
}
// notNegatedTagFilterExpr returns tag filter expressions where keys are surrounded by NOT(...).
func negatedTagFilterExpr(tags map[string]StringFilter) map[string]StringFilter {
result := map[string]StringFilter{}
for key, value := range tags {
if isNegatedTagKey(key) {
key = strings.TrimPrefix(key, "NOT(")
key = strings.TrimSuffix(key, ")")
result[key] = value
}
}
return result
}
func isNegatedTagKey(key string) bool {
return strings.HasPrefix(key, "NOT(") && strings.HasSuffix(key, ")")
}
func (f TypeFilter) matchCreated(creationTime *time.Time) bool {
if f.Created == nil {
return true
}
if creationTime == nil {
return false
}
createdAfter := true
if f.Created.After != nil {
createdAfter = creationTime.Unix() > f.Created.After.Unix()
}
createdBefore := true
if f.Created.Before != nil {
createdBefore = creationTime.Unix() < f.Created.Before.Unix()
}
return createdAfter && createdBefore
}
// Match checks whether a resource matches the filter criteria.
func (f Filter) Match(r terraform.Resource) bool {
resTypeFilters, found := f[r.Type]
if !found {
return false
}
if len(resTypeFilters) == 0 {
return true
}
for _, rtf := range resTypeFilters {
if rtf.MatchTagged(r.Tags) &&
rtf.MatchTags(r.Tags) &&
rtf.matchID(r.ID) &&
rtf.matchCreated(r.CreatedAt) {
return true
}
}
return false
}
func (f *StringFilter) matches(s string) (bool, error) {
ok, err := regexp.MatchString(f.Pattern, s)
if err != nil {
return false, err
}
if f.Negate {
return !ok, nil
}
return ok, err
}
func (f *StringFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v string
if err := unmarshal(&v); err != nil {
return err
}
if strings.HasPrefix(v, "NOT(") && strings.HasSuffix(v, ")") {
*f = StringFilter{strings.TrimSuffix(strings.TrimPrefix(v, "NOT("), ")"), true}
} else {
*f = StringFilter{v, false}
}
return nil
}
func (c *CreatedTime) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v interface{}
if err := unmarshal(&v); err != nil {
return err
}
switch value := v.(type) {
case time.Time:
t, _ := v.(time.Time)
*c = CreatedTime{t}
return nil
case string:
d, err := time.ParseDuration(value)
if err == nil {
*c = CreatedTime{time.Now().UTC().Add(-d)}
return nil
}
var t time.Time
err = yaml.Unmarshal([]byte("!!timestamp "+value), &t)
if err == nil {
*c = CreatedTime{t}
return nil
}
if strings.HasSuffix(value, "d") {
d, err := strconv.ParseInt(value[0:len(value)-1], 10, 32)
if err == nil {
*c = CreatedTime{time.Now().UTC().AddDate(0, 0, -int(d))}
return nil
}
}
if strings.HasSuffix(value, "w") {
w, err := strconv.ParseInt(value[0:len(value)-1], 10, 32)
if err == nil {
*c = CreatedTime{time.Now().UTC().AddDate(0, 0, -int(w*7))}
return nil
}
}
if strings.HasSuffix(value, "M") {
m, err := strconv.ParseInt(value[0:len(value)-1], 10, 32)
if err == nil {
*c = CreatedTime{time.Now().UTC().AddDate(0, -int(m), 0)}
return nil
}
}
if strings.HasSuffix(value, "y") {
y, err := strconv.ParseInt(value[0:len(value)-1], 10, 32)
if err == nil {
*c = CreatedTime{time.Now().UTC().AddDate(-int(y), 0, 0)}
return nil
}
}
}
return errors.New("invalid created time")
}