fix(ec2): preserve elastic IP pool and border group (#5821)

* fix(ec2): preserve elastic IP pool and border group

* test(ec2): share account-scoped query requests
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-17 08:23:47 -04:00
1 parent 5eb4eecf99
commit 5fc58a9a9c
8 files changed
+228 -15

No files matched your search

@@ -0,0 +1,133 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static io.github.hectorvent.floci.testing.Ec2Requests.ec2;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
class Ec2AddressMetadataIntegrationTest {
private static final String ACCOUNT_ID = "000000000000";
private static final String REGION = "us-west-2";
private static final String ALLOCATED = "AllocateAddressResponse.";
private static final String DESCRIBED = "DescribeAddressesResponse.addressesSet.item.";
private String allocationId;
private String associationId;
private String instanceId;
@AfterEach
void deleteCreatedResources() {
try {
if (associationId != null) {
ec2("DisassociateAddress", ACCOUNT_ID, REGION).formParam("AssociationId", associationId)
.post("/").then().statusCode(200);
}
} finally {
try {
if (allocationId != null) {
ec2("ReleaseAddress", ACCOUNT_ID, REGION).formParam("AllocationId", allocationId)
.post("/").then().statusCode(200);
}
} finally {
if (instanceId != null) {
ec2("TerminateInstances", ACCOUNT_ID, REGION).formParam("InstanceId.1", instanceId)
.post("/").then().statusCode(200);
}
}
}
}
@Test
void omittedMetadataUsesAmazonPoolAndTheRequestRegion() {
Response allocated = allocate(ec2("AllocateAddress", ACCOUNT_ID, REGION));
allocated.then()
.body(ALLOCATED + "publicIpv4Pool", equalTo("amazon"))
.body(ALLOCATED + "networkBorderGroup", equalTo(REGION));
describe().then()
.body(DESCRIBED + "publicIpv4Pool", equalTo("amazon"))
.body(DESCRIBED + "networkBorderGroup", equalTo(REGION));
}
@ParameterizedTest
@CsvSource({"amazon,us-west-2", "ipv4pool-ec2-0123456789abcdef0,us-west-2-lax-1"})
void explicitMetadataIsAllocatedDescribedAndFiltered(String pool, String borderGroup) {
Response allocated = allocate(ec2("AllocateAddress", ACCOUNT_ID, REGION)
.formParam("PublicIpv4Pool", pool)
.formParam("NetworkBorderGroup", borderGroup));
allocated.then()
.body(ALLOCATED + "publicIpv4Pool", equalTo(pool))
.body(ALLOCATED + "networkBorderGroup", equalTo(borderGroup));
describe().then()
.body(DESCRIBED + "publicIpv4Pool", equalTo(pool))
.body(DESCRIBED + "networkBorderGroup", equalTo(borderGroup));
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId)
.formParam("Filter.1.Name", "network-border-group")
.formParam("Filter.1.Value.1", borderGroup)
.post("/").then().statusCode(200)
.body("DescribeAddressesResponse.addressesSet.item.size()", equalTo(1))
.body(DESCRIBED + "allocationId", equalTo(allocationId));
ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId)
.formParam("Filter.1.Name", "network-border-group")
.formParam("Filter.1.Value.1", "us-east-1")
.post("/").then().statusCode(200)
.body("DescribeAddressesResponse.addressesSet.item.size()", equalTo(0));
}
@Test
void metadataSurvivesInstanceAssociationAndDisassociation() {
String pool = "ipv4pool-ec2-0123456789abcdef0";
allocate(ec2("AllocateAddress", ACCOUNT_ID, REGION)
.formParam("PublicIpv4Pool", pool)
.formParam("NetworkBorderGroup", REGION));
instanceId = ec2("RunInstances", ACCOUNT_ID, REGION)
.formParam("ImageId", "ami-0abcdef1234567890")
.formParam("InstanceType", "t3.micro")
.formParam("MinCount", "1").formParam("MaxCount", "1")
.post("/").then().statusCode(200)
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
associationId = ec2("AssociateAddress", ACCOUNT_ID, REGION)
.formParam("AllocationId", allocationId)
.formParam("InstanceId", instanceId)
.post("/").then().statusCode(200)
.extract().path("AssociateAddressResponse.associationId");
describe().then()
.body(DESCRIBED + "associationId", equalTo(associationId))
.body(DESCRIBED + "instanceId", equalTo(instanceId))
.body(DESCRIBED + "publicIpv4Pool", equalTo(pool))
.body(DESCRIBED + "networkBorderGroup", equalTo(REGION));
ec2("DisassociateAddress", ACCOUNT_ID, REGION).formParam("AssociationId", associationId)
.post("/").then().statusCode(200);
associationId = null;
describe().then()
.body(DESCRIBED + "associationId.size()", equalTo(0))
.body(DESCRIBED + "instanceId.size()", equalTo(0))
.body(DESCRIBED + "publicIpv4Pool", equalTo(pool))
.body(DESCRIBED + "networkBorderGroup", equalTo(REGION));
}
private Response allocate(RequestSpecification request) {
Response allocated = request.formParam("Domain", "vpc")
.post("/").then().statusCode(200).extract().response();
allocationId = allocated.path(ALLOCATED + "allocationId");
return allocated;
}
private Response describe() {
return ec2("DescribeAddresses", ACCOUNT_ID, REGION).formParam("AllocationId.1", allocationId)
.post("/").then().statusCode(200).extract().response();
}
}
@@ -62,6 +62,46 @@ class Ec2ServicePersistenceTest {
private static final String REGION = "us-east-1";
@Test
void elasticIpMetadataSurvivesRestart(@TempDir Path dir) {
String region = "us-west-2";
String pool = "ipv4pool-ec2-0123456789abcdef0";
String borderGroup = "us-west-2-lax-1";
Ec2Service first = newService(dir);
Address allocated = first.allocateAddress(region, pool, borderGroup);
Ec2Service restarted = newService(dir);
Address described = restarted.describeAddresses(
region, List.of(allocated.getAllocationId()), Map.of()).getFirst();
assertEquals(allocated.getAllocationId(), described.getAllocationId());
assertEquals(pool, described.getPublicIpv4Pool());
assertEquals(borderGroup, described.getNetworkBorderGroup());
}
@Test
void legacyElasticIpMetadataDefaultsToAmazonAndItsStoredRegion(@TempDir Path dir) throws IOException {
String region = "us-west-2";
String allocationId = "eipalloc-0123456789abcdef0";
String legacyJson = """
{
"%s::%s": {
"allocationId": "%s",
"publicIp": "54.1.2.3",
"domain": "vpc",
"region": "%s"
}
}
""".formatted(region, allocationId, allocationId, region);
Files.writeString(dir.resolve("ec2-addresses.json"), legacyJson);
Ec2Service restarted = newService(dir);
Address described = restarted.describeAddresses(region, List.of(allocationId), Map.of()).getFirst();
assertEquals("amazon", described.getPublicIpv4Pool());
assertEquals(region, described.getNetworkBorderGroup());
}
@Test
void deletedVpcDefaultResourcesRemainAbsentAfterRestart(@TempDir Path dir) {
Ec2Service first = newService(dir);
@@ -1,31 +1,28 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static io.github.hectorvent.floci.testing.Ec2Requests.ec2;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
class Ec2SubnetVpcRangeIntegrationTest {
private static RequestSpecification ec2(String action) {
return given().header("Authorization", "AWS4-HMAC-SHA256 Credential=855555555555/20261011/us-east-1/ec2/aws4_request")
.formParam("Action", action);
}
private static final String ACCOUNT_ID = "855555555555";
private static final String REGION = "us-east-1";
@Test
void subnetRangeMustFitPrimaryOrAssociatedSecondaryVpcBlock() {
String vpcId = ec2("CreateVpc").formParam("CidrBlock", "10.0.0.0/16").post("/").then()
String vpcId = ec2("CreateVpc", ACCOUNT_ID, REGION).formParam("CidrBlock", "10.0.0.0/16").post("/").then()
.statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.1.0/24")
ec2("CreateSubnet", ACCOUNT_ID, REGION).formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.1.0/24")
.post("/").then().statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidSubnet.Range"));
ec2("AssociateVpcCidrBlock").formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.0.0/16")
ec2("AssociateVpcCidrBlock", ACCOUNT_ID, REGION).formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.0.0/16")
.post("/").then().statusCode(200);
String subnetId = ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.1.9/24")
String subnetId = ec2("CreateSubnet", ACCOUNT_ID, REGION).formParam("VpcId", vpcId).formParam("CidrBlock", "10.1.1.9/24")
.post("/").then().statusCode(200).body("CreateSubnetResponse.subnet.cidrBlock", equalTo("10.1.1.0/24"))
.extract().path("CreateSubnetResponse.subnet.subnetId");
ec2("DeleteSubnet").formParam("SubnetId", subnetId).post("/").then().statusCode(200);
ec2("DeleteVpc").formParam("VpcId", vpcId).post("/").then().statusCode(200);
ec2("DeleteSubnet", ACCOUNT_ID, REGION).formParam("SubnetId", subnetId).post("/").then().statusCode(200);
ec2("DeleteVpc", ACCOUNT_ID, REGION).formParam("VpcId", vpcId).post("/").then().statusCode(200);
}
}
@@ -0,0 +1,15 @@
package io.github.hectorvent.floci.testing;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.given;
public final class Ec2Requests {
private Ec2Requests() {}
public static RequestSpecification ec2(String action, String accountId, String region) {
return given().header("Authorization",
"AWS4-HMAC-SHA256 Credential=" + accountId + "/20261016/" + region + "/ec2/aws4_request")
.formParam("Action", action);
}
}