mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ec2): auto-create default security group and main route table on CreateVpc (closes #1091) (#1177)
* feat(ec2): implement default security group and main route table creation for VPC * fix(ec2): update route table association check to use boolean value * test(ec2): update security group test to validate group name and VPC ID * fix(ec2): update security group description in Ec2Service and corresponding tests * test(ec2): fix integration test failures * fix(ec2): fix integration test error * feat(ec2): update description * fix(ec2): fix SG id to use 17 character hex code
This commit is contained in:
1 parent
40c1c4339d
commit
3065774e80
2 files changed
+79
-24
No files matched your search
@@ -136,21 +136,7 @@ public class Ec2Service {
|
||||
subnets.put(key(region, subnetIds[i]), subnet);
|
||||
}
|
||||
|
||||
// Default security group
|
||||
String sgId = "sg-default";
|
||||
SecurityGroup defaultSg = new SecurityGroup();
|
||||
defaultSg.setGroupId(sgId);
|
||||
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);
|
||||
createDefaultSecurityGroup(region, vpcId, "sg-default");
|
||||
|
||||
// Default internet gateway
|
||||
String igwId = "igw-default";
|
||||
@@ -161,22 +147,48 @@ public class Ec2Service {
|
||||
igw.getAttachments().add(new InternetGatewayAttachment(vpcId, "available"));
|
||||
internetGateways.put(key(region, igwId), igw);
|
||||
|
||||
// Main route table for default VPC
|
||||
String rtId = "rtb-default";
|
||||
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(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, securityGroupId), defaultSg);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user