diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2DescribeSubnetsNotFoundTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2DescribeSubnetsNotFoundTest.java new file mode 100644 index 0000000000..c47ad313d4 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2DescribeSubnetsNotFoundTest.java @@ -0,0 +1,67 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.ec2.Ec2Client; +import software.amazon.awssdk.services.ec2.model.Ec2Exception; +import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.ElasticLoadBalancingV2Exception; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class Ec2DescribeSubnetsNotFoundTest { + private static final String MISSING = "subnet-0000000000000dead"; + + @Test + @DisplayName("DescribeSubnets raises InvalidSubnetID.NotFound for an id that does not exist") + void missingSubnetIdIsAnSdkError() { + try (Ec2Client ec2 = TestFixtures.ec2Client()) { + Ec2Exception error = assertThrows(Ec2Exception.class, + () -> ec2.describeSubnets(r -> r.subnetIds(MISSING))); + assertThat(error.statusCode()).isEqualTo(400); + assertThat(error.awsErrorDetails().errorCode()).isEqualTo("InvalidSubnetID.NotFound"); + + String vpcId = ec2.createVpc(r -> r.cidrBlock("10.75.0.0/16")).vpc().vpcId(); + String subnetId = null; + try { + subnetId = ec2.createSubnet(r -> r.vpcId(vpcId).cidrBlock("10.75.1.0/24")) + .subnet().subnetId(); + String created = subnetId; + + assertThat(ec2.describeSubnets(r -> r.subnetIds(created)).subnets()) + .singleElement().satisfies(s -> assertThat(s.vpcId()).isEqualTo(vpcId)); + + Ec2Exception mixed = assertThrows(Ec2Exception.class, + () -> ec2.describeSubnets(r -> r.subnetIds(created, MISSING))); + assertThat(mixed.awsErrorDetails().errorCode()).isEqualTo("InvalidSubnetID.NotFound"); + } finally { + if (subnetId != null) { + String cleanup = subnetId; + ec2.deleteSubnet(r -> r.subnetId(cleanup)); + } + ec2.deleteVpc(r -> r.vpcId(vpcId)); + } + } + } + + /** + * describeSubnets is shared with the services that build on it, and each reports a missing + * subnet in its own words. ELBv2 owes its callers SubnetNotFound, so the EC2 error must not + * reach through. + */ + @Test + @DisplayName("ELBv2 still reports its own SubnetNotFound for a missing subnet") + void elbV2KeepsItsOwnSubnetError() { + try (ElasticLoadBalancingV2Client elb = TestFixtures.elbV2Client()) { + ElasticLoadBalancingV2Exception error = assertThrows( + ElasticLoadBalancingV2Exception.class, + () -> elb.createLoadBalancer(r -> r.name("lb-" + UUID.randomUUID().toString().substring(0, 8)) + .type("application") + .subnets(MISSING))); + assertThat(error.awsErrorDetails().errorCode()).isEqualTo("SubnetNotFound"); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index 2f0a4bb230..eed5bbc983 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -2911,6 +2911,18 @@ public class Ec2QueryHandler { private Response handleDescribeSubnets(MultivaluedMap p, String region) { List ids = getList(p, "SubnetId"); Map> filters = getFilters(p); + // A requested id that does not exist is an error, not an omission from the result. Returning + // an empty list tells a caller the subnet is gone when the id may simply be wrong, and a + // waiter polling for a subnet it just created cannot tell "not yet" from "never". + // + // The check belongs here rather than in describeSubnets because that method is shared with + // ELB, ELBv2, DMS, ElastiCache, RDS and others, each of which reads an absent id as its own + // error. ElbV2Service raises SubnetNotFound and DmsService raises InvalidSubnet, and both + // are the codes those APIs are supposed to return. Raising in the service would replace + // them with this one. + for (String subnetId : ids) { + service.requireSubnet(region, subnetId); + } List subnets = service.describeSubnets(region, ids, filters); XmlBuilder xml = new XmlBuilder() .start("DescribeSubnetsResponse", AwsNamespaces.EC2) diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeSubnetsNotFoundIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeSubnetsNotFoundIntegrationTest.java new file mode 100644 index 0000000000..cbab978656 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeSubnetsNotFoundIntegrationTest.java @@ -0,0 +1,89 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; + +/** + * DescribeSubnets reports a subnet id that does not exist as an error. + * + *

It used to return an empty list, which tells a caller the subnet is gone when the id may + * simply be wrong, and leaves a waiter polling for a subnet it just created unable to tell "not + * yet" from "never". DescribeVpcs already reads its own ids this way. + * + *

Assertions read the XML elements rather than the response text, so a body carrying the right + * words in the wrong shape fails. + */ +@QuarkusTest +class Ec2DescribeSubnetsNotFoundIntegrationTest { + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/ec2/aws4_request"; + + private static ValidatableResponse ec2(String action, String... formParams) { + RequestSpecification request = given().header("Authorization", AUTH) + .formParam("Action", action); + for (int i = 0; i < formParams.length; i += 2) { + request = request.formParam(formParams[i], formParams[i + 1]); + } + return request.when().post("/").then(); + } + + @Test + void anUnknownSubnetIdIsRefused() { + ec2("DescribeSubnets", "SubnetId.1", "subnet-0000000000000dead") + .statusCode(400) + .body("Response.Errors.Error.Code", equalTo("InvalidSubnetID.NotFound")) + .body("Response.Errors.Error.Message", + equalTo("The subnet ID 'subnet-0000000000000dead' does not exist")); + } + + @Test + void aKnownSubnetIdStillReturns() { + String vpcId = ec2("CreateVpc", "CidrBlock", "10.72.0.0/16") + .statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId"); + String subnetId = ec2("CreateSubnet", "VpcId", vpcId, "CidrBlock", "10.72.1.0/24") + .statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId"); + + ec2("DescribeSubnets", "SubnetId.1", subnetId) + .statusCode(200) + .body("DescribeSubnetsResponse.subnetSet.item.subnetId", equalTo(subnetId)) + .body("DescribeSubnetsResponse.subnetSet.item.vpcId", equalTo(vpcId)); + } + + /** One bad id among good ones still fails, the way a per-id lookup has to. */ + @Test + void oneUnknownIdAmongKnownOnesIsRefused() { + String vpcId = ec2("CreateVpc", "CidrBlock", "10.73.0.0/16") + .statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId"); + String subnetId = ec2("CreateSubnet", "VpcId", vpcId, "CidrBlock", "10.73.1.0/24") + .statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId"); + + ec2("DescribeSubnets", "SubnetId.1", subnetId, "SubnetId.2", "subnet-0000000000000beef") + .statusCode(400) + .body("Response.Errors.Error.Code", equalTo("InvalidSubnetID.NotFound")) + .body("Response.Errors.Error.Message", + equalTo("The subnet ID 'subnet-0000000000000beef' does not exist")); + } + + /** A describe with no ids lists the region and raises nothing. */ + @Test + void describingWithNoIdsStillLists() { + String vpcId = ec2("CreateVpc", "CidrBlock", "10.74.0.0/16") + .statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId"); + String subnetId = ec2("CreateSubnet", "VpcId", vpcId, "CidrBlock", "10.74.1.0/24") + .statusCode(200).extract().path("CreateSubnetResponse.subnet.subnetId"); + + ec2("DescribeSubnets") + .statusCode(200) + .body("DescribeSubnetsResponse.subnetSet.item.subnetId", hasItem(subnetId)) + .body("DescribeSubnetsResponse.subnetSet.item.subnetId", + not(hasItem("subnet-0000000000000dead"))); + } +}