mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 18:48:00 -05:00
fix(elbv2): answer a single-subnet ALB with ValidationError (#6098)
* test(elbv2): expect ValidationError for a single-subnet ALB AWS answers a CreateLoadBalancer for an Application Load Balancer with one subnet with ValidationError (400) "At least two subnets in two different Availability Zones must be specified", the answer it gives one with none, and it counts the subnets before it looks them up. Floci answers InvalidConfigurationRequest with a message of its own. - ElbV2ServiceTest, ElbV2IntegrationTest (Query protocol), CloudFormationElbV2SubnetMappingsIntegrationTest and the compatibility suite's ElbV2Test expect that error, and no balancer, for one subnet in Subnets or SubnetMappings, with or without Type, and for one subnet that does not exist. A Network or Gateway Load Balancer on one subnet is still created. - The tests that sent an Application Load Balancer one missing subnet to get SubnetNotFound now send it an existing subnet as well, or ask for a Network Load Balancer, so they still reach the subnet lookup. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(elbv2): answer a single-subnet ALB with ValidationError CreateLoadBalancer for an Application Load Balancer with one subnet now answers ValidationError (400) "At least two subnets in two different Availability Zones must be specified", as AWS does, instead of InvalidConfigurationRequest with a message of Floci's own. requireSubnets, which already refused a balancer with no subnets this way, now refuses an Application Load Balancer with fewer than two. It runs before any subnet is looked up, so one subnet that does not exist gets the same answer, as on AWS. One SubnetMappings entry goes through the same check, following the rule the API reference states for both fields. CloudFormation creates and replacements go through it too. A Network or Gateway Load Balancer on one subnet is still created. SetSubnets with one subnet and two subnets in one Availability Zone keep their InvalidConfigurationRequest answers. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): cover a one-subnet ALB replacement Review follow-ups: - CloudFormationElbV2ReplacementIntegrationTest renames an Application Load Balancer onto one subnet: the update rolls back with CreateLoadBalancer's ValidationError, no balancer gets the new name, and the one it was to replace keeps both subnets. - ElbV2CfnProvisioner's Javadoc no longer says a blank SubnetId is refused as an unknown subnet, since an Application Load Balancer with only that mapping now gets the subnet count error first. - ElbV2ServiceTest says why its one-subnet test stubs a lookup that it then expects never to happen. - Ec2DescribeSubnetsNotFoundTest says the missing subnet is sent alongside an existing one, rather than that its position matters. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): give the one-subnet NLB and GWLB test its own order Review follow-up: the test createLoadBalancerOnOneSubnetIsAllowedForNetworkAndGatewayLoadBalancers shared @Order(12) with createLoadBalancerWithOneSubnetIsAValidationError, so JUnit ran the two in no defined order. It now runs at @Order(78), after the other load balancer subnet tests. Neither test depends on the other: each uses its own load balancer names, and the network and gateway one deletes the balancers it creates. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): move the one-subnet NLB and GWLB test to order 79 The draft #6100 adds setSubnetsWithNeitherSubnetsNorSubnetMappingsIsRefused at @Order(78) in the same class, and the two branches merge without a conflict, so whichever lands second would bring back a shared order. 79 is unused on main and on both branches; no other order changes. 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:
1 parent
73e890dbc7
commit
61a42591a9
9 files changed
+219
-24
No files matched your search
+44
@@ -141,6 +141,50 @@ class CloudFormationElbV2ReplacementIntegrationTest {
|
||||
deleteStack(stackName);
|
||||
}
|
||||
|
||||
/**
|
||||
* A new name replaces the load balancer, and the replacement is created the way CreateLoadBalancer
|
||||
* creates one: an Application Load Balancer renamed onto one subnet fails the update with that
|
||||
* call's error, no balancer has the new name, and the one it was to replace keeps both subnets.
|
||||
*/
|
||||
@Test
|
||||
void aReplacementOnOneSubnetFailsTheUpdateAndKeepsTheBalancer() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "elb-replace-one-" + suffix;
|
||||
String subnetA = Ec2Service.defaultSubnetId(REGION, "a");
|
||||
String subnetB = Ec2Service.defaultSubnetId(REGION, "b");
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Alb": {
|
||||
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
|
||||
"Properties": {"Name": "%s", "Type": "application", "Subnets": [%s]}
|
||||
}
|
||||
},
|
||||
"Outputs": {"AlbRef": {"Value": {"Ref": "Alb"}}}
|
||||
}
|
||||
""";
|
||||
createStack(stackName, template.formatted("alb-kept-" + suffix, "\"" + subnetA + "\", \"" + subnetB + "\""));
|
||||
String kept = output(describeStack(stackName), "AlbRef");
|
||||
|
||||
updateStack(stackName, template.formatted("alb-renamed-" + suffix, "\"" + subnetA + "\""));
|
||||
|
||||
CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_COMPLETE");
|
||||
assertEquals(kept, output(describeStack(stackName), "AlbRef"));
|
||||
assertEquals(true, describeEvents(stackName)
|
||||
.contains("At least two subnets in two different Availability Zones must be specified"),
|
||||
"the replacement fails with CreateLoadBalancer's error: " + describeEvents(stackName));
|
||||
given().formParam("Action", "DescribeLoadBalancers").formParam("Version", "2015-12-01")
|
||||
.formParam("LoadBalancerArns.member.1", kept)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<SubnetId>" + subnetA + "</SubnetId>"))
|
||||
.body(containsString("<SubnetId>" + subnetB + "</SubnetId>"));
|
||||
given().formParam("Action", "DescribeLoadBalancers").formParam("Version", "2015-12-01")
|
||||
.formParam("Names.member.1", "alb-renamed-" + suffix)
|
||||
.when().post("/").then().statusCode(400).body(containsString("LoadBalancerNotFound"));
|
||||
|
||||
deleteStack(stackName);
|
||||
}
|
||||
|
||||
/** The same two balancers with a listener that needs no target group. */
|
||||
private static String fixedResponseTemplate(String suffix) {
|
||||
return """
|
||||
|
||||
+36
-1
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -23,7 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
* subnet fails its stack, as on AWS, rather than the balancer being created with no subnets, while a
|
||||
* mapping an {@code Fn::If} drops with {@code AWS::NoValue} is left out. {@code Subnets} and
|
||||
* {@code SubnetMappings} together fail it too, as ELBv2 takes one or the other, and so does a
|
||||
* balancer with neither.
|
||||
* balancer with neither, or an Application Load Balancer on one subnet.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationElbV2SubnetMappingsIntegrationTest {
|
||||
@@ -124,6 +125,40 @@ class CloudFormationElbV2SubnetMappingsIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An Application Load Balancer on one subnet, in {@code Subnets} or in {@code SubnetMappings},
|
||||
* fails the stack with CreateLoadBalancer's error and leaves no balancer.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"Subnets", "SubnetMappings"})
|
||||
void anApplicationLoadBalancerOnOneSubnetFailsTheStack(String property) {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stack = "alb-one-subnet-" + suffix;
|
||||
String name = "alb-one-" + suffix;
|
||||
String template = template(name, "{\"SubnetId\": {\"Ref\": \"SubnetA\"}}")
|
||||
.replace("\"Type\": \"network\"", "\"Type\": \"application\"");
|
||||
if ("Subnets".equals(property)) {
|
||||
template = template.replace("\"SubnetMappings\": [{\"SubnetId\": {\"Ref\": \"SubnetA\"}}]",
|
||||
"\"Subnets\": [{\"Ref\": \"SubnetA\"}]");
|
||||
}
|
||||
assertTrue(template.contains("\"" + property + "\": [{"), template);
|
||||
assertTrue(template.contains("\"Type\": \"application\""), template);
|
||||
try {
|
||||
request(CFN_AUTH, "CreateStack", "StackName", stack, "TemplateBody", template).statusCode(200);
|
||||
CfnStackWaits.awaitStatus(stack, "ROLLBACK_COMPLETE");
|
||||
String events = request(CFN_AUTH, "DescribeStackEvents", "StackName", stack).statusCode(200)
|
||||
.extract().asString();
|
||||
List<String> reasons = XmlParser.extractAll(events, "ResourceStatusReason");
|
||||
assertTrue(reasons.stream().anyMatch(
|
||||
r -> r.contains("At least two subnets in two different Availability Zones must be specified")),
|
||||
String.join(" | ", reasons));
|
||||
request(ELB_AUTH, "DescribeLoadBalancers", "Names.member.1", name).statusCode(400)
|
||||
.body(containsString("LoadBalancerNotFound"));
|
||||
} finally {
|
||||
request(CFN_AUTH, "DeleteStack", "StackName", stack);
|
||||
}
|
||||
}
|
||||
|
||||
/** A second mapping an {@code Fn::If} drops leaves the balancer on the first mapping's subnet. */
|
||||
@Test
|
||||
void aMappingAnFnIfDropsIsLeftOut() {
|
||||
|
||||
@@ -447,20 +447,77 @@ class ElbV2IntegrationTest {
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
|
||||
}
|
||||
|
||||
@Test
|
||||
/**
|
||||
* An Application Load Balancer takes subnets in at least two Availability Zones, so one subnet, in
|
||||
* Subnets or in SubnetMappings, is the ValidationError a balancer with none gets, and nothing is
|
||||
* created. AWS counts the subnets before it looks them up, so one that does not exist gets it too.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', nullValues = "NONE", value = {
|
||||
"single-subnet-lb|application|Subnets.member.1|a",
|
||||
"single-subnet-default-lb|NONE|Subnets.member.1|a",
|
||||
"single-mapping-lb|application|SubnetMappings.member.1.SubnetId|a",
|
||||
"single-missing-subnet-lb|application|Subnets.member.1|NONE"
|
||||
})
|
||||
@Order(12)
|
||||
void createLoadBalancerWithSingleSubnetThrowsInvalidConfigurationRequest() {
|
||||
given()
|
||||
void createLoadBalancerWithOneSubnetIsAValidationError(String name, String type, String parameter, String zone) {
|
||||
String subnet = zone == null ? "subnet-does-not-exist" : Ec2Service.defaultSubnetId("us-east-1", zone);
|
||||
RequestSpecification request = given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "single-subnet-lb")
|
||||
.formParam("Type", "application")
|
||||
.formParam("Subnets.member.1", Ec2Service.defaultSubnetId("us-east-1", "a"))
|
||||
.formParam("Name", name)
|
||||
.formParam(parameter, subnet)
|
||||
.header("Authorization", AUTH);
|
||||
if (type != null) {
|
||||
request.formParam("Type", type);
|
||||
}
|
||||
request
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message",
|
||||
equalTo("At least two subnets in two different Availability Zones must be specified"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeLoadBalancers")
|
||||
.formParam("Names.member.1", name)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
|
||||
.body("ErrorResponse.Error.Code", equalTo("LoadBalancerNotFound"));
|
||||
}
|
||||
|
||||
/** A Network or Gateway Load Balancer can be in a single Availability Zone. */
|
||||
@ParameterizedTest
|
||||
@CsvSource({"network", "gateway"})
|
||||
@Order(79)
|
||||
void createLoadBalancerOnOneSubnetIsAllowedForNetworkAndGatewayLoadBalancers(String type) {
|
||||
String arn = given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "single-subnet-" + type)
|
||||
.formParam("Type", type)
|
||||
.formParam("Subnets.member.1", Ec2Service.defaultSubnetId("us-east-1", "a"))
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
|
||||
equalTo(Ec2Service.defaultSubnetId("us-east-1", "a")))
|
||||
.extract()
|
||||
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
|
||||
|
||||
given()
|
||||
.formParam("Action", "DeleteLoadBalancer")
|
||||
.formParam("LoadBalancerArn", arn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -629,7 +686,8 @@ class ElbV2IntegrationTest {
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "missing-subnet-lb")
|
||||
.formParam("Type", "application")
|
||||
.formParam("Subnets.member.1", "subnet-does-not-exist")
|
||||
.formParam("Subnets.member.1", Ec2Service.defaultSubnetId("us-east-1", "a"))
|
||||
.formParam("Subnets.member.2", "subnet-does-not-exist")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
@@ -1278,7 +1336,7 @@ class ElbV2IntegrationTest {
|
||||
given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "lb-missing-subnet")
|
||||
.formParam("Type", "application")
|
||||
.formParam("Type", "network")
|
||||
.formParam("Subnets.member.1", "subnet-does-not-exist")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
|
||||
@@ -125,6 +125,40 @@ class ElbV2ServiceTest {
|
||||
verify(ec2Service, never()).describeSubnets(anyString(), anyList(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* An Application Load Balancer takes subnets in at least two Availability Zones, so one subnet gets
|
||||
* the refusal a balancer with none gets. AWS counts before it looks the subnet up, so the subnet is
|
||||
* not looked up here either, and nothing is stored.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource(nullValues = "NONE", value = {"NONE", "application"})
|
||||
void anApplicationLoadBalancerOnOneSubnetIsRefusedBeforeTheSubnetIsLookedUp(String type) {
|
||||
// Stubbed so that a lookup before the count would reach the zone check, not a SubnetNotFound from the mock.
|
||||
lenient().when(ec2Service.describeSubnets(REGION, List.of("subnet-a"), Map.of()))
|
||||
.thenReturn(List.of(subnet("subnet-a", REGION + "a")));
|
||||
AwsException refusal = assertThrows(AwsException.class, () -> service.createLoadBalancer(REGION,
|
||||
"one-subnet", null, type, null, List.of("subnet-a"), List.of(), Map.of()));
|
||||
assertEquals("ValidationError", refusal.getErrorCode());
|
||||
assertEquals("At least two subnets in two different Availability Zones must be specified",
|
||||
refusal.getMessage());
|
||||
assertEquals(400, refusal.getHttpStatus());
|
||||
assertTrue(service.describeLoadBalancers(REGION, null, null, null, null).isEmpty());
|
||||
verify(ec2Service, never()).describeSubnets(anyString(), anyList(), any());
|
||||
}
|
||||
|
||||
/** A Network or Gateway Load Balancer can be in a single Availability Zone. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"network", "gateway"})
|
||||
void aNetworkOrGatewayLoadBalancerCanBeOnOneSubnet(String type) {
|
||||
when(ec2Service.describeSubnets(REGION, List.of("subnet-a"), Map.of()))
|
||||
.thenReturn(List.of(subnet("subnet-a", REGION + "a")));
|
||||
LoadBalancer created = service.createLoadBalancer(REGION, "one-subnet", null, type, null,
|
||||
List.of("subnet-a"), List.of(), Map.of());
|
||||
assertEquals("vpc-a", created.getVpcId());
|
||||
assertEquals(List.of("subnet-a"),
|
||||
created.getAvailabilityZones().stream().map(AvailabilityZone::getSubnetId).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* The API handler and the CloudFormation provisioner both take a request's subnets from here: one
|
||||
* side as given, an empty list for neither, and a refusal for both.
|
||||
|
||||
Reference in new issue
Block a user