Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(elbv2): enforce ALB subnet availability zone rules (#1247)

shrimptails-f70e63caf103f
diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java
index 0e975c0397..2fea04ea5f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java
@@ -158,7 +158,7 @@ public class ElbV2Service {
String id = randomHex16();
String arn = AwsArnUtils.Arn.of("elasticloadbalancing", region, regionResolver.getAccountId(), "loadbalancer/" + typePrefix + "/" + name + "/" + id).toString();
String dnsName = name + "-" + id + ".elb.localhost";
- String vpcId = resolveSubnetVpcId(region, subnets);
+ String vpcId = resolveSubnetVpcId(region, lbType, subnets);
LoadBalancer lb = new LoadBalancer();
lb.setLoadBalancerArn(arn);
@@ -267,7 +267,7 @@ public class ElbV2Service {
public void setSubnets(String region, String arn, List<String> subnets) {
LoadBalancer lb = requireLoadBalancer(region, arn);
- String vpcId = resolveSubnetVpcId(region, subnets);
+ String vpcId = resolveSubnetVpcId(region, lb.getType(), subnets);
if (vpcId != null && lb.getVpcId() != null && !lb.getVpcId().equals(vpcId)) {
throw new AwsException("InvalidConfigurationRequest",
"All subnets must belong to the load balancer VPC.", 400);
@@ -826,18 +826,14 @@ public class ElbV2Service {
}
private List<AvailabilityZone> resolveAvailabilityZones(String region, List<String> subnetIds) {
+ if (subnetIds == null || subnetIds.isEmpty()) {
+ return new ArrayList<>();
+ }
+
+ Map<String, Subnet> subnetsById = resolveSubnetsById(region, subnetIds);
List<AvailabilityZone> availabilityZones = new ArrayList<>();
for (String subnetId : subnetIds) {
- Subnet subnet;
- try {
- subnet = ec2Service.requireSubnet(region, subnetId);
- } catch (AwsException e) {
- if ("InvalidSubnetID.NotFound".equals(e.getErrorCode())) {
- throw new AwsException("SubnetNotFound",
- "Subnet '" + subnetId + "' not found", 400);
- }
- throw e;
- }
+ Subnet subnet = subnetsById.get(subnetId);
AvailabilityZone availabilityZone = new AvailabilityZone();
availabilityZone.setSubnetId(subnet.getSubnetId());
availabilityZone.setZoneName(subnet.getAvailabilityZone());
@@ -846,19 +842,27 @@ public class ElbV2Service {
return availabilityZones;
}
- private String resolveSubnetVpcId(String region, List<String> subnetIds) {
+ private String resolveSubnetVpcId(String region, String lbType, List<String> subnetIds) {
if (subnetIds == null || subnetIds.isEmpty()) {
return null;
}
- List<Subnet> subnets = ec2Service.describeSubnets(region, subnetIds, Map.of());
- Map<String, Subnet> subnetsById = subnets.stream()
- .collect(Collectors.toMap(Subnet::getSubnetId, subnet -> subnet, (left, right) -> left));
+ Map<String, Subnet> subnetsById = resolveSubnetsById(region, subnetIds);
- for (String subnetId : subnetIds) {
- if (!subnetsById.containsKey(subnetId)) {
- throw new AwsException("SubnetNotFound",
- "The subnet ID '" + subnetId + "' does not exist", 400);
+ if ("application".equals(lbType)) {
+ if (subnetIds.size() < 2) {
+ throw new AwsException("InvalidConfigurationRequest",
+ "Application Load Balancers must be attached to subnets in at least two Availability Zones.", 400);
+ }
+
+ long distinctAvailabilityZones = subnetIds.stream()
+ .map(subnetsById::get)
+ .map(Subnet::getAvailabilityZone)
+ .distinct()
+ .count();
+ if (distinctAvailabilityZones < 2) {
+ throw new AwsException("InvalidConfigurationRequest",
+ "Application Load Balancers must be attached to subnets in at least two Availability Zones.", 400);
}
}
@@ -875,6 +879,19 @@ public class ElbV2Service {
return vpcIds.iterator().next();
}
+ private Map<String, Subnet> resolveSubnetsById(String region, List<String> subnetIds) {
+ List<Subnet> subnets = ec2Service.describeSubnets(region, subnetIds, Map.of());
+ Map<String, Subnet> subnetsById = subnets.stream()
+ .collect(Collectors.toMap(Subnet::getSubnetId, subnet -> subnet, (left, right) -> left));
+
+ for (String subnetId : subnetIds) {
+ if (!subnetsById.containsKey(subnetId)) {
+ throw new AwsException("SubnetNotFound",
+ "The subnet ID '" + subnetId + "' does not exist", 400);
+ }
+ }
+ return subnetsById;
+ }
private TargetGroup requireTargetGroup(String region, String arn) {
TargetGroup tg = targetGroups.getOrDefault(region, Map.of()).get(arn);
if (tg == null) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index d2e04df302..c5a32c8f90 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -5127,7 +5127,7 @@ class CloudFormationIntegrationTest {
"Name": "cfn-tg",
"Protocol": "HTTP",
"Port": 80,
- "VpcId": "vpc-00000001",
+ "VpcId": "vpc-default",
"TargetType": "ip",
"HealthCheckPath": "/health",
"Matcher": { "HttpCode": "200-299" }
diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java
index 7f48888802..1ab1792043 100644
--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java
@@ -295,6 +295,7 @@ class ElbV2IntegrationTest {
.formParam("Name", "set-subnets-test-lb")
.formParam("Type", "application")
.formParam("Subnets.member.1", "subnet-default-a")
+ .formParam("Subnets.member.2", "subnet-default-b")
.header("Authorization", AUTH)
.when()
.post("/")
@@ -318,6 +319,93 @@ class ElbV2IntegrationTest {
@Test
@Order(12)
+ void createLoadBalancerWithSingleSubnetThrowsInvalidConfigurationRequest() {
+ given()
+ .formParam("Action", "CreateLoadBalancer")
+ .formParam("Name", "single-subnet-lb")
+ .formParam("Type", "application")
+ .formParam("Subnets.member.1", "subnet-default-a")
+ .header("Authorization", AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
+ }
+
+ @Test
+ @Order(13)
+ void createLoadBalancerWithSubnetsInSameAvailabilityZoneThrowsInvalidConfigurationRequest() {
+ String secondSubnetId = given()
+ .formParam("Action", "CreateSubnet")
+ .formParam("VpcId", "vpc-default")
+ .formParam("CidrBlock", "172.31.64.0/24")
+ .formParam("AvailabilityZone", "us-east-1a")
+ .header("Authorization", EC2_AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract()
+ .path("CreateSubnetResponse.subnet.subnetId");
+
+ given()
+ .formParam("Action", "CreateLoadBalancer")
+ .formParam("Name", "duplicate-az-lb")
+ .formParam("Type", "application")
+ .formParam("Subnets.member.1", "subnet-default-a")
+ .formParam("Subnets.member.2", secondSubnetId)
+ .header("Authorization", AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
+ }
+
+ @Test
+ @Order(14)
+ void setSubnetsWithSubnetsInSameAvailabilityZoneThrowsInvalidConfigurationRequest() {
+ String secondSubnetId = given()
+ .formParam("Action", "CreateSubnet")
+ .formParam("VpcId", "vpc-default")
+ .formParam("CidrBlock", "172.31.65.0/24")
+ .formParam("AvailabilityZone", "us-east-1a")
+ .header("Authorization", EC2_AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract()
+ .path("CreateSubnetResponse.subnet.subnetId");
+
+ String subnetlessLbArn = given()
+ .formParam("Action", "CreateLoadBalancer")
+ .formParam("Name", "set-duplicate-az-lb")
+ .formParam("Type", "application")
+ .header("Authorization", AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract()
+ .path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
+
+ given()
+ .formParam("Action", "SetSubnets")
+ .formParam("LoadBalancerArn", subnetlessLbArn)
+ .formParam("Subnets.member.1", "subnet-default-a")
+ .formParam("Subnets.member.2", secondSubnetId)
+ .header("Authorization", AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
+ }
+
+ @Test
+ @Order(15)
void setSubnetsAfterSubnetlessCreateCanAdoptCustomVpc() {
String customVpcId = given()
.formParam("Action", "CreateVpc")
@@ -330,10 +418,23 @@ class ElbV2IntegrationTest {
.extract()
.path("CreateVpcResponse.vpc.vpcId");
- String customSubnetId = given()
+ String customSubnetAId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", customVpcId)
.formParam("CidrBlock", "10.2.1.0/24")
+ .formParam("AvailabilityZone", "us-east-1b")
+ .header("Authorization", EC2_AUTH)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract()
+ .path("CreateSubnetResponse.subnet.subnetId");
+
+ String customSubnetBId = given()
+ .formParam("Action", "CreateSubnet")
+ .formParam("VpcId", customVpcId)
+ .formParam("CidrBlock", "10.2.2.0/24")
.formParam("AvailabilityZone", "us-east-1c")
.header("Authorization", EC2_AUTH)
.when()
@@ -345,7 +446,7 @@ class ElbV2IntegrationTest {
String subnetlessLbArn = given()
.formParam("Action", "CreateLoadBalancer")
- .formParam("Name", "subnetless-lb")
+ .formParam("Name", "subnetless-adopt-vpc-lb")
.formParam("Type", "application")
.header("Authorization", AUTH)
.when()
@@ -358,7 +459,8 @@ class ElbV2IntegrationTest {
given()
.formParam("Action", "SetSubnets")
.formParam("LoadBalancerArn", subnetlessLbArn)
- .formParam("Subnets.member.1", customSubnetId)
+ .formParam("Subnets.member.1", customSubnetAId)
+ .formParam("Subnets.member.2", customSubnetBId)
.header("Authorization", AUTH)
.when()
.post("/")
@@ -375,12 +477,12 @@ class ElbV2IntegrationTest {
.statusCode(200)
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.VpcId",
equalTo(customVpcId))
- .body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
- equalTo(customSubnetId));
+ .body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.size()",
+ equalTo(2));
}
@Test
- @Order(13)
+ @Order(16)
void createLoadBalancerWithMissingSubnetThrowsSubnetNotFound() {
given()
.formParam("Action", "CreateLoadBalancer")
@@ -398,7 +500,7 @@ class ElbV2IntegrationTest {
// ── Target Groups ─────────────────────────────────────────────────────────
@Test
- @Order(14)
+ @Order(20)
void createTargetGroup() {
tgArn = given()
.formParam("Action", "CreateTargetGroup")
@@ -429,7 +531,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(15)
+ @Order(21)
void describeTargetGroups() {
given()
.formParam("Action", "DescribeTargetGroups")
@@ -444,7 +546,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(16)
+ @Order(22)
void duplicateTargetGroupNameThrows() {
given()
.formParam("Action", "CreateTargetGroup")
@@ -458,7 +560,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(17)
+ @Order(23)
void modifyTargetGroupAttributes() {
given()
.formParam("Action", "ModifyTargetGroupAttributes")
@@ -475,7 +577,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(18)
+ @Order(24)
void describeTargetGroupsByMissingNameThrows() {
given()
.formParam("Action", "DescribeTargetGroups")
@@ -489,7 +591,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(19)
+ @Order(25)
void describeTargetGroupsByMissingArnThrows() {
given()
.formParam("Action", "DescribeTargetGroups")
@@ -506,7 +608,7 @@ class ElbV2IntegrationTest {
// ── Targets ───────────────────────────────────────────────────────────────
@Test
- @Order(20)
+ @Order(26)
void registerTargets() {
given()
.formParam("Action", "RegisterTargets")
@@ -523,7 +625,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(21)
+ @Order(27)
void describeTargetHealthReturnsInitial() {
given()
.formParam("Action", "DescribeTargetHealth")
@@ -927,7 +1029,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(73)
+ @Order(74)
void createLoadBalancerWithMissingSubnetReturnsSubnetNotFound() {
given()
.formParam("Action", "CreateLoadBalancer")
@@ -943,7 +1045,7 @@ class ElbV2IntegrationTest {
}
@Test
- @Order(74)
+ @Order(75)
void setSubnetsWithMissingSubnetReturnsSubnetNotFound() {
String setSubnetsLbArn = given()
.formParam("Action", "CreateLoadBalancer")