-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathtable_aws_ec2_ami_shared.go
420 lines (386 loc) · 13.9 KB
/
table_aws_ec2_ami_shared.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package aws
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
ec2v1 "github.com/aws/aws-sdk-go/service/ec2"
go_kit_pack "github.com/turbot/go-kit/types"
"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"
"github.com/turbot/steampipe-plugin-sdk/v5/query_cache"
)
//// TABLE DEFINITION
func tableAwsEc2AmiShared(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_ec2_ami_shared",
Description: "AWS EC2 AMI - All public, private, and shared AMIs",
List: &plugin.ListConfig{
Hydrate: listAmisByOwner,
Tags: map[string]string{"service": "ec2", "action": "DescribeImages"},
KeyColumns: []*plugin.KeyColumn{
{Name: "owner_id", Require: plugin.Optional, CacheMatch: query_cache.CacheMatchExact},
{Name: "owner_ids", Require: plugin.Optional, CacheMatch: query_cache.CacheMatchExact, Operators: []string{"="}},
{Name: "architecture", Require: plugin.Optional},
{Name: "description", Require: plugin.Optional},
{Name: "ena_support", Require: plugin.Optional, Operators: []string{"=", "<>"}},
{Name: "hypervisor", Require: plugin.Optional},
{Name: "image_id", Require: plugin.Optional, CacheMatch: query_cache.CacheMatchExact},
{Name: "image_ids", Require: plugin.Optional, CacheMatch: query_cache.CacheMatchExact, Operators: []string{"="}},
{Name: "image_type", Require: plugin.Optional},
{Name: "public", Require: plugin.Optional, Operators: []string{"=", "<>"}},
{Name: "kernel_id", Require: plugin.Optional},
{Name: "name", Require: plugin.Optional},
{Name: "platform", Require: plugin.Optional},
{Name: "ramdisk_id", Require: plugin.Optional},
{Name: "root_device_name", Require: plugin.Optional},
{Name: "root_device_type", Require: plugin.Optional},
{Name: "state", Require: plugin.Optional},
{Name: "sriov_net_support", Require: plugin.Optional},
{Name: "virtualization_type", Require: plugin.Optional},
},
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"InvalidAMIID.NotFound", "InvalidAMIID.Unavailable", "InvalidAMIID.Malformed"}),
},
},
GetMatrixItemFunc: SupportedRegionMatrix(ec2v1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "name",
Description: "The name of the AMI that was provided during image creation.",
Type: proto.ColumnType_STRING,
},
{
Name: "image_id",
Description: "The ID of the AMI.",
Type: proto.ColumnType_STRING,
},
{
Name: "state",
Description: "The current state of the AMI. If the state is available, the image is successfully registered and can be used to launch an instance.",
Type: proto.ColumnType_STRING,
},
{
Name: "image_type",
Description: "The type of image.",
Type: proto.ColumnType_STRING,
},
{
Name: "image_location",
Description: "The location of the AMI.",
Type: proto.ColumnType_STRING,
},
{
Name: "boot_mode",
Description: "The boot mode of the image.",
Type: proto.ColumnType_STRING,
},
{
Name: "source_instance_id",
Description: "The ID of the instance that the AMI was created from if the AMI was created using CreateImage.",
Type: proto.ColumnType_STRING,
},
{
Name: "tpm_support",
Description: "If the image is configured for NitroTPM support, the value is v2.0.",
Type: proto.ColumnType_STRING,
},
{
Name: "creation_date",
Description: "The date and time when the image was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "deprecation_time",
Description: "The date and time to deprecate the AMI.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("DeprecationTime").Transform(transform.NullIfZeroValue),
},
{
Name: "architecture",
Description: "The architecture of the image.",
Type: proto.ColumnType_STRING,
},
{
Name: "description",
Description: "The description of the AMI that was provided during image creation.",
Type: proto.ColumnType_STRING,
},
{
Name: "ena_support",
Description: "Specifies whether enhanced networking with ENA is enabled.",
Type: proto.ColumnType_BOOL,
},
{
Name: "hypervisor",
Description: "The hypervisor type of the image.",
Type: proto.ColumnType_STRING,
},
{
Name: "image_owner_alias",
Description: "The AWS account alias (for example, amazon, self) or the AWS account ID of the AMI owner.",
Type: proto.ColumnType_STRING,
Hydrate: getImageOwnerAlias,
Transform: transform.FromValue(),
},
{
Name: "imds_support",
Description: "If v2.0, it indicates that IMDSv2 is specified in the AMI.",
Type: proto.ColumnType_STRING,
},
{
Name: "kernel_id",
Description: "The kernel associated with the image, if any. Only applicable for machine images.",
Type: proto.ColumnType_STRING,
},
{
Name: "owner_id",
Description: "The AWS account ID of the image owner.",
Type: proto.ColumnType_STRING,
},
{
Name: "platform",
Description: "This value is set to windows for Windows AMIs; otherwise, it is blank.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Platform").NullIfZero(),
},
{
Name: "platform_details",
Description: "The platform details associated with the billing code of the AMI. For more information, see Obtaining Billing Information (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ami-billing-info.html) in the Amazon Elastic Compute Cloud User Guide.",
Type: proto.ColumnType_STRING,
},
{
Name: "public",
Description: "Indicates whether the image has public launch permissions. The value is true if this image has public launch permissions or false if it has only implicit and explicit launch permissions.",
Type: proto.ColumnType_BOOL,
},
{
Name: "ramdisk_id",
Description: "The RAM disk associated with the image, if any. Only applicable for machine images.",
Type: proto.ColumnType_STRING,
},
{
Name: "root_device_name",
Description: "The device name of the root device volume (for example, /dev/sda1).",
Type: proto.ColumnType_STRING,
},
{
Name: "root_device_type",
Description: "The type of root device used by the AMI. The AMI can use an EBS volume or an instance store volume.",
Type: proto.ColumnType_STRING,
},
{
Name: "sriov_net_support",
Description: "Specifies whether enhanced networking with the Intel 82599 Virtual Function interface is enabled.",
Type: proto.ColumnType_STRING,
},
{
Name: "usage_operation",
Description: "The operation of the Amazon EC2 instance and the billing code that is associated with the AMI. For the list of UsageOperation codes, see Platform Details and [Usage Operation Billing Codes](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ami-billing-info.html#billing-info) in the Amazon Elastic Compute Cloud User Guide.",
Type: proto.ColumnType_STRING,
},
{
Name: "virtualization_type",
Description: "The type of virtualization of the AMI.",
Type: proto.ColumnType_STRING,
},
{
Name: "image_ids",
Description: "The ID of the AMIs in the form of array of strings.",
Type: proto.ColumnType_JSON,
Transform: transform.FromQual("image_ids"),
},
{
Name: "owner_ids",
Description: "The AWS account IDs of the image owners.",
Type: proto.ColumnType_JSON,
Transform: transform.FromQual("owner_ids"),
},
{
Name: "block_device_mappings",
Description: "Any block device mapping entries.",
Type: proto.ColumnType_JSON,
},
{
Name: "state_reason",
Description: "The reason for the state change.",
Type: proto.ColumnType_JSON,
},
{
Name: "product_codes",
Description: "Any product codes associated with the AMI.",
Type: proto.ColumnType_JSON,
},
{
Name: "tags_src",
Description: "A list of tags attached to the AMI.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Tags"),
},
// Steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.From(getEc2AmiTurbotTitle),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Transform: transform.From(getEc2AmiTurbotTags),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Hydrate: getAwsEc2AmiAkas,
Transform: transform.FromValue(),
},
}),
}
}
//// LIST FUNCTION
func listAmisByOwner(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
owner_id := d.EqualsQuals["owner_id"].GetStringValue()
image_id := d.EqualsQuals["image_id"].GetStringValue()
image_ids := d.EqualsQuals["image_ids"].GetJsonbValue()
owner_ids := d.EqualsQuals["owner_ids"].GetJsonbValue()
// check if owner_id and image_id is empty
if owner_id == "" && image_id == "" && image_ids == "" {
return nil, errors.New("please provide either owner_id, image_id or image_ids")
}
// Limiting the results
maxLimit := int32(1000)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
maxLimit = limit
}
}
// Create Session
svc, err := EC2Client(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_ec2_ami_shared.listAmisByOwner", "connection_error", err)
return nil, err
}
input := &ec2.DescribeImagesInput{}
if owner_id != "" {
input.Owners = []string{owner_id}
}
if image_id != "" {
input.ImageIds = []string{image_id}
}
if image_ids != "" {
var imageIds []string
err := json.Unmarshal([]byte(image_ids), &imageIds)
if err != nil {
return nil, errors.New("unable to parse the 'image_ids' query parameter the value must be in the format '[\"ami-000165ee3e0c1d6c7\", \"ami-0002ab43c99ec70ec\"]'")
}
input.ImageIds = imageIds
}
if owner_ids != "" {
var ownerIds []string
err := json.Unmarshal([]byte(owner_ids), &ownerIds)
if err != nil {
return nil, errors.New("unable to parse the 'image_ids' query parameter the value must be in the format '[\"123456789089\", \"345345678567\"]'")
}
input.Owners = ownerIds
}
filters := buildSharedAmisWithOwnerFilter(d.Quals, ctx, d, h)
if len(filters) != 0 {
input.Filters = filters
}
paginator := ec2.NewDescribeImagesPaginator(svc, input, func(o *ec2.DescribeImagesPaginatorOptions) {
// api error InvalidParameterCombination: The parameter imageIdsSet cannot be used with the parameter maxResults
if len(input.ImageIds) == 0 {
o.Limit = maxLimit
}
o.StopOnDuplicateToken = true
})
// List call
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_ec2_instance.listEc2Instance", "api_error", err)
return nil, err
}
for _, item := range output.Images {
d.StreamListItem(ctx, item)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
func getImageOwnerAlias(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
image := h.Item.(types.Image)
c, err := getCommonColumns(ctx, d, h)
if err != nil {
return nil, err
}
commonColumnData := c.(*awsCommonColumnData)
if image.ImageOwnerAlias == nil && commonColumnData.AccountId != *image.OwnerId {
return *image.OwnerId, nil
} else if image.ImageOwnerAlias == nil {
return "self", nil
} else {
return *image.ImageOwnerAlias, nil
}
}
// // UTILITY FUNCTION
// Build AMI's list call input filter
func buildSharedAmisWithOwnerFilter(quals plugin.KeyColumnQualMap, ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) []types.Filter {
filters := make([]types.Filter, 0)
filterQuals := map[string]string{
"architecture": "architecture",
"description": "description",
"ena_support": "ena-support",
"hypervisor": "hypervisor",
"image_type": "image-type",
"kernel_id": "kernel-id",
"name": "name",
"platform": "platform",
"public": "is-public",
"ramdisk_id": "ramdisk-id",
"root_device_name": "root-device-name",
"root_device_type": "root-device-type",
"state": "state",
"sriov_net_support": "sriov-net-support",
"virtualization_type": "virtualization-type",
}
columnsBool := []string{"ena_support", "public"}
for columnName, filterName := range filterQuals {
if quals[columnName] != nil {
filter := types.Filter{
Name: go_kit_pack.String(filterName),
}
//check Bool columns
if strings.Contains(fmt.Sprint(columnsBool), columnName) {
value := getQualsValueByColumn(quals, columnName, "boolean")
filter.Values = []string{fmt.Sprint(value)}
} else {
value := getQualsValueByColumn(quals, columnName, "string")
val, ok := value.(string)
if ok {
filter.Values = []string{val}
}
}
filters = append(filters, filter)
}
}
return filters
}