/
web_acls.go
187 lines (182 loc) · 6.2 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
package wafregional
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/wafregional"
"github.com/aws/aws-sdk-go-v2/service/wafregional/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"
)
//go:generate cq-gen --resource web_acls --config web_acls.hcl --output .
func WebAcls() *schema.Table {
return &schema.Table{
Name: "aws_wafregional_web_acls",
Description: "Contains the Rules that identify the requests that you want to allow, block, or count.",
Resolver: fetchWafregionalWebAcls,
Multiplex: client.ServiceAccountRegionMultiplexer("waf-regional"),
IgnoreError: client.IgnoreCommonErrors,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"account_id", "region", "id"}},
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "tags",
Description: "Web ACL tags.",
Type: schema.TypeJSON,
Resolver: resolveWafregionalWebACLTags,
},
{
Name: "default_action",
Description: "Specifies how you want AWS WAF to respond to requests that match the settings in a Rule",
Type: schema.TypeString,
Resolver: schema.PathResolver("DefaultAction.Type"),
},
{
Name: "id",
Description: "A unique identifier for a WebACL",
Type: schema.TypeString,
Resolver: schema.PathResolver("WebACLId"),
},
{
Name: "metric_name",
Description: "A friendly name or description for the metrics for this WebACL",
Type: schema.TypeString,
},
{
Name: "name",
Description: "A friendly name or description of the WebACL",
Type: schema.TypeString,
},
{
Name: "arn",
Description: "Tha Amazon Resource Name (ARN) of the web ACL.",
Type: schema.TypeString,
Resolver: schema.PathResolver("WebACLArn"),
},
},
Relations: []*schema.Table{
{
Name: "aws_wafregional_web_acl_rules",
Description: "The action for each Rule in a WebACL",
Resolver: schema.PathTableResolver("Rules"),
Columns: []schema.Column{
{
Name: "web_acl_cq_id",
Description: "Unique CloudQuery ID of aws_wafregional_web_acls table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "priority",
Description: "Specifies the order in which the Rules in a WebACL are evaluated",
Type: schema.TypeBigInt,
},
{
Name: "rule_id",
Description: "The RuleId for a Rule",
Type: schema.TypeString,
},
{
Name: "action",
Description: "Specifies how you want AWS WAF to respond to requests that match the settings in a Rule",
Type: schema.TypeString,
Resolver: schema.PathResolver("Action.Type"),
},
{
Name: "excluded_rules",
Description: "An array of rules to exclude from a rule group",
Type: schema.TypeStringArray,
Resolver: resolveWebACLRulesExcludedRules,
},
{
Name: "override_action",
Description: "Describes an override action for the rule.",
Type: schema.TypeString,
Resolver: schema.PathResolver("OverrideAction.Type"),
IgnoreInTests: true,
},
{
Name: "type",
Description: "The rule type, either REGULAR, as defined by Rule, RATE_BASED, as defined by RateBasedRule, or GROUP, as defined by RuleGroup",
Type: schema.TypeString,
},
},
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchWafregionalWebAcls(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
cl := meta.(*client.Client)
svc := cl.Services().WafRegional
var params wafregional.ListWebACLsInput
for {
result, err := svc.ListWebACLs(ctx, ¶ms, func(o *wafregional.Options) { o.Region = cl.Region })
if err != nil {
return diag.WrapError(err)
}
for _, item := range result.WebACLs {
detail, err := svc.GetWebACL(
ctx,
&wafregional.GetWebACLInput{WebACLId: item.WebACLId},
func(o *wafregional.Options) { o.Region = cl.Region },
)
if err != nil {
return diag.WrapError(err)
}
if detail.WebACL == nil {
continue
}
res <- *detail.WebACL
}
if aws.ToString(result.NextMarker) == "" {
break
}
params.NextMarker = result.NextMarker
}
return nil
}
func resolveWafregionalWebACLTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
cl := meta.(*client.Client)
svc := cl.Services().WafRegional
params := wafregional.ListTagsForResourceInput{ResourceARN: resource.Item.(types.WebACL).WebACLArn}
tags := make(map[string]string)
for {
result, err := svc.ListTagsForResource(ctx, ¶ms)
if err != nil {
return diag.WrapError(err)
}
if result.TagInfoForResource != nil {
client.TagsIntoMap(result.TagInfoForResource.TagList, tags)
}
if aws.ToString(result.NextMarker) == "" {
break
}
params.NextMarker = result.NextMarker
}
return diag.WrapError(resource.Set(c.Name, tags))
}
func resolveWebACLRulesExcludedRules(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rule := resource.Item.(types.ActivatedRule)
ids := make([]string, len(rule.ExcludedRules))
for _, item := range rule.ExcludedRules {
if item.RuleId != nil {
ids = append(ids, *item.RuleId)
}
}
return diag.WrapError(resource.Set(c.Name, ids))
}