mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): delete VPC default security group, route table and network ACL with the VPC (#3878)
* fix(ec2): delete VPC default security group, route table and network ACL with the VPC * test(ec2): check deleted VPC defaults by VPC scope instead of by ID --------- Co-authored-by: Jason van Zyl <jason@vanzyl.ca>
This commit is contained in:
1 parent
25fc8e452f
commit
75fee00adc
5 files changed
+169
-1
No files matched your search
+47
@@ -0,0 +1,47 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.CreateVpcRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.DeleteVpcRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.Filter;
|
||||
import software.amazon.awssdk.services.ec2.model.SecurityGroup;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class Ec2VpcDefaultResourceCleanupTest {
|
||||
|
||||
@Test
|
||||
void deleteVpcRemovesVpcOwnedDefaultResourcesAndSecurityGroupRules() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
String vpcId = ec2.createVpc(CreateVpcRequest.builder()
|
||||
.cidrBlock("10.77.0.0/16")
|
||||
.build())
|
||||
.vpc().vpcId();
|
||||
Filter vpcFilter = Filter.builder().name("vpc-id").values(vpcId).build();
|
||||
|
||||
SecurityGroup defaultGroup = ec2.describeSecurityGroups(request -> request.filters(vpcFilter))
|
||||
.securityGroups().stream()
|
||||
.filter(group -> "default".equals(group.groupName()))
|
||||
.findFirst().orElseThrow();
|
||||
assertThat(ec2.describeRouteTables(request -> request.filters(vpcFilter)).routeTables())
|
||||
.anyMatch(table -> table.associations().stream().anyMatch(association -> association.main()));
|
||||
assertThat(ec2.describeNetworkAcls(request -> request.filters(vpcFilter)).networkAcls())
|
||||
.anyMatch(acl -> acl.isDefault());
|
||||
Filter groupFilter = Filter.builder().name("group-id").values(defaultGroup.groupId()).build();
|
||||
assertThat(ec2.describeSecurityGroupRules(request -> request.filters(groupFilter))
|
||||
.securityGroupRules()).isNotEmpty();
|
||||
|
||||
ec2.deleteVpc(DeleteVpcRequest.builder().vpcId(vpcId).build());
|
||||
|
||||
assertThat(ec2.describeSecurityGroups(request -> request.filters(vpcFilter))
|
||||
.securityGroups()).isEmpty();
|
||||
assertThat(ec2.describeRouteTables(request -> request.filters(vpcFilter))
|
||||
.routeTables()).isEmpty();
|
||||
assertThat(ec2.describeNetworkAcls(request -> request.filters(vpcFilter))
|
||||
.networkAcls()).isEmpty();
|
||||
assertThat(ec2.describeSecurityGroupRules(request -> request.filters(groupFilter))
|
||||
.securityGroupRules()).isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,7 +194,7 @@ Floci seeds the following resources on first use in each region so Terraform, th
|
||||
|--------|-------------|
|
||||
| CreateVpc | Creates a VPC with the requested CIDR block. |
|
||||
| DescribeVpcs | Lists or returns stored VPCs. |
|
||||
| DeleteVpc | Deletes a VPC from the local EC2 store. |
|
||||
| DeleteVpc | Deletes a VPC from the local EC2 store, together with its default security group and rules, main route table and default network ACL. |
|
||||
| ModifyVpcAttribute | Updates supported VPC attributes. |
|
||||
| DescribeVpcAttribute | Returns a supported VPC attribute. |
|
||||
| DescribeVpcEndpointServices | Returns an empty local VPC endpoint service catalog. |
|
||||
|
||||
@@ -3570,6 +3570,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
requireNoTransitGatewayAttachment(region,
|
||||
attachment -> vpcId.equals(attachment.getVpcId()),
|
||||
"The vpc '" + vpcId + "' has dependencies and cannot be deleted.");
|
||||
deleteVpcDefaultResources(region, vpcId);
|
||||
vpcs.delete(key(region, vpcId));
|
||||
}
|
||||
if (vpcNetworkManager != null) {
|
||||
@@ -3577,6 +3578,43 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteVpcDefaultResources(String region, String vpcId) {
|
||||
List<SecurityGroup> defaultGroups = securityGroups.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.filter(group -> region.equals(group.getRegion()))
|
||||
.filter(group -> vpcId.equals(group.getVpcId()))
|
||||
.filter(group -> "default".equals(group.getGroupName()))
|
||||
.toList();
|
||||
for (SecurityGroup group : defaultGroups) {
|
||||
// Same lock the authorize/revoke paths hold while they read and re-save a group; without
|
||||
// it, one of them can save the group back after this delete and resurrect it.
|
||||
synchronized (lockFor(key(region, group.getGroupId()))) {
|
||||
List<String> ruleIds = securityGroupRules.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.filter(rule -> group.getGroupId().equals(rule.getGroupId()))
|
||||
.map(SecurityGroupRule::getSecurityGroupRuleId)
|
||||
.toList();
|
||||
ruleIds.forEach(ruleId -> securityGroupRules.delete(key(region, ruleId)));
|
||||
securityGroups.delete(key(region, group.getGroupId()));
|
||||
}
|
||||
}
|
||||
|
||||
List<String> mainRouteTableIds = routeTables.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.filter(table -> region.equals(table.getRegion()))
|
||||
.filter(table -> vpcId.equals(table.getVpcId()))
|
||||
.filter(table -> table.getAssociations().stream()
|
||||
.anyMatch(association -> association.isMain()))
|
||||
.map(RouteTable::getRouteTableId)
|
||||
.toList();
|
||||
mainRouteTableIds.forEach(routeTableId -> routeTables.delete(key(region, routeTableId)));
|
||||
|
||||
List<String> defaultNetworkAclIds = networkAcls.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.filter(acl -> region.equals(acl.getRegion()))
|
||||
.filter(acl -> vpcId.equals(acl.getVpcId()))
|
||||
.filter(acl -> acl.isDefault())
|
||||
.map(NetworkAcl::getNetworkAclId)
|
||||
.toList();
|
||||
defaultNetworkAclIds.forEach(aclId -> networkAcls.delete(key(region, aclId)));
|
||||
}
|
||||
|
||||
public void modifyVpcAttribute(String region, String vpcId, String attribute, String value) {
|
||||
ensureDefaultResources(region);
|
||||
Vpc vpc = getRequiredVpc(region, vpcId);
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -60,6 +61,29 @@ class Ec2ServicePersistenceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Test
|
||||
void deletedVpcDefaultResourcesRemainAbsentAfterRestart(@TempDir Path dir) {
|
||||
Ec2Service first = newService(dir);
|
||||
Vpc vpc = first.createVpc(REGION, "10.77.0.0/16", false);
|
||||
SecurityGroup defaultGroup = first.describeSecurityGroups(REGION, List.of(), List.of(), Map.of()).stream()
|
||||
.filter(group -> vpc.getVpcId().equals(group.getVpcId()) && "default".equals(group.getGroupName()))
|
||||
.findFirst().orElseThrow();
|
||||
|
||||
first.deleteVpc(REGION, vpc.getVpcId());
|
||||
Ec2Service restarted = newService(dir);
|
||||
|
||||
assertTrue(restarted.describeVpcs(REGION, List.of(), Map.of()).stream()
|
||||
.noneMatch(candidate -> vpc.getVpcId().equals(candidate.getVpcId())));
|
||||
assertTrue(restarted.describeSecurityGroups(REGION, List.of(), List.of(), Map.of()).stream()
|
||||
.noneMatch(group -> vpc.getVpcId().equals(group.getVpcId())));
|
||||
assertTrue(restarted.describeSecurityGroupRules(
|
||||
REGION, List.of(defaultGroup.getGroupId()), List.of()).isEmpty());
|
||||
assertTrue(restarted.describeRouteTables(REGION, List.of(), Map.of()).stream()
|
||||
.noneMatch(table -> vpc.getVpcId().equals(table.getVpcId())));
|
||||
assertTrue(restarted.describeNetworkAcls(REGION, List.of(), Map.of()).stream()
|
||||
.noneMatch(acl -> vpc.getVpcId().equals(acl.getVpcId())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcAndSubnetSurviveRestart(@TempDir Path dir) {
|
||||
Ec2Service first = newService(dir);
|
||||
|
||||
@@ -80,6 +80,65 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
class Ec2ServiceTest {
|
||||
|
||||
@Test
|
||||
void deleteVpcRemovesVpcOwnedDefaultResourcesAndRules() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
String region = "us-east-1";
|
||||
String vpcId = service.createVpc(region, "10.77.0.0/16", false).getVpcId();
|
||||
SecurityGroup defaultGroup = service.describeSecurityGroups(region, List.of(), List.of(), Map.of()).stream()
|
||||
.filter(group -> vpcId.equals(group.getVpcId()) && "default".equals(group.getGroupName()))
|
||||
.findFirst().orElseThrow();
|
||||
assertTrue(service.describeRouteTables(region, List.of(), Map.of()).stream()
|
||||
.anyMatch(table -> vpcId.equals(table.getVpcId())
|
||||
&& table.getAssociations().stream().anyMatch(association -> association.isMain())));
|
||||
assertTrue(service.describeNetworkAcls(region, List.of(), Map.of()).stream()
|
||||
.anyMatch(acl -> vpcId.equals(acl.getVpcId()) && acl.isDefault()));
|
||||
assertFalse(service.describeSecurityGroupRules(region, List.of(defaultGroup.getGroupId()), List.of()).isEmpty());
|
||||
|
||||
service.deleteVpc(region, vpcId);
|
||||
|
||||
assertTrue(service.describeSecurityGroups(region, List.of(), List.of(), Map.of()).stream()
|
||||
.noneMatch(group -> vpcId.equals(group.getVpcId())));
|
||||
assertTrue(service.describeRouteTables(region, List.of(), Map.of()).stream()
|
||||
.noneMatch(table -> vpcId.equals(table.getVpcId())));
|
||||
assertTrue(service.describeNetworkAcls(region, List.of(), Map.of()).stream()
|
||||
.noneMatch(acl -> vpcId.equals(acl.getVpcId())));
|
||||
assertTrue(service.describeSecurityGroupRules(region, List.of(defaultGroup.getGroupId()), List.of()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteVpcDoesNotRemoveAnotherVpcDefaults() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
String region = "us-east-1";
|
||||
String deletedVpcId = service.createVpc(region, "10.78.0.0/16", false).getVpcId();
|
||||
String preservedVpcId = service.createVpc(region, "10.79.0.0/16", false).getVpcId();
|
||||
SecurityGroup preservedGroup = service.describeSecurityGroups(region, List.of(), List.of(), Map.of()).stream()
|
||||
.filter(group -> preservedVpcId.equals(group.getVpcId()) && "default".equals(group.getGroupName()))
|
||||
.findFirst().orElseThrow();
|
||||
String preservedRouteTableId = service.describeRouteTables(region, List.of(), Map.of()).stream()
|
||||
.filter(table -> preservedVpcId.equals(table.getVpcId()))
|
||||
.filter(table -> table.getAssociations().stream().anyMatch(association -> association.isMain()))
|
||||
.findFirst().orElseThrow().getRouteTableId();
|
||||
String preservedNetworkAclId = service.describeNetworkAcls(region, List.of(), Map.of()).stream()
|
||||
.filter(acl -> preservedVpcId.equals(acl.getVpcId()) && acl.isDefault())
|
||||
.findFirst().orElseThrow().getNetworkAclId();
|
||||
|
||||
service.deleteVpc(region, deletedVpcId);
|
||||
|
||||
assertEquals(preservedGroup.getGroupId(), service.describeSecurityGroups(
|
||||
region, List.of(preservedGroup.getGroupId()), List.of(), Map.of()).getFirst().getGroupId());
|
||||
assertEquals(preservedRouteTableId,
|
||||
service.describeRouteTables(region, List.of(preservedRouteTableId), Map.of()).getFirst().getRouteTableId());
|
||||
assertEquals(preservedNetworkAclId,
|
||||
service.describeNetworkAcls(region, List.of(preservedNetworkAclId), Map.of()).getFirst().getNetworkAclId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockModeTreatsExistingNonTerminatedInstanceAsRunningContainer() {
|
||||
Ec2ContainerManager containerManager = mock(Ec2ContainerManager.class);
|
||||
|
||||
Reference in new issue
Block a user