feat(core): derive ARN partition from the region (#3334)

AwsArnUtils.Arn.of hardcoded the "aws" partition, so every ARN the emulator
minted for a GovCloud or China region named the wrong partition. Correctness
here was not a property of the call site: all 115 Arn.of callers and the 132
that reach it through RegionResolver.buildArn were equally wrong, including the
ones following the documented pattern.

AwsRegions gains partitionFor and dnsSuffixFor, with the region patterns
transcribed from botocore's partitions.json regionRegex values so they stay
comparable against it. Only the non-commercial partitions need a rule; an
unrecognised region resolves to "aws", which is what the SDKs do and keeps a
region AWS launches tomorrow working rather than failing closed.

EksTokenMinter carried the same table for STS hostnames and now delegates to
it, so a partition cannot go missing from one and not the other.

Global services pass an empty region and keep "aws". That gap is documented on
Arn.of rather than closed: an IAM ARN in a GovCloud deployment really is
arn:aws-us-gov:iam::..., but nothing in the region argument can say so, and
those call sites need a partition from the deployment instead.

AwsArnUtils is referenced from 120 files and had no test of its own, so this
adds one, written against the previous behaviour before the change and covering
the resource field's colon handling that the callers depend on.

No test in src/test or compatibility-tests constructs an ARN from a
non-commercial region: the six that mention one are parsing a hostname or
validating a supplied ARN. The emitted ARNs are therefore unchanged for the
whole suite.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-10 08:46:04 -04:00
1 parent 31f8860c11
commit dc61c1a8a5
5 files changed
+326 -36

No files matched your search

@@ -19,11 +19,21 @@ public final class AwsArnUtils {
public record Arn(String partition, String service, String region, String accountId, String resource) {
/**
* Factory for standard AWS ARNs using the {@code aws} partition.
* Produces: {@code arn:aws:<service>:<region>:<accountId>:<resource>}
* Factory for AWS ARNs, deriving the partition from the region.
* Produces: {@code arn:<partition>:<service>:<region>:<accountId>:<resource>}
*
* <p>A resource in {@code cn-north-1} gets {@code aws-cn}, one in {@code us-gov-west-1}
* gets {@code aws-us-gov}, and everything else gets {@code aws}. See
* {@link AwsRegions#partitionFor}.
*
* <p>Global services pass an empty region and therefore keep {@code aws}. That is a known
* gap rather than a decision: an IAM ARN in a GovCloud deployment really is
* {@code arn:aws-us-gov:iam::…}, but nothing in the region argument can say so. Those call
* sites need a partition from the deployment, not from the resource, and they are left
* alone until there is one.
*/
public static Arn of(String service, String region, String accountId, String resource) {
return new Arn("aws", service, region, accountId, resource);
return new Arn(AwsRegions.partitionFor(region), service, region, accountId, resource);
}
@Override
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Catalog of AWS regions the emulator advertises. Single source of truth for
@@ -54,6 +55,76 @@ public final class AwsRegions {
return label != null && KNOWN_IDS.contains(label.toLowerCase(Locale.ROOT));
}
/** The partition every region falls back to: the commercial one. */
public static final String DEFAULT_PARTITION = "aws";
/** The DNS suffix of the commercial partition, shared with {@code aws-us-gov}. */
public static final String DEFAULT_DNS_SUFFIX = "amazonaws.com";
private record PartitionRule(Pattern regionPattern, String partition, String dnsSuffix) {
}
/**
* Region-to-partition rules, transcribed from the AWS SDKs' own public partition metadata
* (botocore's {@code partitions.json}, MIT-licensed and openly published at
* https://github.com/boto/botocore/blob/develop/botocore/data/partitions.json). The patterns
* are that file's {@code regionRegex} values verbatim, so they stay comparable against it.
*
* <p>Only the non-commercial partitions need a rule: the commercial {@code aws} partition is
* the fallback, and {@code aws-us-gov} shares its DNS suffix, which is why GovCloud carries a
* rule for the partition id alone. The rules are mutually exclusive by construction (nothing
* matching {@code us-gov-*}, {@code us-iso-*} or {@code us-isob-*} matches the commercial
* shape, because {@code \w} does not cross a hyphen), so evaluation order is not load-bearing.
*
* <p>An unrecognised region resolves to {@code aws}. That is what the SDKs themselves do, and
* it keeps a region AWS launches tomorrow working rather than failing closed.
*/
private static final List<PartitionRule> PARTITION_RULES = List.of(
new PartitionRule(Pattern.compile("^cn-\\w+-\\d+$"), "aws-cn", "amazonaws.com.cn"),
new PartitionRule(Pattern.compile("^us-gov-\\w+-\\d+$"), "aws-us-gov", DEFAULT_DNS_SUFFIX),
new PartitionRule(Pattern.compile("^us-iso-\\w+-\\d+$"), "aws-iso", "c2s.ic.gov"),
new PartitionRule(Pattern.compile("^us-isob-\\w+-\\d+$"), "aws-iso-b", "sc2s.sgov.gov"),
new PartitionRule(Pattern.compile("^eu-isoe-\\w+-\\d+$"), "aws-iso-e", "cloud.adc-e.uk"),
new PartitionRule(Pattern.compile("^us-isof-\\w+-\\d+$"), "aws-iso-f", "csp.hci.ic.gov"),
new PartitionRule(Pattern.compile("^eusc-de-\\w+-\\d+$"), "aws-eusc", "amazonaws.eu"));
/**
* The partition id a region belongs to: {@code aws}, {@code aws-us-gov}, {@code aws-cn}, or
* one of the classified partitions. This is the second segment of every ARN minted for a
* resource in that region.
*
* <p>A null, blank or unrecognised region gives {@code aws}. Blank is not an error case: many
* ARNs are global and carry no region at all ({@code arn:aws:iam::…}), and those keep the
* commercial partition because the resource's own region cannot say otherwise.
*/
public static String partitionFor(String region) {
PartitionRule rule = ruleFor(region);
return rule == null ? DEFAULT_PARTITION : rule.partition();
}
/**
* The DNS suffix endpoints in a region's partition are served under, e.g.
* {@code amazonaws.com.cn} for {@code cn-north-1}. Same fallback rule as
* {@link #partitionFor}.
*/
public static String dnsSuffixFor(String region) {
PartitionRule rule = ruleFor(region);
return rule == null ? DEFAULT_DNS_SUFFIX : rule.dnsSuffix();
}
private static PartitionRule ruleFor(String region) {
if (region == null || region.isBlank()) {
return null;
}
String normalized = region.trim().toLowerCase(Locale.ROOT);
for (PartitionRule rule : PARTITION_RULES) {
if (rule.regionPattern().matcher(normalized).matches()) {
return rule;
}
}
return null;
}
private AwsRegions() {
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsRegions;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@@ -305,41 +306,13 @@ final class EksTokenMinter {
record AwsCredentials(String accessKeyId, String secretAccessKey, String sessionToken) {
}
/** Maps a region to its STS hostname, honoring the region's actual partition. */
private static String stsHost(String region) {
return "sts." + region + "." + partitionDnsSuffix(region);
}
/**
* Sourced from the AWS SDKs' own public partition metadata (e.g. botocore's
* {@code partitions.json}, MIT-licensed and openly published at
* https://github.com/boto/botocore/blob/develop/botocore/data/partitions.json), rather than
* assumed. Covers every partition that metadata currently defines: the commercial
* {@code aws} partition and {@code aws-us-gov} share {@code amazonaws.com} so neither needs
* a case of its own, {@code aws-cn} is the one commonly-hit split, and the classified
* ISO/ISOB/ISOE/ISOF partitions (regions matching {@code us-iso-*}, {@code us-isob-*},
* {@code eu-isoe-*}, {@code us-isof-*}) each resolve under their own distinct domain.
* Maps a region to its STS hostname, honoring the region's actual partition. The partition
* table lives in {@link AwsRegions}, shared with ARN construction, so a region added in one
* place cannot be missing from the other.
*/
private static String partitionDnsSuffix(String region) {
if (region.matches("cn-\\w+-\\d+")) {
return "amazonaws.com.cn";
}
if (region.matches("us-iso-\\w+-\\d+")) {
return "c2s.ic.gov";
}
if (region.matches("us-isob-\\w+-\\d+")) {
return "sc2s.sgov.gov";
}
if (region.matches("eu-isoe-\\w+-\\d+")) {
return "cloud.adc-e.uk";
}
if (region.matches("us-isof-\\w+-\\d+")) {
return "csp.hci.ic.gov";
}
if (region.matches("eusc-de-\\w+-\\d+")) {
return "amazonaws.eu";
}
return "amazonaws.com";
private static String stsHost(String region) {
return "sts." + region + "." + AwsRegions.dnsSuffixFor(region);
}
/**