From 999ccde67603344e001c8ad21110dc7666809897 Mon Sep 17 00:00:00 2001 From: Matheus Oliveira <99829531+omatheusmesmo@users.noreply.github.com> Date: Mon, 12 Oct 2026 22:32:08 -0300 Subject: [PATCH] fix(elbv2): honor SubnetMappings in CreateLoadBalancer and SetSubnets (#5533) * fix(elbv2): honor SubnetMappings in CreateLoadBalancer and SetSubnets CreateLoadBalancer and SetSubnets only read Subnets.member.N, so a network load balancer created with SubnetMappings (the form the AWS Load Balancer Controller always sends) had no availability zones and an empty VpcId. The mappings' SubnetId values are now used as the subnet list, and passing both Subnets and SubnetMappings is rejected with the AWS ValidationError message. Signed-off-by: Matheus Oliveira * fix(elbv2): require every subnet mapping to carry a SubnetId A mapping member with only AllocationId was silently dropped, so Subnets plus an allocation-only mapping slipped past the mutual-exclusion check and a later mapping member with a SubnetId was ignored. Detect mapping presence from the request keys and reject a member without SubnetId with the AWS ValidationError text. Signed-off-by: Matheus Oliveira --------- Signed-off-by: Matheus Oliveira --- .../services/elbv2/ElbV2QueryHandler.java | 50 ++++++++++- .../services/elbv2/ElbV2IntegrationTest.java | 85 +++++++++++++++++++ 2 files changed, 131 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2QueryHandler.java index 541c7d844b..f44a7a117a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2QueryHandler.java @@ -124,11 +124,17 @@ public class ElbV2QueryHandler { String type = p.getFirst("Type"); String ipAddressType = p.getFirst("IpAddressType"); List subnets = memberList(p, "Subnets"); + List subnetMappings = subnetMappingSubnetIds(p); + if (!subnets.isEmpty() && !subnetMappings.isEmpty()) { + throw new AwsException("ValidationError", + "You can specify either subnets or subnet mappings, not both", 400); + } + List effectiveSubnets = subnetMappings.isEmpty() ? subnets : subnetMappings; List securityGroups = memberList(p, "SecurityGroups"); Map tags = parseTags(p); LoadBalancer lb = service.createLoadBalancer(region, name, scheme, type, ipAddressType, - subnets, securityGroups, tags); + effectiveSubnets, securityGroups, tags); // return provisioning state in create response only LoadBalancer provisioning = shallowCopy(lb); @@ -256,7 +262,12 @@ public class ElbV2QueryHandler { private Response handleSetSubnets(MultivaluedMap p, String region) { String arn = p.getFirst("LoadBalancerArn"); List subnets = memberList(p, "Subnets"); - service.setSubnets(region, arn, subnets); + List subnetMappings = subnetMappingSubnetIds(p); + if (!subnets.isEmpty() && !subnetMappings.isEmpty()) { + throw new AwsException("ValidationError", + "You can specify either subnets or subnet mappings, not both", 400); + } + service.setSubnets(region, arn, subnetMappings.isEmpty() ? subnets : subnetMappings); return voidResponse("SetSubnetsResponse"); } @@ -1064,17 +1075,48 @@ public class ElbV2QueryHandler { // ── Parsing helpers ─────────────────────────────────────────────────────── private List memberList(MultivaluedMap p, String prefix) { + return memberList(p, prefix, ""); + } + + private List memberList(MultivaluedMap p, String prefix, String suffix) { List result = new ArrayList<>(); int i = 1; while (true) { - String val = p.getFirst(prefix + ".member." + i); - if (val == null) break; + String val = p.getFirst(prefix + ".member." + i + suffix); + if (val == null) { + break; + } result.add(val); i++; } return result; } + private List subnetMappingSubnetIds(MultivaluedMap p) { + List result = new ArrayList<>(); + int i = 1; + while (true) { + boolean memberPresent = false; + for (String key : p.keySet()) { + if (key.startsWith("SubnetMappings.member." + i + ".")) { + memberPresent = true; + break; + } + } + if (!memberPresent) { + break; + } + String subnetId = p.getFirst("SubnetMappings.member." + i + ".SubnetId"); + if (subnetId == null) { + throw new AwsException("ValidationError", + "You must specify a subnet ID for each subnet mapping.", 400); + } + result.add(subnetId); + i++; + } + return result; + } + private Map parseTags(MultivaluedMap p) { Map result = new LinkedHashMap<>(); int i = 1; diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java index 2d569d9940..463d0b9572 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java @@ -260,6 +260,91 @@ class ElbV2IntegrationTest { hasItems(Ec2Service.defaultSubnetId("us-east-1", "b"), Ec2Service.defaultSubnetId("us-east-1", "c"))); } + @Test + @Order(17) + void createAndSetSubnetMappingsDriveSubnetsAndVpcId() { + // The AWS Load Balancer Controller sends SubnetMappings rather than Subnets; both actions + // must treat the mappings' SubnetId values as the subnet list (address pinning is not modelled). + String subnetMappedArn = given() + .formParam("Action", "CreateLoadBalancer") + .formParam("Name", "nlb-with-subnet-mappings") + .formParam("Type", "network") + .formParam("SubnetMappings.member.1.SubnetId", Ec2Service.defaultSubnetId("us-east-1", "a")) + .formParam("SubnetMappings.member.2.SubnetId", Ec2Service.defaultSubnetId("us-east-1", "b")) + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(200) + .body("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.VpcId", + equalTo(Ec2Service.defaultVpcId("us-east-1"))) + .body("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.AvailabilityZones.member.size()", + equalTo(2)) + .body("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.AvailabilityZones.member.SubnetId", + hasItems(Ec2Service.defaultSubnetId("us-east-1", "a"), Ec2Service.defaultSubnetId("us-east-1", "b"))) + .extract() + .path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn"); + + given() + .formParam("Action", "SetSubnets") + .formParam("LoadBalancerArn", subnetMappedArn) + .formParam("SubnetMappings.member.1.SubnetId", Ec2Service.defaultSubnetId("us-east-1", "b")) + .formParam("SubnetMappings.member.2.SubnetId", Ec2Service.defaultSubnetId("us-east-1", "c")) + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .formParam("Action", "DescribeLoadBalancers") + .formParam("LoadBalancerArns.member.1", subnetMappedArn) + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(200) + .body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.size()", + equalTo(2)) + .body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId", + hasItems(Ec2Service.defaultSubnetId("us-east-1", "b"), Ec2Service.defaultSubnetId("us-east-1", "c"))); + } + + @Test + @Order(18) + void createLoadBalancerRejectsBothSubnetsAndSubnetMappings() { + given() + .formParam("Action", "CreateLoadBalancer") + .formParam("Name", "both-subnet-forms") + .formParam("Subnets.member.1", Ec2Service.defaultSubnetId("us-east-1", "a")) + .formParam("SubnetMappings.member.1.SubnetId", Ec2Service.defaultSubnetId("us-east-1", "b")) + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")) + .body("ErrorResponse.Error.Message", + equalTo("You can specify either subnets or subnet mappings, not both")); + } + + @Test + @Order(19) + void createLoadBalancerRejectsMappingWithoutSubnetId() { + given() + .formParam("Action", "CreateLoadBalancer") + .formParam("Name", "mapping-without-subnet-id") + .formParam("SubnetMappings.member.1.AllocationId", "eipalloc-12345678") + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")) + .body("ErrorResponse.Error.Message", + equalTo("You must specify a subnet ID for each subnet mapping.")); + } + // ── Target Groups ───────────────────────────────────────────────────────── @Test