fix(ec2): place a subnet in the zone its request names, not always az1 (#2890)

* fix(ec2): place a subnet in the zone its request names, not always az1

CreateSubnet never read AvailabilityZoneId, and hardcoded every new subnet's
zone id to the region's first zone. Two consequences, and the second bites even
a caller that never mentions a zone id: a subnet asked for us-east-1-az2 came
back in us-east-1-az1, and a subnet placed correctly by zone *name* still
reported az1, contradicting its own availabilityZone.

Anything that selects subnets by zone id therefore saw one zone -- Terraform's
aws_subnet.availability_zone_id, and any client that pairs subnets against the
DescribeAvailabilityZones list it just read.

AvailabilityZoneId is now honoured, and the zone id is derived from the zone
rather than fixed. The derivation is the mapping DescribeAvailabilityZones and
the seeded default subnets were already using independently; all three now come
through one helper, so a subnet's zone id agrees with the zone list by
construction instead of by coincidence.

A zone and a zone id that disagree are refused with InvalidParameterCombination
rather than silently resolved to one of them -- Terraform's aws_subnet forbids
setting both, so a request carrying both contradictory values is a bug in the
caller, not something to guess at. A malformed zone id is refused too.

* fix(ec2): refuse a zone id this region does not publish

The first version resolved any well-formed zone id, so us-east-1-az4 created a
subnet in a zone DescribeAvailabilityZones does not list. That is the same
inconsistency the hardcoded az1 produced, only quieter: a client pairing subnets
against the published zone inventory still cannot resolve the subnet's zone.

The three modelled zones are now one constant that DescribeAvailabilityZones,
the seeded default subnets and this validation all read, so they cannot drift,
and the error names the range rather than only rejecting.

Also logs the tolerated NumberFormatException instead of swallowing it, and
drops this branch's em-dashes, both per AGENTS.md.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-09-01 12:13:34 -04:00
1 parent 2a637d3e05
commit c32f8b5a24
3 files changed
+266 -7

No files matched your search

@@ -2291,7 +2291,8 @@ public class Ec2QueryHandler {
String vpcId = p.getFirst("VpcId");
String cidrBlock = p.getFirst("CidrBlock");
String az = p.getFirst("AvailabilityZone");
Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az);
String azId = p.getFirst("AvailabilityZoneId");
Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az, azId);
applyResourceTags(p, region, "subnet", subnet.getSubnetId());
XmlBuilder xml = new XmlBuilder()
.start("CreateSubnetResponse", AwsNamespaces.EC2)
@@ -109,6 +109,13 @@ import jakarta.inject.Inject;
public class Ec2Service implements ContainerTeardown, ResourceProvider {
private static final Logger LOG = Logger.getLogger(Ec2Service.class);
/**
* The availability zones every region is modelled with. DescribeAvailabilityZones publishes
* exactly these, the seeded default subnets sit in them, and CreateSubnet refuses a zone id
* outside them, so the three cannot drift apart.
*/
private static final String[] MODELLED_ZONE_SUFFIXES = {"a", "b", "c"};
private static final DateTimeFormatter ISO_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC);
private static final int DEFAULT_ROOT_VOLUME_SIZE_GIB = 8;
@@ -428,7 +435,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
vpcs.put(key(region, vpcId), defaultVpc);
// Default subnets (a/b/c)
String[] azSuffixes = {"a", "b", "c"};
String[] azSuffixes = MODELLED_ZONE_SUFFIXES;
String[] cidrBlocks = {"172.31.0.0/20", "172.31.16.0/20", "172.31.32.0/20"};
String[] subnetIds = {
defaultSubnetId(region, azSuffixes[0]),
@@ -441,7 +448,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
subnet.setCidrBlock(cidrBlocks[i]);
subnet.setState("available");
subnet.setAvailabilityZone(region + azSuffixes[i]);
subnet.setAvailabilityZoneId(region + "-az" + (i + 1));
subnet.setAvailabilityZoneId(zoneIdForZoneName(region, region + azSuffixes[i]));
subnet.setAvailableIpAddressCount(4091);
subnet.setDefaultForAz(true);
subnet.setMapPublicIpOnLaunch(true);
@@ -3020,21 +3027,91 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// ─── Subnets ───────────────────────────────────────────────────────────────
/**
* The zone id Floci publishes for a zone name. AWS's real zone ids are opaque and per-account
* ({@code use1-az4}); Floci derives a deterministic one instead, and what matters is that
* every surface derives it the same way, DescribeAvailabilityZones, the seeded default
* subnets and CreateSubnet all come through here, so a subnet's zone id agrees with the zone
* list a client just read. A zone name that is not this region's {@code <region><letter>}
* keeps the first zone's id, which is what an unrecognised name resolved to before.
*/
static String zoneIdForZoneName(String region, String zoneName) {
if (zoneName != null && zoneName.length() == region.length() + 1 && zoneName.startsWith(region)) {
char letter = Character.toLowerCase(zoneName.charAt(zoneName.length() - 1));
if (letter >= 'a' && letter <= 'z') {
return region + "-az" + (letter - 'a' + 1);
}
}
return region + "-az1";
}
/**
* Resolves the zone a new subnet lands in from whichever of AvailabilityZone and
* AvailabilityZoneId the caller supplied. Terraform's aws_subnet exposes both
* ({@code availability_zone} and {@code availability_zone_id}) and forbids setting both at
* once, so in practice exactly one arrives; a pair that disagrees is refused rather than
* silently resolved to one of them.
*/
private String resolveSubnetZoneName(String region, String availabilityZone, String availabilityZoneId) {
if (!isSet(availabilityZoneId)) {
return isSet(availabilityZone) ? availabilityZone : region + "a";
}
String fromId = zoneNameForZoneId(region, availabilityZoneId);
if (isSet(availabilityZone) && !availabilityZone.equals(fromId)) {
throw new AwsException("InvalidParameterCombination",
"The availability zone '" + availabilityZone + "' does not match the availability zone ID '"
+ availabilityZoneId + "'", 400);
}
return fromId;
}
/**
* The inverse of {@link #zoneIdForZoneName}. An id outside the zones this region publishes is
* refused rather than resolved: placing a subnet in a zone DescribeAvailabilityZones does not
* list would leave a client unable to pair the two, which is the same inconsistency the
* hardcoded az1 produced, only quieter.
*/
private static String zoneNameForZoneId(String region, String availabilityZoneId) {
String prefix = region + "-az";
if (availabilityZoneId.startsWith(prefix)) {
try {
int index = Integer.parseInt(availabilityZoneId.substring(prefix.length()));
if (index >= 1 && index <= MODELLED_ZONE_SUFFIXES.length) {
return region + MODELLED_ZONE_SUFFIXES[index - 1];
}
} catch (NumberFormatException e) {
LOG.debugv("Availability zone ID {0} has a non-numeric zone index: {1}",
availabilityZoneId, e.getMessage());
}
}
throw new AwsException("InvalidParameterValue",
"Invalid availability zone ID: '" + availabilityZoneId + "'. This region publishes "
+ MODELLED_ZONE_SUFFIXES.length + " availability zones, " + prefix + "1 to "
+ prefix + MODELLED_ZONE_SUFFIXES.length + ".", 400);
}
public Subnet createSubnet(String region, String vpcId, String cidrBlock, String availabilityZone) {
return createSubnet(region, vpcId, cidrBlock, availabilityZone, null);
}
public Subnet createSubnet(String region, String vpcId, String cidrBlock, String availabilityZone,
String availabilityZoneId) {
if (vpcId == null || vpcId.isBlank()) {
throw new AwsException("MissingParameter", "The request must contain the parameter VpcId", 400);
}
ensureDefaultResources(region);
getRequiredVpc(region, vpcId);
String zoneName = resolveSubnetZoneName(region, availabilityZone, availabilityZoneId);
String subnetId = "subnet-" + randomHex(8);
Subnet subnet = new Subnet();
subnet.setSubnetId(subnetId);
subnet.setVpcId(vpcId);
subnet.setCidrBlock(cidrBlock);
subnet.setState("available");
subnet.setAvailabilityZone(availabilityZone != null ? availabilityZone : region + "a");
subnet.setAvailabilityZoneId(region + "-az1");
subnet.setAvailabilityZone(zoneName);
subnet.setAvailabilityZoneId(zoneIdForZoneName(region, zoneName));
subnet.setAvailableIpAddressCount(251);
subnet.setOwnerId(accountId);
subnet.setRegion(region);
@@ -5387,13 +5464,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
public List<Map<String, String>> describeAvailabilityZones(String region) {
List<Map<String, String>> zones = new ArrayList<>();
String[] azSuffixes = {"a", "b", "c"};
String[] azSuffixes = MODELLED_ZONE_SUFFIXES;
for (String suffix : azSuffixes) {
Map<String, String> az = new LinkedHashMap<>();
az.put("zoneName", region + suffix);
az.put("state", "available");
az.put("regionName", region);
az.put("zoneId", region + "-az" + (suffix.charAt(0) - 'a' + 1));
az.put("zoneId", zoneIdForZoneName(region, region + suffix));
az.put("zoneType", "availability-zone");
zones.add(az);
}
@@ -0,0 +1,181 @@
package io.github.hectorvent.floci.services.ec2;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
/**
* CreateSubnet never read AvailabilityZoneId and hardcoded every new subnet's zone id to the
* region's first zone, so a subnet asked for us-east-1-az2 came back in us-east-1-az1, and even
* a subnet placed correctly by zone <em>name</em> reported a zone id that contradicted it.
* Anything selecting subnets by zone id (Terraform's aws_subnet.availability_zone_id, and any
* client pairing subnets with the DescribeAvailabilityZones list) therefore saw one zone.
*/
@QuarkusTest
class Ec2SubnetAvailabilityZoneIdIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private String vpcId;
@BeforeEach
void createVpc() {
vpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.90.0.0/16")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("CreateVpcResponse.vpc.vpcId");
}
@Test
void theZoneIdFollowsTheZoneTheSubnetWasPlacedIn() {
given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.1.0/24")
.formParam("AvailabilityZone", "us-east-1b")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateSubnetResponse.subnet.availabilityZone", equalTo("us-east-1b"))
.body("CreateSubnetResponse.subnet.availabilityZoneId", equalTo("us-east-1-az2"));
}
@Test
void aSubnetCanBePlacedByZoneIdAlone() {
String subnetId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.2.0/24")
.formParam("AvailabilityZoneId", "us-east-1-az3")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateSubnetResponse.subnet.availabilityZoneId", equalTo("us-east-1-az3"))
.body("CreateSubnetResponse.subnet.availabilityZone", equalTo("us-east-1c"))
.extract().path("CreateSubnetResponse.subnet.subnetId");
// ... and it reads back the same way, not just in the create response.
given()
.formParam("Action", "DescribeSubnets")
.formParam("SubnetId.1", subnetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeSubnetsResponse.subnetSet.item.availabilityZoneId", equalTo("us-east-1-az3"));
}
/**
* The zone id a subnet reports has to be one DescribeAvailabilityZones actually publishes,
* or a client that pairs the two lists finds no subnet in the zone it just looked up.
*/
@Test
void theZoneIdMatchesTheZoneListTheSameClientCanRead() {
given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.3.0/24")
.formParam("AvailabilityZone", "us-east-1c")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateSubnetResponse.subnet.availabilityZoneId", equalTo("us-east-1-az3"));
given()
.formParam("Action", "DescribeAvailabilityZones")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeAvailabilityZonesResponse.availabilityZoneInfo.item.zoneId",
hasItem("us-east-1-az3"));
}
@Test
void aZoneAndZoneIdThatDisagreeAreRefusedRatherThanSilentlyResolved() {
given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.4.0/24")
.formParam("AvailabilityZone", "us-east-1a")
.formParam("AvailabilityZoneId", "us-east-1-az2")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
}
@Test
void aZoneAndZoneIdNamingTheSameZoneAreAccepted() {
given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.5.0/24")
.formParam("AvailabilityZone", "us-east-1b")
.formParam("AvailabilityZoneId", "us-east-1-az2")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("CreateSubnetResponse.subnet.availabilityZone", equalTo("us-east-1b"));
}
/**
* A well-formed id past the zones this region publishes is refused too. Resolving it would put
* the subnet in a zone DescribeAvailabilityZones does not list, leaving a client unable to
* pair the two: the same inconsistency the hardcoded az1 produced, only quieter.
*/
@Test
void aZoneIdBeyondThePublishedZonesIsRefused() {
given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.7.0/24")
.formParam("AvailabilityZoneId", "us-east-1-az4")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"))
.body("Response.Errors.Error.Message", containsString("us-east-1-az1 to us-east-1-az3"));
}
@Test
void aMalformedZoneIdIsRefused() {
given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", vpcId)
.formParam("CidrBlock", "10.90.6.0/24")
.formParam("AvailabilityZoneId", "not-a-zone-id")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
}
}