/
table_aws_wafregional_rule_group.go
322 lines (278 loc) · 9.51 KB
/
table_aws_wafregional_rule_group.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
package aws
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"
wafregionalv1 "github.com/aws/aws-sdk-go/service/wafregional"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
//// TABLE DEFINITION
func tableAwsWafRegionalRuleGroup(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_wafregional_rule_group",
Description: "AWS WAF Regional Rule Group",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"rule_group_id"}),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"NonexistentItemException", "WAFNonexistentItemException"}),
},
Hydrate: getWafRegionalRuleGroup,
Tags: map[string]string{"service": "waf-regional", "action": "GetRuleGroup"},
},
List: &plugin.ListConfig{
Hydrate: listWafRegionalRuleGroups,
Tags: map[string]string{"service": "waf-regional", "action": "ListRuleGroups"},
},
GetMatrixItemFunc: SupportedRegionMatrix(wafregionalv1.EndpointsID),
HydrateConfig: []plugin.HydrateConfig{
{
Func: getWafRegionalRuleGroup,
Tags: map[string]string{"service": "waf-regional", "action": "GetRuleGroup"},
},
{
Func: getWafRegionalRuleGroupActivatedRules,
Tags: map[string]string{"service": "waf-regional", "action": "ListActivatedRulesInRuleGroup"},
},
{
Func: listTagsForWafRegionalRuleGroup,
Tags: map[string]string{"service": "waf-regional", "action": "ListTagsForResource"},
},
},
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "name",
Description: "The name of the rule group.",
Type: proto.ColumnType_STRING,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the entity.",
Type: proto.ColumnType_STRING,
Hydrate: getWafRegionalRuleGroupArn,
Transform: transform.FromValue(),
},
{
Name: "rule_group_id",
Description: "A unique identifier for the rule group.",
Type: proto.ColumnType_STRING,
},
{
Name: "metric_name",
Description: "A friendly name or description for the metrics for this RuleGroup.",
Type: proto.ColumnType_STRING,
Hydrate: getWafRegionalRuleGroup,
},
{
Name: "activated_rules",
Description: "A list of activated rules associated with the resource.",
Type: proto.ColumnType_JSON,
Hydrate: getWafRegionalRuleGroupActivatedRules,
},
{
Name: "tags_src",
Description: "A list of tags associated with the resource.",
Type: proto.ColumnType_JSON,
Hydrate: listTagsForWafRegionalRuleGroup,
Transform: transform.FromField("TagInfoForResource.TagList"),
},
// steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name", "RuleGroup.Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: listTagsForWafRegionalRuleGroup,
Transform: transform.FromField("TagInfoForResource.TagList").Transform(wafRegionalRuleGroupTagListToTurbotTags),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Hydrate: getWafRegionalRuleGroupArn,
Transform: transform.FromValue().Transform(transform.EnsureStringArray),
},
}),
}
}
//// LIST FUNCTION
func listWafRegionalRuleGroups(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := WAFRegionalClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.listWafRegionalRuleGroups", "client_error", err)
return nil, err
}
// Unsupported region, return no data
if svc == nil {
return nil, nil
}
// List all rule groups
pagesLeft := true
params := &wafregional.ListRuleGroupsInput{
Limit: int32(100),
}
// Reduce the basic request limit down if the user has only requested a small number of rows
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < int64(params.Limit) {
params.Limit = int32(*limit)
}
}
for pagesLeft {
// apply rate limiting
d.WaitForListRateLimit(ctx)
response, err := svc.ListRuleGroups(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.listWafRegionalRuleGroups", "api_error", err)
return nil, err
}
for _, ruleGroups := range response.RuleGroups {
d.StreamListItem(ctx, ruleGroups)
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if response.NextMarker != nil {
pagesLeft = true
params.NextMarker = response.NextMarker
} else {
pagesLeft = false
}
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getWafRegionalRuleGroup(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var id string
if h.Item != nil {
dataMap, err := getWafRegionalRuleGroupData(ctx, d, h)
data := dataMap.(map[string]string)
if err != nil {
return err, nil
}
id = data["rule_group_id"]
} else {
id = d.EqualsQuals["rule_group_id"].GetStringValue()
if id == "" {
return nil, nil
}
}
// Create session
svc, err := WAFRegionalClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.getWafRegionalRuleGroup", "client_error", err)
return nil, err
}
// Unsupported region, return no data
if svc == nil {
return nil, nil
}
params := &wafregional.GetRuleGroupInput{
RuleGroupId: &id,
}
op, err := svc.GetRuleGroup(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.getWafRegionalRuleGroup", "api_error", err)
return nil, err
}
return op.RuleGroup, nil
}
func getWafRegionalRuleGroupActivatedRules(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
dataMap, err := getWafRegionalRuleGroupData(ctx, d, h)
if err != nil {
return err, nil
}
data := dataMap.(map[string]string)
id := data["rule_group_id"]
// Create session
svc, err := WAFRegionalClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.getWafRegionalRuleGroupActivatedRules", "client_error", err)
return nil, err
}
params := &wafregional.ListActivatedRulesInRuleGroupInput{
RuleGroupId: &id,
}
op, err := svc.ListActivatedRulesInRuleGroup(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.getWafRegionalRuleGroupActivatedRules", "api_error", err)
return nil, err
}
return op, nil
}
func listTagsForWafRegionalRuleGroup(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
dataMap, err := getWafRegionalRuleGroupData(ctx, d, h)
data := dataMap.(map[string]string)
if err != nil {
return err, nil
}
// Create session
svc, err := WAFRegionalClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.listTagsForWafRegionalRuleGroup", "client_error", err)
return nil, err
}
// Build param with maximum limit set
param := &wafregional.ListTagsForResourceInput{
ResourceARN: aws.String(data["rule_group_arn"]),
Limit: int32(100),
}
ruleGroupTags, err := svc.ListTagsForResource(ctx, param)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.listTagsForWafRegionalRuleGroup", "api_error", err)
return nil, err
}
return ruleGroupTags, nil
}
func getWafRegionalRuleGroupArn(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
dataMap, err := getWafRegionalRuleGroupData(ctx, d, h)
data := dataMap.(map[string]string)
if err != nil {
plugin.Logger(ctx).Error("aws_wafregional_rule_group.getWafRegionalRuleGroupArn", "error", err)
return err, nil
}
return data["rule_group_arn"], nil
}
func getWafRegionalRuleGroupData(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
data := map[string]string{}
commonData, err := getCommonColumns(ctx, d, h)
if err != nil {
return nil, err
}
commonColumnData := commonData.(*awsCommonColumnData)
region := d.EqualsQualString(matrixKeyRegion)
switch item := h.Item.(type) {
case *types.RuleGroup:
data["rule_group_id"] = *item.RuleGroupId
data["rule_group_arn"] = "arn:" + commonColumnData.Partition + ":waf-regional:" + region + ":" + commonColumnData.AccountId + ":rulegroup/" + *item.RuleGroupId
case types.RuleGroupSummary:
data["rule_group_id"] = *item.RuleGroupId
data["rule_group_arn"] = "arn:" + commonColumnData.Partition + ":waf-regional:" + region + ":" + commonColumnData.AccountId + ":rulegroup/" + *item.RuleGroupId
}
return data, nil
}
//// TRANSFORM FUNCTIONS
func wafRegionalRuleGroupTagListToTurbotTags(ctx context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(*wafregional.ListTagsForResourceOutput)
if data.TagInfoForResource.TagList == nil || len(data.TagInfoForResource.TagList) < 1 {
return nil, nil
}
// Mapping the resource tags inside turbotTags
var turbotTagsMap map[string]string
if data.TagInfoForResource.TagList != nil {
turbotTagsMap = map[string]string{}
for _, i := range data.TagInfoForResource.TagList {
turbotTagsMap[*i.Key] = *i.Value
}
}
return turbotTagsMap, nil
}