diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index 7b4142d1d7..6040948193 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -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 params, String region) { @@ -4408,7 +4411,9 @@ public class Ec2QueryHandler { } private Response handleDescribeRegions(MultivaluedMap 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 requested = new HashSet<>(getList(p, "RegionName")); XmlBuilder xml = new XmlBuilder() diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java index 3e31cb0ec5..0b4c5bf0c8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java @@ -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()); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java new file mode 100644 index 0000000000..b39562af47 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java @@ -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 names = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName"); + List 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 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 getConfigOverrides() { + return Map.of( + "floci.default-region", "cn-north-1", + "floci.partitions.allow-unknown-regions", "true"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java index 50a7d9ebf9..0e5aa18b02 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandlerTest.java @@ -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("cn-north-1"), xml); + assertTrue(xml.contains("ec2.cn-north-1.amazonaws.com.cn"), 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("ec2.us-gov-west-1.amazonaws.com"), 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 createSubnetParams(String cidrBlock) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesPublicIpParamTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesPublicIpParamTest.java index 7aeae110a8..87a55bbc02 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesPublicIpParamTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesPublicIpParamTest.java @@ -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 associatePublicIp = ArgumentCaptor.forClass(Boolean.class);