fix(core): mint every regionless ARN in its partition and gate blank-region Arn.of calls (#4787)

* fix(core): mint every regionless ARN in its partition and gate blank-region Arn.of calls

Part of #4272 (P10a). Arn.of derives the partition from the region, so a
blank region always minted arn:aws:, and the partition literal gate could not
see it because it is a call shape, not a literal. Sixteen call sites did this,
and three gated literals were left:

- RdsSigV4Validator built the rds-db:connect resource as arn:aws:rds-db:, so a
  China IAM policy naming arn:aws-cn:rds-db: never matched and IAM auth to an
  RDS proxy was denied there.
- CloudFront, Organizations, Cost Explorer anomaly monitors and the SSO portal
  role ARN now take the request's partition (RegionResolver injected).
- S3 bucket ARNs (event notifications, AWS::S3::Bucket Arn, Cost Explorer S3
  usage) take the bucket's or the stack region's partition; the Lambda CFN
  default role, the Kinesis ESM invokeIdentityArn, the EC2 launch template
  CreatedBy and the SQS AddPermission root principal take their own region's.
- FlinkContainerManager's fallback key goes through Arn.of, and the
  CloudFormation S3 TemplateURL host check accepts every partition's DNS suffix.

partition_literals.py gains a gated, code-scoped blank-region-arn category
(Arn.of with a "" or null region, comments excluded). The baseline is now
empty, so any new literal or blank-region mint fails Partition Literals.

* fix(core): scan blank-region Arn.of calls across lines and skip text blocks

Greptile on #4787: the blank-region-arn category ran per line, so a call whose
region argument sits on the next line escaped it, and text-block contents were
kept, so an example inside one would count as a call against the empty
baseline. code_text now blanks comments and text blocks over the whole source
and the code categories match across lines, reporting the line the call starts
on.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-01 00:32:39 -04:00
1 parent 3a56aad670
commit 27ddf0bb00
28 files changed
+278 -70

No files matched your search

@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import org.junit.jupiter.api.BeforeEach;
@@ -32,7 +33,17 @@ class CostAnomalyServiceTest {
@BeforeEach
void setUp() {
service = new CostAnomalyService(AccountAwareStorageBackend.inMemory(ACCOUNT),
AccountAwareStorageBackend.inMemory(ACCOUNT), mapper);
AccountAwareStorageBackend.inMemory(ACCOUNT), mapper, new RegionResolver("us-east-1", ACCOUNT));
}
@Test
void monitorArnsTakeTheRequestsPartition() {
CostAnomalyService china = new CostAnomalyService(AccountAwareStorageBackend.inMemory(ACCOUNT),
AccountAwareStorageBackend.inMemory(ACCOUNT), mapper, new RegionResolver("cn-north-1", ACCOUNT));
String arn = china.createMonitor(customMonitor("china")).path("MonitorArn").asText();
assertTrue(arn.startsWith("arn:aws-cn:ce::" + ACCOUNT + ":anomalymonitor/"), arn);
}
@Test
@@ -212,7 +223,8 @@ class CostAnomalyServiceTest {
monitors.load();
subscriptions.load();
return new CostAnomalyService(new AccountAwareStorageBackend<>(monitors, null, ACCOUNT),
new AccountAwareStorageBackend<>(subscriptions, null, ACCOUNT), mapper);
new AccountAwareStorageBackend<>(subscriptions, null, ACCOUNT), mapper,
new RegionResolver("us-east-1", ACCOUNT));
}
private ObjectNode customMonitor(String name) {
@@ -79,6 +79,15 @@ class CloudFormationTemplateUrlTest {
assertThat(ref.key(), equalTo("key"));
}
@Test
void pathStyleAgainstChina_readsBucketFromThePath() {
S3TemplateRef ref = CloudFormationService.parseTemplateUrl(
"https://s3.cn-north-1.amazonaws.com.cn/bucket/key", SUFFIX);
assertThat(ref.bucket(), equalTo("bucket"));
assertThat(ref.key(), equalTo("key"));
}
@Test
void virtualHostedAgainstAws_readsBucketFromTheHost() {
S3TemplateRef ref = CloudFormationService.parseTemplateUrl(
@@ -134,6 +134,7 @@ class S3CfnProvisionerTest {
assertEquals("my-bucket.s3.eu-isoe-west-1.cloud.adc-e.uk", r.getAttributes().get("RegionalDomainName"));
assertFalse(r.getAttributes().containsKey("DualStackDomainName"));
assertEquals("arn:aws-iso-e:s3:::my-bucket", r.getAttributes().get("Arn"));
}
/**
@@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -61,7 +62,7 @@ class CloudFrontControllerTest {
CloudFrontFunction second = function("beta");
CloudFrontFunction third = function("gamma");
CloudFrontFunction fourth = function("omega");
when(service.getAccountId()).thenReturn("000000000000");
when(service.arn(anyString())).thenAnswer(call -> "arn:aws:cloudfront::000000000000:" + call.getArgument(0));
when(service.listFunctions(null, null, 3))
.thenReturn(List.of(first, second, third));
when(service.listFunctions(null, "beta", 3))
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudfront;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -123,6 +124,10 @@ class CloudFrontServiceTest {
+ "</GeoRestriction></Restrictions>";
private CloudFrontService serviceWithDomainSuffix(String domainSuffix) {
return serviceWithDomainSuffix(domainSuffix, "us-east-1");
}
private CloudFrontService serviceWithDomainSuffix(String domainSuffix, String region) {
StorageFactory storageFactory = Mockito.mock(StorageFactory.class);
when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any()))
.thenAnswer(invocation -> AccountAwareStorageBackend.inMemory("000000000000"));
@@ -140,7 +145,7 @@ class CloudFrontServiceTest {
when(servicesConfig.cloudfront()).thenReturn(cloudFrontConfig);
when(cloudFrontConfig.domainSuffix()).thenReturn(domainSuffix);
return new CloudFrontService(storageFactory, config);
return new CloudFrontService(storageFactory, config, new RegionResolver(region, ACCOUNT));
}
@Test
@@ -227,6 +232,15 @@ class CloudFrontServiceTest {
assertEquals("NoSuchFunctionExists", error.getErrorCode());
}
@Test
void distributionArnTakesTheRequestsPartition() {
CloudFrontService china = serviceWithDomainSuffix("cloudfront.net", "cn-north-1");
Distribution dist = china.createDistribution(new Distribution(), Map.of());
assertEquals("arn:aws-cn:cloudfront::" + ACCOUNT + ":distribution/" + dist.getId(), dist.getArn());
}
@Test
void createDistributionUsesDefaultDomainSuffix() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.organizations;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.organizations.model.CreateAccountStatus;
import io.github.hectorvent.floci.services.organizations.model.Handshake;
@@ -33,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class OrganizationsServiceTest {
private static final String MANAGEMENT_ACCOUNT = "100000000001";
private static final RegionResolver REGION_RESOLVER = new RegionResolver("us-east-1", MANAGEMENT_ACCOUNT);
private static final String OUTSIDER_ACCOUNT = "100000000009";
private OrganizationsService service;
@@ -43,6 +45,7 @@ class OrganizationsServiceTest {
handshakes = AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT);
service = new OrganizationsService(
new ObjectMapper(),
REGION_RESOLVER,
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
@@ -74,6 +77,23 @@ class OrganizationsServiceTest {
assertTrue(status.getAccountId().matches("\\d{12}"), status.getAccountId());
}
@Test
void organizationsArnsTakeTheRequestsPartition() {
OrganizationsService china = new OrganizationsService(
new ObjectMapper(),
new RegionResolver("cn-north-1", MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT));
Organization organization = china.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
assertEquals("arn:aws-cn:organizations::" + MANAGEMENT_ACCOUNT + ":organization/" + organization.getId(),
organization.getArn());
}
@Test
void organizationsArnsCarryNoRegion() {
Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
@@ -348,6 +368,7 @@ class OrganizationsServiceTest {
void effectiveScpLevelsAreNullWhenEnforcementDisabled() {
OrganizationsService disabled = new OrganizationsService(
new ObjectMapper(),
REGION_RESOLVER,
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
@@ -372,6 +393,7 @@ class OrganizationsServiceTest {
void managementAccountEmailOverrideIsUsedForOrganizationAndAccount() {
OrganizationsService configured = new OrganizationsService(
new ObjectMapper(),
REGION_RESOLVER,
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
@@ -393,6 +415,7 @@ class OrganizationsServiceTest {
void malformedManagementAccountEmailOverrideIsRejected() {
assertThrows(IllegalArgumentException.class, () -> new OrganizationsService(
new ObjectMapper(),
REGION_RESOLVER,
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
@@ -78,6 +78,22 @@ class RdsSigV4ValidatorTest {
assertTrue(validator.validate(token, "jane_doe", exampleBinding()));
}
/** A China database's rds-db:connect resource is an arn:aws-cn: ARN, as a China IAM policy names it. */
@Test
void validateAcceptsAChinaConnectGrantForADatabaseInChina() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithUserPolicy(
"AKIDRDS", "secret-rds", "jane", connectPolicyFor(
"arn:aws-cn:rds-db:cn-north-1:123456789012:dbuser:db-ABCDEFGHIJKL01234/jane_doe"));
RdsSigV4Validator validator = new RdsSigV4Validator(iamService, () -> true);
String token = SigV4TokenTestHelper.createRdsTokenWithScope(
"db.example.local", 3307, "jane_doe", "AKIDRDS", "secret-rds", "cn-north-1", "rds-db",
Instant.now().minusSeconds(60), 900);
RdsProxyBinding chinaBinding = new RdsProxyBinding(
"db.example.local", 3307, "cn-north-1", "123456789012", "db-ABCDEFGHIJKL01234", true);
assertTrue(validator.validate(token, "jane_doe", chinaBinding));
}
@Test
void validateRejectsCallerWhoseConnectGrantIsForAnotherDbUser() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithUserPolicy(
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.ssoportal;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.organizations.OrganizationsService;
import io.github.hectorvent.floci.services.ssoadmin.SsoAdminService;
@@ -39,7 +40,8 @@ class SsoPortalServiceTest {
ssoAdminService = mock(SsoAdminService.class);
organizationsService = mock(OrganizationsService.class);
iamService = mock(IamService.class);
service = new SsoPortalService(oidcService, ssoAdminService, organizationsService, iamService);
service = new SsoPortalService(oidcService, ssoAdminService, organizationsService, iamService,
new RegionResolver("cn-north-1", "000000000000"));
}
@Test
@@ -69,7 +71,8 @@ class SsoPortalServiceTest {
ArgumentCaptor<String> roleArn = ArgumentCaptor.forClass(String.class);
verify(iamService).registerSessionForAccount(eq(accountId), any(), any(), any(), roleArn.capture(), any(), eq(null));
assertTrue(roleArn.getValue().contains(":iam::" + accountId + ":role/aws-reserved/sso.amazonaws.com/AWSReservedSSO_PlatformAdmins_floci"));
assertEquals("arn:aws-cn:iam::" + accountId
+ ":role/aws-reserved/sso.amazonaws.com/AWSReservedSSO_PlatformAdmins_floci", roleArn.getValue());
}
@Test