Skip to content

Commit

Permalink
Get default aws subnet ids (#1131)
Browse files Browse the repository at this point in the history
* add function for getting default aws subnet ids

* check main route table when checking if subnet without route tables is public

* fix aws TestGetVpcsE test

* remove redundant true check

* add check for recently created subnet on GetDefaultSubnetIDsForVpc test
  • Loading branch information
Etiene committed Jun 14, 2022
1 parent 99d3665 commit 0dd0f81
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 3 deletions.
76 changes: 74 additions & 2 deletions modules/aws/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Vpc struct {
type Subnet struct {
Id string // The ID of the Subnet
AvailabilityZone string // The Availability Zone the subnet is in
DefaultForAz bool // If the subnet is default for the Availability Zone
Tags map[string]string // The tags associated with the subnet
}

Expand All @@ -34,6 +35,7 @@ const vpcResourceTypeFilterValue = "vpc"
const subnetResourceTypeFilterValue = "subnet"
const isDefaultFilterName = "isDefault"
const isDefaultFilterValue = "true"
const defaultVPCName = "Default"

// GetDefaultVpc fetches information about the default VPC in the given region.
func GetDefaultVpc(t testing.TestingT, region string) *Vpc {
Expand Down Expand Up @@ -117,7 +119,7 @@ func FindVpcName(vpc *ec2.Vpc) string {
}

if *vpc.IsDefault {
return "Default"
return defaultVPCName
}

return ""
Expand Down Expand Up @@ -149,7 +151,7 @@ func GetSubnetsForVpcE(t testing.TestingT, vpcID string, region string) ([]Subne

for _, ec2Subnet := range subnetOutput.Subnets {
subnetTags := GetTagsForSubnet(t, *ec2Subnet.SubnetId, region)
subnet := Subnet{Id: aws.StringValue(ec2Subnet.SubnetId), AvailabilityZone: aws.StringValue(ec2Subnet.AvailabilityZone), Tags: subnetTags}
subnet := Subnet{Id: aws.StringValue(ec2Subnet.SubnetId), AvailabilityZone: aws.StringValue(ec2Subnet.AvailabilityZone), DefaultForAz: aws.BoolValue(ec2Subnet.DefaultForAz), Tags: subnetTags}
subnets = append(subnets, subnet)
}

Expand Down Expand Up @@ -182,6 +184,34 @@ func GetTagsForVpcE(t testing.TestingT, vpcID string, region string) (map[string
return tags, nil
}

// GetDefaultSubnetIDsForVpc gets the ids of the subnets that are the default subnet for the AvailabilityZone
func GetDefaultSubnetIDsForVpc(t testing.TestingT, vpc Vpc) []string {
subnetIDs, err := GetDefaultSubnetIDsForVpcE(t, vpc)
require.NoError(t, err)
return subnetIDs
}

// GetDefaultSubnetIDsForVpcE gets the ids of the subnets that are the default subnet for the AvailabilityZone
func GetDefaultSubnetIDsForVpcE(t testing.TestingT, vpc Vpc) ([]string, error) {
if vpc.Name != defaultVPCName {
// You cannot create a default subnet in a nondefault VPC
// https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html
return nil, fmt.Errorf("Only default VPCs have default subnets but VPC with id %s is not default VPC", vpc.Id)
}
subnetIDs := []string{}
numSubnets := len(vpc.Subnets)
if numSubnets == 0 {
return nil, fmt.Errorf("Expected to find at least one subnet in vpc with ID %s but found zero", vpc.Id)
}

for _, subnet := range vpc.Subnets {
if subnet.DefaultForAz {
subnetIDs = append(subnetIDs, subnet.Id)
}
}
return subnetIDs, nil
}

// GetTagsForSubnet gets the tags for the specified subnet.
func GetTagsForSubnet(t testing.TestingT, subnetId string, region string) map[string]string {
tags, err := GetTagsForSubnetE(t, subnetId, region)
Expand Down Expand Up @@ -234,6 +264,14 @@ func IsPublicSubnetE(t testing.TestingT, subnetId string, region string) (bool,
return false, err
}

if len(rts.RouteTables) == 0 {
// Subnets not explicitly associated with any route table are implicitly associated with the main route table
rts, err = getImplicitRouteTableForSubnetE(t, subnetId, region)
if err != nil {
return false, err
}
}

for _, rt := range rts.RouteTables {
for _, r := range rt.Routes {
if strings.HasPrefix(aws.StringValue(r.GatewayId), "igw-") {
Expand All @@ -245,6 +283,40 @@ func IsPublicSubnetE(t testing.TestingT, subnetId string, region string) (bool,
return false, nil
}

func getImplicitRouteTableForSubnetE(t testing.TestingT, subnetId string, region string) (*ec2.DescribeRouteTablesOutput, error) {
mainRouteFilterName := "association.main"
mainRouteFilterValue := "true"
subnetFilterName := "subnet-id"

client, err := NewEc2ClientE(t, region)
if err != nil {
return nil, err
}

subnetFilter := ec2.Filter{
Name: &subnetFilterName,
Values: []*string{&subnetId},
}
subnetOutput, err := client.DescribeSubnets(&ec2.DescribeSubnetsInput{Filters: []*ec2.Filter{&subnetFilter}})
if err != nil {
return nil, err
}
numSubnets := len(subnetOutput.Subnets)
if numSubnets != 1 {
return nil, fmt.Errorf("Expected to find one subnet with id %s but found %s", subnetId, strconv.Itoa(numSubnets))
}

mainRouteFilter := ec2.Filter{
Name: &mainRouteFilterName,
Values: []*string{&mainRouteFilterValue},
}
vpcFilter := ec2.Filter{
Name: aws.String(vpcIDFilterName),
Values: []*string{subnetOutput.Subnets[0].VpcId},
}
return client.DescribeRouteTables(&ec2.DescribeRouteTablesInput{Filters: []*ec2.Filter{&mainRouteFilter, &vpcFilter}})
}

// GetRandomPrivateCidrBlock gets a random CIDR block from the range of acceptable private IP addresses per RFC 1918
// (https://tools.ietf.org/html/rfc1918#section-3)
// The routingPrefix refers to the "/28" in 1.2.3.4/28.
Expand Down
75 changes: 74 additions & 1 deletion modules/aws/vpc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestGetVpcsE(t *testing.T) {

// the default VPC has by default one subnet per availability zone
// https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html
assert.Equal(t, len(vpcs[0].Subnets), len(azs))
assert.True(t, len(vpcs[0].Subnets) >= len(azs))
}

func TestGetFirstTwoOctets(t *testing.T) {
Expand All @@ -76,6 +77,46 @@ func TestIsPublicSubnet(t *testing.T) {
assert.True(t, IsPublicSubnet(t, *subnet.SubnetId, region))
}

func TestGetDefaultSubnetIDsForVpc(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
defaultVpcBeforeSubnetCreation := GetDefaultVpc(t, region)

// Creates a subnet in the default VPC with deferred deletion
// and fetches vpc object again
subnetName := fmt.Sprintf("%s-subnet", t.Name())
subnet := createPrivateSubnetInDefaultVpc(t, defaultVpcBeforeSubnetCreation.Id, subnetName, region)
defer deleteSubnet(t, *subnet.SubnetId, region)
defaultVpc := GetDefaultVpc(t, region)

defaultSubnetIDs := GetDefaultSubnetIDsForVpc(t, *defaultVpc)
assert.NotEmpty(t, defaultSubnetIDs)
// Checks that the amount of default subnets is smaller than
// total number of subnets in default vpc
assert.True(t, len(defaultSubnetIDs) < len(defaultVpc.Subnets))

availabilityZones := []string{}
for _, id := range defaultSubnetIDs {
// check if the recently created subnet does not come up here
assert.NotEqual(t, id, subnet.SubnetId)
// default subnets are by default public
// https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html
assert.True(t, IsPublicSubnet(t, id, region))
for _, subnet := range defaultVpc.Subnets {
if id == subnet.Id {
availabilityZones = append(availabilityZones, subnet.AvailabilityZone)
}
}
}
// only one default subnet is allowed per AZ
uniqueAZs := map[string]bool{}
for _, az := range availabilityZones {
uniqueAZs[az] = true
}
assert.Equal(t, len(defaultSubnetIDs), len(uniqueAZs))
}

func TestGetTagsForVpc(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -178,6 +219,38 @@ func createSubnet(t *testing.T, vpcId string, routeTableId string, region string
return *createSubnetOutput.Subnet
}

func createPrivateSubnetInDefaultVpc(t *testing.T, vpcId string, subnetName string, region string) ec2.Subnet {
ec2Client := NewEc2Client(t, region)

createSubnetOutput, err := ec2Client.CreateSubnet(&ec2.CreateSubnetInput{
CidrBlock: aws.String("172.31.172.0/24"),
VpcId: aws.String(vpcId),
TagSpecifications: []*ec2.TagSpecification{
{
ResourceType: aws.String("subnet"),
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(subnetName),
},
},
},
},
})
require.NoError(t, err)

return *createSubnetOutput.Subnet
}

func deleteSubnet(t *testing.T, subnetId string, region string) {
ec2Client := NewEc2Client(t, region)

_, err := ec2Client.DeleteSubnet(&ec2.DeleteSubnetInput{
SubnetId: aws.String(subnetId),
})
require.NoError(t, err)
}

func createVpc(t *testing.T, region string) ec2.Vpc {
ec2Client := NewEc2Client(t, region)

Expand Down

0 comments on commit 0dd0f81

Please sign in to comment.