fix(ec2): list the request partition's regions in DescribeRegions (#5550)

With floci.partitions.allow-unknown-regions on, a request signed for a
region no partition publishes is served in the deployment's partition,
and its ARNs are minted there. DescribeRegions picked its partition
with forRegionOrCommercial instead, so on a China deployment such a
request listed the commercial regions, and disagreed with the Step
Functions aws-sdk:ec2:describeRegions integration.

The handler now asks RegionResolver for the partition the request is
scoped to. A published region still lists its own partition.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-12 22:59:08 -04:00
1 parent d55b830a39
commit b4758b0018
5 files changed
+112 -5

No files matched your search

@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.AwsPartition;
import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.AwsRegionFacts;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.services.ec2.model.*;
import jakarta.enterprise.context.ApplicationScoped;
@@ -45,18 +46,20 @@ public class Ec2QueryHandler {
private final Ec2EbsEncryptionService ebsEncryptionService;
private final Ec2SnapshotBlockPublicAccessService snapshotBlockPublicAccessService;
private final Ec2IpamService ipamService;
private final RegionResolver regionResolver;
@Inject
public Ec2QueryHandler(Ec2Service service, EmulatorConfig config, FlowLogService flowLogService,
Ec2EbsEncryptionService ebsEncryptionService,
Ec2SnapshotBlockPublicAccessService snapshotBlockPublicAccessService,
Ec2IpamService ipamService) {
Ec2IpamService ipamService, RegionResolver regionResolver) {
this.service = service;
this.config = config;
this.flowLogService = flowLogService;
this.ebsEncryptionService = ebsEncryptionService;
this.snapshotBlockPublicAccessService = snapshotBlockPublicAccessService;
this.ipamService = ipamService;
this.regionResolver = regionResolver;
}
public Response handle(String action, MultivaluedMap<String, String> params, String region) {
@@ -4408,7 +4411,9 @@ public class Ec2QueryHandler {
}
private Response handleDescribeRegions(MultivaluedMap<String, String> p, String region) {
AwsPartition partition = AwsPartitions.forRegionOrCommercial(region);
// The partition the request is scoped to, not the region's own: a region no partition
// publishes is served in the deployment's partition, where its ARNs are minted too.
AwsPartition partition = AwsPartitions.byId(regionResolver.partitionForRegion(region));
boolean allRegions = Boolean.parseBoolean(p.getFirst("AllRegions"));
Set<String> requested = new HashSet<>(getList(p, "RegionName"));
XmlBuilder xml = new XmlBuilder()
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
@@ -51,7 +52,8 @@ class Ec2CreateFleetRollbackTest {
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class));
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
mock(RegionResolver.class));
Response response = handler.handle("CreateFleet", params(), REGION);
assertEquals(400, response.getStatus());
@@ -0,0 +1,64 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.testing.PartitionMatrix;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* A China deployment that allows unknown regions serves a region no partition publishes in its
* own partition, so DescribeRegions lists the China regions for it, not the commercial ones.
*/
@QuarkusTest
@TestProfile(Ec2DescribeRegionsUnknownRegionIntegrationTest.ChinaAllowUnknownRegionsProfile.class)
class Ec2DescribeRegionsUnknownRegionIntegrationTest {
private static final String UNKNOWN = "polygondwanaland-west-1";
private Response describeRegions(String region) {
return given()
.header("Authorization", PartitionMatrix.sigV4Auth(region, "ec2"))
.formParam("Action", "DescribeRegions")
.formParam("Version", "2016-11-15")
.when().post("/").then().statusCode(200).contentType("application/xml")
.extract().response();
}
@Test
void anUnknownRegionListsTheDeploymentPartitionsRegions() {
Response response = describeRegions(UNKNOWN);
List<String> names = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName");
List<String> endpoints = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionEndpoint");
assertTrue(names.contains("cn-north-1"), names.toString());
assertFalse(names.contains("us-east-1"), names.toString());
assertTrue(endpoints.contains("ec2.cn-north-1.amazonaws.com.cn"), endpoints.toString());
}
@Test
void aPublishedRegionStillListsItsOwnPartition() {
Response response = describeRegions("us-east-1");
List<String> names = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName");
assertTrue(names.contains("us-east-1"), names.toString());
assertFalse(names.contains("cn-north-1"), names.toString());
}
public static final class ChinaAllowUnknownRegionsProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.default-region", "cn-north-1",
"floci.partitions.allow-unknown-regions", "true");
}
}
}
@@ -1,6 +1,8 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsPartition;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
@@ -15,10 +17,13 @@ import org.mockito.ArgumentCaptor;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
@@ -184,11 +189,40 @@ class Ec2QueryHandlerTest {
assertEquals(cleanupFails ? List.of(cleanupFailure) : List.of(), List.of(thrown.getSuppressed()));
}
@Test
void describeRegionsListsTheDeploymentPartitionForARegionNoPartitionPublishes() {
String xml = describeRegions(new RegionResolver("cn-north-1", "000000000000"), "polygondwanaland-west-1");
assertTrue(xml.contains("<regionName>cn-north-1</regionName>"), xml);
assertTrue(xml.contains("<regionEndpoint>ec2.cn-north-1.amazonaws.com.cn</regionEndpoint>"), xml);
assertFalse(xml.contains("us-east-1"), xml);
}
@Test
void describeRegionsKeepsAPublishedRegionInItsOwnPartition() {
String xml = describeRegions(new RegionResolver("cn-north-1", "000000000000"), "us-gov-west-1");
assertTrue(xml.contains("<regionEndpoint>ec2.us-gov-west-1.amazonaws.com</regionEndpoint>"), xml);
assertFalse(xml.contains("cn-north-1"), xml);
}
private String describeRegions(RegionResolver regionResolver, String region) {
Ec2Service service = mock(Ec2Service.class);
when(service.describeRegions(any(AwsPartition.class), anyBoolean())).thenCallRealMethod();
Response response = handler(service, regionResolver)
.handle("DescribeRegions", new MultivaluedHashMap<>(), region);
return (String) response.getEntity();
}
private Ec2QueryHandler handler(Ec2Service service) {
return handler(service, new RegionResolver("us-east-1", "000000000000"));
}
private Ec2QueryHandler handler(Ec2Service service, RegionResolver regionResolver) {
return new Ec2QueryHandler(
service, mock(EmulatorConfig.class), mock(FlowLogService.class),
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
mock(Ec2IpamService.class));
mock(Ec2IpamService.class), regionResolver);
}
private MultivaluedMap<String, String> createSubnetParams(String cidrBlock) {
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import jakarta.ws.rs.core.MultivaluedHashMap;
@@ -66,7 +67,8 @@ class Ec2RunInstancesPublicIpParamTest {
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class));
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
mock(RegionResolver.class));
handler.handle("RunInstances", p, REGION);
ArgumentCaptor<Boolean> associatePublicIp = ArgumentCaptor.forClass(Boolean.class);