mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(elbv2): refuse CreateLoadBalancer with neither Subnets nor SubnetMappings (#6087)
* test(elbv2): give the tests' subnetless load balancers subnets CreateLoadBalancer takes Subnets or SubnetMappings for every load balancer type, and AWS refuses a request with neither. Tests that created a balancer with neither, to exercise listeners, the data plane, CodeDeploy, API Gateway or CloudFormation, now give it two default subnets: the src/test classes through Ec2Service.defaultSubnetId, the Java compatibility suite through a new TestFixtures.defaultVpcSubnets. setSubnetsAfterSubnetlessCreateCanAdoptCustomVpc becomes setSubnetsInAnotherVpcIsRefused, since a balancer created with subnets keeps its VPC. The adoption it covered still applies to a balancer an earlier version saved without subnets, and ElbV2ServiceTest now covers that. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(elbv2): refuse CreateLoadBalancer with neither Subnets nor SubnetMappings A CreateLoadBalancer that named neither Subnets nor SubnetMappings made a load balancer with no Availability Zone and no VPC, where the API reference says every type takes one or the other. It now answers ValidationError and creates nothing, with the message AWS gives for the 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 CloudFormation load balancer with neither fails its stack the same way. The check runs after the duplicate-name check, because a stack update finds the balancer it keeps through DuplicateLoadBalancerName whatever subnets its template names. A balancer an earlier version saved without subnets still works, and its first SetSubnets still gives it a VPC. Follow-up to the review of #5881. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * refactor(elbv2): take a request's Subnets or SubnetMappings from ElbV2Service The rule that Subnets and SubnetMappings are alternatives, and its ValidationError, was written out three times: in the Query handler's CreateLoadBalancer and SetSubnets, and in the CloudFormation provisioner. ElbV2Service.requestedSubnets now holds it, and all three take the request's subnets from there. Valid requests resolve the same subnets as before, and both together are still refused with the same error. A new test pins the SetSubnets path, which had none. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(elbv2): say when CloudFormation refuses a load balancer without subnets The behaviour note said a CloudFormation load balancer with neither Subnets nor SubnetMappings fails its stack the same way as the API does. That holds when the stack creates or replaces the balancer. An update that keeps the balancer finds it by its taken name before the subnet check, so it leaves the balancer on its subnets, as before. The note now says so. The requireSubnets Javadoc no longer presents the Gateway Load Balancer message as AWS's own. No public record of it was found; it is the Network Load Balancer one, since a Gateway Load Balancer takes subnets the same way. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): give the remaining subnetless test requests subnets Review follow-ups: - The two duplicate-name tests, ElbV2IntegrationTest and the compatibility suite's ElbV2Test, sent a create with no subnets, so they passed only because the name is checked first. They now name subnets. The compatibility one also changes the scheme, because AWS answers a create that repeats a balancer's settings with success. - ElbV2CfnProvisionerTest's successful creates and replacements now provision templates with two subnets and match them, rather than a subnetless create the real service refuses. The kept-balancer updates keep their subnetless templates. - The CloudFormation case that leaves both properties out checks that its template really has no SubnetMappings, so a reformatted template cannot quietly turn it into the empty-list case. - TestFixtures.defaultVpcSubnets() fails with a message when the region has no default VPC or fewer than two default subnets. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): say which subnet messages are AWS's in the test docs Second-round review follow-ups, wording only: - The ELBv2 unit and Query-protocol tests of a create with neither Subnets nor SubnetMappings now say the messages are the ones AWS gives an Application or a Network Load Balancer, and that a Gateway Load Balancer gets the Network one, as the service's Javadoc does. - The provisioner test's onTwoSubnets builds its list from TWO_SUBNETS, the list its stubs match, and its Javadoc no longer reads as two subnets for every type. - TestFixtures.defaultVpcSubnets() says an Application Load Balancer needs two zones, not every load balancer. - The ELBv2 docs say a CloudFormation update moves a balancer when it names other subnets, since one that names none leaves it in place. 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
484255871e
commit
a434e52892
23 files changed
+413
-76
No files matched your search
+7
-11
@@ -211,12 +211,12 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
/**
|
||||
* The template's subnets: {@code Subnets}, or else the {@code SubnetId} of each
|
||||
* {@code SubnetMappings} entry, the form a Network Load Balancer uses to give a zone an Elastic IP.
|
||||
* Both non-empty fails the resource with the error CreateLoadBalancer gives, as the resource allows
|
||||
* one or the other; an empty side, such as one an Fn::If drops, does not count. {@code SubnetId} is
|
||||
* required in a mapping, so one without it fails the resource before anything is created or moved,
|
||||
* with the error the resource schema gives. A blank one counts as a mapping and, with no
|
||||
* {@code Subnets}, is passed on as given for ELBv2 to refuse as an unknown subnet; dropping it would
|
||||
* create or move the balancer without it.
|
||||
* Both non-empty fails the resource with the error CreateLoadBalancer gives, through
|
||||
* {@link ElbV2Service#requestedSubnets}, as the resource allows one or the other; an empty side, such
|
||||
* as one an Fn::If drops, does not count. {@code SubnetId} is required in a mapping, so one without it
|
||||
* fails the resource before anything is created or moved, with the error the resource schema gives.
|
||||
* A blank one counts as a mapping and, with no {@code Subnets}, is passed on as given for ELBv2 to
|
||||
* refuse as an unknown subnet; dropping it would create or move the balancer without it.
|
||||
*/
|
||||
private static List<String> templateSubnets(JsonNode props, ProvisionContext ctx) {
|
||||
List<String> subnets = ctx.resolveStringList(props, "Subnets");
|
||||
@@ -236,11 +236,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
mapped.add(subnetId);
|
||||
}
|
||||
}
|
||||
if (!subnets.isEmpty() && !mapped.isEmpty()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"You can specify either subnets or subnet mappings, not both", 400);
|
||||
}
|
||||
return subnets.isEmpty() ? mapped : subnets;
|
||||
return ElbV2Service.requestedSubnets(subnets, mapped);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -123,18 +123,12 @@ public class ElbV2QueryHandler {
|
||||
String scheme = p.getFirst("Scheme");
|
||||
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> subnets = ElbV2Service.requestedSubnets(memberList(p, "Subnets"), subnetMappingSubnetIds(p));
|
||||
List<String> securityGroups = memberList(p, "SecurityGroups");
|
||||
Map<String, String> tags = parseTags(p);
|
||||
|
||||
LoadBalancer lb = service.createLoadBalancer(region, name, scheme, type, ipAddressType,
|
||||
effectiveSubnets, securityGroups, tags);
|
||||
subnets, securityGroups, tags);
|
||||
|
||||
// return provisioning state in create response only
|
||||
LoadBalancer provisioning = shallowCopy(lb);
|
||||
@@ -261,13 +255,8 @@ public class ElbV2QueryHandler {
|
||||
|
||||
private Response handleSetSubnets(MultivaluedMap<String, String> p, String region) {
|
||||
String arn = p.getFirst("LoadBalancerArn");
|
||||
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);
|
||||
}
|
||||
service.setSubnets(region, arn, subnetMappings.isEmpty() ? subnets : subnetMappings);
|
||||
List<String> subnets = ElbV2Service.requestedSubnets(memberList(p, "Subnets"), subnetMappingSubnetIds(p));
|
||||
service.setSubnets(region, arn, subnets);
|
||||
return voidResponse("SetSubnetsResponse");
|
||||
}
|
||||
|
||||
|
||||
@@ -209,6 +209,19 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
|
||||
// ── Load Balancers ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* The subnets a CreateLoadBalancer or SetSubnets request names: {@code Subnets}, or else the subnet
|
||||
* of each {@code SubnetMappings} entry. The two are alternatives, so a request that names subnets
|
||||
* both ways is refused with the error AWS gives; an empty one does not count.
|
||||
*/
|
||||
public static List<String> requestedSubnets(List<String> subnets, List<String> mappingSubnetIds) {
|
||||
if (!subnets.isEmpty() && !mappingSubnetIds.isEmpty()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"You can specify either subnets or subnet mappings, not both", 400);
|
||||
}
|
||||
return mappingSubnetIds.isEmpty() ? subnets : mappingSubnetIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolving the subnets and publishing the balancer happen under EC2's subnet lock, so a
|
||||
* DeleteSubnet cannot pass its dependency check in between and leave the balancer on a subnet
|
||||
@@ -242,6 +255,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
String id = randomHex16();
|
||||
String arn = AwsArnUtils.Arn.of("elasticloadbalancing", region, regionResolver.getAccountId(), "loadbalancer/" + typePrefix + "/" + name + "/" + id).toString();
|
||||
String dnsName = name + "-" + id + ".elb." + loadBalancerDnsSuffix();
|
||||
requireSubnets(lbType, subnets);
|
||||
String vpcId = resolveSubnetVpcId(region, lbType, subnets);
|
||||
|
||||
LoadBalancer lb = new LoadBalancer();
|
||||
@@ -256,7 +270,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
lb.setType(lbType);
|
||||
lb.setIpAddressType(ipType);
|
||||
lb.setRegion(region);
|
||||
if (subnets != null) lb.setAvailabilityZones(resolveAvailabilityZones(region, subnets));
|
||||
lb.setAvailabilityZones(resolveAvailabilityZones(region, subnets));
|
||||
if (securityGroups != null) lb.setSecurityGroups(new ArrayList<>(securityGroups));
|
||||
|
||||
regionLbs.put(arn, lb);
|
||||
@@ -268,6 +282,22 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
return lb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every load balancer type takes {@code Subnets} or {@code SubnetMappings}, so a request with
|
||||
* neither is refused before anything is created, with the message AWS gives an Application or a
|
||||
* Network Load Balancer. A Gateway Load Balancer, which takes subnets the way a Network Load
|
||||
* Balancer does, gets the Network one. This runs after the duplicate-name check, because a
|
||||
* CloudFormation stack update finds the balancer it keeps through DuplicateLoadBalancerName,
|
||||
* whatever subnets its template names.
|
||||
*/
|
||||
private static void requireSubnets(String lbType, List<String> subnets) {
|
||||
if (subnets == null || subnets.isEmpty()) {
|
||||
throw new AwsException("ValidationError", "application".equals(lbType)
|
||||
? "At least two subnets in two different Availability Zones must be specified"
|
||||
: "At least one subnet must be specified", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A subnet is in use while a load balancer is configured with it. Account-scoped through the
|
||||
* request-scoped storage, and a target group does not count: it pins nothing in AWS either.
|
||||
|
||||
Reference in new issue
Block a user