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 <matheus.6148@gmail.com>

* 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 <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-12 21:32:08 -04:00
1 parent d1fe27cb92
commit 999ccde676
2 files changed
+131 -4

No files matched your search

@@ -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