From 88408d85748d35418dc166bf5eb4150d8d4b02c1 Mon Sep 17 00:00:00 2001 From: Feyikemi Ogunsanya <54103882+Agboolafeyikemi@users.noreply.github.com> Date: Wed, 5 Aug 2026 03:48:02 +0100 Subject: [PATCH] fix(ec2): return AWS-parity MissingParameter for CreateSubnet without VpcId (#2094) * fix(ec2): return AWS-parity MissingParameter for CreateSubnet without VpcId CreateSubnet passed a null VpcId straight into the VPC lookup, producing "The vpc ID 'null' does not exist" instead of the MissingParameter error real EC2 returns for a required-but-absent parameter (#2089). * test(ec2): cover CreateSubnet missing VpcId at unit level Asserts createSubnet(null vpcId) throws MissingParameter rather than InvalidVpcID.NotFound with a literal "null" in the message. * test(ec2): cover CreateSubnet missing VpcId over the wire Exercises the Query API handler directly (no VpcId form param) to pin the AWS-parity MissingParameter response end-to-end. * test(ec2): cover blank VpcId for CreateSubnet at unit level Greptile flagged that isBlank() also covers empty/whitespace VpcId, a branch the original tests didn't exercise directly. * test(ec2): cover blank VpcId for CreateSubnet over the wire Companion to the unit-level blank VpcId case, exercised through the Query API handler. --- .../floci/services/ec2/Ec2Service.java | 3 ++ .../services/ec2/Ec2IntegrationTest.java | 31 +++++++++++++++++++ .../floci/services/ec2/Ec2ServiceTest.java | 30 ++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index 94ae3468d2..2e80477eb7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -1302,6 +1302,9 @@ public class Ec2Service implements ContainerTeardown { // ─── Subnets ─────────────────────────────────────────────────────────────── public Subnet createSubnet(String region, String vpcId, String cidrBlock, String availabilityZone) { + if (vpcId == null || vpcId.isBlank()) { + throw new AwsException("MissingParameter", "The request must contain the parameter VpcId", 400); + } ensureDefaultResources(region); getRequiredVpc(region, vpcId); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java index ee22c5a21a..dce3eb15c9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java @@ -671,6 +671,37 @@ class Ec2IntegrationTest { .extract().path("CreateSubnetResponse.subnet.subnetId"); } + @Test + @Order(20) + void createSubnetWithoutVpcIdReturnsMissingParameter() { + given() + .formParam("Action", "CreateSubnet") + .formParam("CidrBlock", "10.0.2.0/24") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(400) + .body("Response.Errors.Error.Code", equalTo("MissingParameter")) + .body("Response.Errors.Error.Message", equalTo("The request must contain the parameter VpcId")); + } + + @Test + @Order(20) + void createSubnetWithBlankVpcIdReturnsMissingParameter() { + given() + .formParam("Action", "CreateSubnet") + .formParam("VpcId", " ") + .formParam("CidrBlock", "10.0.2.0/24") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(400) + .body("Response.Errors.Error.Code", equalTo("MissingParameter")) + .body("Response.Errors.Error.Message", equalTo("The request must contain the parameter VpcId")); + } + @Test @Order(21) void describeSubnetById() { diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java index e5bd7be78b..dd708df4cc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java @@ -69,6 +69,36 @@ class Ec2ServiceTest { assertEquals(400, error.getHttpStatus()); } + @Test + void createSubnetRequiresVpcIdInsteadOfNotFound() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), + mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), + new InMemoryStorageFactory()); + + AwsException error = assertThrows(AwsException.class, () -> service.createSubnet( + "us-east-1", null, "10.0.1.0/24", null)); + + assertEquals("MissingParameter", error.getErrorCode()); + assertEquals("The request must contain the parameter VpcId", error.getMessage()); + assertEquals(400, error.getHttpStatus()); + } + + @Test + void createSubnetRejectsBlankVpcIdInsteadOfNotFound() { + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + mock(Ec2PortForwardManager.class), + mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), + new InMemoryStorageFactory()); + + AwsException error = assertThrows(AwsException.class, () -> service.createSubnet( + "us-east-1", " ", "10.0.1.0/24", null)); + + assertEquals("MissingParameter", error.getErrorCode()); + assertEquals("The request must contain the parameter VpcId", error.getMessage()); + assertEquals(400, error.getHttpStatus()); + } + @Test void runInstancesStoresArchitectureFromImageCatalog() { Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),