fix(elbv2): refuse SetSubnets with neither Subnets nor SubnetMappings (#6100)

* test(elbv2): cover SetSubnets with neither Subnets nor SubnetMappings

SetSubnets takes Subnets or SubnetMappings, and AWS refuses a request
with neither. These tests pin that answer, which Floci does not give
yet: a ValidationError with the CreateLoadBalancer message for the
balancer's type, with the balancer left on its subnets and in its VPC,
whether the two lists are left out or sent empty. They fail until the
fix that follows.

They also pin the paths that must keep working: a balancer that does
not exist is reported first, a CloudFormation update that names
neither leaves its kept balancer alone, and a rollback can still move
a balancer an earlier version saved without subnets back onto none.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(elbv2): refuse SetSubnets with neither Subnets nor SubnetMappings

A SetSubnets that named neither Subnets nor SubnetMappings answered
success and left the load balancer in no Availability Zone, still in
its VPC. The API reference says the request takes one or the other,
and AWS answers a Network Load Balancer's with "ValidationError: At
least one subnet must be specified", the message CreateLoadBalancer
gives it.

SetSubnets with neither now answers ValidationError and leaves the
balancer on its subnets, with the message CreateLoadBalancer gives the
balancer's type: "At least two subnets in two different Availability
Zones must be specified" for an Application Load Balancer, "At least
one subnet must be specified" for a Network or Gateway Load Balancer.
A balancer that does not exist is still LoadBalancerNotFound, since
the message depends on its type, and a request that names subnets is
checked as before.

The check runs in the new ElbV2Service.setRequestedSubnets, which the
Query handler calls. ElbV2Service.setSubnets still takes an empty
list: CloudFormation moves a kept balancer through it, never onto none
on an update, and onto none on a rollback only for a balancer an
earlier version saved without subnets, which rolls back as before.

Follow-up to #6087.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* refactor(elbv2): keep the SetSubnets action check in the package

Review follow-ups, no change in behaviour:
- The action's entry point is now the package-private
  ElbV2Service.setSubnetsFromRequest, which only the Query handler
  calls; setRequestedSubnets read like a property setter. The
  CloudFormation provisioner, in another package, cannot reach it and
  keeps moving balancers through setSubnets.
- Its Javadoc says why only an empty list is refused there: a request
  that names subnets is checked by setSubnets. The setSubnets and test
  Javadocs speak of a balancer an earlier Floci version saved without
  subnets.
- The provisioner test of an update that names neither says the real
  reason the update must not call setSubnets: it would move the
  balancer onto none.
- The docs line for SetSubnets gives both messages.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(elbv2): give the SetSubnets neither test the class's last order

#6098 adds a test at @Order(79) earlier in ElbV2IntegrationTest, while
this one stays the last method in the file at @Order(78). Once both
land, whoever appends a test would read 78 and pick 79, so this one
takes 80 and the last method keeps the highest order. The two tests
are independent, so the order between them does not matter.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(elbv2): give the SetSubnets compat test its own order

setSubnetsWithoutSubnets and createLoadBalancerWithoutSubnets in the
compatibility suite's ElbV2Test both carried @Order(6), so JUnit's
order between them was left to its tie-break. The SetSubnets test now
has @Order(8), the first value not taken once #6098's @Order(7) test
is in the class, so every order in the class is unique. The test stays
where it is in the file: moving it below the create test would edit
the lines #6098 added on main. The two tests create and delete their
own balancers, so neither depends on running before the other.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-21 00:07:32 -04:00
1 parent 0dab646373
commit 883cd4f1c2
7 files changed
+224 -2

No files matched your search

@@ -495,6 +495,43 @@ class ElbV2CfnProvisionerTest {
assertEquals(Set.of("subnet-1", "subnet-2"), usedSubnets(moved));
}
/**
* An update whose template names neither {@code Subnets} nor {@code SubnetMappings} leaves the kept
* load balancer on its subnets: it does not call ElbV2Service.setSubnets, which would move the
* balancer onto none.
*/
@Test
void anUpdateThatNamesNoSubnetsLeavesTheKeptLoadBalancerOnItsSubnets() {
when(elb.createLoadBalancer(eq(REGION), eq("web"), isNull(), isNull(), isNull(), anyList(), anyList(), any()))
.thenThrow(new AwsException("DuplicateLoadBalancerName", "exists", 400));
when(elb.describeLoadBalancers(REGION, null, List.of("web"), null, null))
.thenReturn(List.of(loadBalancerOn("web", "subnet-1", "subnet-2")));
StackResource lb = resource("AWS::ElasticLoadBalancingV2::LoadBalancer", "Alb");
lb.getAttributes().put("LoadBalancerName", "web");
provisioner.provision(lb, mapper.createObjectNode().put("Name", "web"), ctx(LB_ARN));
assertEquals(Set.of("subnet-1", "subnet-2"), usedSubnets(lb));
assertTrue(provisioner.rollbackUpdate(lb));
verify(elb, never()).setSubnets(anyString(), anyString(), anyList());
}
/**
* A load balancer an earlier Floci version saved without subnets is moved back onto none when an
* update that gave it subnets rolls back: the move back goes through ElbV2Service.setSubnets, which
* takes an empty list, where the SetSubnets action refuses one.
*/
@Test
void aRollbackMovesALoadBalancerSavedWithoutSubnetsBackOntoNone() {
StackResource lb = keptLoadBalancerUpdatedTo(loadBalancerOn("web"), "subnet-1", "subnet-2");
verify(elb).setSubnets(REGION, LB_ARN, List.of("subnet-1", "subnet-2"));
assertTrue(provisioner.rollbackUpdate(lb));
verify(elb).setSubnets(REGION, LB_ARN, List.of());
assertEquals(Set.of(), usedSubnets(lb));
}
@Test
void theSameSubnetsInAnotherOrderLeaveTheKeptLoadBalancerAlone() {
StackResource lb = keptLoadBalancerUpdatedTo(loadBalancerOn("web", "subnet-1", "subnet-2"),
@@ -1459,4 +1459,75 @@ class ElbV2IntegrationTest {
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
contains(Ec2Service.defaultSubnetId("us-east-1", "a"), Ec2Service.defaultSubnetId("us-east-1", "b")));
}
/**
* SetSubnets with neither Subnets nor SubnetMappings, left out or sent as empty lists, is a
* ValidationError, and the balancer stays on its subnets and in its VPC. A Network Load Balancer
* gets the message AWS gives it; an Application Load Balancer gets the one CreateLoadBalancer gives
* it, and a Gateway Load Balancer the Network one.
*/
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"application|false|At least two subnets in two different Availability Zones must be specified",
"application|true|At least two subnets in two different Availability Zones must be specified",
"network|false|At least one subnet must be specified",
"network|true|At least one subnet must be specified",
"gateway|false|At least one subnet must be specified",
"gateway|true|At least one subnet must be specified"
})
@Order(80)
void setSubnetsWithNeitherSubnetsNorSubnetMappingsIsRefused(String type, boolean emptyLists, String message) {
String subnetA = Ec2Service.defaultSubnetId("us-east-1", "a");
String subnetB = Ec2Service.defaultSubnetId("us-east-1", "b");
String arn = given()
.formParam("Action", "CreateLoadBalancer")
.formParam("Name", "set-none-" + type + (emptyLists ? "-empty" : ""))
.formParam("Type", type)
.formParam("Subnets.member.1", subnetA)
.formParam("Subnets.member.2", subnetB)
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
RequestSpecification setSubnets = given()
.formParam("Action", "SetSubnets")
.formParam("LoadBalancerArn", arn)
.header("Authorization", AUTH);
if (emptyLists) {
setSubnets.formParam("Subnets", "").formParam("SubnetMappings", "");
}
setSubnets
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(message));
given()
.formParam("Action", "DescribeLoadBalancers")
.formParam("LoadBalancerArns.member.1", arn)
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
contains(subnetA, subnetB))
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.VpcId",
equalTo(Ec2Service.defaultVpcId("us-east-1")));
given()
.formParam("Action", "DeleteLoadBalancer")
.formParam("LoadBalancerArn", arn)
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(200);
}
}
@@ -21,6 +21,8 @@ import io.github.hectorvent.floci.services.elbv2.model.RuleCondition;
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -175,6 +177,73 @@ class ElbV2ServiceTest {
assertEquals(400, both.getHttpStatus());
}
/**
* SetSubnets with neither Subnets nor SubnetMappings, left out or sent as empty lists, is refused
* before any subnet is looked up, and the balancer keeps its zones and its VPC. A Network Load
* Balancer gets the message AWS gives it; an Application Load Balancer gets the one
* CreateLoadBalancer gives it, and a Gateway Load Balancer the Network one.
*/
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"application|At least two subnets in two different Availability Zones must be specified",
"network|At least one subnet must be specified",
"gateway|At least one subnet must be specified"
})
void setSubnetsWithNeitherSubnetsNorSubnetMappingsIsRefused(String type, String message) {
LoadBalancer lb = service.createLoadBalancer(REGION, "set-none-" + type, "internal", type, "ipv4",
ALB_SUBNETS, List.of(), Map.of());
ElbV2QueryHandler handler = new ElbV2QueryHandler(service);
clearInvocations(ec2Service);
for (boolean emptyLists : new boolean[] {false, true}) {
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("LoadBalancerArn", lb.getLoadBalancerArn());
if (emptyLists) {
params.add("Subnets", "");
params.add("SubnetMappings", "");
}
Response response = handler.handle("SetSubnets", params, REGION);
String xml = (String) response.getEntity();
assertEquals(400, response.getStatus(), xml);
assertTrue(xml.contains("<Code>ValidationError</Code>"), xml);
assertTrue(xml.contains("<Message>" + message + "</Message>"), xml);
}
LoadBalancer kept = service.describeLoadBalancers(REGION, List.of(lb.getLoadBalancerArn()), null, null,
null).getFirst();
assertEquals(ALB_SUBNETS, kept.getAvailabilityZones().stream().map(AvailabilityZone::getSubnetId).toList());
assertEquals("vpc-a", kept.getVpcId());
verify(ec2Service, never()).describeSubnets(anyString(), anyList(), any());
}
/** The message depends on the balancer's type, so a balancer that does not exist is reported first. */
@Test
void setSubnetsForAMissingBalancerReportsItBeforeMissingSubnets() {
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("LoadBalancerArn",
"arn:aws:elasticloadbalancing:" + REGION + ":000000000000:loadbalancer/net/missing/0123456789abcdef");
Response response = new ElbV2QueryHandler(service).handle("SetSubnets", params, REGION);
String xml = (String) response.getEntity();
assertEquals(400, response.getStatus(), xml);
assertTrue(xml.contains("<Code>LoadBalancerNotFound</Code>"), xml);
}
/**
* Only the SetSubnets action refuses an empty list. A CloudFormation rollback moves a kept
* balancer back onto the subnets it had before the update, and for a balancer an earlier Floci
* version saved without subnets that is none.
*/
@Test
void aMoveBackOntoNoSubnetsIsStillTaken() {
LoadBalancer lb = service.createLoadBalancer(REGION, "moved-back", "internal", "application", "ipv4",
ALB_SUBNETS, List.of(), Map.of());
service.setSubnets(REGION, lb.getLoadBalancerArn(), List.of());
assertTrue(service.describeLoadBalancers(REGION, List.of(lb.getLoadBalancerArn()), null, null, null)
.getFirst().getAvailabilityZones().isEmpty());
}
/**
* A taken name is reported before missing subnets: a CloudFormation stack update finds the
* balancer it keeps through DuplicateLoadBalancerName, whatever subnets its template names.