/
table_aws_guardduty_detector.go
256 lines (223 loc) · 7.36 KB
/
table_aws_guardduty_detector.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
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/guardduty"
guarddutyv1 "github.com/aws/aws-sdk-go/service/guardduty"
"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"
)
type detectorInfo = struct {
guardduty.GetDetectorOutput
DetectorID string
}
//// TABLE DEFINITION
func tableAwsGuardDutyDetector(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_guardduty_detector",
Description: "AWS GuardDuty Detector",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("detector_id"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"InvalidInputException", "BadRequestException"}),
},
Hydrate: getGuardDutyDetector,
Tags: map[string]string{"service": "guardduty", "action": "GetDetector"},
},
List: &plugin.ListConfig{
Hydrate: listGuardDutyDetectors,
Tags: map[string]string{"service": "guardduty", "action": "ListDetectors"},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getGuardDutyDetector,
Tags: map[string]string{"service": "guardduty", "action": "GetDetector"},
},
{
Func: getGuardDutyDetectorMasterAccount,
Tags: map[string]string{"service": "guardduty", "action": "GetAdministratorAccount"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(guarddutyv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "detector_id",
Description: "The ID of the detector.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DetectorID"),
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) specifying the detector.",
Type: proto.ColumnType_STRING,
Hydrate: getGuardDutyDetectorARN,
Transform: transform.FromValue(),
},
{
Name: "status",
Description: "The detector status.",
Type: proto.ColumnType_STRING,
Hydrate: getGuardDutyDetector,
},
{
Name: "created_at",
Description: "The timestamp of when the detector was created.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getGuardDutyDetector,
},
{
Name: "finding_publishing_frequency",
Description: "The publishing frequency of the finding.",
Type: proto.ColumnType_STRING,
Hydrate: getGuardDutyDetector,
},
{
Name: "service_role",
Description: "The GuardDuty service role.",
Type: proto.ColumnType_STRING,
Hydrate: getGuardDutyDetector,
},
{
Name: "updated_at",
Description: "The last-updated timestamp for the detector.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getGuardDutyDetector,
},
{
Name: "data_sources",
Description: "Describes which data sources are enabled for the detector.",
Type: proto.ColumnType_JSON,
Hydrate: getGuardDutyDetector,
},
{
Name: "features",
Description: "Describes the features that have been enabled for the detector.",
Type: proto.ColumnType_JSON,
Hydrate: getGuardDutyDetector,
},
{
Name: "master_account",
Description: "Contains information about the administrator account and invitation.",
Type: proto.ColumnType_JSON,
Hydrate: getGuardDutyDetectorMasterAccount,
Transform: transform.FromValue(),
},
// Standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DetectorID"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getGuardDutyDetector,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Hydrate: getGuardDutyDetectorARN,
Transform: transform.FromValue().Transform(transform.EnsureStringArray),
},
}),
}
}
//// LIST FUNCTION
func listGuardDutyDetectors(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := GuardDutyClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_guardduty_detector.listGuardDutyDetectors", "get_client_error", err)
return nil, err
}
maxItems := int32(50)
params := &guardduty.ListDetectorsInput{
MaxResults: &maxItems,
}
// Reduce the basic request limit down if the user has only requested a small number of rows
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxItems {
params.MaxResults = &limit
}
}
paginator := guardduty.NewListDetectorsPaginator(svc, params, func(o *guardduty.ListDetectorsPaginatorOptions) {
o.Limit = maxItems
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_guardduty_detector.listGuardDutyDetectors", "api_error", err)
return nil, err
}
for _, item := range output.DetectorIds {
d.StreamListItem(ctx, detectorInfo{DetectorID: item})
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getGuardDutyDetector(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var id string
if h.Item != nil {
id = h.Item.(detectorInfo).DetectorID
} else {
quals := d.EqualsQuals
id = quals["detector_id"].GetStringValue()
}
// Create Session
svc, err := GuardDutyClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_guardduty_detector.getGuardDutyDetector", "client_error", err)
return nil, err
}
params := &guardduty.GetDetectorInput{
DetectorId: &id,
}
op, err := svc.GetDetector(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_guardduty_detector.getGuardDutyDetector", "api_error", err)
return nil, err
}
return detectorInfo{*op, id}, nil
}
func getGuardDutyDetectorMasterAccount(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
id := h.Item.(detectorInfo).DetectorID
// Create Session
svc, err := GuardDutyClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_guardduty_detector.listGuardDutyDetectors", "get_client_error", err)
return nil, err
}
params := &guardduty.GetAdministratorAccountInput{
DetectorId: &id,
}
op, err := svc.GetAdministratorAccount(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_guardduty_detector.getGuardDutyDetectorMasterAccount", "api_error", err)
return nil, err
}
return op.Administrator, nil
}
func getGuardDutyDetectorARN(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
data := h.Item.(detectorInfo)
c, err := getCommonColumns(ctx, d, h)
if err != nil {
plugin.Logger(ctx).Error("aws_guardduty_detector.getGuardDutyDetectorARN", "error", err)
return nil, err
}
commonColumnData := c.(*awsCommonColumnData)
arn := "arn:" + commonColumnData.Partition + ":guardduty:" + region + ":" + commonColumnData.AccountId + ":detector/" + data.DetectorID
return arn, nil
}