diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Subnet.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Subnet.java index 9a33f67f4f..a7b046218a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Subnet.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Subnet.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.ec2.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.github.hectorvent.floci.core.common.AvailabilityZoneIds; import io.quarkus.runtime.annotations.RegisterForReflection; import java.util.ArrayList; @@ -45,7 +46,18 @@ public class Subnet { public String getAvailabilityZone() { return availabilityZone; } public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; } - public String getAvailabilityZoneId() { return availabilityZoneId; } + /** + * Derived from the zone name rather than read back as stored: a subnet persisted before + * zone ids took AWS's shape still carries {@code -az}, which + * DescribeAvailabilityZones no longer publishes. + */ + public String getAvailabilityZoneId() { + if (region == null || availabilityZone == null) { + return availabilityZoneId; + } + return AvailabilityZoneIds.forZoneName(region, availabilityZone); + } + public void setAvailabilityZoneId(String availabilityZoneId) { this.availabilityZoneId = availabilityZoneId; } public int getAvailableIpAddressCount() { return availableIpAddressCount; } diff --git a/src/main/java/io/github/hectorvent/floci/services/efs/model/FileSystem.java b/src/main/java/io/github/hectorvent/floci/services/efs/model/FileSystem.java index 641c6879fa..a89ba07323 100644 --- a/src/main/java/io/github/hectorvent/floci/services/efs/model/FileSystem.java +++ b/src/main/java/io/github/hectorvent/floci/services/efs/model/FileSystem.java @@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.efs.model; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; +import io.github.hectorvent.floci.core.common.AvailabilityZoneIds; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.quarkus.runtime.annotations.RegisterForReflection; import java.util.List; @@ -31,8 +33,16 @@ public class FileSystem { + /** + * Derived from the zone name and the region of the file system's ARN rather than read back + * as stored: a file system persisted before zone ids took AWS's shape carries the old id. + */ public String getAvailabilityZoneId() { - return availabilityZoneId; + String region = AwsArnUtils.regionOrDefault(fileSystemArn, null); + if (region == null || availabilityZoneName == null) { + return availabilityZoneId; + } + return AvailabilityZoneIds.forZoneName(region, availabilityZoneName); } public void setAvailabilityZoneId(String availabilityZoneId) { diff --git a/src/main/java/io/github/hectorvent/floci/services/efs/model/MountTarget.java b/src/main/java/io/github/hectorvent/floci/services/efs/model/MountTarget.java index 762d8b0fa7..23f58694ac 100644 --- a/src/main/java/io/github/hectorvent/floci/services/efs/model/MountTarget.java +++ b/src/main/java/io/github/hectorvent/floci/services/efs/model/MountTarget.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.efs.model; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; +import io.github.hectorvent.floci.core.common.AvailabilityZoneIds; import io.quarkus.runtime.annotations.RegisterForReflection; import java.util.List; @@ -24,8 +25,17 @@ public class MountTarget { private String subnetId; private String vpcId; + /** + * Derived from the zone name, {@code }, rather than read back as stored: a + * mount target persisted before zone ids took AWS's shape carries the old id. + */ public String getAvailabilityZoneId() { - return availabilityZoneId; + if (availabilityZoneName == null || availabilityZoneName.length() < 2 + || !Character.isLetter(availabilityZoneName.charAt(availabilityZoneName.length() - 1))) { + return availabilityZoneId; + } + String region = availabilityZoneName.substring(0, availabilityZoneName.length() - 1); + return AvailabilityZoneIds.forZoneName(region, availabilityZoneName); } public void setAvailabilityZoneId(String availabilityZoneId) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java index 5ec0a68ba6..15684b8296 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServicePersistenceTest.java @@ -218,6 +218,37 @@ class Ec2ServicePersistenceTest { assertEquals("10.0.1.0/24", subnets.get(0).getCidrBlock()); } + /** + * Issue #5580: a subnet persisted before zone ids took AWS's shape stored + * {@code ap-southeast-1-az1}, which DescribeAvailabilityZones no longer publishes. + */ + @Test + void legacySubnetZoneIdReadsBackAsTheIdDescribeAvailabilityZonesPublishes(@TempDir Path dir) + throws IOException { + Files.writeString(dir.resolve("ec2-subnets.json"), """ + { + "ap-southeast-1::subnet-legacy": { + "subnetId": "subnet-legacy", + "vpcId": "vpc-legacy", + "cidrBlock": "172.31.0.0/20", + "state": "available", + "availabilityZone": "ap-southeast-1b", + "availabilityZoneId": "ap-southeast-1-az2", + "region": "ap-southeast-1" + } + } + """); + + Ec2Service restarted = newService(dir); + + Subnet subnet = restarted.describeSubnets("ap-southeast-1", List.of("subnet-legacy"), Map.of()) + .getFirst(); + assertEquals("apse1-az2", subnet.getAvailabilityZoneId()); + List> zones = restarted.describeAvailabilityZones("ap-southeast-1", List.of(), + List.of(subnet.getAvailabilityZoneId()), List.of()); + assertEquals("ap-southeast-1b", zones.getFirst().get("zoneName")); + } + @Test void registeredImageAndSnapshotSurviveRestart(@TempDir Path dir) { Ec2Service first = newService(dir); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetAvailabilityZoneIdIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetAvailabilityZoneIdIntegrationTest.java index 5ce1f0b0f5..98ea0ef038 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetAvailabilityZoneIdIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2SubnetAvailabilityZoneIdIntegrationTest.java @@ -1,13 +1,17 @@ package io.github.hectorvent.floci.services.ec2; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.xml.XmlPath; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.List; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * CreateSubnet never read AvailabilityZoneId and hardcoded every new subnet's zone id to the @@ -110,6 +114,42 @@ class Ec2SubnetAvailabilityZoneIdIntegrationTest { hasItem("use1-az3")); } + /** + * Issue #5580: the AWS Load Balancer Controller resolves each subnet's zone by passing its + * AvailabilityZoneId to DescribeAvailabilityZones --zone-ids, the default VPC's subnets + * included, so every id a subnet reports has to be accepted there. + */ + @Test + void everyDefaultSubnetZoneIdCanBeLookedUpByZoneId() { + String auth = "AWS4-HMAC-SHA256 Credential=test/20260205/ap-southeast-1/ec2/aws4_request"; + XmlPath subnets = given() + .formParam("Action", "DescribeSubnets") + .formParam("Filter.1.Name", "default-for-az") + .formParam("Filter.1.Value.1", "true") + .header("Authorization", auth) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath(); + List zoneNames = subnets.getList("DescribeSubnetsResponse.subnetSet.item.availabilityZone"); + List zoneIds = subnets.getList("DescribeSubnetsResponse.subnetSet.item.availabilityZoneId"); + + assertFalse(zoneIds.isEmpty()); + for (int i = 0; i < zoneIds.size(); i++) { + given() + .formParam("Action", "DescribeAvailabilityZones") + .formParam("ZoneId.1", zoneIds.get(i)) + .header("Authorization", auth) + .when() + .post("/") + .then() + .statusCode(200) + .body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item.zoneName", + equalTo(zoneNames.get(i))); + } + } + @Test void aZoneAndZoneIdThatDisagreeAreRefusedRatherThanSilentlyResolved() { given() diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/model/SubnetZoneIdTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/model/SubnetZoneIdTest.java new file mode 100644 index 0000000000..1e965cd878 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/model/SubnetZoneIdTest.java @@ -0,0 +1,56 @@ +package io.github.hectorvent.floci.services.ec2.model; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Issue #5580: subnets persisted before zone ids took AWS's shape stored {@code -az}; + * the id is derived from the zone name, so the old form reads back as the current one. + */ +class SubnetZoneIdTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + @Test + void theStoredLegacyIdReadsBackAsTheCurrentId() throws Exception { + Subnet subnet = mapper.readValue(""" + { + "subnetId": "subnet-legacy", + "availabilityZone": "ap-southeast-1a", + "availabilityZoneId": "ap-southeast-1-az1", + "region": "ap-southeast-1" + } + """, Subnet.class); + + assertEquals("apse1-az1", subnet.getAvailabilityZoneId()); + } + + @Test + void theRoundTripWritesTheCurrentIdAndReadsItBack() throws Exception { + Subnet legacy = mapper.readValue(""" + { + "availabilityZoneId": "ap-southeast-1-az3", + "availabilityZone": "ap-southeast-1c", + "region": "ap-southeast-1" + } + """, Subnet.class); + + String written = mapper.writeValueAsString(legacy); + Subnet reread = mapper.readValue(written, Subnet.class); + + assertEquals("apse1-az3", mapper.readTree(written).get("availabilityZoneId").asText()); + assertEquals("apse1-az3", reread.getAvailabilityZoneId()); + assertEquals("ap-southeast-1c", reread.getAvailabilityZone()); + } + + @Test + void aSubnetWithoutAZoneNameKeepsTheStoredId() throws Exception { + Subnet subnet = mapper.readValue(""" + {"availabilityZoneId": "use1-az2", "region": "us-east-1"} + """, Subnet.class); + + assertEquals("use1-az2", subnet.getAvailabilityZoneId()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/efs/model/EfsZoneIdTest.java b/src/test/java/io/github/hectorvent/floci/services/efs/model/EfsZoneIdTest.java new file mode 100644 index 0000000000..54d19d1e6e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/efs/model/EfsZoneIdTest.java @@ -0,0 +1,61 @@ +package io.github.hectorvent.floci.services.efs.model; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * Issue #5580: file systems and mount targets persisted before zone ids took AWS's shape stored + * {@code -az}; the id is derived from the zone name, so the old form reads back as the + * current one. + */ +class EfsZoneIdTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + @Test + void aLegacyFileSystemReadsBackTheCurrentId() throws Exception { + FileSystem fs = mapper.readValue(""" + { + "FileSystemId": "fs-legacy", + "FileSystemArn": "arn:aws:elasticfilesystem:ap-southeast-1:000000000000:file-system/fs-legacy", + "AvailabilityZoneName": "ap-southeast-1b", + "AvailabilityZoneId": "ap-southeast-1-az2" + } + """, FileSystem.class); + + assertEquals("apse1-az2", fs.getAvailabilityZoneId()); + + String written = mapper.writeValueAsString(fs); + assertEquals("apse1-az2", mapper.readTree(written).get("AvailabilityZoneId").asText()); + assertEquals("apse1-az2", mapper.readValue(written, FileSystem.class).getAvailabilityZoneId()); + } + + @Test + void aRegionalFileSystemHasNoZoneId() throws Exception { + FileSystem fs = mapper.readValue(""" + {"FileSystemArn": "arn:aws:elasticfilesystem:us-east-1:000000000000:file-system/fs-1"} + """, FileSystem.class); + + assertNull(fs.getAvailabilityZoneId()); + } + + @Test + void aLegacyMountTargetReadsBackTheCurrentId() throws Exception { + MountTarget mt = mapper.readValue(""" + { + "MountTargetId": "fsmt-legacy", + "AvailabilityZoneName": "ap-southeast-1a", + "AvailabilityZoneId": "ap-southeast-1-az1" + } + """, MountTarget.class); + + assertEquals("apse1-az1", mt.getAvailabilityZoneId()); + + String written = mapper.writeValueAsString(mt); + assertEquals("apse1-az1", mapper.readTree(written).get("AvailabilityZoneId").asText()); + assertEquals("apse1-az1", mapper.readValue(written, MountTarget.class).getAvailabilityZoneId()); + } +}