/
iot_security_profiles.go
270 lines (255 loc) · 9.06 KB
/
iot_security_profiles.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
package iot
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iot"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
func IotSecurityProfiles() *schema.Table {
return &schema.Table{
Name: "aws_iot_security_profiles",
Resolver: fetchIotSecurityProfiles,
Multiplex: client.ServiceAccountRegionMultiplexer("iot"),
IgnoreError: client.IgnoreCommonErrors,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
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: "targets",
Description: "Targets associated with the security profile",
Type: schema.TypeStringArray,
Resolver: ResolveIotSecurityProfileTargets,
},
{
Name: "tags",
Description: "Tags of the resource",
Type: schema.TypeJSON,
Resolver: ResolveIotSecurityProfileTags,
},
{
Name: "additional_metrics_to_retain",
Description: "Please use DescribeSecurityProfileResponse$additionalMetricsToRetainV2 instead. A list of metrics whose data is retained (stored)",
Type: schema.TypeStringArray,
},
{
Name: "additional_metrics_to_retain_v2",
Description: "A list of metrics whose data is retained (stored)",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("AdditionalMetricsToRetainV2"),
},
{
Name: "alert_targets",
Description: "Where the alerts are sent",
Type: schema.TypeJSON,
},
{
Name: "creation_date",
Description: "The time the security profile was created.",
Type: schema.TypeTimestamp,
},
{
Name: "last_modified_date",
Description: "The time the security profile was last modified.",
Type: schema.TypeTimestamp,
},
{
Name: "arn",
Description: "The ARN of the security profile.",
Type: schema.TypeString,
Resolver: schema.PathResolver("SecurityProfileArn"),
},
{
Name: "description",
Description: "A description of the security profile (associated with the security profile when it was created or updated).",
Type: schema.TypeString,
Resolver: schema.PathResolver("SecurityProfileDescription"),
},
{
Name: "name",
Description: "The name of the security profile.",
Type: schema.TypeString,
Resolver: schema.PathResolver("SecurityProfileName"),
},
{
Name: "version",
Description: "The version of the security profile",
Type: schema.TypeBigInt,
},
},
Relations: []*schema.Table{
{
Name: "aws_iot_security_profile_behaviors",
Description: "A Device Defender security profile behavior.",
Resolver: schema.PathTableResolver("Behaviors"),
Columns: []schema.Column{
{
Name: "security_profile_cq_id",
Description: "Unique CloudQuery ID of aws_iot_security_profiles table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "name",
Description: "The name you've given to the behavior. ",
Type: schema.TypeString,
},
{
Name: "criteria_comparison_operator",
Description: "The operator that relates the thing measured (metric) to the criteria (containing a value or statisticalThreshold)",
Type: schema.TypeString,
Resolver: schema.PathResolver("Criteria.ComparisonOperator"),
},
{
Name: "criteria_consecutive_datapoints_to_alarm",
Description: "If a device is in violation of the behavior for the specified number of consecutive datapoints, an alarm occurs",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Criteria.ConsecutiveDatapointsToAlarm"),
},
{
Name: "criteria_consecutive_datapoints_to_clear",
Description: "If an alarm has occurred and the offending device is no longer in violation of the behavior for the specified number of consecutive datapoints, the alarm is cleared",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Criteria.ConsecutiveDatapointsToClear"),
},
{
Name: "criteria_duration_seconds",
Description: "Use this to specify the time duration over which the behavior is evaluated, for those criteria that have a time dimension (for example, NUM_MESSAGES_SENT)",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Criteria.DurationSeconds"),
},
{
Name: "criteria_ml_detection_config_confidence_level",
Description: "The sensitivity of anomalous behavior evaluation",
Type: schema.TypeString,
Resolver: schema.PathResolver("Criteria.MlDetectionConfig.ConfidenceLevel"),
},
{
Name: "criteria_statistical_threshold_statistic",
Description: "The percentile that resolves to a threshold value by which compliance with a behavior is determined",
Type: schema.TypeString,
Resolver: schema.PathResolver("Criteria.StatisticalThreshold.Statistic"),
},
{
Name: "criteria_value",
Description: "The value to be compared with the metric.",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("Criteria.Value"),
},
{
Name: "metric",
Description: "What is measured by the behavior.",
Type: schema.TypeString,
},
{
Name: "metric_dimension_dimension_name",
Description: "A unique identifier for the dimension. ",
Type: schema.TypeString,
Resolver: schema.PathResolver("MetricDimension.DimensionName"),
},
{
Name: "metric_dimension_operator",
Description: "Defines how the dimensionValues of a dimension are interpreted",
Type: schema.TypeString,
Resolver: schema.PathResolver("MetricDimension.Operator"),
},
{
Name: "suppress_alerts",
Description: "Suppresses alerts.",
Type: schema.TypeBool,
},
},
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchIotSecurityProfiles(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
cl := meta.(*client.Client)
svc := cl.Services().IOT
input := iot.ListSecurityProfilesInput{
MaxResults: aws.Int32(250),
}
for {
response, err := svc.ListSecurityProfiles(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
for _, s := range response.SecurityProfileIdentifiers {
profile, err := svc.DescribeSecurityProfile(ctx, &iot.DescribeSecurityProfileInput{
SecurityProfileName: s.Name,
}, func(options *iot.Options) {
options.Region = cl.Region
})
if err != nil {
return diag.WrapError(err)
}
res <- profile
}
if aws.ToString(response.NextToken) == "" {
break
}
input.NextToken = response.NextToken
}
return nil
}
func ResolveIotSecurityProfileTargets(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
i := resource.Item.(*iot.DescribeSecurityProfileOutput)
cl := meta.(*client.Client)
svc := cl.Services().IOT
input := iot.ListTargetsForSecurityProfileInput{
SecurityProfileName: i.SecurityProfileName,
MaxResults: aws.Int32(250),
}
var targets []string
for {
response, err := svc.ListTargetsForSecurityProfile(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
for _, t := range response.SecurityProfileTargets {
targets = append(targets, *t.Arn)
}
if aws.ToString(response.NextToken) == "" {
break
}
input.NextToken = response.NextToken
}
return diag.WrapError(resource.Set(c.Name, targets))
}
func ResolveIotSecurityProfileTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
i := resource.Item.(*iot.DescribeSecurityProfileOutput)
cl := meta.(*client.Client)
svc := cl.Services().IOT
input := iot.ListTagsForResourceInput{
ResourceArn: i.SecurityProfileArn,
}
tags := make(map[string]string)
for {
response, err := svc.ListTagsForResource(ctx, &input)
if err != nil {
return diag.WrapError(err)
}
client.TagsIntoMap(response.Tags, tags)
if aws.ToString(response.NextToken) == "" {
break
}
input.NextToken = response.NextToken
}
return diag.WrapError(diag.WrapError(resource.Set(c.Name, tags)))
}