mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): report an unknown subnet id from DescribeSubnets as an error (#4408)
* fix(ec2): report an unknown subnet id from DescribeSubnets as an error A requested SubnetId that does not exist was dropped from the result instead of raising. An empty list tells the 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. describeVpcs already reads its own requested ids this way, so this is the same shape applied to subnets. A describe naming no ids still lists the region. * fix(ec2): raise the unknown subnet id from the query handler, not the service describeSubnets 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 owe their callers. Raising InvalidSubnetID.NotFound in the service replaced them. Move the per-id check to handleDescribeSubnets so only the EC2 query surface changes, and reuse the existing requireSubnet helper. Assertions now read the XML elements rather than the response text. * test(ec2): validate the unknown subnet id with SDK clients AGENTS.md asks for management APIs to be exercised through an SDK client rather than handcrafted requests alone. Ec2Client.describeSubnets raises InvalidSubnetID.NotFound for a missing id, on its own and mixed with a good one, and resolves the known id. The ELBv2 case pins the regression the query handler placement avoids. With the check in describeSubnets, CreateLoadBalancer answers a missing subnet with InvalidSubnetID.NotFound rather than the SubnetNotFound that API owes its callers, and this test fails. * test(ec2): delete the VPC even when subnet creation fails Creating the subnet outside the try meant a failure there left the VPC behind, which accumulates against persistent storage.
This commit is contained in:
1 parent
b0ad799c5c
commit
49f253d0ec
3 files changed
+168
No files matched your search
+67
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2911,6 +2911,18 @@ public class Ec2QueryHandler {
|
||||
private Response handleDescribeSubnets(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> ids = getList(p, "SubnetId");
|
||||
Map<String, List<String>> 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<Subnet> subnets = service.describeSubnets(region, ids, filters);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeSubnetsResponse", AwsNamespaces.EC2)
|
||||
|
||||
+89
@@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user