Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

refactor(bedrock-agentcore): dedupe AgentCore tag validation (#3710)

Priamo Germosen8451571adb05
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()) {