/
table_aws_securityhub_standards_subscription.go
204 lines (179 loc) · 5.75 KB
/
table_aws_securityhub_standards_subscription.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
package aws
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/service/securityhub"
"github.com/aws/aws-sdk-go-v2/service/securityhub/types"
"github.com/aws/smithy-go"
securityhubv1 "github.com/aws/aws-sdk-go/service/securityhub"
"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 tableAwsSecurityHubStandardsSubscription(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_securityhub_standards_subscription",
Description: "AWS Security Hub Standards Subscription",
List: &plugin.ListConfig{
Hydrate: listSecurityHubStandardsSubcriptions,
Tags: map[string]string{"service": "securityhub", "action": "GetEnabledStandards"},
},
GetMatrixItemFunc: SupportedRegionMatrix(securityhubv1.EndpointsID),
HydrateConfig: []plugin.HydrateConfig{
{
Func: GetEnabledStandards,
Tags: map[string]string{"service": "securityhub", "action": "GetEnabledStandards"},
},
},
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "name",
Description: "The name of the standard.",
Type: proto.ColumnType_STRING,
},
{
Name: "standards_arn",
Description: "The ARN of a standard.",
Type: proto.ColumnType_STRING,
},
{
Name: "description",
Description: "The description of the standard.",
Type: proto.ColumnType_STRING,
},
{
Name: "enabled_by_default",
Description: "Indicates whether the standard is enabled by default.",
Type: proto.ColumnType_BOOL,
},
{
Name: "standards_status",
Description: "The status of the standard subscription.",
Type: proto.ColumnType_STRING,
Hydrate: GetEnabledStandards,
},
{
Name: "standards_status_reason_code",
Description: "The reason code that represents the reason for the current status of a standard subscription.",
Type: proto.ColumnType_STRING,
Hydrate: GetEnabledStandards,
Transform: transform.FromField("StandardsStatusReason.StatusReasonCode"),
},
{
Name: "standards_subscription_arn",
Description: "The ARN of a resource that represents your subscription to a supported standard.",
Type: proto.ColumnType_STRING,
Hydrate: GetEnabledStandards,
},
// JSON columns
{
Name: "standards_input",
Description: "A key-value pair of input for the standard.",
Type: proto.ColumnType_JSON,
Hydrate: GetEnabledStandards,
},
{
Name: "standards_managed_by",
Description: "Provides details about the management of a security standard.",
Type: proto.ColumnType_JSON,
},
// Standard columns for all tables
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("StandardsArn").Transform(arnToAkas),
},
}),
}
}
//// LIST FUNCTION
func listSecurityHubStandardsSubcriptions(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create session
svc, err := SecurityHubClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_securityhub_standards_subscription.listSecurityHubStandardsSubcriptions", "client_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}
// Limiting the results
maxLimit := int32(100)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
if limit < 1 {
maxLimit = 1
} else {
maxLimit = limit
}
}
}
input := &securityhub.DescribeStandardsInput{
MaxResults: &maxLimit,
}
paginator := securityhub.NewDescribeStandardsPaginator(svc, input, func(o *securityhub.DescribeStandardsPaginatorOptions) {
o.Limit = maxLimit
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_securityhub_standards_subscription.listSecurityHubStandardsSubcriptions", "api_error", err)
return nil, err
}
for _, standards := range output.Standards {
d.StreamListItem(ctx, standards)
// 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, nil
}
//// HYDRATE FUNCTIONS
func GetEnabledStandards(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
standardArn := *h.Item.(types.Standard).StandardsArn
// Create session
svc, err := SecurityHubClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_securityhub_standards_subscription.GetEnabledStandards", "client_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}
// Build the input
input := &securityhub.GetEnabledStandardsInput{}
// Get call
standardsSubscriptions, err := svc.GetEnabledStandards(ctx, input)
if err != nil {
plugin.Logger(ctx).Error("aws_securityhub_standards_subscription.GetEnabledStandards", "api_error", err)
var ae smithy.APIError
if errors.As(err, &ae) {
// If the service is not enabled, API throws InvalidAccessException error
if ae.ErrorCode() == "InvalidAccessException" {
return nil, nil
}
}
}
for _, item := range standardsSubscriptions.StandardsSubscriptions {
if *item.StandardsArn == standardArn {
return item, nil
}
}
return nil, nil
}