diff --git a/src/main/java/io/floci/aws/services/ec2/Ec2QueryHandler.java b/src/main/java/io/floci/aws/services/ec2/Ec2QueryHandler.java index 54098601ef..eeb5b0c687 100644 --- a/src/main/java/io/floci/aws/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/floci/aws/services/ec2/Ec2QueryHandler.java @@ -5561,7 +5561,8 @@ public class Ec2QueryHandler { Integer ipv6AddressCount = parseOptionalInt(p.getFirst("Ipv6AddressCount"), "Ipv6AddressCount"); NetworkInterface ni = service.createNetworkInterface(region, subnetId, description, - privateIpAddress, privateIpAddresses, ipv6Addresses, ipv6AddressCount, securityGroupIds, tagList); + privateIpAddress, privateIpAddresses, ipv6Addresses, ipv6AddressCount, securityGroupIds, tagList, + intParam(p, "SecondaryPrivateIpAddressCount")); XmlBuilder xml = new XmlBuilder() .start("CreateNetworkInterfaceResponse", AwsNamespaces.EC2) diff --git a/src/main/java/io/floci/aws/services/ec2/Ec2Service.java b/src/main/java/io/floci/aws/services/ec2/Ec2Service.java index 73a477ea78..a6b85f23b3 100644 --- a/src/main/java/io/floci/aws/services/ec2/Ec2Service.java +++ b/src/main/java/io/floci/aws/services/ec2/Ec2Service.java @@ -16130,7 +16130,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab String privateIpAddress, List privateIpAddresses, List securityGroupIds, List tagList) { return createNetworkInterface(region, subnetId, description, privateIpAddress, privateIpAddresses, - List.of(), null, securityGroupIds, tagList); + List.of(), null, securityGroupIds, tagList, null); + } + + public NetworkInterface createNetworkInterface(String region, String subnetId, String description, + String privateIpAddress, List privateIpAddresses, + List securityGroupIds, List tagList, + Integer secondaryCount) { + return createNetworkInterface(region, subnetId, description, privateIpAddress, privateIpAddresses, + List.of(), null, securityGroupIds, tagList, secondaryCount); + } + + public NetworkInterface createNetworkInterface(String region, String subnetId, String description, + String privateIpAddress, List privateIpAddresses, + List ipv6Addresses, Integer ipv6AddressCount, + List securityGroupIds, List tagList) { + return createNetworkInterface(region, subnetId, description, privateIpAddress, privateIpAddresses, + ipv6Addresses, ipv6AddressCount, securityGroupIds, tagList, null); } /** @@ -16140,7 +16156,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab public NetworkInterface createNetworkInterface(String region, String subnetId, String description, String privateIpAddress, List privateIpAddresses, List ipv6Addresses, Integer ipv6AddressCount, - List securityGroupIds, List tagList) { + List securityGroupIds, List tagList, + Integer secondaryCount) { + if (secondaryCount != null && secondaryCount < 0) { + throw new AwsException("InvalidParameterValue", "SecondaryPrivateIpAddressCount must not be negative", 400); + } + if (secondaryCount != null && privateIpAddresses != null && !privateIpAddresses.isEmpty()) { + throw new AwsException("InvalidParameterCombination", + "SecondaryPrivateIpAddressCount cannot be combined with explicit secondary addresses", 400); + } if (subnetId == null || subnetId.isBlank()) { throw new AwsException("MissingParameter", "The request must contain the parameter SubnetId", 400); @@ -16190,6 +16214,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab } List ipv6 = ipv6AddressesForNewInterface(region, subnet, ipv6Addresses, ipv6AddressCount); String primaryIp = requestedIp != null ? requestedIp : assignPrivateIp(region, subnetId); + List allocated = new ArrayList<>(); + if (requestedIp == null) { + allocated.add(primaryIp); + } + Set pending = new LinkedHashSet<>(secondaries); + pending.add(primaryIp); + try { + for (int i = 0; i < (secondaryCount == null ? 0 : secondaryCount); i++) { + String extra = assignPrivateIp(region, subnetId, pending); + allocated.add(extra); + pending.add(extra); + secondaries.add(extra); + } + } catch (AwsException e) { + releasePrivateIps(region, subnetId, allocated); + throw e; + } String primaryDns = AwsRegions.ec2PrivateIpDnsName(primaryIp, region); NetworkInterface ni = new NetworkInterface(); @@ -16900,7 +16941,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab releaseInterfaceAddresses(region, networkInterfaceId, true); networkInterfaces.delete(key(region, networkInterfaceId)); removeTags(region, networkInterfaceId); - releasePrivateIps(region, ni.getSubnetId(), List.of(ni.getPrivateIpAddress())); + releasePrivateIps(region, ni.getSubnetId(), ni.getPrivateIpAddresses().stream() + .map(NetworkInterfacePrivateIpAddress::getPrivateIpAddress).toList()); } } diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2CreateNetworkInterfaceSecondaryCountIntegrationTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2CreateNetworkInterfaceSecondaryCountIntegrationTest.java new file mode 100644 index 0000000000..4c150024c1 --- /dev/null +++ b/src/test/java/io/floci/aws/services/ec2/Ec2CreateNetworkInterfaceSecondaryCountIntegrationTest.java @@ -0,0 +1,116 @@ +package io.floci.aws.services.ec2; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.ec2.Ec2Client; +import software.amazon.awssdk.services.ec2.model.CreateNetworkInterfaceRequest; +import software.amazon.awssdk.services.ec2.model.Filter; +import software.amazon.awssdk.services.ec2.model.NetworkInterface; +import software.amazon.awssdk.services.ec2.model.NetworkInterfacePrivateIpAddress; +import software.amazon.awssdk.services.ec2.model.PrivateIpAddressSpecification; + +import java.util.HashSet; +import java.util.List; + +import static io.floci.aws.testing.AwsSdkErrors.assertAwsError; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class Ec2CreateNetworkInterfaceSecondaryCountIntegrationTest { + private Ec2Client ec2; + private String vpcId; + private String subnetId; + + @BeforeEach + void createNetwork() { + ec2 = AwsTestClients.create(Ec2Client.builder(), "850700000001", Region.US_EAST_1); + vpcId = ec2.createVpc(r -> r.cidrBlock("10.85.0.0/16")).vpc().vpcId(); + subnetId = ec2.createSubnet(r -> r.vpcId(vpcId).cidrBlock("10.85.1.0/28")).subnet().subnetId(); + } + + @AfterEach + void deleteNetwork() { + try { + for (NetworkInterface eni : interfaces()) { + ec2.deleteNetworkInterface(r -> r.networkInterfaceId(eni.networkInterfaceId())); + } + ec2.deleteSubnet(r -> r.subnetId(subnetId)); + ec2.deleteVpc(r -> r.vpcId(vpcId)); + } finally { + ec2.close(); + } + } + + @Test + void automaticAddressesRoundTripAndSupportBothExplicitPrimaryForms() { + for (int primaryForm = 0; primaryForm < 3; primaryForm++) { + CreateNetworkInterfaceRequest.Builder request = request(2); + if (primaryForm == 1) { + request.privateIpAddress("10.85.1.4"); + } else if (primaryForm == 2) { + request.privateIpAddresses(PrivateIpAddressSpecification.builder() + .privateIpAddress("10.85.1.4").primary(true).build()); + } + NetworkInterface eni = ec2.createNetworkInterface(request.build()).networkInterface(); + assertAddresses(eni, 3); + if (primaryForm != 0) { + assertEquals("10.85.1.4", eni.privateIpAddress()); + } + NetworkInterface described = ec2.describeNetworkInterfaces(r -> r + .networkInterfaceIds(eni.networkInterfaceId())).networkInterfaces().getFirst(); + assertEquals(new HashSet<>(eni.privateIpAddresses()), new HashSet<>(described.privateIpAddresses())); + ec2.deleteNetworkInterface(r -> r.networkInterfaceId(eni.networkInterfaceId())); + } + assertAddresses(ec2.createNetworkInterface(request(0).build()).networkInterface(), 1); + } + + @Test + void invalidCountsAndExplicitSecondariesFailBeforeAllocation() { + assertAwsError("InvalidParameterValue", 400, () -> ec2.createNetworkInterface(request(-1).build())); + assertAwsError("InvalidParameterCombination", 400, () -> ec2.createNetworkInterface(request(2).privateIpAddresses( + PrivateIpAddressSpecification.builder().privateIpAddress("10.85.1.5").primary(false).build()).build())); + assertTrue(interfaces().isEmpty()); + assertAddresses(ec2.createNetworkInterface(request(10).build()).networkInterface(), 11); + } + + @Test + void exhaustionRollsBackAndDeletionMakesAllAddressesReusable() { + for (boolean explicitPrimary : List.of(false, true)) { + CreateNetworkInterfaceRequest.Builder exhausted = request(11); + if (explicitPrimary) { + exhausted.privateIpAddress("10.85.1.4"); + } + assertAwsError("InsufficientFreeAddressesInSubnet", 400, + () -> ec2.createNetworkInterface(exhausted.build())); + assertTrue(interfaces().isEmpty()); + for (int attempt = 0; attempt < 2; attempt++) { + NetworkInterface eni = ec2.createNetworkInterface(request(10).build()).networkInterface(); + assertAddresses(eni, 11); + ec2.deleteNetworkInterface(r -> r.networkInterfaceId(eni.networkInterfaceId())); + } + } + } + + private CreateNetworkInterfaceRequest.Builder request(int count) { + return CreateNetworkInterfaceRequest.builder().subnetId(subnetId).secondaryPrivateIpAddressCount(count); + } + + private List interfaces() { + return ec2.describeNetworkInterfaces(r -> r.filters(Filter.builder() + .name("subnet-id").values(subnetId).build())).networkInterfaces(); + } + + private static void assertAddresses(NetworkInterface eni, int count) { + List addresses = eni.privateIpAddresses(); + assertEquals(count, addresses.size()); + assertEquals(count, addresses.stream().map(NetworkInterfacePrivateIpAddress::privateIpAddress).distinct().count()); + assertEquals(1L, addresses.stream().filter(NetworkInterfacePrivateIpAddress::primary).count()); + assertEquals(eni.privateIpAddress(), addresses.stream().filter(NetworkInterfacePrivateIpAddress::primary) + .findFirst().orElseThrow().privateIpAddress()); + } +} diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIntegrationTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIntegrationTest.java index 3eaecf4428..00a2817afa 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIntegrationTest.java @@ -798,6 +798,13 @@ class Ec2NetworkInterfaceIntegrationTest { return out; } + @Test + @Order(40) + void nonIntegerSecondaryCountIsRejected() { + createWithList("SecondaryPrivateIpAddressCount", "not-an-integer").statusCode(400) + .body("Response.Errors.Error.Code", equalTo("InvalidParameterValue")); + } + private static void deleteEni(String id) { given().formParam("Action", "DeleteNetworkInterface").formParam("NetworkInterfaceId", id) .header("Authorization", AUTH_HEADER).when().post("/").then().statusCode(200); diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIpv6IntegrationTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIpv6IntegrationTest.java index 9e85fa2e36..4deeb4b3db 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIpv6IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2NetworkInterfaceIpv6IntegrationTest.java @@ -235,6 +235,20 @@ class Ec2NetworkInterfaceIpv6IntegrationTest { assertEquals("UnsupportedOperation", error.awsErrorDetails().errorCode()); } + @Test + void secondaryIpv4AndIpv6CountsAreAppliedTogether() { + Fixture fixture = fixture(); + String eni = create(fixture, request -> request.secondaryPrivateIpAddressCount(2).ipv6AddressCount(2)); + + NetworkInterface described = describe(fixture, eni); + assertEquals(3, described.privateIpAddresses().size()); + assertEquals(3L, described.privateIpAddresses().stream().map(address -> address.privateIpAddress()) + .distinct().count()); + assertEquals(1L, described.privateIpAddresses().stream().filter(address -> address.primary()).count()); + assertEquals(2, addresses(described).size()); + assertNotEquals(addresses(described).get(0), addresses(described).get(1)); + } + private Fixture fixture() { return fixture(64); } diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java index 65808b5bde..175d5e9002 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java @@ -5303,6 +5303,43 @@ class Ec2ServiceTest { assertEquals(List.of("10.50.1.10"), fixture.released()); } + @Test + void deletingAnInterfaceReleasesAllAutomaticallyAllocatedAddresses() { + MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", + "10.50.1.10", "10.50.1.11", "10.50.1.12"); + NetworkInterface eni = fixture.service().createNetworkInterface("us-east-1", fixture.subnetId(), null, + null, List.of(), List.of(), List.of(), 2); + + fixture.service().deleteNetworkInterface("us-east-1", eni.getNetworkInterfaceId()); + + assertEquals(List.of("10.50.1.10", "10.50.1.11", "10.50.1.12"), fixture.released()); + } + + @Test + void failedSecondaryAllocationReleasesEveryLeaseButNotAnExplicitPrimary() { + for (String primary : List.of("", "10.50.1.4")) { + MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/28", "10.50.1.10", "10.50.1.11"); + AwsException error = assertThrows(AwsException.class, () -> fixture.service().createNetworkInterface( + "us-east-1", fixture.subnetId(), null, primary, List.of(), List.of(), List.of(), 11)); + + assertEquals("InsufficientFreeAddressesInSubnet", error.getErrorCode()); + assertTrue(fixture.released().containsAll(List.of("10.50.1.10", "10.50.1.11"))); + assertFalse(fixture.released().contains(primary)); + } + } + + @Test + void invalidSecondaryCountRequestsNeverAllocateAnAddress() { + MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.10"); + assertEquals("InvalidParameterValue", assertThrows(AwsException.class, + () -> fixture.service().createNetworkInterface("us-east-1", fixture.subnetId(), null, + null, List.of(), List.of(), List.of(), -1)).getErrorCode()); + assertEquals("InvalidParameterCombination", assertThrows(AwsException.class, + () -> fixture.service().createNetworkInterface("us-east-1", fixture.subnetId(), null, + null, List.of("10.50.1.20"), List.of(), List.of(), 2)).getErrorCode()); + verify(fixture.vpcNetworks(), never()).allocatePrivateIp(anyString(), anyString()); + } + @Test void anInterfaceRefusedForAHeldSecondaryLeasesNoPrimaryOnTheVpcNetwork() { MockedNetworkFixture fixture = mockedNetworkFixture("10.50.1.0/24", "10.50.1.10");