/
ec2.go
496 lines (439 loc) · 14.3 KB
/
ec2.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package ec2 provides a client to make API requests to Amazon Elastic Compute Cloud.
package ec2
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
const (
defaultForAZFilterName = "default-for-az"
internetGatewayIDPrefix = "igw-"
cloudFrontPrefixListName = "com.amazonaws.global.cloudfront.origin-facing"
// FmtTagFilter is the filter name format for tag filters
FmtTagFilter = "tag:%s"
tagKeyFilter = "tag-key"
)
var (
// FilterForDefaultVPCSubnets is a pre-defined filter for the default subnets at the availability zone.
FilterForDefaultVPCSubnets = Filter{
Name: defaultForAZFilterName,
Values: []string{"true"},
}
)
type api interface {
DescribeSubnets(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error)
DescribeSecurityGroups(*ec2.DescribeSecurityGroupsInput) (*ec2.DescribeSecurityGroupsOutput, error)
DescribeVpcs(input *ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error)
DescribeVpcAttribute(input *ec2.DescribeVpcAttributeInput) (*ec2.DescribeVpcAttributeOutput, error)
DescribeNetworkInterfaces(input *ec2.DescribeNetworkInterfacesInput) (*ec2.DescribeNetworkInterfacesOutput, error)
DescribeRouteTables(input *ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error)
DescribeAvailabilityZones(input *ec2.DescribeAvailabilityZonesInput) (*ec2.DescribeAvailabilityZonesOutput, error)
DescribeManagedPrefixLists(input *ec2.DescribeManagedPrefixListsInput) (*ec2.DescribeManagedPrefixListsOutput, error)
}
// Filter contains the name and values of a filter.
type Filter struct {
// Name of a filter that will be applied to subnets,
// for available filter names see: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSubnets.html.
Name string
// Value of the filter.
Values []string
}
// FilterForTags takes a key and optional values to construct an EC2 filter.
func FilterForTags(key string, values ...string) Filter {
if len(values) == 0 {
return Filter{Name: tagKeyFilter, Values: []string{key}}
}
return Filter{
Name: fmt.Sprintf(FmtTagFilter, key),
Values: values,
}
}
// EC2 wraps an AWS EC2 client.
type EC2 struct {
client api
}
// New returns a EC2 configured against the input session.
func New(s *session.Session) *EC2 {
return &EC2{
client: ec2.New(s),
}
}
// Resource contains the ID and name of a EC2 resource.
type Resource struct {
ID string
Name string
}
// VPC contains the ID and name of a VPC.
type VPC struct {
Resource
}
// Subnet contains the ID and name of a subnet.
type Subnet struct {
Resource
CIDRBlock string
}
// AZ represents an availability zone.
type AZ Resource
// String formats the elements of a VPC into a display-ready string.
// For example: VPCResource{"ID": "vpc-0576efeea396efee2", "Name": "video-store-test"}
// will return "vpc-0576efeea396efee2 (copilot-video-store-test)".
// while VPCResource{"ID": "subnet-018ccb78d353cec9b", "Name": "public-subnet-1"}
// will return "subnet-018ccb78d353cec9b (public-subnet-1)"
func (r *Resource) String() string {
if r.Name != "" {
return fmt.Sprintf("%s (%s)", r.ID, r.Name)
}
return r.ID
}
// ExtractVPC extracts the vpc ID from the resource display string.
// For example: vpc-0576efeea396efee2 (copilot-video-store-test)
// will return VPC{ID: "vpc-0576efeea396efee2", Name: "copilot-video-store-test"}.
func ExtractVPC(label string) (*VPC, error) {
resource, err := extractResource(label)
if err != nil {
return nil, err
}
return &VPC{
Resource: *resource,
}, nil
}
// ExtractSubnet extracts the subnet ID from the resource display string.
func ExtractSubnet(label string) (*Subnet, error) {
resource, err := extractResource(label)
if err != nil {
return nil, err
}
return &Subnet{
Resource: *resource,
}, nil
}
func extractResource(label string) (*Resource, error) {
if label == "" {
return nil, fmt.Errorf("extract resource ID from string: %s", label)
}
splitResource := strings.SplitN(label, " ", 2)
// TODO: switch to regex to make more robust
var name string
if len(splitResource) == 2 {
name = strings.Trim(splitResource[1], "()")
}
return &Resource{
ID: splitResource[0],
Name: name,
}, nil
}
// PublicIP returns the public ip associated with the network interface.
func (c *EC2) PublicIP(eni string) (string, error) {
response, err := c.client.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
NetworkInterfaceIds: aws.StringSlice([]string{eni}),
})
if err != nil {
return "", fmt.Errorf("describe network interface with ENI %s: %w", eni, err)
}
// `response.NetworkInterfaces` contains at least one result; if no matching ENI is found, the API call will return
// an error instead of an empty list of `NetworkInterfaces` (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNetworkInterfaces.html)
association := response.NetworkInterfaces[0].Association
if association == nil {
return "", fmt.Errorf("no association information found for ENI %s", eni)
}
return aws.StringValue(association.PublicIp), nil
}
// ListVPCs returns names and IDs (or just IDs, if Name tag does not exist) of all VPCs.
func (c *EC2) ListVPCs() ([]VPC, error) {
var ec2vpcs []*ec2.Vpc
response, err := c.client.DescribeVpcs(&ec2.DescribeVpcsInput{})
if err != nil {
return nil, fmt.Errorf("describe VPCs: %w", err)
}
ec2vpcs = append(ec2vpcs, response.Vpcs...)
for response.NextToken != nil {
response, err = c.client.DescribeVpcs(&ec2.DescribeVpcsInput{
NextToken: response.NextToken,
})
if err != nil {
return nil, fmt.Errorf("describe VPCs: %w", err)
}
ec2vpcs = append(ec2vpcs, response.Vpcs...)
}
var vpcs []VPC
for _, vpc := range ec2vpcs {
var name string
for _, tag := range vpc.Tags {
if aws.StringValue(tag.Key) == "Name" {
name = aws.StringValue(tag.Value)
}
}
vpcs = append(vpcs, VPC{
Resource: Resource{
ID: aws.StringValue(vpc.VpcId),
Name: name,
},
})
}
return vpcs, nil
}
// ListAZs returns the list of opted-in and available availability zones.
func (c *EC2) ListAZs() ([]AZ, error) {
resp, err := c.client.DescribeAvailabilityZones(&ec2.DescribeAvailabilityZonesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("zone-type"),
Values: aws.StringSlice([]string{"availability-zone"}),
},
{
Name: aws.String("state"),
Values: aws.StringSlice([]string{"available"}),
},
},
})
if err != nil {
return nil, fmt.Errorf("describe availability zones: %w", err)
}
var out []AZ
for _, az := range resp.AvailabilityZones {
out = append(out, AZ{
ID: aws.StringValue(az.ZoneId),
Name: aws.StringValue(az.ZoneName),
})
}
return out, nil
}
// HasDNSSupport returns if DNS resolution is enabled for the VPC.
func (c *EC2) HasDNSSupport(vpcID string) (bool, error) {
resp, err := c.client.DescribeVpcAttribute(&ec2.DescribeVpcAttributeInput{
VpcId: aws.String(vpcID),
Attribute: aws.String(ec2.VpcAttributeNameEnableDnsSupport),
})
if err != nil {
return false, fmt.Errorf("describe %s attribute for VPC %s: %w", ec2.VpcAttributeNameEnableDnsSupport, vpcID, err)
}
return aws.BoolValue(resp.EnableDnsSupport.Value), nil
}
// VPCSubnets are all subnets within a VPC.
type VPCSubnets struct {
Public []Subnet
Private []Subnet
}
// ListVPCSubnets lists all subnets with a given VPC ID. Note that public subnets
// are subnets associated with an internet gateway through a route table.
// And the rest of the subnets are private.
func (c *EC2) ListVPCSubnets(vpcID string) (*VPCSubnets, error) {
vpcFilter := Filter{
Name: "vpc-id",
Values: []string{vpcID},
}
routeTables, err := c.routeTables(vpcFilter)
if err != nil {
return nil, err
}
rtIndex := indexRouteTables(routeTables)
var publicSubnets, privateSubnets []Subnet
respSubnets, err := c.subnets(vpcFilter)
if err != nil {
return nil, err
}
for _, subnet := range respSubnets {
var name string
for _, tag := range subnet.Tags {
if aws.StringValue(tag.Key) == "Name" {
name = aws.StringValue(tag.Value)
}
}
s := Subnet{
Resource: Resource{
ID: aws.StringValue(subnet.SubnetId),
Name: name,
},
CIDRBlock: aws.StringValue(subnet.CidrBlock),
}
if rtIndex.IsPublicSubnet(s.ID) {
publicSubnets = append(publicSubnets, s)
} else {
privateSubnets = append(privateSubnets, s)
}
}
return &VPCSubnets{
Public: publicSubnets,
Private: privateSubnets,
}, nil
}
// SubnetIDs finds the subnet IDs with optional filters.
func (c *EC2) SubnetIDs(filters ...Filter) ([]string, error) {
subnets, err := c.subnets(filters...)
if err != nil {
return nil, err
}
subnetIDs := make([]string, len(subnets))
for idx, subnet := range subnets {
subnetIDs[idx] = aws.StringValue(subnet.SubnetId)
}
return subnetIDs, nil
}
// SecurityGroups finds the security group IDs with optional filters.
func (c *EC2) SecurityGroups(filters ...Filter) ([]string, error) {
inputFilters := toEC2Filter(filters)
response, err := c.client.DescribeSecurityGroups(&ec2.DescribeSecurityGroupsInput{
Filters: inputFilters,
})
if err != nil {
return nil, fmt.Errorf("describe security groups: %w", err)
}
securityGroups := make([]string, len(response.SecurityGroups))
for idx, sg := range response.SecurityGroups {
securityGroups[idx] = aws.StringValue(sg.GroupId)
}
return securityGroups, nil
}
func (c *EC2) subnets(filters ...Filter) ([]*ec2.Subnet, error) {
inputFilters := toEC2Filter(filters)
var subnets []*ec2.Subnet
response, err := c.client.DescribeSubnets(&ec2.DescribeSubnetsInput{
Filters: inputFilters,
})
if err != nil {
return nil, fmt.Errorf("describe subnets: %w", err)
}
subnets = append(subnets, response.Subnets...)
for response.NextToken != nil {
response, err = c.client.DescribeSubnets(&ec2.DescribeSubnetsInput{
Filters: inputFilters,
NextToken: response.NextToken,
})
if err != nil {
return nil, fmt.Errorf("describe subnets: %w", err)
}
subnets = append(subnets, response.Subnets...)
}
if len(subnets) == 0 {
return nil, fmt.Errorf("cannot find any subnets")
}
return subnets, nil
}
func (c *EC2) routeTables(filters ...Filter) ([]*ec2.RouteTable, error) {
var routeTables []*ec2.RouteTable
input := &ec2.DescribeRouteTablesInput{
Filters: toEC2Filter(filters),
}
for {
resp, err := c.client.DescribeRouteTables(input)
if err != nil {
return nil, fmt.Errorf("describe route tables: %w", err)
}
routeTables = append(routeTables, resp.RouteTables...)
if resp.NextToken == nil {
break
}
input.NextToken = resp.NextToken
}
return routeTables, nil
}
func toEC2Filter(filters []Filter) []*ec2.Filter {
var ec2Filter []*ec2.Filter
for _, filter := range filters {
ec2Filter = append(ec2Filter, &ec2.Filter{
Name: aws.String(filter.Name),
Values: aws.StringSlice(filter.Values),
})
}
return ec2Filter
}
type routeTable ec2.RouteTable
// IsMain returns true if the route table is the default route table for the VPC.
// If a subnet is not associated with a particular route table, then it will default to the main route table.
func (rt *routeTable) IsMain() bool {
for _, association := range rt.Associations {
if aws.BoolValue(association.Main) {
return true
}
}
return false
}
// HasIGW returns true if the route table has a route to an internet gateway.
func (rt *routeTable) HasIGW() bool {
for _, route := range rt.Routes {
if strings.HasPrefix(aws.StringValue(route.GatewayId), internetGatewayIDPrefix) {
return true
}
}
return false
}
// AssociatedSubnets returns the list of subnet IDs associated with the route table.
func (rt *routeTable) AssociatedSubnets() []string {
var subnetIDs []string
for _, association := range rt.Associations {
if association.SubnetId == nil {
continue
}
subnetIDs = append(subnetIDs, aws.StringValue(association.SubnetId))
}
return subnetIDs
}
// routeTableIndex holds cached data to quickly return information about route tables in a VPC.
type routeTableIndex struct {
// Route table that subnets default to. There is always one main table in the VPC.
mainTable *routeTable
// Explicit route table association for a subnet. A subnet can only be associated to one route table.
routeTableForSubnet map[string]*routeTable
}
func indexRouteTables(tables []*ec2.RouteTable) *routeTableIndex {
index := &routeTableIndex{
routeTableForSubnet: make(map[string]*routeTable),
}
for _, table := range tables { // Index all properties in a single pass.
table := (*routeTable)(table)
for _, subnetID := range table.AssociatedSubnets() {
index.routeTableForSubnet[subnetID] = table
}
if table.IsMain() {
index.mainTable = table
}
}
return index
}
// IsPublicSubnet returns true if the subnet has a route to an internet gateway.
// We consider the subnet to have internet access if there is an explicit route in the route table to an internet gateway.
// Or if there is an implicit route, where the subnet defaults to the main route table with an internet gateway.
func (idx *routeTableIndex) IsPublicSubnet(subnetID string) bool {
rt, ok := idx.routeTableForSubnet[subnetID]
if ok {
return rt.HasIGW()
}
return idx.mainTable.HasIGW()
}
// managedPrefixList returns the DescribeManagedPrefixListsOutput of a query by name.
func (c *EC2) managedPrefixList(prefixListName string) (*ec2.DescribeManagedPrefixListsOutput, error) {
prefixListOutput, err := c.client.DescribeManagedPrefixLists(&ec2.DescribeManagedPrefixListsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("prefix-list-name"),
Values: aws.StringSlice([]string{prefixListName}),
},
},
})
if err != nil {
return nil, fmt.Errorf("describe managed prefix list with name %s: %w", prefixListName, err)
}
return prefixListOutput, nil
}
// CloudFrontManagedPrefixListID returns the PrefixListId of the associated cloudfront prefix list as a *string.
func (c *EC2) CloudFrontManagedPrefixListID() (string, error) {
prefixListsOutput, err := c.managedPrefixList(cloudFrontPrefixListName)
if err != nil {
return "", err
}
var ids []string
for _, v := range prefixListsOutput.PrefixLists {
ids = append(ids, *v.PrefixListId)
}
if len(ids) == 0 {
return "", fmt.Errorf("cannot find any prefix list with name: %s", cloudFrontPrefixListName)
}
if len(ids) > 1 {
return "", fmt.Errorf("found more than one prefix list with the name %s: %v", cloudFrontPrefixListName, ids)
}
return ids[0], nil
}