From 8451571adb05675219b3282fee7bbad262d3edac Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Tue, 15 Sep 2026 23:09:48 -0400 Subject: [PATCH] refactor(bedrock-agentcore): dedupe AgentCore tag validation (#3710) BedrockAgentCoreCredentialProviderService and BedrockAgentCoreToolsService each carried an identical 22-line validateTags method (AgentCore's tag key/value character-set and length constraints), found via CPD (#3704). This is distinct from the controller-layer duplication already fixed in #3592/#3637 (BedrockAgentCoreControllerSupport); this one was at the service layer. Extracted it into BedrockAgentCoreTagValidation, package-private in bedrockagentcorecontrol, following the same convention as BedrockAgentCoreControllerSupport, and pointed both services at it. No behavior change: the validation rules and error messages are identical, only their location changed. BedrockAgentCoreToolsIntegrationTest already asserts on the exact shared error messages ("tag value does not satisfy AgentCore constraints"), so that path is directly covered. BedrockAgentCoreCredentialProviderService has no dedicated test in src/test today (a pre-existing gap, not introduced here); only compatibility-tests/sdk-test-java exercises it. --- ...ockAgentCoreCredentialProviderService.java | 27 +------------ .../BedrockAgentCoreTagValidation.java | 39 +++++++++++++++++++ .../BedrockAgentCoreToolsService.java | 27 +------------ 3 files changed, 43 insertions(+), 50 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagValidation.java diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreCredentialProviderService.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreCredentialProviderService.java index 36b0eaafdb..b3ed7ab5c3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreCredentialProviderService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreCredentialProviderService.java @@ -69,7 +69,7 @@ public class BedrockAgentCoreCredentialProviderService { throw new AwsException("ValidationException", "jsonKey must be between 1 and 128 characters", 400); } } - validateTags(request.get("tags")); + BedrockAgentCoreTagValidation.validateTags(request.get("tags")); Instant now = Instant.now(); ObjectNode item = JsonNodeFactory.instance.objectNode(); item.put("name", name); @@ -196,7 +196,7 @@ public class BedrockAgentCoreCredentialProviderService { if (clientSecret != null && clientSecret.length() > 2048) { throw new AwsException("ValidationException", "clientSecret exceeds maximum length of 2048", 400); } - validateTags(request.get("tags")); + BedrockAgentCoreTagValidation.validateTags(request.get("tags")); Instant now = Instant.now(); ObjectNode item = JsonNodeFactory.instance.objectNode(); @@ -436,29 +436,6 @@ public class BedrockAgentCoreCredentialProviderService { } } - private static void validateTags(JsonNode tags) { - if (tags == null || tags.isNull()) { - return; - } - if (!tags.isObject() || tags.size() > 50) { - throw new AwsException("ValidationException", "tags must be an object with at most 50 entries", 400); - } - tags.fields().forEachRemaining(entry -> { - String key = entry.getKey(); - JsonNode rawValue = entry.getValue(); - if (key.length() < 1 || key.length() > 128 || !key.matches("[a-zA-Z0-9\\s._:/=+@-]*")) { - throw new AwsException("ValidationException", "tag key does not satisfy AgentCore constraints", 400); - } - if (!rawValue.isTextual()) { - throw new AwsException("ValidationException", "tag value must be a string", 400); - } - String value = rawValue.asText(); - if (value.length() > 256 || !value.matches("[a-zA-Z0-9\\s._:/=+@-]*")) { - throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400); - } - }); - } - private static String text(JsonNode node, String field) { JsonNode value = node.get(field); return value == null || value.isNull() ? null : value.asText(); diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagValidation.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagValidation.java new file mode 100644 index 0000000000..4e73821f3a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagValidation.java @@ -0,0 +1,39 @@ +package io.github.hectorvent.floci.services.bedrockagentcorecontrol; + +import com.fasterxml.jackson.databind.JsonNode; +import io.github.hectorvent.floci.core.common.AwsException; + +/** + * AgentCore's tag key/value validation rules, shared by the service classes under this package + * that accept a {@code tags} field on create/update requests. {@code BedrockAgentCoreCredentialProviderService} + * and {@code BedrockAgentCoreToolsService} each carried their own copy of this, found duplicated + * by CPD. + */ +final class BedrockAgentCoreTagValidation { + + private BedrockAgentCoreTagValidation() { + } + + static void validateTags(JsonNode tags) { + if (tags == null || tags.isNull()) { + return; + } + if (!tags.isObject() || tags.size() > 50) { + throw new AwsException("ValidationException", "tags must be an object with at most 50 entries", 400); + } + tags.fields().forEachRemaining(entry -> { + String key = entry.getKey(); + JsonNode rawValue = entry.getValue(); + if (key.length() < 1 || key.length() > 128 || !key.matches("[a-zA-Z0-9\\s._:/=+@-]*")) { + throw new AwsException("ValidationException", "tag key does not satisfy AgentCore constraints", 400); + } + if (!rawValue.isTextual()) { + throw new AwsException("ValidationException", "tag value must be a string", 400); + } + String value = rawValue.asText(); + if (value.length() > 256 || !value.matches("[a-zA-Z0-9\\s._:/=+@-]*")) { + throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400); + } + }); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsService.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsService.java index 634a64e30d..350d263846 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsService.java @@ -106,7 +106,7 @@ public class BedrockAgentCoreToolsService { "name must match [a-zA-Z][a-zA-Z0-9_]{0,47}", 400); } validateDescription(request.get("description")); - validateTags(request.get("tags")); + BedrockAgentCoreTagValidation.validateTags(request.get("tags")); String clientToken = optionalText(request, "clientToken"); if (clientToken != null) { if (clientToken.length() < 33 || clientToken.length() > 256 @@ -345,7 +345,7 @@ public class BedrockAgentCoreToolsService { private static void validateCommonCreateFields(ObjectNode request, boolean browser) { validateDescription(request.get("description")); - validateTags(request.get("tags")); + BedrockAgentCoreTagValidation.validateTags(request.get("tags")); JsonNode executionRoleArn = request.get("executionRoleArn"); if (executionRoleArn != null && !executionRoleArn.isNull()) { if (!executionRoleArn.isTextual()) { @@ -384,29 +384,6 @@ public class BedrockAgentCoreToolsService { } } - private static void validateTags(JsonNode tags) { - if (tags == null || tags.isNull()) { - return; - } - if (!tags.isObject() || tags.size() > 50) { - throw new AwsException("ValidationException", "tags must be an object with at most 50 entries", 400); - } - tags.fields().forEachRemaining(entry -> { - String key = entry.getKey(); - JsonNode rawValue = entry.getValue(); - if (key.length() < 1 || key.length() > 128 || !key.matches("[a-zA-Z0-9\\s._:/=+@-]*")) { - throw new AwsException("ValidationException", "tag key does not satisfy AgentCore constraints", 400); - } - if (!rawValue.isTextual()) { - throw new AwsException("ValidationException", "tag value must be a string", 400); - } - String value = rawValue.asText(); - if (value.length() > 256 || !value.matches("[a-zA-Z0-9\\s._:/=+@-]*")) { - throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400); - } - }); - } - private static void validateNetworkConfiguration(JsonNode networkConfiguration, boolean codeInterpreter) { JsonNode modeNode = networkConfiguration.get("networkMode"); if (modeNode == null || !modeNode.isTextual() || modeNode.asText().isBlank()) {