/
web_acls.go
474 lines (458 loc) · 17.5 KB
/
web_acls.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package wafv2
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/aws/aws-sdk-go-v2/service/wafv2"
"github.com/aws/aws-sdk-go-v2/service/wafv2/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
type WebACLWrapper struct {
*types.WebACL
LoggingConfiguration *types.LoggingConfiguration
}
func Wafv2WebAcls() *schema.Table {
return &schema.Table{
Name: "aws_wafv2_web_acls",
Description: "A Web ACL defines a collection of rules to use to inspect and control web requests",
Resolver: fetchWafv2WebAcls,
Multiplex: client.ServiceAccountRegionScopeMultiplexer("waf-regional"),
IgnoreError: client.IgnoreCommonErrors,
DeleteFilter: client.DeleteAccountRegionScopeFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"account_id", "id"}},
Columns: []schema.Column{
{
Name: "account_id",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "scope",
Description: "Specifies whether this is for an Amazon CloudFront distribution or for a regional application.",
Type: schema.TypeString,
Resolver: client.ResolveWAFScope,
},
{
Name: "resources_for_web_acl",
Type: schema.TypeStringArray,
Resolver: resolveWafv2webACLResourcesForWebACL,
},
{
Name: "tags",
Type: schema.TypeJSON,
Resolver: resolveWafv2webACLTags,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the Web ACL that you want to associate with the resource. ",
Type: schema.TypeString,
Resolver: schema.PathResolver("ARN"),
},
{
Name: "default_action",
Description: "The action to perform if none of the Rules contained in the WebACL match. ",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("DefaultAction"),
},
{
Name: "id",
Description: "A unique identifier for the WebACL",
Type: schema.TypeString,
Resolver: schema.PathResolver("Id"),
},
{
Name: "name",
Description: "The name of the Web ACL",
Type: schema.TypeString,
},
{
Name: "visibility_config_cloud_watch_metrics_enabled",
Description: "A boolean indicating whether the associated resource sends metrics to CloudWatch",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.CloudWatchMetricsEnabled"),
},
{
Name: "visibility_config_metric_name",
Description: "A name of the CloudWatch metric",
Type: schema.TypeString,
Resolver: schema.PathResolver("VisibilityConfig.MetricName"),
},
{
Name: "visibility_config_sampled_requests_enabled",
Description: "A boolean indicating whether AWS WAF should store a sampling of the web requests that match the rules",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.SampledRequestsEnabled"),
},
{
Name: "capacity",
Description: "The web ACL capacity units (WCUs) currently being used by this web ACL",
Type: schema.TypeBigInt,
},
{
Name: "custom_response_bodies",
Description: "A map of custom response keys and content bodies",
Type: schema.TypeJSON,
},
{
Name: "description",
Description: "A description of the Web ACL that helps with identification.",
Type: schema.TypeString,
},
{
Name: "label_namespace",
Description: "The label namespace prefix for this web ACL",
Type: schema.TypeString,
},
{
Name: "managed_by_firewall_manager",
Description: "Indicates whether this web ACL is managed by AWS Firewall Manager",
Type: schema.TypeBool,
},
{
Name: "logging_configuration",
Description: "The LoggingConfiguration for the specified web ACL.",
Type: schema.TypeStringArray,
Resolver: schema.PathResolver("LoggingConfiguration.LogDestinationConfigs"),
},
},
Relations: []*schema.Table{
{
Name: "aws_wafv2_web_acl_rules",
Description: "A single rule, which you can use in a WebACL or RuleGroup to identify web requests that you want to allow, block, or count",
Resolver: schema.PathTableResolver("Rules"),
Columns: []schema.Column{
{
Name: "web_acl_cq_id",
Description: "Unique CloudQuery ID of aws_wafv2_web_acls table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "name",
Description: "The name of the rule",
Type: schema.TypeString,
},
{
Name: "priority",
Description: "If you define more than one Rule in a WebACL, AWS WAF evaluates each request against the Rules in order based on the value of Priority",
Type: schema.TypeInt,
},
{
Name: "statement",
Description: "The AWS WAF processing statement for the rule, for example ByteMatchStatement or SizeConstraintStatement. ",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("Statement"),
},
{
Name: "visibility_config_cloud_watch_metrics_enabled",
Description: "A boolean indicating whether the associated resource sends metrics to CloudWatch",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.CloudWatchMetricsEnabled"),
},
{
Name: "visibility_config_metric_name",
Description: "A name of the CloudWatch metric",
Type: schema.TypeString,
Resolver: schema.PathResolver("VisibilityConfig.MetricName"),
},
{
Name: "visibility_config_sampled_requests_enabled",
Description: "A boolean indicating whether AWS WAF should store a sampling of the web requests that match the rules",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.SampledRequestsEnabled"),
},
{
Name: "action",
Description: "The action that AWS WAF should take on a web request when it matches the rule statement",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("Action"),
},
{
Name: "override_action",
Description: "The override action to apply to the rules in a rule group",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("OverrideAction"),
},
{
Name: "labels",
Description: "Labels to apply to web requests that match the rule match statement",
Type: schema.TypeStringArray,
Resolver: resolveWafv2webACLRuleLabels,
},
},
},
{
Name: "aws_wafv2_web_acl_post_process_firewall_manager_rule_groups",
Description: "A rule group that's defined for an AWS Firewall Manager WAF policy. ",
Resolver: schema.PathTableResolver("PostProcessFirewallManagerRuleGroups"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "web_acl_cq_id",
Description: "Unique CloudQuery ID of aws_wafv2_web_acls table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "statement",
Description: "The processing guidance for an AWS Firewall Manager rule",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("FirewallManagerStatement"),
},
{
Name: "name",
Description: "The name of the rule group",
Type: schema.TypeString,
},
{
Name: "override_action",
Description: "The override action to apply to the rules in a rule group",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("OverrideAction"),
},
{
Name: "priority",
Description: "If you define more than one rule group in the first or last Firewall Manager rule groups, AWS WAF evaluates each request against the rule groups in order, starting from the lowest priority setting",
Type: schema.TypeInt,
},
{
Name: "visibility_config_cloud_watch_metrics_enabled",
Description: "A boolean indicating whether the associated resource sends metrics to CloudWatch",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.CloudWatchMetricsEnabled"),
},
{
Name: "visibility_config_metric_name",
Description: "A name of the CloudWatch metric",
Type: schema.TypeString,
Resolver: schema.PathResolver("VisibilityConfig.MetricName"),
},
{
Name: "visibility_config_sampled_requests_enabled",
Description: "A boolean indicating whether AWS WAF should store a sampling of the web requests that match the rules",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.SampledRequestsEnabled"),
},
},
},
{
Name: "aws_wafv2_web_acl_pre_process_firewall_manager_rule_groups",
Description: "A rule group that's defined for an AWS Firewall Manager WAF policy. ",
Resolver: schema.PathTableResolver("PreProcessFirewallManagerRuleGroups"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "web_acl_cq_id",
Description: "Unique CloudQuery ID of aws_wafv2_web_acls table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "statement",
Description: "The processing guidance for an AWS Firewall Manager rule",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("FirewallManagerStatement"),
},
{
Name: "name",
Description: "The name of the rule group",
Type: schema.TypeString,
},
{
Name: "override_action",
Description: "The override action to apply to the rules in a rule group",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("OverrideAction"),
},
{
Name: "priority",
Description: "If you define more than one rule group in the first or last Firewall Manager rule groups, AWS WAF evaluates each request against the rule groups in order, starting from the lowest priority setting",
Type: schema.TypeInt,
},
{
Name: "visibility_config_cloud_watch_metrics_enabled",
Description: "A boolean indicating whether the associated resource sends metrics to CloudWatch",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.CloudWatchMetricsEnabled"),
},
{
Name: "visibility_config_metric_name",
Description: "A name of the CloudWatch metric",
Type: schema.TypeString,
Resolver: schema.PathResolver("VisibilityConfig.MetricName"),
},
{
Name: "visibility_config_sampled_requests_enabled",
Description: "A boolean indicating whether AWS WAF should store a sampling of the web requests that match the rules",
Type: schema.TypeBool,
Resolver: schema.PathResolver("VisibilityConfig.SampledRequestsEnabled"),
},
},
},
{
Name: "aws_wafv2_web_acl_logging_configuration",
Description: "The LoggingConfiguration for the specified web ACL.",
Resolver: schema.PathTableResolver("LoggingConfiguration"),
Columns: []schema.Column{
{
Name: "web_acl_cq_id",
Description: "Unique CloudQuery ID of aws_wafv2_web_acls table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "log_destination_configs",
Description: "The Amazon Resource Names (ARNs) of the logging destinations that you want to associate with the web ACL.",
Type: schema.TypeStringArray,
},
{
Name: "resource_arn",
Description: "The Amazon Resource Name (ARN) of the web ACL that you want to associate with LogDestinationConfigs.",
Type: schema.TypeString,
},
{
Name: "logging_filter",
Description: "Filtering that specifies which web requests are kept in the logs and which are dropped. You can filter on the rule action and on the web request labels that were applied by matching rules during web ACL evaluation.",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("LoggingFilter"),
},
{
Name: "managed_by_firewall_manager",
Description: "Indicates whether the logging configuration was created by Firewall Manager, as part of an WAF policy configuration. If true, only Firewall Manager can modify or delete the configuration.",
Type: schema.TypeBool,
},
{
Name: "redacted_fields",
Description: "The parts of the request that you want redacted from the logs. For example, if you redact the cookie field, the cookie field in the firehose will be xxx. You can specify only the following fields for redaction: UriPath, QueryString, SingleHeader, Method, and JsonBody.",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("RedactedFields"),
},
},
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchWafv2WebAcls(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
c := meta.(*client.Client)
service := c.Services().WafV2
config := wafv2.ListWebACLsInput{
Scope: c.WAFScope,
Limit: aws.Int32(100),
}
for {
output, err := service.ListWebACLs(ctx, &config)
if err != nil {
return diag.WrapError(err)
}
for _, webAcl := range output.WebACLs {
webAclConfig := wafv2.GetWebACLInput{Id: webAcl.Id, Name: webAcl.Name, Scope: c.WAFScope}
webAclOutput, err := service.GetWebACL(ctx, &webAclConfig, func(options *wafv2.Options) {
options.Region = c.Region
})
if err != nil {
return diag.WrapError(err)
}
cfg := wafv2.GetLoggingConfigurationInput{
ResourceArn: webAclOutput.WebACL.ARN,
}
loggingConfigurationOutput, err := service.GetLoggingConfiguration(ctx, &cfg, func(options *wafv2.Options) {
options.Region = c.Region
})
if err != nil {
if client.IsAWSError(err, "WAFNonexistentItemException") {
c.Logger().Debug("Logging configuration not found for: %s", webAclOutput.WebACL.Name)
} else {
c.Logger().Error("GetLoggingConfiguration failed with error: %s", err.Error())
}
}
var webAclLoggingConfiguration *types.LoggingConfiguration
if loggingConfigurationOutput != nil {
webAclLoggingConfiguration = loggingConfigurationOutput.LoggingConfiguration
}
res <- &WebACLWrapper{
webAclOutput.WebACL,
webAclLoggingConfiguration,
}
}
if aws.ToString(output.NextMarker) == "" {
break
}
config.NextMarker = output.NextMarker
}
return nil
}
func resolveWafv2webACLResourcesForWebACL(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
webACL := resource.Item.(*WebACLWrapper)
cl := meta.(*client.Client)
service := cl.Services().WafV2
resourceArns := []string{}
if cl.WAFScope == types.ScopeCloudfront {
cloudfrontService := cl.Services().Cloudfront
params := &cloudfront.ListDistributionsByWebACLIdInput{
WebACLId: webACL.Id,
MaxItems: aws.Int32(100),
}
for {
output, err := cloudfrontService.ListDistributionsByWebACLId(ctx, params, func(options *cloudfront.Options) {
options.Region = cl.Region
})
if err != nil {
return diag.WrapError(err)
}
for _, item := range output.DistributionList.Items {
resourceArns = append(resourceArns, *item.ARN)
}
if aws.ToString(output.DistributionList.NextMarker) == "" {
break
}
params.Marker = output.DistributionList.NextMarker
}
} else {
output, err := service.ListResourcesForWebACL(ctx, &wafv2.ListResourcesForWebACLInput{WebACLArn: webACL.ARN})
if err != nil {
return diag.WrapError(err)
}
resourceArns = output.ResourceArns
}
return diag.WrapError(resource.Set(c.Name, resourceArns))
}
func resolveWafv2webACLTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
webACL := resource.Item.(*WebACLWrapper)
cl := meta.(*client.Client)
service := cl.Services().WafV2
// Resolve tags
outputTags := make(map[string]*string)
tagsConfig := wafv2.ListTagsForResourceInput{ResourceARN: webACL.ARN}
for {
tags, err := service.ListTagsForResource(ctx, &tagsConfig)
if err != nil {
return diag.WrapError(err)
}
for _, t := range tags.TagInfoForResource.TagList {
outputTags[*t.Key] = t.Value
}
if aws.ToString(tags.NextMarker) == "" {
break
}
tagsConfig.NextMarker = tags.NextMarker
}
return diag.WrapError(resource.Set(c.Name, outputTags))
}
func resolveWafv2webACLRuleLabels(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rule := resource.Item.(types.Rule)
labels := make([]string, len(rule.RuleLabels))
for i := range rule.RuleLabels {
labels[i] = aws.ToString(rule.RuleLabels[i].Name)
}
return diag.WrapError(resource.Set(c.Name, labels))
}