Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(ec2): auto-create default security group and main route table on CreateVpc (closes #1091) (#1177)

Prakhar Kapoor3065774e80a3
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
index ed4daea8fc..d2152efbec 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
@@ -136,47 +136,59 @@ public class Ec2Service {
subnets.put(key(region, subnetIds[i]), subnet);
}
- // Default security group
- String sgId = "sg-default";
+ createDefaultSecurityGroup(region, vpcId, "sg-default");
+
+ // Default internet gateway
+ String igwId = "igw-default";
+ InternetGateway igw = new InternetGateway();
+ igw.setInternetGatewayId(igwId);
+ igw.setOwnerId(accountId);
+ igw.setRegion(region);
+ igw.getAttachments().add(new InternetGatewayAttachment(vpcId, "available"));
+ internetGateways.put(key(region, igwId), igw);
+
+ String rtId = createMainRouteTable(region, defaultVpc, "rtb-default", "rtbassoc-default");
+
+ RouteTable mainRt = routeTables.get(key(region, rtId));
+ if (mainRt != null) {
+ mainRt.getRoutes().add(new Route("0.0.0.0/0", igwId, "CreateRoute"));
+ }
+ }
+
+ private void createDefaultSecurityGroup(String region, String vpcId, String securityGroupId) {
SecurityGroup defaultSg = new SecurityGroup();
- defaultSg.setGroupId(sgId);
+ defaultSg.setGroupId(securityGroupId);
defaultSg.setGroupName("default");
defaultSg.setDescription("default VPC security group");
defaultSg.setVpcId(vpcId);
defaultSg.setOwnerId(accountId);
defaultSg.setRegion(region);
+
// Default egress: all traffic
IpPermission egressAll = new IpPermission();
egressAll.setIpProtocol("-1");
egressAll.getIpRanges().add(new IpRange("0.0.0.0/0"));
defaultSg.getIpPermissionsEgress().add(egressAll);
- securityGroups.put(key(region, sgId), defaultSg);
-
- // Default internet gateway
- String igwId = "igw-default";
- InternetGateway igw = new InternetGateway();
- igw.setInternetGatewayId(igwId);
- igw.setOwnerId(accountId);
- igw.setRegion(region);
- igw.getAttachments().add(new InternetGatewayAttachment(vpcId, "available"));
- internetGateways.put(key(region, igwId), igw);
+ securityGroups.put(key(region, securityGroupId), defaultSg);
+ }
- // Main route table for default VPC
- String rtId = "rtb-default";
+ private String createMainRouteTable(String region, Vpc vpc, String routeTableId, String associationId) {
RouteTable mainRt = new RouteTable();
- mainRt.setRouteTableId(rtId);
- mainRt.setVpcId(vpcId);
+ mainRt.setRouteTableId(routeTableId);
+ mainRt.setVpcId(vpc.getVpcId());
mainRt.setOwnerId(accountId);
mainRt.setRegion(region);
- mainRt.getRoutes().add(new Route("172.31.0.0/16", "local", "CreateRouteTable"));
- mainRt.getRoutes().add(new Route("0.0.0.0/0", igwId, "CreateRoute"));
+ mainRt.getRoutes().add(new Route(vpc.getCidrBlock(), "local", "CreateRouteTable"));
+
RouteTableAssociation mainAssoc = new RouteTableAssociation();
- mainAssoc.setRouteTableAssociationId("rtbassoc-default");
- mainAssoc.setRouteTableId(rtId);
+ mainAssoc.setRouteTableAssociationId(associationId);
+ mainAssoc.setRouteTableId(routeTableId);
mainAssoc.setMain(true);
mainAssoc.setAssociationState("associated");
mainRt.getAssociations().add(mainAssoc);
- routeTables.put(key(region, rtId), mainRt);
+
+ routeTables.put(key(region, routeTableId), mainRt);
+ return routeTableId;
}
private String key(String region, String id) {
@@ -541,6 +553,9 @@ public class Ec2Service {
vpc.getCidrBlockAssociationSet().add(
new VpcCidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), cidrBlock));
vpcs.put(key(region, vpcId), vpc);
+
+ createDefaultSecurityGroup(region, vpcId, "sg-" + randomHex(17));
+ createMainRouteTable(region, vpc, "rtb-" + randomHex(17), "rtbassoc-" + randomHex(17));
return vpc;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
index fe8403785a..e642a6099f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ec2;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.not;
@@ -91,6 +92,8 @@ class Ec2IntegrationTest {
.statusCode(200)
.contentType("application/xml")
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item[0].groupName", equalTo("default"))
+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item[0].groupDescription",
+ equalTo("default VPC security group"))
.body("DescribeSecurityGroupsResponse.securityGroupInfo.item[0].vpcId", equalTo("vpc-default"));
}
@@ -243,6 +246,43 @@ class Ec2IntegrationTest {
@Test
@Order(14)
+ void describeCreatedVpcDefaultSecurityGroup() {
+ given()
+ .formParam("Action", "DescribeSecurityGroups")
+ .formParam("Filter.1.Name", "vpc-id")
+ .formParam("Filter.1.Value.1", vpcId)
+ .formParam("Filter.2.Name", "group-name")
+ .formParam("Filter.2.Value.1", "default")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.groupName", equalTo("default"))
+ .body("DescribeSecurityGroupsResponse.securityGroupInfo.item.vpcId", equalTo(vpcId));
+ }
+
+ @Test
+ @Order(15)
+ void describeCreatedVpcMainRouteTable() {
+ given()
+ .formParam("Action", "DescribeRouteTables")
+ .formParam("Filter.1.Name", "vpc-id")
+ .formParam("Filter.1.Value.1", vpcId)
+ .formParam("Filter.2.Name", "association.main")
+ .formParam("Filter.2.Value.1", "true")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("DescribeRouteTablesResponse.routeTableSet.item.vpcId", equalTo(vpcId))
+ .body("DescribeRouteTablesResponse.routeTableSet.item.associationSet.item.main",
+ anyOf(equalTo(true), equalTo("true")));
+ }
+
+ @Test
+ @Order(16)
void describeVpcEndpointServices() {
given()
.formParam("Action", "DescribeVpcEndpointServices")