Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ec2): report missing network interface security groups (#6582)

Andre Kurait42314fcd4e98
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 3858380faf..31386ddedc 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
@@ -13875,7 +13875,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
List<GroupIdentifier> sgIdentifiers = new ArrayList<>();
if (securityGroupIds != null && !securityGroupIds.isEmpty()) {
for (String sgId : securityGroupIds) {
- SecurityGroup sg = getRequiredSecurityGroup(region, sgId);
+ SecurityGroup sg = securityGroups.get(key(region, sgId)).orElseThrow(() ->
+ new AwsException("InvalidSecurityGroupID.NotFound",
+ "The securityGroup ID '" + sgId + "' does not exist", 400));
if (!subnet.getVpcId().equals(sg.getVpcId())) {
throw new AwsException("InvalidGroup.NotFound",
"Security group " + sgId + " does not belong to the subnet VPC", 400);
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
index 4088132aea..c73d5a165b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java
@@ -109,7 +109,7 @@ public class EcsContainerManager {
/** EC2 error codes the task ENI path can raise, none of which RunTask declares. */
private static final Set<String> EC2_NETWORK_LOOKUP_FAILURES =
- Set.of("InvalidSubnetID.NotFound", "InvalidGroup.NotFound");
+ Set.of("InvalidSubnetID.NotFound", "InvalidGroup.NotFound", "InvalidSecurityGroupID.NotFound");
/** How long a container waits for a dependency that has to COMPLETE, SUCCEED or get HEALTHY. */
private static final int DEPENDENCY_WAIT_SECONDS = 60;
private static final long DEPENDENCY_POLL_MILLIS = 200;
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NetworkInterfaceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NetworkInterfaceIntegrationTest.java
index 9b57d9388c..216fcce544 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NetworkInterfaceIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2NetworkInterfaceIntegrationTest.java
@@ -83,6 +83,24 @@ class Ec2NetworkInterfaceIntegrationTest {
org.junit.jupiter.api.Assertions.assertTrue(securityGroupId.startsWith("sg-"));
}
+ @Test
+ @Order(18)
+ void missingSecurityGroupReturnsTheNetworkInterfaceError() {
+ String missingGroup = "sg-0f294603eafad4835";
+ given()
+ .formParam("Action", "CreateNetworkInterface")
+ .formParam("SubnetId", subnetId)
+ .formParam("SecurityGroupId.1", missingGroup)
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo("InvalidSecurityGroupID.NotFound"))
+ .body("Response.Errors.Error.Message",
+ equalTo("The securityGroup ID '" + missingGroup + "' does not exist"));
+ }
+
// ─── Create ────────────────────────────────────────────────────────────────
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
index 85e0f79873..f76b2ead1d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
@@ -224,6 +224,82 @@ class Ec2ServiceTest {
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void createNetworkInterfaceRejectsMissingGroupsBeforeAllocatingAddresses(boolean includeExistingGroup) {
+ Ec2Service service = mockModeService();
+ String region = "us-east-1";
+ Subnet subnet = service.describeSubnets(region, List.of(), Map.of()).getFirst();
+ String missingGroup = "sg-0f294603eafad4835";
+ SecurityGroup existingGroup = service.createSecurityGroup(region, "interface-validation",
+ "interface validation", subnet.getVpcId());
+ List<String> groupIds = includeExistingGroup
+ ? List.of(existingGroup.getGroupId(), missingGroup) : List.of(missingGroup);
+ int availableAddresses = subnet.getAvailableIpAddressCount();
+
+ AwsException error = assertThrows(AwsException.class, () -> service.createNetworkInterface(
+ region, subnet.getSubnetId(), null, null, List.of(), groupIds, List.of()));
+
+ assertEquals("InvalidSecurityGroupID.NotFound", error.getErrorCode());
+ assertEquals("The securityGroup ID '" + missingGroup + "' does not exist", error.getMessage());
+ assertEquals(400, error.getHttpStatus());
+ assertTrue(service.describeNetworkInterfaces(region, List.of(), Map.of()).networkInterfaces().isEmpty());
+ assertEquals(availableAddresses, service.describeSubnets(region, List.of(subnet.getSubnetId()), Map.of())
+ .getFirst().getAvailableIpAddressCount());
+ }
+
+ @Test
+ void createNetworkInterfaceRejectsGroupsFromAnotherVpcBeforeAllocatingAddresses() {
+ Ec2Service service = mockModeService();
+ String region = "us-east-1";
+ Subnet subnet = service.describeSubnets(region, List.of(), Map.of()).getFirst();
+ String foreignVpc = service.createVpc(region, "10.93.0.0/16", false).getVpcId();
+ SecurityGroup foreignGroup = service.createSecurityGroup(region, "foreign-interface-group",
+ "foreign interface group", foreignVpc);
+ int availableAddresses = subnet.getAvailableIpAddressCount();
+
+ AwsException error = assertThrows(AwsException.class, () -> service.createNetworkInterface(
+ region, subnet.getSubnetId(), null, null, List.of(),
+ List.of(foreignGroup.getGroupId()), List.of()));
+
+ assertEquals("InvalidGroup.NotFound", error.getErrorCode());
+ assertTrue(service.describeNetworkInterfaces(region, List.of(), Map.of()).networkInterfaces().isEmpty());
+ assertEquals(availableAddresses, service.describeSubnets(region, List.of(subnet.getSubnetId()), Map.of())
+ .getFirst().getAvailableIpAddressCount());
+ }
+
+ @Test
+ void createNetworkInterfaceRetainsAnExistingSubnetVpcGroup() {
+ Ec2Service service = mockModeService();
+ String region = "us-east-1";
+ Subnet subnet = service.describeSubnets(region, List.of(), Map.of()).getFirst();
+ SecurityGroup group = service.createSecurityGroup(region, "valid-interface-group",
+ "valid interface group", subnet.getVpcId());
+
+ NetworkInterface networkInterface = service.createNetworkInterface(region, subnet.getSubnetId(),
+ null, null, List.of(), List.of(group.getGroupId()), List.of());
+
+ assertEquals(List.of(group.getGroupId()), networkInterface.getGroups().stream()
+ .map(GroupIdentifier::getGroupId).toList());
+ assertEquals(subnet.getVpcId(), networkInterface.getVpcId());
+ assertEquals(1, service.describeNetworkInterfaces(region, List.of(), Map.of()).networkInterfaces().size());
+ }
+
+ @Test
+ void runInstancesRetainsItsMissingSecurityGroupErrorWithoutAllocatingResources() {
+ Ec2Service service = mockModeService();
+ String region = "us-east-1";
+ Subnet subnet = service.describeSubnets(region, List.of(), Map.of()).getFirst();
+
+ AwsException error = assertThrows(AwsException.class, () -> service.runInstances(
+ region, "ami-test", "t3.micro", 1, 1, null, List.of("sg-0f294603eafad4835"),
+ subnet.getSubnetId(), null, List.of(), null, null));
+
+ assertEquals("InvalidGroup.NotFound", error.getErrorCode());
+ assertTrue(service.describeInstances(region, List.of(), Map.of()).isEmpty());
+ assertTrue(service.describeVolumes(region, List.of(), Map.of()).isEmpty());
+ }
+
@Test
void subnetAvailableAddressesTrackAllocationsAndRelease() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),