From 5bf89a50611a0e21aa0fd3326a748ef8680dce77 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Thu, 29 Oct 2026 20:39:02 -0500 Subject: [PATCH] refactor: read and check ARNs through AwsArnUtils in Bedrock, AgentCore and SageMaker (#6970) The arn-probe and arn-split sites in these services now go through looksLikeArn, isArn, parse and regionOrDefault. The four AgentCore services share BedrockAgentCoreArns.resource, which reads an ARN's resource without its qualifier. A well-formed ARN reads the same as before. tools/partition/baseline.tsv drops from 44 to 34. Signed-off-by: Hector Ventura --- .../services/bedrock/BedrockService.java | 6 ++-- .../BedrockAgentCoreArns.java | 27 ++++++++++++++++++ .../BedrockAgentCoreControlService.java | 18 ++++++------ .../BedrockAgentCoreGatewayService.java | 6 ++-- .../BedrockAgentCoreMemoryService.java | 6 ++-- .../BedrockAgentCoreTagHandler.java | 17 +++++------ .../services/sagemaker/SageMakerService.java | 6 ++-- .../BedrockAgentCoreArnsTest.java | 28 +++++++++++++++++++ tools/partition/baseline.tsv | 6 ---- 9 files changed, 85 insertions(+), 35 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java index 4017083780..68d9d8f788 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java @@ -333,7 +333,7 @@ public class BedrockService implements ResourceProvider { return null; } String value = identifier.asText(); - String profileId = value.startsWith("arn:") + String profileId = AwsArnUtils.looksLikeArn(value) ? value.substring(value.lastIndexOf('/') + 1) : value; ObjectNode details = objectMapper.createObjectNode(); @@ -413,7 +413,7 @@ public class BedrockService implements ResourceProvider { } String guardrailId = resolveGuardrailId(resourceArn); return guardrails.get(storageKey(region, guardrailId, DRAFT_VERSION)) - .filter(guardrail -> !resourceArn.startsWith("arn:") || resourceArn.equals(guardrail.getGuardrailArn())) + .filter(guardrail -> !AwsArnUtils.looksLikeArn(resourceArn) || resourceArn.equals(guardrail.getGuardrailArn())) .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Resource " + resourceArn + " does not exist.", 404)); } @@ -422,7 +422,7 @@ public class BedrockService implements ResourceProvider { if (identifier == null || identifier.isBlank()) { throw new AwsException("ValidationException", "guardrailIdentifier is required", 400); } - if (!identifier.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(identifier)) { return identifier; } String resource; diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java new file mode 100644 index 0000000000..88414ad85f --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java @@ -0,0 +1,27 @@ +package io.github.hectorvent.floci.services.bedrockagentcorecontrol; + +import io.github.hectorvent.floci.core.common.AwsArnUtils; + +/** + * Reads the resource out of an AgentCore ARN for the service classes under this package that look + * a runtime, endpoint, gateway or memory up by ARN. + */ +final class BedrockAgentCoreArns { + + private BedrockAgentCoreArns() { + } + + /** + * The resource of an AgentCore ARN without its qualifier: {@code agent/} for + * {@code arn:aws:bedrock-agentcore:::agent/:}. Null when the value + * is not an ARN. + */ + static String resource(String arn) { + if (!AwsArnUtils.isArn(arn)) { + return null; + } + String resource = AwsArnUtils.parse(arn).resource(); + int qualifier = resource.indexOf(':'); + return qualifier < 0 ? resource : resource.substring(0, qualifier); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java index be28bf7723..74d6a2ecc7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java @@ -280,11 +280,11 @@ public class BedrockAgentCoreControlService { * validated here — the data plane treats them permissively. */ public boolean runtimeArnExists(String region, String arn) { - String[] parts = arn == null ? new String[0] : arn.split(":"); - if (parts.length < 6 || !parts[5].startsWith("agent/")) { + String resource = BedrockAgentCoreArns.resource(arn); + if (resource == null || !resource.startsWith("agent/")) { return true; } - String uuid = parts[5].substring("agent/".length()); + String uuid = resource.substring("agent/".length()); String prefix = keyPrefix(region); return storage.scan(k -> k.startsWith(prefix)).stream() .anyMatch(r -> uuid.equals(r.getUuid())); @@ -325,11 +325,11 @@ public class BedrockAgentCoreControlService { private static String uuidFromArn(String arn) { // arn:aws:bedrock-agentcore:::agent/: - String[] parts = arn == null ? new String[0] : arn.split(":"); - if (parts.length < 6 || !parts[5].startsWith("agent/")) { + String resource = BedrockAgentCoreArns.resource(arn); + if (resource == null || !resource.startsWith("agent/")) { throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400); } - return parts[5].substring("agent/".length()); + return resource.substring("agent/".length()); } // ──────────────────────────── Endpoints (Phase 2) ──────────────────────────── @@ -453,11 +453,11 @@ public class BedrockAgentCoreControlService { * API, so this is the only way back from the ARN CloudFormation keeps as the physical id. */ public Optional findEndpointByArn(String region, String arn) { - String[] parts = arn == null ? new String[0] : arn.split(":"); - if (parts.length < 6 || !parts[5].startsWith("agentEndpoint/")) { + String resource = BedrockAgentCoreArns.resource(arn); + if (resource == null || !resource.startsWith("agentEndpoint/")) { return Optional.empty(); } - String uuid = parts[5].substring("agentEndpoint/".length()); + String uuid = resource.substring("agentEndpoint/".length()); String prefix = keyPrefix(region); for (AgentRuntime runtime : storage.scan(k -> k.startsWith(prefix))) { for (AgentRuntimeEndpoint endpoint : runtime.getEndpoints()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java index 0e4dc2af53..b7249f40f6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java @@ -99,11 +99,11 @@ public class BedrockAgentCoreGatewayService { } private Gateway findByArn(String region, String arn) { - String[] parts = arn == null ? new String[0] : arn.split(":"); - if (parts.length < 6 || !parts[5].startsWith("gateway/")) { + String resource = BedrockAgentCoreArns.resource(arn); + if (resource == null || !resource.startsWith("gateway/")) { throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400); } - return get(parts[5].substring("gateway/".length()), region); + return get(resource.substring("gateway/".length()), region); } public Gateway get(String id, String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java index 02e8e144ae..4b26da6d29 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java @@ -226,11 +226,11 @@ public class BedrockAgentCoreMemoryService { /** The memory a memory ARN names; ValidationException for any other ARN, ResourceNotFoundException when gone. */ public Memory getByArn(String region, String arn) { - String[] parts = arn == null ? new String[0] : arn.split(":"); - if (parts.length < 6 || !parts[5].startsWith("memory/")) { + String resource = BedrockAgentCoreArns.resource(arn); + if (resource == null || !resource.startsWith("memory/")) { throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400); } - return get(parts[5].substring("memory/".length()), region); + return get(resource.substring("memory/".length()), region); } private static String sanitize(String name) { diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java index fef0c91a81..87b1e66778 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.bedrockagentcorecontrol; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.TagHandler; @@ -69,19 +70,19 @@ public class BedrockAgentCoreTagHandler implements TagHandler { private enum ResourceKind { RUNTIME, GATEWAY, MEMORY } private ResourceKind kind(String region, String arn) { - String[] parts = arn == null ? new String[0] : arn.split(":"); - if (parts.length < 6) { + String resource = BedrockAgentCoreArns.resource(arn); + if (resource == null) { throw new AwsException("ValidationException", "Tagging is not supported for this AgentCore resource: " + arn, 400); } - requireLocalIdentity(region, arn, parts); - if (parts[5].startsWith("gateway/")) { + requireLocalIdentity(region, arn, AwsArnUtils.parse(arn)); + if (resource.startsWith("gateway/")) { return ResourceKind.GATEWAY; } - if (parts[5].startsWith("agent/")) { + if (resource.startsWith("agent/")) { return ResourceKind.RUNTIME; } - if (parts[5].startsWith("memory/")) { + if (resource.startsWith("memory/")) { return ResourceKind.MEMORY; } // Not a runtime, gateway, or memory → not a taggable AgentCore resource. @@ -95,8 +96,8 @@ public class BedrockAgentCoreTagHandler implements TagHandler { * account or region and read or change its tags. Reported as not found rather than a * validation error, so the response does not confirm what exists behind the foreign identity. */ - private void requireLocalIdentity(String region, String arn, String[] parts) { - if (!region.equals(parts[3]) || !regionResolver.getAccountId().equals(parts[4])) { + private void requireLocalIdentity(String region, String arn, AwsArnUtils.Arn parsed) { + if (!region.equals(parsed.region()) || !regionResolver.getAccountId().equals(parsed.accountId())) { throw new AwsException("ResourceNotFoundException", "AgentCore resource not found: " + arn, 404); } diff --git a/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java b/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java index 175f4abd94..55251785c5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java @@ -547,11 +547,11 @@ public class SageMakerService { /** ARN shape: {@code arn:aws:sagemaker:::/}. */ private static String arnRegion(String arn) { - String[] parts = arn.split(":", 6); - if (parts.length < 4 || parts[3].isBlank()) { + String region = AwsArnUtils.regionOrDefault(arn, ""); + if (region.isBlank()) { throw validation("ResourceArn is invalid"); } - return parts[3]; + return region; } private static String regionKey(String region, String name) { diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java new file mode 100644 index 0000000000..6ae0de6e7d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java @@ -0,0 +1,28 @@ +package io.github.hectorvent.floci.services.bedrockagentcorecontrol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class BedrockAgentCoreArnsTest { + + @Test + void resourceDropsTheQualifier() { + assertEquals("agent/abc", + BedrockAgentCoreArns.resource("arn:aws:bedrock-agentcore:us-east-1:123456789012:agent/abc:7")); + } + + @Test + void resourceWithoutAQualifierIsKeptWhole() { + assertEquals("memory/m-1", + BedrockAgentCoreArns.resource("arn:aws-us-gov:bedrock-agentcore:us-gov-west-1:123456789012:memory/m-1")); + } + + @Test + void resourceIsNullForAnythingThatIsNotAnArn() { + assertNull(BedrockAgentCoreArns.resource(null)); + assertNull(BedrockAgentCoreArns.resource("agent/abc")); + assertNull(BedrockAgentCoreArns.resource("arn:aws:bedrock-agentcore:us-east-1")); + } +} diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index dad13a1b86..e33f8668b5 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -11,17 +11,11 @@ arn-concat src/main/java/io/github/hectorvent/floci/services/stepfunctions/State 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/bedrock/BedrockService.java 3 arn-probe src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java 8 arn-probe src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java 3 arn-probe src/main/java/io/github/hectorvent/floci/services/ssooidc/SsoOidcController.java 2 arn-split src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java 3 -arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 3 arn-split src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java 1