diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/IamAuthValidator.java b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/IamAuthValidator.java index 13c1797f9c..31ea80e65c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/IamAuthValidator.java +++ b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/IamAuthValidator.java @@ -320,23 +320,22 @@ public class IamAuthValidator { return "test".equals(accessKeyId); } + static String requestArn(String region, String accountId, String apiId) { + return appsyncArn(region, accountId, "apis/" + apiId + "/*"); + } + + static String fieldArn(String region, String accountId, String apiId, String typeName, String fieldName) { + return appsyncArn(region, accountId, "apis/" + apiId + "/types/" + typeName + "/fields/" + fieldName); + } + /** * The resource these two build is matched against a policy the customer wrote using the ARN * AppSync handed them, and that ARN carries the region's partition. Pinning {@code aws} here * meant a GovCloud policy naming its own API never matched, and the request was denied. */ - private static String appsyncArnPrefix(String region) { - return "arn:" + AwsRegions.partitionFor(region) + ":appsync:"; - } - - static String requestArn(String region, String accountId, String apiId) { - return appsyncArnPrefix(region) + nullToEmpty(region) + ":" + nullToEmpty(accountId) - + ":apis/" + apiId + "/*"; - } - - static String fieldArn(String region, String accountId, String apiId, String typeName, String fieldName) { - return appsyncArnPrefix(region) + nullToEmpty(region) + ":" + nullToEmpty(accountId) - + ":apis/" + apiId + "/types/" + typeName + "/fields/" + fieldName; + private static String appsyncArn(String region, String accountId, String resource) { + return new AwsArnUtils.Arn(AwsRegions.partitionFor(region), "appsync", nullToEmpty(region), + nullToEmpty(accountId), resource).toString(); } static String usernameFromArn(String userArn, String accessKeyId) { diff --git a/src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java b/src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java index 4f1f67b8f9..2948116f0a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java +++ b/src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java @@ -64,7 +64,8 @@ public class ControlCatalogService implements Resettable { ObjectNode response = objectMapper.createObjectNode(); String responseArn = definition.globalIdentifier() == null ? controlArn - : "arn:" + partition + ":controlcatalog:::control/" + definition.globalIdentifier(); + : AwsArnUtils.Arn.global(partition, "controlcatalog", "", "control/" + definition.globalIdentifier()) + .toString(); response.put("Arn", responseArn); response.set("Aliases", stringArray(definition.aliases())); response.put("Name", definition.name()); diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java index 623cec510f..05bdd56797 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java +++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.JsonNode; 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.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator; import io.github.hectorvent.floci.services.iam.PublicAccessConditions; @@ -187,7 +188,7 @@ final class DsqlClusterPolicies { List arns = new ArrayList<>(); for (String value : texts(type.getValue())) { String arn = ACCOUNT_ID.matcher(value).matches() - ? "arn:" + partition + ":iam::" + value + ":root" : value; + ? AwsArnUtils.Arn.global(partition, "iam", value, "root").toString() : value; boolean known = "*".equals(arn) || SESSION_ARN.matcher(arn).matches() || IAM_ARN.matcher(arn).matches() && principalExists.test(arn); if (!known) { diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java b/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java index 617b4acf9d..838f611e0e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java @@ -111,7 +111,7 @@ final class AwsManagedPolicies { /** {@code arn::iam::aws:policy}, without the trailing slash of the path. */ static String arnPrefix(String partition) { - return "arn:" + partition + ":iam::aws:policy"; + return AwsArnUtils.Arn.global(partition, "iam", "aws", "policy").toString(); } /** True for an AWS managed policy ARN in any partition ({@code arn:aws-cn:iam::aws:policy/...}). */ @@ -136,7 +136,7 @@ final class AwsManagedPolicies { private static List derive(AwsPartition partition) { long started = System.nanoTime(); - String arnPrefix = "arn:" + partition.id() + ":"; + String arnPrefix = "arn:" + partition.id() + ":"; // partition-literal: the text that replaces "arn:aws:" in a bundled policy document, not an ARN List derived = new ArrayList<>(POLICIES.size()); for (ManagedPolicyDef def : POLICIES) { derived.add(new ManagedPolicyDef(partition.id(), def.name(), def.path(), def.description(), diff --git a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java index 6ea8aca96d..93deb14e59 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java @@ -559,10 +559,8 @@ public class Route53ResolverService { if (arn == null || arn.isBlank()) { throw new AwsException(INVALID_PARAMETER, "ResourceArn is required", 400); } - String callerPrefix = "arn:" + AwsRegions.partitionFor(region) + ":route53resolver:" + region + ":" - + accountId + ":"; - String resource = arn.startsWith(callerPrefix) - ? AwsArnUtils.resourceIfArnFor(arn, "route53resolver").orElse("") : ""; + AwsArnUtils.Arn parsed = resolverArnIn(arn, region); + String resource = parsed != null && parsed.accountId().equals(accountId) ? parsed.resource() : ""; int slash = resource.indexOf('/'); StorageBackend store = slash < 0 ? null : switch (resource.substring(0, slash)) { case "resolver-endpoint" -> endpointStore; @@ -577,6 +575,16 @@ public class Route53ResolverService { "Resource not found: " + arn, 400)); } + /** The parsed ARN when it is a Route 53 Resolver ARN in {@code region} and its partition, else null. */ + private static AwsArnUtils.Arn resolverArnIn(String arn, String region) { + if (!AwsArnUtils.isArnFor(arn, "route53resolver")) { + return null; + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + return parsed.partition().equals(AwsRegions.partitionFor(region)) && parsed.region().equals(region) + ? parsed : null; + } + /** * The caller's resource by id. Unlike {@link StorageBackend#get}, which migrates an unprefixed * legacy entry into whichever account reads it first, a legacy entry moves only when @@ -685,13 +693,9 @@ public class Route53ResolverService { if (creatorRequestId == null || creatorRequestId.isBlank()) { return Optional.empty(); } - String regionPrefix = "arn:" + AwsRegions.partitionFor(region) + ":route53resolver:" + region + ":"; return store.scan(key -> true).stream() .filter(existing -> creatorRequestId.equals(text(existing, "CreatorRequestId"))) - .filter(existing -> { - String arn = text(existing, "Arn"); - return arn != null && arn.startsWith(regionPrefix); - }) + .filter(existing -> resolverArnIn(text(existing, "Arn"), region) != null) .findFirst() .map(ObjectNode::deepCopy); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index fe7f6b9d91..133aaca862 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -1181,24 +1181,24 @@ public class SesService { if (tenant == null) { return; } - String arnPrefix = "arn:" + AwsRegions.partitionFor(region) + ":ses:" + region + ":" + callerAccountId() - + ":"; + String accountId = callerAccountId(); List used = new ArrayList<>(); if (source != null && !source.isBlank()) { String identityName = sendIdentityName(source, region); used.add(new SesTenantService.AssociationResource( SesTenantService.RESOURCE_TYPE_IDENTITY, identityName, - arnPrefix + "identity/" + identityName)); + AwsArnUtils.Arn.of("ses", region, accountId, "identity/" + identityName).toString())); } if (effectiveConfigSet != null && !effectiveConfigSet.isBlank()) { used.add(new SesTenantService.AssociationResource( SesTenantService.RESOURCE_TYPE_CONFIGURATION_SET, effectiveConfigSet, - arnPrefix + "configuration-set/" + effectiveConfigSet)); + AwsArnUtils.Arn.of("ses", region, accountId, "configuration-set/" + effectiveConfigSet) + .toString())); } if (templateName != null && !templateName.isBlank()) { used.add(new SesTenantService.AssociationResource( SesTenantService.RESOURCE_TYPE_TEMPLATE, templateName, - arnPrefix + "template/" + templateName)); + AwsArnUtils.Arn.of("ses", region, accountId, "template/" + templateName).toString())); } tenantService.requireResourcesAssociated(tenant, used, region); } diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StatesIntegration.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StatesIntegration.java index 0242023a13..af71e9b3a1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StatesIntegration.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StatesIntegration.java @@ -82,7 +82,7 @@ public record StatesIntegration(String partition, String service, String api, bo /** The id without its suffix, as an error message names it. */ public String withoutSuffix() { - return "arn:" + partition + ":states:::" + (sdk ? "aws-sdk:" : "") + service + ":" + api; + return AwsArnUtils.Arn.global(partition, "states", "", (sdk ? "aws-sdk:" : "") + service + ":" + api).toString(); } @Override diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java index 14ff67883f..570a25d19b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java @@ -531,20 +531,23 @@ public class StepFunctionsService implements Resettable, ResourceProvider { } /** - * Key prefix matching every Step Functions ARN in {@code region}. This store is keyed by the - * full ARN, unlike the other services, which key by {@code region::name}, so the prefix has to - * carry the region's partition or a list cannot find what a create wrote. + * True for a store key that is a Step Functions ARN in {@code region}. This store is keyed by + * the full ARN, unlike the other services, which key by {@code region::name}, so the match has + * to carry the region's partition or a list cannot find what a create wrote. * - *

Deliberately stops before the account segment: the store holds resources for more than - * one account under account isolation, so {@code RegionResolver.buildArn} is the wrong helper - * here, since it appends its own account id. + *

Deliberately ignores the account segment: the store holds resources for more than one + * account under account isolation. */ - private static String regionArnPrefix(String region) { - return "arn:" + AwsRegions.partitionFor(region) + ":states:" + region + ":"; + private static boolean isInRegion(String key, String region) { + if (!AwsArnUtils.isArnFor(key, "states")) { + return false; + } + AwsArnUtils.Arn arn = AwsArnUtils.parse(key); + return arn.partition().equals(AwsRegions.partitionFor(region)) && arn.region().equals(region); } public List listStateMachines(String region) { - return stateMachineStore.scan(k -> k.startsWith(regionArnPrefix(region))); + return stateMachineStore.scan(k -> isInRegion(k, region)); } public PaginatedResult listStateMachines(String region, Integer maxResults, String nextToken) { @@ -1249,8 +1252,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider { } public List listActivities(String region) { - String prefix = regionArnPrefix(region); - return activityStore.scan(k -> k.startsWith(prefix) && k.contains(":activity:")); + return activityStore.scan(k -> isInRegion(k, region) && k.contains(":activity:")); } public PaginatedResult listActivities(String region, Integer maxResults, String nextToken) { diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index e33f8668b5..7d52026d71 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -1,14 +1,6 @@ # category path count # Generated by tools/partition/partition_literals.py --write-baseline; do not hand-edit. -arn-concat src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/IamAuthValidator.java 1 -arn-concat src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java 1 -arn-concat src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java 1 arn-concat src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 1 -arn-concat src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java 2 -arn-concat src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java 2 -arn-concat src/main/java/io/github/hectorvent/floci/services/ses/SesService.java 1 -arn-concat src/main/java/io/github/hectorvent/floci/services/stepfunctions/StatesIntegration.java 1 -arn-concat src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java 1 arn-probe src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java 1 arn-probe src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java 2 arn-probe src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java 8