/
table_aws_config_rule.go
291 lines (250 loc) · 8.81 KB
/
table_aws_config_rule.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
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/configservice"
"github.com/aws/aws-sdk-go-v2/service/configservice/types"
configservicev1 "github.com/aws/aws-sdk-go/service/configservice"
"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 tableAwsConfigRule(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_config_rule",
Description: "AWS Config Rule",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"NoSuchConfigRuleException", "ResourceNotFoundException", "ValidationException"}),
},
Hydrate: getConfigRule,
Tags: map[string]string{"service": "config", "action": "DescribeConfigRules"},
},
List: &plugin.ListConfig{
Hydrate: listConfigRules,
Tags: map[string]string{"service": "config", "action": "DescribeConfigRules"},
KeyColumns: []*plugin.KeyColumn{
{
Name: "name",
Require: plugin.Optional,
},
},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getConfigRuleTags,
Tags: map[string]string{"service": "config", "action": "ListTagsForResource"},
},
{
Func: getComplianceByConfigRules,
Tags: map[string]string{"service": "config", "action": "DescribeComplianceByConfigRule"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(configservicev1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "name",
Description: "The name that you assign to the AWS Config rule.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ConfigRuleName"),
},
{
Name: "rule_id",
Description: "The ID of the AWS Config rule.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ConfigRuleId"),
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the AWS Config rule.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ConfigRuleArn"),
},
{
Name: "rule_state",
Description: "It indicate the evaluation status for the AWS Config rule.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ConfigRuleState"),
},
{
Name: "created_by",
Description: "Service principal name of the service that created the rule.",
Type: proto.ColumnType_STRING,
},
{
Name: "description",
Description: "The description that you provide for the AWS Config rule.",
Type: proto.ColumnType_STRING,
},
{
Name: "maximum_execution_frequency",
Description: "The maximum frequency with which AWS Config runs evaluations for a rule.",
Type: proto.ColumnType_STRING,
},
{
Name: "compliance_by_config_rule",
Description: "The compliance information of the config rule.",
Type: proto.ColumnType_JSON,
Hydrate: getComplianceByConfigRules,
Transform: transform.FromValue(),
},
{
Name: "evaluation_modes",
Description: "The modes the Config rule can be evaluated in. The valid values are distinct objects. By default, the value is Detective evaluation mode only.",
Type: proto.ColumnType_JSON,
},
{
Name: "input_parameters",
Description: "A string, in JSON format, that is passed to the AWS Config rule Lambda function.",
Type: proto.ColumnType_JSON,
},
{
Name: "scope",
Description: "Defines which resources can trigger an evaluation for the rule. The scope can include one or more resource types, a combination of one resource type and one resource ID, or a combination of a tag key and value. Specify a scope to constrain the resources that can trigger an evaluation for the rule. If you do not specify a scope, evaluations are triggered when any resource in the recording group changes.",
Type: proto.ColumnType_JSON,
},
{
Name: "source",
Description: "Provides the rule owner (AWS or customer), the rule identifier, and the notifications that cause the function to evaluate your AWS resources.",
Type: proto.ColumnType_JSON,
},
{
Name: "tags_src",
Description: "A list of tags assigned to the rule.",
Type: proto.ColumnType_JSON,
Hydrate: getConfigRuleTags,
Transform: transform.FromField("Tags"),
},
// Steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ConfigRuleName"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getConfigRuleTags,
Transform: transform.From(configRuleTurbotTags),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ConfigRuleArn").Transform(transform.EnsureStringArray),
},
}),
}
}
//// LIST FUNCTION
func listConfigRules(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := ConfigClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.listConfigRules", "get_client_error", err)
return nil, err
}
input := &configservice.DescribeConfigRulesInput{}
// Additonal Filter
equalQuals := d.EqualsQuals
if equalQuals["name"] != nil {
input.ConfigRuleNames = []string{equalQuals["name"].GetStringValue()}
}
paginator := configservice.NewDescribeConfigRulesPaginator(svc, input, func(o *configservice.DescribeConfigRulesPaginatorOptions) {
o.StopOnDuplicateToken = true
})
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_config_conformance_pack.listConfigConformancePacks", "api_error", err)
return nil, err
}
for _, configRule := range output.ConfigRules {
d.StreamListItem(ctx, configRule)
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
//// HYDRATE FUNCTIONS
func getConfigRule(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := ConfigClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.getConfigRule", "get_client_error", err)
return nil, err
}
name := d.EqualsQuals["name"].GetStringValue()
// Build params
params := &configservice.DescribeConfigRulesInput{
ConfigRuleNames: []string{name},
}
op, err := svc.DescribeConfigRules(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.getConfigRule", "api_error", err)
return nil, err
}
if op != nil {
return op.ConfigRules[0], nil
}
return nil, nil
}
func getConfigRuleTags(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := ConfigClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.getConfigRuleTags", "get_client_error", err)
return nil, err
}
ruleArn := h.Item.(types.ConfigRule).ConfigRuleArn
// Build params
params := &configservice.ListTagsForResourceInput{
ResourceArn: ruleArn,
}
op, err := svc.ListTagsForResource(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.getConfigRuleTags", "api_error", err)
return nil, err
}
return op, nil
}
func getComplianceByConfigRules(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := ConfigClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.getComplianceByConfigRules", "get_client_error", err)
return nil, err
}
ruleName := h.Item.(types.ConfigRule).ConfigRuleName
// Build params
params := &configservice.DescribeComplianceByConfigRuleInput{
ConfigRuleNames: []string{*ruleName},
}
op, err := svc.DescribeComplianceByConfigRule(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_config_rule.getComplianceByConfigRules", "DescribeComplianceByConfigRule", err)
return nil, err
}
return op.ComplianceByConfigRules, nil
}
//// TRANSFORM FUNCTIONS
func configRuleTurbotTags(_ context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(*configservice.ListTagsForResourceOutput)
if data.Tags == nil {
return nil, nil
}
// Mapping the resource tags inside turbotTags
turbotTagsMap := map[string]string{}
for _, i := range data.Tags {
turbotTagsMap[*i.Key] = *i.Value
}
return turbotTagsMap, nil
}