Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(partition): serve a pseudo signing region as its partition's global region (#5233)

Hector Venturae1cd6fe6d434
diff --git a/docs/configuration/partitions.md b/docs/configuration/partitions.md
index 1dbab80b30..b6c31db358 100644
--- a/docs/configuration/partitions.md
+++ b/docs/configuration/partitions.md
@@ -45,7 +45,10 @@ The scope region of a global service is its *signing* region, not a place to put
resources: China IAM always signs `cn-north-1` even from Ningxia, GovCloud IAM signs
`us-gov-west-1`. Floci uses it to pick the partition and nothing else. The
`<partition>-global` pseudo-regions the SDKs accept (`aws-global`, `aws-cn-global`, ...)
-resolve to their partition as well; `aws-eusc` publishes none.
+resolve to their partition as well; `aws-eusc` publishes none. AWS never signs with one (the SDKs
+map each to a real signing region), so a scope that names one is served as the partition's
+implicit global region: a request signed `aws-cn-global` is a `cn-northwest-1` request, and no
+ARN or storage namespace carries the pseudo-region.
A scope region that no partition publishes or admits by its region pattern, such as
`polygondwanaland-west-1`, is refused with a 400: an S3-signed request gets S3's
@@ -56,7 +59,9 @@ The pattern rule is the AWS SDKs' own, so a region AWS launches after the vendor
refreshed (`eu-south-9`, say) is still served; it is looser than S3's `LocationConstraint` enum,
which stays published-only. Set `FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS=true`
(`floci.partitions.allow-unknown-regions`) to serve any label with its own namespace, as Floci
-did before.
+did before. Such a label belongs to the deployment's partition. ARNs minted through Floci's
+shared region resolver use that partition too; services that build their ARNs directly still fall
+back to `aws` for a label no partition publishes.
## What changes per partition
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java
index 8a703e0f6e..ea4a944e5b 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/RegionResolver.java
@@ -89,14 +89,32 @@ public class RegionResolver {
/**
* The region label the SigV4 credential scope names, unvalidated: {@code AccountContextFilter}
* decides whether an unknown label is served or refused (see {@link #isKnownRegion}), and
- * needs the raw label either way so the rejection can name it.
+ * needs the raw label either way so the rejection can name it. A {@code <partition>-global}
+ * pseudo-region is the one exception, see {@link #withoutPseudoRegion}.
*/
public String resolveRegionFromAuth(String authorizationHeader) {
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
return defaultRegion;
}
Matcher matcher = CREDENTIAL_REGION_PATTERN.matcher(authorizationHeader);
- return matcher.find() ? matcher.group(1) : defaultRegion;
+ return matcher.find() ? withoutPseudoRegion(matcher.group(1)) : defaultRegion;
+ }
+
+ /**
+ * A {@code <partition>-global} pseudo-region named as a signing region becomes that
+ * partition's implicit global region ({@code aws-cn-global} is served as
+ * {@code cn-northwest-1}). AWS never signs with a pseudo-region: botocore's
+ * {@code endpoints.json} maps each to a real {@code credentialScope} region, so serving the
+ * label as-is would give a hand-rolled client a storage namespace and ARNs
+ * ({@code arn:aws-cn:sqs:aws-cn-global:...}) for a region that does not exist. Every other
+ * label, an unknown one included, is returned untouched.
+ */
+ static String withoutPseudoRegion(String label) {
+ if (label == null) {
+ return null;
+ }
+ String normalized = AwsPartitions.normalizeRegion(label);
+ return normalized.equals(label.trim().toLowerCase(Locale.ROOT)) ? label : normalized;
}
/**
@@ -122,7 +140,7 @@ public class RegionResolver {
}
String[] parts = credentialValue.split("/");
if (parts.length >= 3) {
- return parts[2];
+ return withoutPseudoRegion(parts[2]);
}
return defaultRegion;
}
@@ -139,7 +157,7 @@ public class RegionResolver {
return null;
}
Matcher matcher = CREDENTIAL_REGION_PATTERN.matcher(authorizationHeader);
- return matcher.find() ? matcher.group(1) : null;
+ return matcher.find() ? withoutPseudoRegion(matcher.group(1)) : null;
}
/**
@@ -249,15 +267,18 @@ public class RegionResolver {
}
/**
- * Mints a regional ARN, deriving the partition from {@code region}. A blank region means a
- * global service, whose partition is the request's rather than something the region can
- * say, so it routes to {@link #buildGlobalArn(String, String)}.
+ * Mints a regional ARN, deriving the partition from {@code region} the same way
+ * {@link #partitionForRegion} scopes the request, so a region no partition publishes (served
+ * only with {@code floci.partitions.allow-unknown-regions}) gets the deployment's partition
+ * here too rather than {@code aws}. A blank region means a global service, whose partition is
+ * the request's rather than something the region can say, so it routes to
+ * {@link #buildGlobalArn(String, String)}.
*/
public String buildArn(String service, String region, String resource) {
if (region == null || region.isBlank()) {
return buildGlobalArn(service, resource);
}
- return AwsArnUtils.Arn.of(service, region, getAccountId(), resource).toString();
+ return new AwsArnUtils.Arn(partitionForRegion(region), service, region, getAccountId(), resource).toString();
}
/** Mints a regionless ARN ({@code arn:<partition>:iam::<account>:...}) in the request's partition. */
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java
index b7e07027d2..fee22db36a 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/AccountContextFilterTest.java
@@ -115,7 +115,7 @@ class AccountContextFilterTest {
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("cn-northwest-1", requestContext.getRegion(), "a pseudo-region is never a request region");
assertEquals("aws-cn", requestContext.getPartition());
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionArnMintingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionArnMintingIntegrationTest.java
index 1b4e486368..60514467b3 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionArnMintingIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionArnMintingIntegrationTest.java
@@ -127,6 +127,37 @@ class PartitionArnMintingIntegrationTest {
assertEquals("arn:aws-cn:ssm:" + REGION + ":000000000000:parameter" + name, arn);
}
+ /**
+ * A hand-rolled client that signs with the {@code aws-cn-global} pseudo-region is served in
+ * China's implicit global region: the parameter lands in {@code cn-northwest-1}, is readable
+ * there, and no ARN carries the pseudo-region.
+ */
+ @Test
+ void aPseudoSigningRegionIsServedAsThePartitionsGlobalRegion() {
+ String name = "/partition-pseudo/" + Long.toString(System.nanoTime(), 36);
+ given()
+ .header("Authorization", PartitionMatrix.sigV4Auth("aws-cn-global", "ssm"))
+ .header("X-Amz-Target", "AmazonSSM.PutParameter")
+ .contentType(JSON_1_1)
+ .body("{\"Name\":\"" + name + "\",\"Type\":\"String\",\"Value\":\"v\"}")
+ .when().post("/").then().statusCode(200);
+ cleanup.register(() -> given()
+ .header("Authorization", PartitionMatrix.sigV4Auth("cn-northwest-1", "ssm"))
+ .header("X-Amz-Target", "AmazonSSM.DeleteParameter")
+ .contentType(JSON_1_1)
+ .body("{\"Name\":\"" + name + "\"}")
+ .when().post("/"));
+
+ String arn = given()
+ .header("Authorization", PartitionMatrix.sigV4Auth("cn-northwest-1", "ssm"))
+ .header("X-Amz-Target", "AmazonSSM.GetParameter")
+ .contentType(JSON_1_1)
+ .body("{\"Name\":\"" + name + "\"}")
+ .when().post("/").then().statusCode(200)
+ .extract().jsonPath().getString("Parameter.ARN");
+ assertEquals("arn:aws-cn:ssm:cn-northwest-1:000000000000:parameter" + name, arn);
+ }
+
@Test
void serviceCatalogPortfolioArnIsMintedInTheRequestPartition() {
String token = Long.toString(System.nanoTime(), 36);
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java
index dab52f4118..a8cfaf6467 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/RegionResolverTest.java
@@ -220,6 +220,37 @@ class RegionResolverTest {
assertEquals("aws", resolver.partitionForRegion("aws-global"));
}
+ /**
+ * AWS never signs with a pseudo-region, so one named in a credential scope is served as its
+ * partition's implicit global region instead of becoming a regional namespace of its own.
+ */
+ @Test
+ void aPseudoSigningRegionIsServedAsItsPartitionsGlobalRegion() {
+ assertEquals("cn-northwest-1", resolver.resolveRegionFromAuth(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/aws-cn-global/sqs/aws4_request, SignedHeaders=host, Signature=abc"));
+ assertEquals("us-east-1", resolver.resolveRegionFromAuthOrNull(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/aws-global/sqs/aws4_request, SignedHeaders=host, Signature=abc"));
+ assertEquals("us-gov-west-1",
+ resolver.resolveRegionFromPresignedCredential("AKID/20260617/aws-us-gov-global/s3/aws4_request"));
+ assertEquals("cn-north-1", resolver.resolveRegionFromAuth(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/cn-north-1/sqs/aws4_request, SignedHeaders=host, Signature=abc"));
+ assertEquals("xx-nowhere-9", resolver.resolveRegionFromAuth(
+ "AWS4-HMAC-SHA256 Credential=AKID/20260617/xx-nowhere-9/sqs/aws4_request, SignedHeaders=host, Signature=abc"),
+ "an unknown label stays raw so the unknown-region guard can name it");
+ }
+
+ /**
+ * A region no partition publishes is scoped to the deployment's partition when served
+ * ({@code floci.partitions.allow-unknown-regions}), and its ARNs must agree with that scope.
+ */
+ @Test
+ void buildArnMintsAnUnknownRegionInTheDeploymentPartition() {
+ RegionResolver china = new RegionResolver("cn-north-1", "000000000000");
+ assertEquals("arn:aws-cn:sqs:xx-nowhere-9:000000000000:q", china.buildArn("sqs", "xx-nowhere-9", "q"));
+ assertEquals("arn:aws:sqs:us-east-1:000000000000:q", china.buildArn("sqs", "us-east-1", "q"));
+ assertEquals("arn:aws:sqs:xx-nowhere-9:000000000000:q", resolver.buildArn("sqs", "xx-nowhere-9", "q"));
+ }
+
@Test
void getPartitionOutsideARequestIsTheDeploymentPartition() {
assertEquals("aws", resolver.getPartition());