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

Preserve standalone ENI validation before address allocation and the existing ECS network error translation. Keep RunInstances and cross-VPC errors unchanged.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-26 06:29:37 -04:00
1 parent 4ab43eeeab
commit 42314fcd4e
4 files changed
+98 -2

No files matched your search

@@ -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);
@@ -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;
@@ -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
@@ -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),