mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(partition): serve a pseudo signing region as its partition's global region (#5233)
A request whose SigV4 scope named a <partition>-global pseudo-region (aws-cn-global) kept the label as its region, so regional services stored state under it and minted ARNs such as arn:aws-cn:sqs:aws-cn-global:... for a region that does not exist. AWS never signs with a pseudo-region: endpoints.json maps each to a real credential scope region. RegionResolver now serves such a scope as the partition's implicit global region (aws-cn-global becomes cn-northwest-1) wherever it reads the region from a request. With floci.partitions.allow-unknown-regions in a non-commercial deployment, an unknown label was scoped to the deployment's partition but RegionResolver.buildArn minted it as aws. buildArn now uses the same partition as the request. ARNs built directly with Arn.of keep the commercial fallback, which partitions.md now states.
This commit is contained in:
1 parent
c0b2b299b0
commit
e1cd6fe6d4
5 files changed
+99
-11
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
+31
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in new issue
Block a user