mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(ec2): protect subnets and VPCs that load balancers use (#5663)
* fix(elbv2): protect subnets and VPCs that load balancers use DeleteSubnet and DeleteVpc returned success while an Application or Network Load Balancer still used the subnet, and DescribeLoadBalancers went on listing a subnet that no longer existed. EC2 now asks each VpcDependencyProvider, implemented by ELBv2, before deleting a subnet or VPC, under the lock CreateLoadBalancer and SetSubnets hold while they record their subnets, and refuses with DependencyViolation. Covered end to end through CloudFormation, including updates, rollbacks and stack deletes that move or delete load balancers, subnets and VPCs. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(cloudformation): record the subnets a load balancer is on A load balancer's attributes named none of its subnets, so once a template stopped naming it, the stack walks had nothing to order it by and deleted it after them, which EC2 now refuses. The provisioner keeps one hidden uses attribute per subnet, refreshed when an update moves the balancer and when a rollback moves it back. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(cloudformation): delete removed resources dependents first The update cleanup deleted removed and condition-disabled resources in reverse map order, before the replacement cleanup. A subnet an earlier update added sits after the load balancer it moved onto it, so removing both deleted the subnet first, and a renamed balancer's old entity still used a subnet the same update dropped. Both now end DELETE_FAILED, since EC2 refuses to delete a subnet a load balancer uses. The removed resources are now walked in dependency order, and a failed delete is tried once more after the replacement cleanup before it is reported. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(ec2): name every ELBv2 load balancer type as a subnet dependency The DeleteSubnet and DeleteVpc rows named only Application and Network Load Balancers, while the check counts every ELBv2 type. The SDK lifecycle test now deletes its subnets and VPC even when an assertion fails. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudformation): pin the order of the removed-resource deletes The removal test passed through the retry alone, so it now checks that the load balancer is deleted before its subnet is tried, and that the replaced balancer's old entity goes before the dropped subnet. The provisioner attribute test reads only what Fn::GetAtt can see, with the balancer on subnets. 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
0f4f59bbbb
commit
e520166170
13 files changed
+1337
-62
No files matched your search
+56
@@ -0,0 +1,56 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancerSchemeEnum;
|
||||
import software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancerTypeEnum;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("EC2 subnet and VPC lifecycle dependencies")
|
||||
class Ec2ResourceLifecycleTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("DeleteSubnet and DeleteVpc are refused while a load balancer uses the subnet")
|
||||
void subnetAndVpcAreRefusedWhileALoadBalancerUsesThem() {
|
||||
String region = TestFixtures.region().id();
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client();
|
||||
ElasticLoadBalancingV2Client elb = TestFixtures.elbV2Client()) {
|
||||
String vpcId = ec2.createVpc(r -> r.cidrBlock("10.94.0.0/16")).vpc().vpcId();
|
||||
String first = ec2.createSubnet(r -> r.vpcId(vpcId).cidrBlock("10.94.1.0/24")
|
||||
.availabilityZone(region + "a")).subnet().subnetId();
|
||||
String second = ec2.createSubnet(r -> r.vpcId(vpcId).cidrBlock("10.94.2.0/24")
|
||||
.availabilityZone(region + "b")).subnet().subnetId();
|
||||
String loadBalancerArn = elb.createLoadBalancer(r -> r.name(TestFixtures.uniqueName("lifecycle"))
|
||||
.type(LoadBalancerTypeEnum.APPLICATION).scheme(LoadBalancerSchemeEnum.INTERNAL)
|
||||
.subnets(first, second)).loadBalancers().get(0).loadBalancerArn();
|
||||
try {
|
||||
assertThatThrownBy(() -> ec2.deleteSubnet(r -> r.subnetId(first)))
|
||||
.isInstanceOfSatisfying(Ec2Exception.class, e ->
|
||||
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("DependencyViolation"));
|
||||
assertThatThrownBy(() -> ec2.deleteVpc(r -> r.vpcId(vpcId)))
|
||||
.isInstanceOfSatisfying(Ec2Exception.class, e ->
|
||||
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("DependencyViolation"));
|
||||
} finally {
|
||||
// Cleaned up even when an assertion fails, so nothing leaks into the rest of the run.
|
||||
// A refusal that did not happen has already deleted the subnet, hence quietly.
|
||||
elb.deleteLoadBalancer(r -> r.loadBalancerArn(loadBalancerArn));
|
||||
quietly(() -> ec2.deleteSubnet(r -> r.subnetId(first)));
|
||||
quietly(() -> ec2.deleteSubnet(r -> r.subnetId(second)));
|
||||
quietly(() -> ec2.deleteVpc(r -> r.vpcId(vpcId)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void quietly(Runnable cleanup) {
|
||||
try {
|
||||
cleanup.run();
|
||||
} catch (Ec2Exception ignored) {
|
||||
// already gone
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user