fix(core): service principals are universal in every partition; accept the legacy forms (#4633)

* fix(core): service principals are universal in every partition; accept the legacy forms

A service principal is <service>.amazonaws.com in every partition, the rule the CDK applies
(region-info.ts, principals.ts). Floci now emits it through ServicePrincipals.of: generated trust
policies, SAM's Lambda permissions, CloudTrail's eventSource, service-linked role paths (Macie,
GuardDuty, Connect, Application Auto Scaling, RAM, SSO) and the Redshift COPY trust check.

Policies may still name a service in the per-partition form AWS accepted before
(elasticmapreduce.amazonaws.com.cn, logs.cn-north-1.amazonaws.com.cn, config.c2s.ic.gov), and AWS
still honours them. ServicePrincipals.canonical folds a published DNS suffix and a regional label
away, and the trust-policy evaluator and CreateServiceLinkedRole compare in that form, so either
spelling matches.

DescribeVpcEndpointServices now lists China's S3 interface service, cn.com.amazonaws.<region>.s3,
beside the com.amazonaws gateway one, each with its own type; where the names agree it stays one
service carrying both types.

Partition literal baseline 34 -> 4 occurrences. The web-identity and pod-identity audiences keep
their commercial literals, escaped for the STS phase.

Part of #4272

* fix(iam): put a legacy-named service-linked role under the universal principal

CreateServiceLinkedRole derived the role name from the universal principal but kept the caller's legacy spelling (es.amazonaws.com.cn) in the path and the trust policy. All three now use the universal principal, so the role lists under /aws-service-role/es.amazonaws.com/ like one created from the universal name.

ServicePrincipals.canonical no longer lower-cases: service-principal matching is exact and case-sensitive since #4555, and AWS matches principals case-sensitively, so only a suffix written the way AWS publishes it folds.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-29 20:33:07 -04:00
1 parent cea6d3e99f
commit 43ed79d15b
29 files changed
+355 -70

No files matched your search

@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.PartitionMatrix;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -14,6 +15,7 @@ import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -133,14 +135,38 @@ class PartitionRegionGatesIntegrationTest {
assertTrue(names.contains("com.amazonaws.cn-north-1.s3"), names.toString());
assertTrue(names.contains("com.amazonaws.cn-north-1.dynamodb"), names.toString());
List<String> services = given()
XmlPath endpointServices = given()
.header("Authorization", PartitionMatrix.sigV4Auth("cn-north-1", "ec2"))
.formParam("Action", "DescribeVpcEndpointServices")
.formParam("Version", "2016-11-15")
.when().post("/").then().statusCode(200)
.extract().xmlPath().getList("DescribeVpcEndpointServicesResponse.serviceNameSet.item");
.extract().xmlPath();
List<String> services = endpointServices.getList("DescribeVpcEndpointServicesResponse.serviceNameSet.item");
assertTrue(services.contains("cn.com.amazonaws.cn-north-1.lambda"), services.toString());
assertTrue(services.contains("com.amazonaws.cn-north-1.s3"), services.toString());
assertTrue(services.contains("cn.com.amazonaws.cn-north-1.s3"), services.toString());
assertEquals(List.of("Gateway"), serviceTypes(endpointServices, "com.amazonaws.cn-north-1.s3"));
assertEquals(List.of("Interface"), serviceTypes(endpointServices, "cn.com.amazonaws.cn-north-1.s3"));
}
/** Where the two S3 offerings share a name, the one service detail carries both types. */
@Test
void commercialS3IsOneServiceWithBothEndpointTypes() {
XmlPath endpointServices = given()
.header("Authorization", PartitionMatrix.sigV4Auth("us-east-1", "ec2"))
.formParam("Action", "DescribeVpcEndpointServices")
.formParam("Version", "2016-11-15")
.when().post("/").then().statusCode(200)
.extract().xmlPath();
List<String> s3Names = endpointServices.getList("DescribeVpcEndpointServicesResponse.serviceNameSet.item",
String.class).stream().filter(name -> name.endsWith(".s3")).toList();
assertEquals(List.of("com.amazonaws.us-east-1.s3"), s3Names);
assertEquals(List.of("Gateway", "Interface"), serviceTypes(endpointServices, "com.amazonaws.us-east-1.s3"));
}
private static List<String> serviceTypes(XmlPath endpointServices, String serviceName) {
return endpointServices.getList("DescribeVpcEndpointServicesResponse.serviceDetailSet.item"
+ ".find { it.serviceName == '" + serviceName + "' }.serviceType.item.serviceType", String.class);
}
@Test
@@ -0,0 +1,116 @@
package io.github.hectorvent.floci.core.common;
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.MethodSource;
import java.util.Locale;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* Service principals are {@code <service>.amazonaws.com} in every partition (the CDK's rule in
* {@code region-info.ts:130-132} and {@code principals.ts:990-1003}); the retired per-partition
* forms from {@code region-info/lib/default.ts:53-117}, reproduced here by {@link #legacyForm},
* must all fold back to it, so a policy written in either form is matched.
*/
class ServicePrincipalsTest {
@ParameterizedTest
@MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")
void thePrincipalIsUniversalInEveryPartition(PartitionCase partition) {
assertEquals("s3.amazonaws.com", ServicePrincipals.of("s3"));
assertEquals("s3.amazonaws.com", ServicePrincipals.of("s3." + partition.dnsSuffix()));
assertEquals("logs.amazonaws.com",
ServicePrincipals.canonical("logs." + partition.region() + "." + partition.dnsSuffix()));
assertEquals("logs", ServicePrincipals.serviceName("logs." + partition.region() + "." + partition.dnsSuffix()));
}
@Test
void aDottedServiceNameKeepsItsLabels() {
assertEquals("rds.application-autoscaling.amazonaws.com", ServicePrincipals.of("rds.application-autoscaling"));
assertEquals("stacksets.cloudformation.amazonaws.com",
ServicePrincipals.of("stacksets.cloudformation.amazonaws.com.cn"));
assertEquals("rds.application-autoscaling",
ServicePrincipals.serviceName("rds.application-autoscaling.amazonaws.com"));
}
@Test
void aValueWithNoPublishedSuffixPassesThroughUnchanged() {
assertEquals("*", ServicePrincipals.canonical("*"));
assertEquals("*.amazonaws.com", ServicePrincipals.canonical("*.amazonaws.com"));
assertEquals("EC2.internal", ServicePrincipals.canonical("EC2.internal"));
}
/** AWS matches principals case-sensitively, so only a suffix written as published folds. */
@Test
void caseIsKeptAndAnUpperCaseSuffixDoesNotFold() {
assertEquals("Logs.amazonaws.com", ServicePrincipals.canonical("Logs.cn-north-1.amazonaws.com.cn"));
assertEquals("logs.AMAZONAWS.COM.CN", ServicePrincipals.canonical("logs.AMAZONAWS.COM.CN"));
assertNotEquals(ServicePrincipals.of("ec2"), ServicePrincipals.canonical("ec2.internal"));
}
/** The CDK's retired table, pinned case by case so a later cleanup cannot drift it silently. */
@Test
void legacyFormsFollowTheCdkTable() {
assertEquals("codedeploy.cn-north-1.amazonaws.com.cn", legacyForm("codedeploy", "cn-north-1"));
assertEquals("codedeploy.us-east-1.amazonaws.com", legacyForm("codedeploy", "us-east-1"));
assertEquals("codedeploy.amazonaws.com", legacyForm("codedeploy", "us-iso-east-1"));
assertEquals("logs.us-gov-west-1.amazonaws.com", legacyForm("logs", "us-gov-west-1"));
assertEquals("logs.cn-northwest-1.amazonaws.com.cn", legacyForm("logs", "cn-northwest-1"));
assertEquals("states.eu-west-1.amazonaws.com", legacyForm("states", "eu-west-1"));
assertEquals("states.amazonaws.com", legacyForm("states", "us-iso-east-1"));
assertEquals("states.amazonaws.com", legacyForm("states", "us-isob-east-1"));
assertEquals("elasticmapreduce.amazonaws.com.cn", legacyForm("elasticmapreduce", "cn-north-1"));
assertEquals("elasticmapreduce.amazonaws.com", legacyForm("elasticmapreduce", "eu-central-1"));
assertEquals("config.c2s.ic.gov", legacyForm("config", "us-iso-east-1"));
assertEquals("workspaces.c2s.ic.gov", legacyForm("workspaces", "us-iso-west-1"));
assertEquals("dms.sc2s.sgov.gov", legacyForm("dms", "us-isob-east-1"));
assertEquals("dms.amazonaws.com", legacyForm("dms", "us-iso-east-1"));
assertEquals("s3.amazonaws.com", legacyForm("s3", "cn-north-1"));
}
@ParameterizedTest
@MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")
void everyLegacyFormFoldsBackToTheUniversalOne(PartitionCase partition) {
for (String service : Set.of("codedeploy", "logs", "states", "elasticmapreduce", "config", "dms", "lambda")) {
String legacy = legacyForm(service, partition.region());
assertEquals(service + ".amazonaws.com", ServicePrincipals.canonical(legacy), legacy);
}
}
/**
* The per-partition principal AWS used before the universal rule, from the CDK's retired
* table: {@code codedeploy.cn-north-1.amazonaws.com.cn} in China and
* {@code codedeploy.<region>.amazonaws.com} elsewhere, {@code logs.<region>.<suffix>},
* {@code states.<region>.amazonaws.com}, {@code elasticmapreduce.amazonaws.com.cn} in China,
* and the partitional {@code cloudhsm}, {@code config}, {@code workspaces} ({@code us-iso-*})
* and {@code dms} ({@code us-isob-*}); {@code states} is universal in the ISO partitions.
* Every other service is universal there too.
*/
private static String legacyForm(String service, String region) {
String lower = region.toLowerCase(Locale.ROOT);
String suffix = AwsPartitions.forRegionOrCommercial(lower).dnsSuffix();
if (lower.startsWith("us-iso-") && Set.of("cloudhsm", "config", "workspaces").contains(service)) {
return service + "." + suffix;
}
if (lower.startsWith("us-isob-") && "dms".equals(service)) {
return service + "." + suffix;
}
if (lower.startsWith("us-iso") && "states".equals(service)) {
return service + ".amazonaws.com";
}
return switch (service) {
case "codedeploy" -> lower.startsWith("cn-") ? service + "." + lower + "." + suffix
: lower.startsWith("us-iso") ? service + ".amazonaws.com"
: service + "." + lower + ".amazonaws.com";
case "logs" -> service + "." + lower + "." + suffix;
case "states" -> service + "." + lower + ".amazonaws.com";
case "elasticmapreduce" -> lower.startsWith("cn-") ? service + "." + suffix : service + ".amazonaws.com";
default -> service + ".amazonaws.com";
};
}
}
@@ -20,6 +20,38 @@ class AssumeRolePolicyEvaluatorTest {
""".formatted(principal);
}
@Test
void allowsAServicePrincipalInTheUniversalForm() {
assertTrue(evaluator.allowsService(
trust("{\"Service\":\"redshift.amazonaws.com\"}"), "redshift.amazonaws.com"));
assertFalse(evaluator.allowsService(
trust("{\"Service\":\"lambda.amazonaws.com\"}"), "redshift.amazonaws.com"));
}
@Test
void allowsAServicePrincipalWrittenInTheLegacyPartitionForm() {
// AWS still honours the per-partition forms it used before the universal rule, so a
// trust policy written in China or ISO spelling matches the universal principal.
assertTrue(evaluator.allowsService(
trust("{\"Service\":\"elasticmapreduce.amazonaws.com.cn\"}"), "elasticmapreduce.amazonaws.com"));
assertTrue(evaluator.allowsService(
trust("{\"Service\":[\"logs.cn-north-1.amazonaws.com.cn\",\"lambda.amazonaws.com\"]}"),
"logs.amazonaws.com"));
assertTrue(evaluator.allowsService(trust("{\"Service\":\"config.c2s.ic.gov\"}"), "config.amazonaws.com"));
// And the other way round: the check itself may arrive in a legacy spelling.
assertTrue(evaluator.allowsService(
trust("{\"Service\":\"redshift.amazonaws.com\"}"), "redshift.amazonaws.com.cn"));
}
@Test
void aServicePrincipalMatchesExactlyAndCaseSensitivelyInEitherForm() {
assertFalse(evaluator.allowsService(trust("{\"Service\":\"*.amazonaws.com\"}"), "redshift.amazonaws.com"));
assertFalse(evaluator.allowsService(
trust("{\"Service\":\"Redshift.amazonaws.com\"}"), "redshift.amazonaws.com"));
assertFalse(evaluator.allowsService(
trust("{\"Service\":\"redshift.AMAZONAWS.COM.CN\"}"), "redshift.amazonaws.com"));
}
@Test
void allowsAccountRootPrincipal() {
assertTrue(evaluator.allows(
@@ -600,6 +600,20 @@ class IamServiceTest {
role.getArn());
}
@Test
void createServiceLinkedRoleAcceptsTheLegacyPartitionFormOfThePrincipal() {
// AWS still honours the pre-universal China principal; the derived name, the path and the
// trust policy are the universal ones either way.
IamRole role = iamService.createServiceLinkedRole("autoscaling.amazonaws.com.cn", null, null);
assertEquals("AWSServiceRoleForAutoScaling", role.getRoleName());
assertEquals("/aws-service-role/autoscaling.amazonaws.com/", role.getPath());
assertTrue(role.getAssumeRolePolicyDocument().contains("\"Service\":\"autoscaling.amazonaws.com\""),
role.getAssumeRolePolicyDocument());
IamRole es = iamService.createServiceLinkedRole("es.amazonaws.com.cn", null, null);
assertEquals("AWSServiceRoleForEs", es.getRoleName());
}
@Test
void createServiceLinkedRoleForCloud9UsesAwsCanonicalName() {
IamRole role = iamService.createServiceLinkedRole(