-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfilters.go
More file actions
230 lines (203 loc) · 6.36 KB
/
filters.go
File metadata and controls
230 lines (203 loc) · 6.36 KB
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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package connection
import (
"fmt"
"slices"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
type DiscoveryFilters struct {
Ec2 Ec2DiscoveryFilters
Ecr EcrDiscoveryFilters
Ecs EcsDiscoveryFilters
General GeneralDiscoveryFilters
}
func DiscoveryFiltersFromOpts(opts map[string]string) DiscoveryFilters {
d := DiscoveryFilters{
General: GeneralDiscoveryFilters{
Regions: parseCsvSliceOpt(opts, "regions"),
ExcludeRegions: parseCsvSliceOpt(opts, "exclude:regions"),
Tags: parseMapOpt(opts, "tag:"),
ExcludeTags: parseMapOpt(opts, "exclude:tag:"),
},
Ec2: Ec2DiscoveryFilters{
InstanceIds: parseCsvSliceOpt(opts, "ec2:instance-ids"),
ExcludeInstanceIds: parseCsvSliceOpt(opts, "ec2:exclude:instance-ids"),
},
Ecr: EcrDiscoveryFilters{
Tags: parseCsvSliceOpt(opts, "ecr:tags"),
ExcludeTags: parseCsvSliceOpt(opts, "ecr:exclude:tags"),
},
Ecs: EcsDiscoveryFilters{
OnlyRunningContainers: parseBoolOpt(opts, "ecs:only-running-containers", false),
DiscoverInstances: parseBoolOpt(opts, "ecs:discover-instances", false),
DiscoverImages: parseBoolOpt(opts, "ecs:discover-images", false),
},
}
// TODO: backward compatibility, remove in future versions
ec2Tags := parseMapOpt(opts, "ec2:tag:")
ec2ExcludeTags := parseMapOpt(opts, "ec2:exclude:tag:")
for k, v := range ec2Tags {
if _, exists := d.General.Tags[k]; !exists {
d.General.Tags[k] = v
}
}
for k, v := range ec2ExcludeTags {
if _, exists := d.General.ExcludeTags[k]; !exists {
d.General.ExcludeTags[k] = v
}
}
return d
}
type GeneralDiscoveryFilters struct {
Regions []string
ExcludeRegions []string
// note: values can be in a CSV format, e.g. "env": "prod,staging"
Tags map[string]string
// note: values can be in a CSV format, e.g. "env": "prod,staging"
ExcludeTags map[string]string
}
func (f GeneralDiscoveryFilters) HasTags() bool {
return len(f.Tags) > 0 || len(f.ExcludeTags) > 0
}
// helper function to improve the readability of filter application
// some resources do not support server-side filtering, so we need to apply filters client-side
func (f GeneralDiscoveryFilters) IsFilteredOutByTags(resourceTags map[string]string) bool {
return !f.MatchesIncludeTags(resourceTags) || f.MatchesExcludeTags(resourceTags)
}
func (f GeneralDiscoveryFilters) MatchesIncludeTags(resourceTags map[string]string) bool {
if len(f.Tags) == 0 {
return true
}
for k, csv := range f.Tags {
for v := range strings.SplitSeq(csv, ",") {
if tagValue, ok := resourceTags[k]; ok && tagValue == v {
return true
}
}
}
return false
}
// note: if this function returns `true`, it means that the resource should be skipped
func (f GeneralDiscoveryFilters) MatchesExcludeTags(resourceTags map[string]string) bool {
for k, csv := range f.ExcludeTags {
for v := range strings.SplitSeq(csv, ",") {
if tagValue, ok := resourceTags[k]; ok && tagValue == v {
return true
}
}
}
return false
}
// when possible, we should use AWS API filters to reduce data transfer
func (f GeneralDiscoveryFilters) ToServerSideEc2Filters() []ec2types.Filter {
filters := []ec2types.Filter{}
for k, v := range f.Tags {
filters = append(filters, ec2types.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", k)),
Values: strings.Split(v, ","),
})
}
return filters
}
type Ec2DiscoveryFilters struct {
InstanceIds []string
ExcludeInstanceIds []string
}
// note: if this function returns `true`, it means that the resource should be skipped
func (f Ec2DiscoveryFilters) MatchesExcludeInstanceIds(instanceId *string) bool {
return instanceId != nil && slices.Contains(f.ExcludeInstanceIds, *instanceId)
}
type EcrDiscoveryFilters struct {
Tags []string
ExcludeTags []string
}
func (f EcrDiscoveryFilters) IsFilteredOutByTags(imageTags []string) bool {
return !f.MatchesIncludeTags(imageTags) || f.MatchesExcludeTags(imageTags)
}
func (f EcrDiscoveryFilters) MatchesIncludeTags(imageTags []string) bool {
if len(f.Tags) == 0 {
return true
}
for _, filterTag := range f.Tags {
if slices.Contains(imageTags, filterTag) {
return true
}
}
return false
}
// note: if this function returns `true`, it means that the resource should be skipped
func (f EcrDiscoveryFilters) MatchesExcludeTags(imageTags []string) bool {
for _, filterTag := range f.ExcludeTags {
if slices.Contains(imageTags, filterTag) {
return true
}
}
return false
}
type EcsDiscoveryFilters struct {
OnlyRunningContainers bool
DiscoverImages bool
DiscoverInstances bool
}
func (f EcsDiscoveryFilters) MatchesOnlyRunningContainers(containerState string) bool {
if !f.OnlyRunningContainers {
return true
}
return containerState == "RUNNING"
}
// Given a key-value pair that matches a key, return the boolean value of the key.
// If the key is not found or the value cannot be parsed as a boolean, return the default value.
// Example: key = "ecs:only-running-containers", opts = {"ecs:only-running-containers": "true"}
// Returns: true
func parseBoolOpt(opts map[string]string, key string, defaultVal bool) bool {
for k, v := range opts {
if k == key {
parsed, err := strconv.ParseBool(v)
if err == nil {
return parsed
}
}
}
return defaultVal
}
// Given a map of options and a key prefix, return a map of key-value pairs
// where the keys start with the given prefix, with the prefix removed.
// Example:
// keyPrefix = "tag:"
// opts = {"tag:env": "prod", "tag:role": "web"}
// returns {"env": "prod", "role": "web"}
func parseMapOpt(opts map[string]string, keyPrefix string) map[string]string {
res := map[string]string{}
for k, v := range opts {
if k == "" || v == "" {
continue
}
if !strings.HasPrefix(k, keyPrefix) {
continue
}
res[strings.TrimPrefix(k, keyPrefix)] = v
}
return res
}
// Given a map of options and a key, return a slice of strings
// where the key matches the given key. The value is split by commas.
// Example:
// key = "regions"
// opts = {"regions": "us-east-1,us-west-2"}
// returns []string{"us-east-1", "us-west-2"}
func parseCsvSliceOpt(opts map[string]string, key string) []string {
res := []string{}
for k, v := range opts {
if k == "" || v == "" {
continue
}
if k == key {
res = append(res, strings.Split(v, ",")...)
}
}
return res
}