mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
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.
This commit is contained in:
1 parent
c30df83781
commit
8451571adb
3 files changed
+43
-50
No files matched your search
+2
-25
@@ -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();
|
||||
|
||||
+39
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+2
-25
@@ -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()) {
|
||||
|
||||
Reference in new issue
Block a user