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

@@ -124,11 +124,17 @@ public class ElbV2QueryHandler {
String type = p.getFirst("Type");
String ipAddressType = p.getFirst("IpAddressType");
List<String> subnets = memberList(p, "Subnets");
List<String> subnetMappings = subnetMappingSubnetIds(p);
if (!subnets.isEmpty() && !subnetMappings.isEmpty()) {
throw new AwsException("ValidationError",
"You can specify either subnets or subnet mappings, not both", 400);
}
List<String> effectiveSubnets = subnetMappings.isEmpty() ? subnets : subnetMappings;
List<String> securityGroups = memberList(p, "SecurityGroups");
Map<String, String> 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<String, String> p, String region) {
String arn = p.getFirst("LoadBalancerArn");
List<String> subnets = memberList(p, "Subnets");
service.setSubnets(region, arn, subnets);
List<String> 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<String> memberList(MultivaluedMap<String, String> p, String prefix) {
return memberList(p, prefix, "");
}
private List<String> memberList(MultivaluedMap<String, String> p, String prefix, String suffix) {
List<String> 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<String> subnetMappingSubnetIds(MultivaluedMap<String, String> p) {
List<String> 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<String, String> parseTags(MultivaluedMap<String, String> p) {
Map<String, String> result = new LinkedHashMap<>();
int i = 1;
@@ -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