mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(core): partition foundation from botocore's published partition data (#4317)
P1 of the multi-partition initiative (#4272). tools/aws/regen_partitions.py vendors botocore's partitions.json and endpoints.json (8 partitions, 46 regions, the partition-wide endpoints with their signing regions) plus the CDK's opt-in and S3 website rules into src/main/resources/aws/partitions.json, checked by make aws-data-check. AwsPartition and AwsPartitions replace the hand-typed rules behind AwsRegions; RequestContext carries the request's partition from its SigV4 signing region; RegionResolver mints global ARNs in that partition and buildArn routes a blank region there; floci.partitions.id pins the deployment's partition behind a startup validator that refuses a contradiction with the default region. Behaviour a reviewer will see, all AWS-faithful: DescribeRegions follows the request's partition and the opt-in model (17 commercial regions by default, all 34 with AllRegions=true, RegionName.N filters, endpoints on the partition's DNS suffix); TLS SANs cover every published region; the EKS registry mirrors follow the deployment partition's regions. The partition-literal baseline drops by two. Part of #4272
This commit is contained in:
1 parent
e73b80d5ee
commit
0dd080a46f
39 files changed
+3946
-152
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsPartition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PartitionStartupValidatorTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"us-east-1, aws",
|
||||
"cn-north-1, aws-cn",
|
||||
"us-gov-west-1, aws-us-gov",
|
||||
"us-iso-east-1, aws-iso",
|
||||
"us-isob-east-1, aws-iso-b",
|
||||
"eu-isoe-west-1, aws-iso-e",
|
||||
"us-isof-south-1, aws-iso-f",
|
||||
"eusc-de-east-1, aws-eusc"})
|
||||
void thePartitionIsDerivedFromTheDefaultRegion(String region, String partition) {
|
||||
AwsPartition resolved = PartitionStartupValidator.validate(region, Optional.empty());
|
||||
assertEquals(partition, resolved.id());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitPartitionThatAgreesWithTheRegionIsAccepted() {
|
||||
assertEquals("aws-cn", PartitionStartupValidator.validate("cn-north-1", Optional.of("aws-cn")).id());
|
||||
assertEquals("aws-cn", PartitionStartupValidator.validate("cn-north-1", Optional.of(" AWS-CN ")).id());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownPartitionIdIsRefused() {
|
||||
IllegalStateException error = assertThrows(IllegalStateException.class,
|
||||
() -> PartitionStartupValidator.validate("us-east-1", Optional.of("aws-mars")));
|
||||
assertTrue(error.getMessage().contains("aws-mars"));
|
||||
assertTrue(error.getMessage().contains("aws-cn"), "the message lists the published partitions");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPartitionThatContradictsARecognisedRegionIsRefused() {
|
||||
IllegalStateException error = assertThrows(IllegalStateException.class,
|
||||
() -> PartitionStartupValidator.validate("cn-north-1", Optional.of("aws")));
|
||||
assertTrue(error.getMessage().contains("cn-north-1"));
|
||||
assertTrue(error.getMessage().contains("aws-cn"));
|
||||
}
|
||||
|
||||
/** A region the vendored data does not know only warns; the explicit partition wins. */
|
||||
@Test
|
||||
void anUnpublishedRegionIsAcceptedWithAWarning() {
|
||||
assertEquals("aws-us-gov", PartitionStartupValidator.validate("us-gov-north-9", Optional.of("aws-us-gov")).id());
|
||||
assertEquals("aws", PartitionStartupValidator.validate("xx-nowhere-9", Optional.empty()).id());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlankPartitionIdMeansUnset() {
|
||||
assertEquals("aws-us-gov", PartitionStartupValidator.validate("us-gov-west-1", Optional.of(" ")).id());
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,46 @@ class AccountContextFilterTest {
|
||||
filter.filter(ctx);
|
||||
assertEquals(DEFAULT_ACCOUNT, requestContext.getAccountId());
|
||||
assertEquals(DEFAULT_REGION, requestContext.getRegion());
|
||||
assertEquals("aws", requestContext.getPartition());
|
||||
}
|
||||
|
||||
/** The signing region carries the partition: a China-signed request is a China request. */
|
||||
@Test
|
||||
void partitionFollowsTheSigningRegionOfTheAuthorizationHeader() {
|
||||
filter.filter(mockContext(
|
||||
"AWS4-HMAC-SHA256 Credential=000000000001/20260617/cn-north-1/iam/aws4_request, SignedHeaders=host, Signature=abc",
|
||||
null));
|
||||
assertEquals("cn-north-1", requestContext.getRegion());
|
||||
assertEquals("aws-cn", requestContext.getPartition());
|
||||
|
||||
filter.filter(mockContext(
|
||||
"AWS4-HMAC-SHA256 Credential=000000000001/20260617/eusc-de-east-1/sqs/aws4_request, SignedHeaders=host, Signature=abc",
|
||||
null));
|
||||
assertEquals("aws-eusc", requestContext.getPartition());
|
||||
|
||||
filter.filter(mockContext(
|
||||
"AWS4-HMAC-SHA256 Credential=000000000001/20260617/aws-cn-global/iam/aws4_request, SignedHeaders=host, Signature=abc",
|
||||
null));
|
||||
assertEquals("aws-cn-global", requestContext.getRegion());
|
||||
assertEquals("aws-cn", requestContext.getPartition());
|
||||
}
|
||||
|
||||
@Test
|
||||
void partitionFollowsThePresignedCredentialRegion() {
|
||||
filter.filter(mockContext(null, "000000000002/20260617/us-gov-west-1/s3/aws4_request"));
|
||||
assertEquals("us-gov-west-1", requestContext.getRegion());
|
||||
assertEquals("aws-us-gov", requestContext.getPartition());
|
||||
}
|
||||
|
||||
/** With no credential the request belongs to the deployment's partition, whatever it is. */
|
||||
@Test
|
||||
void anUnsignedRequestGetsTheDeploymentPartition() {
|
||||
RegionResolver china = new RegionResolver("cn-north-1", DEFAULT_ACCOUNT);
|
||||
AccountContextFilter chinaFilter = new AccountContextFilter(accountResolver, china, requestContext,
|
||||
akid -> Optional.empty());
|
||||
chinaFilter.filter(mockContext(null, null));
|
||||
assertEquals("cn-north-1", requestContext.getRegion());
|
||||
assertEquals("aws-cn", requestContext.getPartition());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -21,20 +21,29 @@ class AwsArnOwnershipTest {
|
||||
@Test
|
||||
void awsPartitionIsNotForeign() {
|
||||
assertFalse(AwsArnUtils.isForeignPartition(
|
||||
arn("arn:aws:lambda:us-east-1:000000000000:layer:l:1")));
|
||||
arn("arn:aws:lambda:us-east-1:000000000000:layer:l:1"), "aws"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherPartitionsAreForeign() {
|
||||
assertTrue(AwsArnUtils.isForeignPartition(
|
||||
arn("arn:aws-cn:lambda:cn-north-1:000000000000:layer:l:1")));
|
||||
arn("arn:aws-cn:lambda:cn-north-1:000000000000:layer:l:1"), "aws"));
|
||||
assertTrue(AwsArnUtils.isForeignPartition(
|
||||
arn("arn:aws-us-gov:lambda:us-gov-west-1:000000000000:layer:l:1")));
|
||||
arn("arn:aws-us-gov:lambda:us-gov-west-1:000000000000:layer:l:1"), "aws"));
|
||||
}
|
||||
|
||||
/** Foreign is relative to the caller's partition: a China ARN is local to a China request. */
|
||||
@Test
|
||||
void theCallersOwnPartitionIsNotForeign() {
|
||||
assertFalse(AwsArnUtils.isForeignPartition(
|
||||
arn("arn:aws-cn:lambda:cn-north-1:000000000000:layer:l:1"), "aws-cn"));
|
||||
assertTrue(AwsArnUtils.isForeignPartition(
|
||||
arn("arn:aws:lambda:us-east-1:000000000000:layer:l:1"), "aws-cn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmptyPartitionIsNotForeign() {
|
||||
assertFalse(AwsArnUtils.isForeignPartition(arn("arn::lambda:us-east-1:000000000000:layer:l:1")));
|
||||
assertFalse(AwsArnUtils.isForeignPartition(arn("arn::lambda:us-east-1:000000000000:layer:l:1"), "aws"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,7 +67,7 @@ class AwsArnOwnershipTest {
|
||||
|
||||
@Test
|
||||
void nullIsNotForeign() {
|
||||
assertFalse(AwsArnUtils.isForeignPartition(null));
|
||||
assertFalse(AwsArnUtils.isForeignPartition(null, "aws"));
|
||||
assertFalse(AwsArnUtils.isForeignAccount(null, LOCAL));
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,10 @@ package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix;
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix.PartitionCase;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -140,8 +143,8 @@ class AwsArnUtilsTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Global services pass no region, and they keep the commercial partition. Thirty call sites
|
||||
* do this; anything else would rewrite every IAM, S3 and CloudFront ARN the emulator mints.
|
||||
* A regionless {@code Arn.of} keeps the commercial partition: nothing in the region argument
|
||||
* can say otherwise. Call sites that know the request's partition use {@link AwsArnUtils.Arn#global}.
|
||||
*/
|
||||
@Test
|
||||
void aRegionlessArnStaysInTheCommercialPartition() {
|
||||
@@ -149,6 +152,20 @@ class AwsArnUtilsTest {
|
||||
AwsArnUtils.Arn.of("iam", "", "000000000000", "role/r").toString());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")
|
||||
void aGlobalArnCarriesTheGivenPartitionAndNoRegion(PartitionCase partitionCase) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.Arn.global(partitionCase.partition(), "iam", "000000000000", "role/r");
|
||||
assertEquals("arn:" + partitionCase.partition() + ":iam::000000000000:role/r", arn.toString());
|
||||
PartitionMatrix.assertGlobalArnIn(partitionCase, arn.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPseudoRegionResolvesToItsPartitionWhenMinting() {
|
||||
assertEquals("arn:aws-cn:sqs:aws-cn-global:000000000000:q",
|
||||
AwsArnUtils.Arn.of("sqs", "aws-cn-global", "000000000000", "q").toString());
|
||||
}
|
||||
|
||||
/** A region AWS has not launched yet must not fail closed. */
|
||||
@Test
|
||||
void anUnrecognisedRegionFallsBackToTheCommercialPartition() {
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix;
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix.PartitionCase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The vendored partition catalog ({@code aws/partitions.json}) and the lookups over it. The
|
||||
* shape assertions pin what {@code tools/aws/regen_partitions.py} produces from botocore; the
|
||||
* lookup assertions pin the strict-then-fail-open resolution every ARN and hostname relies on.
|
||||
*/
|
||||
class AwsPartitionsTest {
|
||||
|
||||
private static final Set<String> PUBLISHED_PARTITIONS = Set.of(
|
||||
"aws", "aws-cn", "aws-us-gov", "aws-iso", "aws-iso-b", "aws-iso-e", "aws-iso-f", "aws-eusc");
|
||||
|
||||
@Test
|
||||
void allEightPartitionsArePresentExactlyOnce() {
|
||||
assertEquals(PUBLISHED_PARTITIONS, AwsPartitions.ids());
|
||||
assertEquals(8, AwsPartitions.all().size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")
|
||||
void everyMatrixRegionResolvesToItsPartitionWithItsDnsSuffix(PartitionCase partitionCase) {
|
||||
AwsPartition partition = AwsPartitions.forRegion(partitionCase.region()).orElseThrow();
|
||||
assertEquals(partitionCase.partition(), partition.id());
|
||||
assertEquals(partitionCase.dnsSuffix(), partition.dnsSuffix());
|
||||
assertTrue(partition.isPublishedRegion(partitionCase.region()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Membership and botocore's regionRegex must agree for every published region, or a region
|
||||
* would resolve one way from the list and another way once the list is stale.
|
||||
*/
|
||||
@Test
|
||||
void regionRegexAgreesWithMembershipForEveryPublishedRegion() {
|
||||
for (AwsPartition partition : AwsPartitions.all()) {
|
||||
for (String region : partition.regionIds()) {
|
||||
assertTrue(partition.matchesRegionRegex(region),
|
||||
region + " is listed under " + partition.id() + " but does not match its regionRegex");
|
||||
for (AwsPartition other : AwsPartitions.all()) {
|
||||
if (!other.id().equals(partition.id())) {
|
||||
assertFalse(other.matchesRegionRegex(region),
|
||||
region + " matches the regionRegex of both " + partition.id() + " and " + other.id());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyKnownRegionIdBelongsToExactlyOnePartition() {
|
||||
for (String region : AwsPartitions.allRegionIds()) {
|
||||
long owners = AwsPartitions.all().stream().filter(p -> p.isPublishedRegion(region)).count();
|
||||
assertEquals(1, owners, region);
|
||||
assertEquals(AwsPartitions.forRegion(region).orElseThrow().id(),
|
||||
AwsPartitions.all().stream().filter(p -> p.isPublishedRegion(region)).findFirst().orElseThrow().id());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCommercialPartitionPublishesThirtyFourRegionsAndChinaTwo() {
|
||||
assertEquals(34, AwsPartitions.commercial().regions().size());
|
||||
assertEquals(List.of("cn-north-1", "cn-northwest-1"), AwsPartitions.byId("aws-cn").regionIds());
|
||||
assertEquals(List.of("us-gov-east-1", "us-gov-west-1"), AwsPartitions.byId("aws-us-gov").regionIds());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"aws-global, aws, us-east-1",
|
||||
"aws-cn-global, aws-cn, cn-northwest-1",
|
||||
"aws-us-gov-global, aws-us-gov, us-gov-west-1",
|
||||
"aws-iso-global, aws-iso, us-iso-east-1",
|
||||
"aws-iso-b-global, aws-iso-b, us-isob-east-1",
|
||||
"aws-iso-e-global, aws-iso-e, eu-isoe-west-1",
|
||||
"aws-iso-f-global, aws-iso-f, us-isof-south-1"})
|
||||
void pseudoRegionsResolveToTheirPartitionAndNormalizeToItsImplicitGlobalRegion(
|
||||
String pseudo, String partition, String implicitGlobalRegion) {
|
||||
assertEquals(partition, AwsPartitions.forRegion(pseudo).orElseThrow().id());
|
||||
assertEquals(implicitGlobalRegion, AwsPartitions.normalizeRegion(pseudo));
|
||||
assertEquals(implicitGlobalRegion, AwsPartitions.byId(partition).implicitGlobalRegion());
|
||||
assertTrue(AwsPartitions.byId(partition).isPseudoRegion(pseudo));
|
||||
assertFalse(AwsPartitions.isPublishedRegion(pseudo), "a pseudo-region is not a published region");
|
||||
}
|
||||
|
||||
/** botocore publishes no {@code aws-eusc-global}, and EUSC has no partition-wide service at all. */
|
||||
@Test
|
||||
void euscHasNoPseudoRegionAndNoGlobalServices() {
|
||||
AwsPartition eusc = AwsPartitions.byId("aws-eusc");
|
||||
assertTrue(eusc.pseudoRegion().isEmpty());
|
||||
assertTrue(eusc.globalServices().isEmpty());
|
||||
assertTrue(AwsPartitions.forRegion("aws-eusc-global").isEmpty());
|
||||
}
|
||||
|
||||
/** The global STS host exists only in the commercial partition; everywhere else STS is regional. */
|
||||
@Test
|
||||
void onlyTheCommercialPartitionHasGlobalSts() {
|
||||
for (AwsPartition partition : AwsPartitions.all()) {
|
||||
assertEquals("aws".equals(partition.id()), partition.hasGlobalSts(), partition.id());
|
||||
}
|
||||
AwsPartition.GlobalEndpoint sts = AwsPartitions.commercial().globalEndpoint("sts").orElseThrow();
|
||||
assertEquals("sts.amazonaws.com", sts.hostname());
|
||||
assertEquals("us-east-1", sts.signingRegion());
|
||||
assertTrue(sts.regionalized(), "STS keeps per-region endpoints beside the global host");
|
||||
}
|
||||
|
||||
/** China IAM signs {@code cn-north-1} although the partition's implicit global region is Ningxia. */
|
||||
@Test
|
||||
void globalEndpointsCarryTheirOwnSigningRegion() {
|
||||
AwsPartition china = AwsPartitions.byId("aws-cn");
|
||||
AwsPartition.GlobalEndpoint iam = china.globalEndpoint("iam").orElseThrow();
|
||||
assertEquals("iam.cn-north-1.amazonaws.com.cn", iam.hostname());
|
||||
assertEquals("cn-north-1", iam.signingRegion());
|
||||
assertEquals("cn-northwest-1", china.globalEndpoint("route53").orElseThrow().signingRegion());
|
||||
assertEquals("us-gov-west-1", AwsPartitions.byId("aws-us-gov").globalEndpoint("iam").orElseThrow().signingRegion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceAvailabilityFollowsEndpointsJson() {
|
||||
assertTrue(AwsPartitions.commercial().offers("cloudfront"));
|
||||
assertTrue(AwsPartitions.byId("aws-cn").offers("cloudfront"));
|
||||
assertFalse(AwsPartitions.byId("aws-us-gov").offers("cloudfront"));
|
||||
assertFalse(AwsPartitions.byId("aws-eusc").offers("iam"));
|
||||
assertFalse(AwsPartitions.byId("aws-iso-e").offers("iam"));
|
||||
assertFalse(AwsPartitions.byId("aws-cn").offers("cognito-idp"));
|
||||
assertFalse(AwsPartitions.commercial().offers(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void optInAndWebsiteFormFlagsComeFromTheCdkRules() {
|
||||
AwsPartition aws = AwsPartitions.commercial();
|
||||
assertFalse(aws.region("us-east-1").orElseThrow().optIn());
|
||||
assertFalse(aws.region("eu-north-1").orElseThrow().optIn(), "the last region before opt-in began");
|
||||
assertTrue(aws.region("ap-east-1").orElseThrow().optIn());
|
||||
assertTrue(aws.region("il-central-1").orElseThrow().optIn());
|
||||
assertTrue(aws.region("us-east-1").orElseThrow().s3WebsiteDashForm());
|
||||
assertTrue(aws.region("us-west-2").orElseThrow().s3WebsiteDashForm());
|
||||
assertTrue(aws.region("ap-southeast-2").orElseThrow().s3WebsiteDashForm());
|
||||
assertFalse(aws.region("eu-central-1").orElseThrow().s3WebsiteDashForm());
|
||||
assertFalse(aws.region("ap-east-1").orElseThrow().s3WebsiteDashForm());
|
||||
assertTrue(AwsPartitions.byId("aws-us-gov").region("us-gov-west-1").orElseThrow().s3WebsiteDashForm());
|
||||
assertFalse(AwsPartitions.byId("aws-cn").region("cn-north-1").orElseThrow().optIn());
|
||||
assertEquals(17, aws.regions().stream().filter(AwsPartition.Region::optIn).count());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
@ValueSource(strings = {" ", "xx-nowhere-9", "not-a-region", "us-gov", "cn-north", "aws-eusc-global"})
|
||||
void unplaceableLabelsAreEmptyStrictlyAndCommercialLeniently(String region) {
|
||||
assertTrue(AwsPartitions.forRegion(region).isEmpty());
|
||||
assertEquals("aws", AwsPartitions.forRegionOrCommercial(region).id());
|
||||
}
|
||||
|
||||
/** A region AWS launches after the vendored data is placed by botocore's shape rule. */
|
||||
@Test
|
||||
void anUnlistedRegionOfAKnownShapeResolvesByRegex() {
|
||||
assertEquals("aws-cn", AwsPartitions.forRegion("cn-southwest-9").orElseThrow().id());
|
||||
assertEquals("aws-us-gov", AwsPartitions.forRegion("us-gov-north-3").orElseThrow().id());
|
||||
assertEquals("aws", AwsPartitions.forRegion("eu-nowhere-7").orElseThrow().id());
|
||||
assertFalse(AwsPartitions.isPublishedRegion("eu-nowhere-7"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lookupsAreCaseInsensitive() {
|
||||
assertEquals("aws-cn", AwsPartitions.forRegion("CN-NORTH-1").orElseThrow().id());
|
||||
assertEquals("aws-cn", AwsPartitions.byId("AWS-CN").id());
|
||||
assertEquals("cn-north-1", AwsPartitions.normalizeRegion(" CN-North-1 "));
|
||||
assertEquals(Optional.empty(), Optional.ofNullable(AwsPartitions.normalizeRegion(null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownPartitionIdsThrow() {
|
||||
assertThrows(IllegalArgumentException.class, () -> AwsPartitions.byId("aws-mars"));
|
||||
assertTrue(AwsPartitions.find("aws-mars").isEmpty());
|
||||
assertTrue(AwsPartitions.find(null).isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code amazonaws.com.cn} must win over {@code amazonaws.com} or a China host loses its
|
||||
* {@code .cn} and is read as commercial; the dual-stack suffixes are published too.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"bucket.s3.cn-north-1.amazonaws.com.cn, bucket.s3.cn-north-1, amazonaws.com.cn",
|
||||
"bucket.s3.us-east-1.amazonaws.com, bucket.s3.us-east-1, amazonaws.com",
|
||||
"s3.us-iso-east-1.c2s.ic.gov, s3.us-iso-east-1, c2s.ic.gov",
|
||||
"s3.eusc-de-east-1.amazonaws.eu, s3.eusc-de-east-1, amazonaws.eu",
|
||||
"dynamodb.us-east-1.api.aws, dynamodb.us-east-1, api.aws",
|
||||
"sqs.cn-north-1.api.amazonwebservices.com.cn, sqs.cn-north-1, api.amazonwebservices.com.cn",
|
||||
"BUCKET.S3.CN-NORTH-1.AMAZONAWS.COM.CN, bucket.s3.cn-north-1, amazonaws.com.cn"})
|
||||
void stripKnownDnsSuffixTakesTheLongestSuffixFirst(String host, String prefix, String suffix) {
|
||||
AwsPartitions.DnsSuffixMatch match = AwsPartitions.stripKnownDnsSuffix(host).orElseThrow();
|
||||
assertEquals(prefix, match.prefix());
|
||||
assertEquals(suffix, match.dnsSuffix());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
@ValueSource(strings = {"amazonaws.com", "localhost:4566", "bucket.localhost", "example.com", "s3.amazonaws.com.evil"})
|
||||
void hostsWithoutAPublishedSuffixDoNotStrip(String host) {
|
||||
assertTrue(AwsPartitions.stripKnownDnsSuffix(host).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedCatalogFailsLoudly() {
|
||||
assertThrows(IllegalStateException.class, () -> AwsPartitions.parse(
|
||||
new ByteArrayInputStream("{\"partitions\": []}".getBytes(StandardCharsets.UTF_8))));
|
||||
assertThrows(IllegalStateException.class, () -> AwsPartitions.parse(new ByteArrayInputStream(
|
||||
"{\"partitions\": [{\"id\": \"aws-cn\", \"name\": \"x\", \"dnsSuffix\": \"a\", \"dualStackDnsSuffix\": \"b\", \"implicitGlobalRegion\": \"c\", \"regionRegex\": \"d\"}]}"
|
||||
.getBytes(StandardCharsets.UTF_8))), "a catalog without the commercial partition");
|
||||
assertThrows(IllegalStateException.class, () -> AwsPartitions.parse(new ByteArrayInputStream(
|
||||
"{\"partitions\": [{\"id\": \"aws\", \"name\": \"x\"}]}".getBytes(StandardCharsets.UTF_8))),
|
||||
"a partition missing required fields");
|
||||
}
|
||||
|
||||
@Test
|
||||
void partitionMatrixCoversEveryPublishedPartition() {
|
||||
assertEquals(PUBLISHED_PARTITIONS,
|
||||
Set.copyOf(PartitionMatrix.all().stream().map(PartitionCase::partition).toList()));
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
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.assertTrue;
|
||||
@@ -97,17 +99,29 @@ class AwsRegionsTest {
|
||||
assertEquals("aws-cn", AwsRegions.partitionFor("CN-NORTH-1"));
|
||||
}
|
||||
|
||||
/** Every GovCloud and China id the emulator recognises must land outside {@code aws}. */
|
||||
/** Every known id resolves to a partition whose published regions contain it. */
|
||||
@Test
|
||||
void knownNonCommercialIdsAreNotCommercial() {
|
||||
void everyKnownIdResolvesToThePartitionThatPublishesIt() {
|
||||
for (String region : AwsRegions.KNOWN_IDS) {
|
||||
if (region.startsWith("us-gov-")) {
|
||||
assertEquals("aws-us-gov", AwsRegions.partitionFor(region), region);
|
||||
} else if (region.startsWith("cn-")) {
|
||||
assertEquals("aws-cn", AwsRegions.partitionFor(region), region);
|
||||
} else {
|
||||
assertEquals("aws", AwsRegions.partitionFor(region), region);
|
||||
}
|
||||
String partition = AwsRegions.partitionFor(region);
|
||||
assertTrue(AwsRegions.advertised(partition).contains(region),
|
||||
region + " resolves to " + partition + ", which does not publish it");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void advertisedListsArePerPartition() {
|
||||
assertEquals(AwsRegions.ALL, AwsRegions.advertised("aws"));
|
||||
assertEquals(34, AwsRegions.ALL.size());
|
||||
assertEquals(List.of("cn-north-1", "cn-northwest-1"), AwsRegions.advertised("aws-cn"));
|
||||
assertTrue(AwsRegions.KNOWN_IDS.contains("eusc-de-east-1"));
|
||||
assertTrue(AwsRegions.KNOWN_IDS.contains("us-isof-south-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pseudoRegionsResolveToTheirPartition() {
|
||||
assertEquals("aws-cn", AwsRegions.partitionFor("aws-cn-global"));
|
||||
assertEquals("amazonaws.com.cn", AwsRegions.dnsSuffixFor("aws-cn-global"));
|
||||
assertEquals("aws-us-gov", AwsRegions.partitionFor("aws-us-gov-global"));
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -172,6 +173,65 @@ class RegionResolverTest {
|
||||
assertNull(resolver.resolveRegionFromHost(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDeploymentPartitionFollowsTheDefaultRegion() {
|
||||
assertEquals("aws", resolver.getDefaultPartition());
|
||||
assertEquals("aws-cn", new RegionResolver("cn-north-1", "000000000000").getDefaultPartition());
|
||||
assertEquals("aws-us-gov", new RegionResolver("us-gov-west-1", "000000000000").getDefaultPartition());
|
||||
assertEquals("aws-iso-b", new RegionResolver("us-isob-east-1", "000000000000", "aws-iso-b").getDefaultPartition());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitPartitionIdOverridesTheRegionDerivedOne() {
|
||||
assertEquals("aws-cn", RegionResolver.effectivePartition("cn-north-1", Optional.empty()));
|
||||
assertEquals("aws-us-gov", RegionResolver.effectivePartition("us-east-1", Optional.of(" AWS-US-GOV ")));
|
||||
assertEquals("aws", RegionResolver.effectivePartition("us-east-1", Optional.of(" ")));
|
||||
}
|
||||
|
||||
/**
|
||||
* The request's partition comes from its signing region. A blank region (no credential) is
|
||||
* the deployment's partition, never a literal {@code aws}; a pseudo-region resolves to its
|
||||
* partition; and a region the catalog cannot place stays local rather than commercial.
|
||||
*/
|
||||
@Test
|
||||
void partitionForRegionScopesARequest() {
|
||||
RegionResolver china = new RegionResolver("cn-north-1", "000000000000");
|
||||
assertEquals("aws-cn", china.partitionForRegion(null));
|
||||
assertEquals("aws-cn", china.partitionForRegion(""));
|
||||
assertEquals("aws-cn", china.partitionForRegion("xx-nowhere-9"));
|
||||
assertEquals("aws", china.partitionForRegion("us-east-1"));
|
||||
assertEquals("aws-us-gov", china.partitionForRegion("us-gov-west-1"));
|
||||
assertEquals("aws-eusc", china.partitionForRegion("eusc-de-east-1"));
|
||||
assertEquals("aws-cn", resolver.partitionForRegion("aws-cn-global"));
|
||||
assertEquals("aws-iso", resolver.partitionForRegion("aws-iso-global"));
|
||||
assertEquals("aws", resolver.partitionForRegion("aws-global"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPartitionOutsideARequestIsTheDeploymentPartition() {
|
||||
assertEquals("aws", resolver.getPartition());
|
||||
assertEquals("aws-cn", new RegionResolver("cn-north-1", "000000000000").getPartition());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildArnRoutesABlankRegionToTheDeploymentPartition() {
|
||||
RegionResolver govCloud = new RegionResolver("us-gov-west-1", "000000000000");
|
||||
assertEquals("arn:aws-us-gov:iam::000000000000:role/r", govCloud.buildArn("iam", "", "role/r"));
|
||||
assertEquals("arn:aws-us-gov:iam::000000000000:role/r", govCloud.buildArn("iam", null, "role/r"));
|
||||
assertEquals("arn:aws-us-gov:s3:::bucket", govCloud.buildGlobalArn("s3", "", "bucket"));
|
||||
assertEquals("arn:aws-us-gov:sqs:us-gov-west-1:000000000000:q", govCloud.buildArn("sqs", "us-gov-west-1", "q"));
|
||||
assertEquals("arn:aws:iam::000000000000:role/r", resolver.buildArn("iam", "", "role/r"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveRegionFromHostAcceptsEveryPartitionsShapeButOnlyPublishedIds() {
|
||||
assertEquals("eusc-de-east-1", resolver.resolveRegionFromHost("abc123.execute-api.eusc-de-east-1.localhost:4566"));
|
||||
assertEquals("us-isob-east-1", resolver.resolveRegionFromHost("abc123.execute-api.us-isob-east-1.localhost"));
|
||||
assertEquals("cn-north-1", resolver.resolveRegionFromHost("abc123.execute-api.cn-north-1.amazonaws.com.cn"));
|
||||
assertNull(resolver.resolveRegionFromHost("abc123.execute-api.xx-nowhere-9.localhost:4566"));
|
||||
assertNull(resolver.resolveRegionFromHost("abc123.execute-api.my-cd-1.localhost:4566"));
|
||||
}
|
||||
|
||||
private static HttpHeaders stubHeaders(String authorizationValue) {
|
||||
return new HttpHeaders() {
|
||||
@Override public List<String> getRequestHeader(String name) {
|
||||
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* DescribeRegions follows the request's partition and AWS's opt-in model: by default only the
|
||||
* regions enabled for the account (every non-opt-in region), all published regions with
|
||||
* {@code AllRegions}, and the endpoints carry the partition's DNS suffix.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class Ec2DescribeRegionsIntegrationTest {
|
||||
|
||||
private Response describeRegions(String region, String... params) {
|
||||
RequestSpecification request = given()
|
||||
.header("Authorization", PartitionMatrix.sigV4Auth(region, "ec2"))
|
||||
.formParam("Action", "DescribeRegions")
|
||||
.formParam("Version", "2016-11-15");
|
||||
for (int i = 0; i + 1 < params.length; i += 2) {
|
||||
request = request.formParam(params[i], params[i + 1]);
|
||||
}
|
||||
return request.when().post("/").then().statusCode(200).contentType("application/xml")
|
||||
.extract().response();
|
||||
}
|
||||
|
||||
@Test
|
||||
void commercialDefaultListsOnlyEnabledRegionsWithCommercialEndpoints() {
|
||||
Response response = describeRegions("us-east-1");
|
||||
List<String> names = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName");
|
||||
List<String> statuses = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.optInStatus");
|
||||
List<String> endpoints = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionEndpoint");
|
||||
|
||||
assertEquals(17, names.size(), "the commercial regions that need no opt-in: " + names);
|
||||
assertTrue(names.contains("us-east-1"));
|
||||
assertTrue(names.contains("eu-north-1"));
|
||||
assertFalse(names.contains("ap-east-1"), "opt-in regions are hidden until AllRegions is set");
|
||||
assertFalse(names.contains("cn-north-1"));
|
||||
assertTrue(statuses.stream().allMatch("opt-in-not-required"::equals), statuses.toString());
|
||||
assertTrue(endpoints.contains("ec2.us-east-1.amazonaws.com"), endpoints.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void allRegionsIncludesOptInRegionsAsNotOptedIn() {
|
||||
Response response = describeRegions("us-east-1", "AllRegions", "true");
|
||||
List<String> names = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName");
|
||||
assertEquals(34, names.size(), names.toString());
|
||||
int hongKong = names.indexOf("ap-east-1");
|
||||
assertTrue(hongKong >= 0, names.toString());
|
||||
assertEquals("not-opted-in",
|
||||
response.xmlPath().getString("DescribeRegionsResponse.regionInfo.item[" + hongKong + "].optInStatus"));
|
||||
int virginia = names.indexOf("us-east-1");
|
||||
assertEquals("opt-in-not-required",
|
||||
response.xmlPath().getString("DescribeRegionsResponse.regionInfo.item[" + virginia + "].optInStatus"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void regionNamesFilterReturnsTheNamedRegionsWhetherOrNotEnabled() {
|
||||
Response response = describeRegions("us-east-1", "RegionName.1", "ap-east-1", "RegionName.2", "us-west-2");
|
||||
List<String> names = response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName");
|
||||
assertEquals(List.of("ap-east-1", "us-west-2"), names);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aChinaSignedRequestListsTheChinaPartition() {
|
||||
Response response = describeRegions("cn-north-1");
|
||||
assertEquals(List.of("cn-north-1", "cn-northwest-1"),
|
||||
response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName"));
|
||||
assertEquals(List.of("ec2.cn-north-1.amazonaws.com.cn", "ec2.cn-northwest-1.amazonaws.com.cn"),
|
||||
response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionEndpoint"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGovCloudSignedRequestListsGovCloudWithTheCommercialSuffix() {
|
||||
Response response = describeRegions("us-gov-west-1");
|
||||
assertEquals(List.of("us-gov-east-1", "us-gov-west-1"),
|
||||
response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionName"));
|
||||
assertEquals(List.of("ec2.us-gov-east-1.amazonaws.com", "ec2.us-gov-west-1.amazonaws.com"),
|
||||
response.xmlPath().getList("DescribeRegionsResponse.regionInfo.item.regionEndpoint"));
|
||||
}
|
||||
}
|
||||
+2
@@ -296,6 +296,8 @@ class EventBridgeSchedulerIntegrationTest {
|
||||
@Override
|
||||
public String defaultAccountId() { return ACCOUNT; }
|
||||
@Override
|
||||
public PartitionsConfig partitions() { return Optional::empty; }
|
||||
@Override
|
||||
public Optional<String> aiMockConfigFile() { return Optional.empty(); }
|
||||
@Override
|
||||
public StorageConfig storage() { return null; }
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
/**
|
||||
* Deletes what a test created in a non-commercial scope, in reverse order, after each test.
|
||||
* The {@code @QuarkusTest} emulator is shared by every test class, and a resource left behind
|
||||
* under {@code cn-north-1} shows up in every later cross-region listing (ResourceExplorer scans
|
||||
* all resources), so a test that signs a foreign partition registers each create here.
|
||||
*
|
||||
* <pre>{@code
|
||||
* @RegisterExtension
|
||||
* final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
* ...
|
||||
* createTable(region, name);
|
||||
* cleanup.register(() -> deleteTable(region, name));
|
||||
* }</pre>
|
||||
*
|
||||
* A failing cleanup never fails the test: it is reported on stderr so the leak is visible and
|
||||
* the assertion that actually failed stays the headline.
|
||||
*/
|
||||
public final class PartitionCleanup implements AfterEachCallback {
|
||||
|
||||
private final Deque<Runnable> actions = new ArrayDeque<>();
|
||||
|
||||
public void register(Runnable action) {
|
||||
actions.push(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(ExtensionContext context) {
|
||||
while (!actions.isEmpty()) {
|
||||
Runnable action = actions.pop();
|
||||
try {
|
||||
action.run();
|
||||
} catch (RuntimeException e) {
|
||||
System.err.println("PartitionCleanup: cleanup step failed after "
|
||||
+ context.getDisplayName() + ": " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* One representative region per AWS partition, for tests that assert partition-dependent
|
||||
* behaviour (ARN partition segments, DNS suffixes, signing scopes). Tests that are not about
|
||||
* partitions keep their {@code us-east-1} fixtures; nothing here is meant to rewrite them.
|
||||
*/
|
||||
public final class PartitionMatrix {
|
||||
|
||||
public record PartitionCase(String partition, String region, String dnsSuffix) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return partition + " (" + region + ")";
|
||||
}
|
||||
}
|
||||
|
||||
public static final String ACCOUNT = "000000000000";
|
||||
|
||||
private static final List<PartitionCase> ALL = List.of(
|
||||
new PartitionCase("aws", "us-east-1", "amazonaws.com"),
|
||||
new PartitionCase("aws-cn", "cn-north-1", "amazonaws.com.cn"),
|
||||
new PartitionCase("aws-us-gov", "us-gov-west-1", "amazonaws.com"),
|
||||
new PartitionCase("aws-iso", "us-iso-east-1", "c2s.ic.gov"),
|
||||
new PartitionCase("aws-iso-b", "us-isob-east-1", "sc2s.sgov.gov"),
|
||||
new PartitionCase("aws-iso-e", "eu-isoe-west-1", "cloud.adc-e.uk"),
|
||||
new PartitionCase("aws-iso-f", "us-isof-south-1", "csp.hci.ic.gov"),
|
||||
new PartitionCase("aws-eusc", "eusc-de-east-1", "amazonaws.eu"));
|
||||
|
||||
private PartitionMatrix() {
|
||||
}
|
||||
|
||||
public static List<PartitionCase> all() {
|
||||
return ALL;
|
||||
}
|
||||
|
||||
/** For {@code @MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")}. */
|
||||
public static Stream<PartitionCase> cases() {
|
||||
return ALL.stream();
|
||||
}
|
||||
|
||||
/** A SigV4 Authorization header whose credential scope signs {@code region} for {@code service}. */
|
||||
public static String sigV4Auth(String region, String service) {
|
||||
return "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region + "/" + service + "/aws4_request, "
|
||||
+ "SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=abc";
|
||||
}
|
||||
|
||||
/** A resolver for a deployment whose default region is the case's region. */
|
||||
public static RegionResolver regionResolver(PartitionCase partitionCase) {
|
||||
return new RegionResolver(partitionCase.region(), ACCOUNT);
|
||||
}
|
||||
|
||||
/** Asserts {@code arn} is a regional ARN of the case's partition and region. */
|
||||
public static void assertArnIn(PartitionCase partitionCase, String arn) {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
assertEquals(partitionCase.partition(), parsed.partition(), "partition of " + arn);
|
||||
assertEquals(partitionCase.region(), parsed.region(), "region of " + arn);
|
||||
}
|
||||
|
||||
/** Asserts {@code arn} is a regionless ARN of the case's partition. */
|
||||
public static void assertGlobalArnIn(PartitionCase partitionCase, String arn) {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
assertEquals(partitionCase.partition(), parsed.partition(), "partition of " + arn);
|
||||
assertEquals("", parsed.region(), "a global ARN carries no region: " + arn);
|
||||
}
|
||||
|
||||
/** Asserts {@code host} ends with the case's DNS suffix. */
|
||||
public static void assertHostIn(PartitionCase partitionCase, String host) {
|
||||
assertEquals(true, host.endsWith("." + partitionCase.dnsSuffix()),
|
||||
host + " should end with ." + partitionCase.dnsSuffix());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user