mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(bedrock-agentcore): use modeled tag constraint errors (#9193)
This commit is contained in:
1 parent
dda3b96dee
commit
ee722e8da3
5 files changed
+114
-18
No files matched your search
@@ -19,4 +19,26 @@ public final class BedrockTagKeyValueRules {
|
||||
public static boolean validValue(String value) {
|
||||
return value.length() <= VALUE_MAX && TAG_PATTERN.matcher(value).matches();
|
||||
}
|
||||
|
||||
public static String keyViolation(String key) {
|
||||
return stringViolation("tags.key", "Map keys", key, 1, KEY_MAX);
|
||||
}
|
||||
|
||||
public static String valueViolation(String value) {
|
||||
return stringViolation("tags.value", "Map value", value, 0, VALUE_MAX);
|
||||
}
|
||||
|
||||
private static String stringViolation(String path, String mapConstraint, String value,
|
||||
int minLength, int maxLength) {
|
||||
if (value == null) {
|
||||
return SmithyValidationErrors.notNull(path);
|
||||
}
|
||||
if (SmithyValidationErrors.lengthOutOfRange(path, value, minLength, maxLength) == null
|
||||
&& TAG_PATTERN.matcher(value).matches()) {
|
||||
return null;
|
||||
}
|
||||
return SmithyValidationErrors.Violations.WITHOUT_VALUES.constraint("tags", value,
|
||||
mapConstraint + " must satisfy constraint: "
|
||||
+ SmithyValidationErrors.constraintSet(minLength, maxLength, TAG_PATTERN.pattern(), null));
|
||||
}
|
||||
}
|
||||
+19
-6
@@ -3,6 +3,7 @@ package io.floci.aws.services.bedrockagentcorecontrol;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.BedrockTagKeyValueRules;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -23,10 +24,11 @@ final class BedrockAgentCoreTagValidation {
|
||||
if (tags == null || tags.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (!tags.isObject() || tags.size() > MAX_TAGS) {
|
||||
if (!tags.isObject()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"tags must be an object with at most " + MAX_TAGS + " entries", 400);
|
||||
}
|
||||
validateTagCount(tags.size());
|
||||
tags.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey();
|
||||
JsonNode rawValue = entry.getValue();
|
||||
@@ -39,10 +41,11 @@ final class BedrockAgentCoreTagValidation {
|
||||
}
|
||||
|
||||
static void validateTagMap(Map<String, String> tags) {
|
||||
if (tags == null || tags.size() > MAX_TAGS) {
|
||||
if (tags == null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"tags must be an object with at most " + MAX_TAGS + " entries", 400);
|
||||
}
|
||||
validateTagCount(tags.size());
|
||||
tags.forEach((key, value) -> {
|
||||
validateKey(key);
|
||||
validateValue(value);
|
||||
@@ -50,8 +53,9 @@ final class BedrockAgentCoreTagValidation {
|
||||
}
|
||||
|
||||
private static void validateKey(String key) {
|
||||
if (key == null || !BedrockTagKeyValueRules.validKey(key)) {
|
||||
throw new AwsException("ValidationException", "tag key does not satisfy AgentCore constraints", 400);
|
||||
String violation = BedrockTagKeyValueRules.keyViolation(key);
|
||||
if (violation != null) {
|
||||
throw SmithyValidationErrors.validationException(violation);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,8 +63,17 @@ final class BedrockAgentCoreTagValidation {
|
||||
if (value == null) {
|
||||
throw new AwsException("ValidationException", "tag value must be a string", 400);
|
||||
}
|
||||
if (!BedrockTagKeyValueRules.validValue(value)) {
|
||||
throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400);
|
||||
String violation = BedrockTagKeyValueRules.valueViolation(value);
|
||||
if (violation != null) {
|
||||
throw SmithyValidationErrors.validationException(violation);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateTagCount(int size) {
|
||||
String violation = SmithyValidationErrors.sizeOutOfRange("tags", size, 0, MAX_TAGS);
|
||||
if (violation != null) {
|
||||
throw SmithyValidationErrors.validationException(violation);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+2
@@ -6,6 +6,8 @@ import java.util.Map;
|
||||
|
||||
final class BedrockAgentCoreTagFixtures {
|
||||
|
||||
static final String EXPECTED_TAG_PATTERN = "[a-zA-Z0-9\\s._:/=+@-]*";
|
||||
|
||||
private BedrockAgentCoreTagFixtures() {
|
||||
}
|
||||
|
||||
|
||||
+66
-11
@@ -5,9 +5,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@@ -26,23 +32,72 @@ class BedrockAgentCoreTagValidationTest {
|
||||
|
||||
BedrockAgentCoreTagValidation.validateTags(tags);
|
||||
tags.put("extra", "value");
|
||||
assertInvalid(tags, "tags must be an object with at most 50 entries");
|
||||
assertInvalid(tags, "1 validation error detected: Value at 'tags' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 50");
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsAgentCoreKeyAndValueErrors() {
|
||||
for (String key : List.of("", "k".repeat(129), "bad?key")) {
|
||||
assertInvalid(MAPPER.createObjectNode().put(key, "value"),
|
||||
"tag key does not satisfy AgentCore constraints");
|
||||
}
|
||||
for (String value : List.of("v".repeat(257), "bad?value")) {
|
||||
assertInvalid(MAPPER.createObjectNode().put("key", value),
|
||||
"tag value does not satisfy AgentCore constraints");
|
||||
}
|
||||
void keepsMalformedTagTypeErrors() {
|
||||
assertInvalid(MAPPER.createObjectNode().put("key", 123), "tag value must be a string");
|
||||
assertInvalid(MAPPER.createArrayNode(), "tags must be an object with at most 50 entries");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("modeledTagFailures")
|
||||
void createTagRefusalsUseSmithyMessages(Map<String, String> tags, String message) {
|
||||
assertInvalid(MAPPER.valueToTree(tags), message);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("modeledTagFailures")
|
||||
void tagResourceRefusalsUseSmithyMessages(Map<String, String> tags, String message) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> BedrockAgentCoreTagValidation.validateTagMap(tags));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(message, error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preservesEmptyTagsAndModeledBoundariesOnBothPaths() {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTags(MAPPER.valueToTree(tags)));
|
||||
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTagMap(tags));
|
||||
tags.put(" ", "");
|
||||
tags.put("k".repeat(128), "v".repeat(256));
|
||||
for (int i = 0; i < 48; i++) {
|
||||
tags.put("key-" + i, "value-" + i);
|
||||
}
|
||||
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTags(MAPPER.valueToTree(tags)));
|
||||
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTagMap(tags));
|
||||
assertDoesNotThrow(() -> BedrockAgentCoreTagValidation.validateTags(null));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> modeledTagFailures() {
|
||||
String prefix = "1 validation error detected: ";
|
||||
String keyRules = "Value at 'tags' failed to satisfy constraint: Map keys must satisfy constraint: ["
|
||||
+ "Member must have length less than or equal to 128, "
|
||||
+ "Member must have length greater than or equal to 1, "
|
||||
+ "Member must satisfy regular expression pattern: "
|
||||
+ BedrockAgentCoreTagFixtures.EXPECTED_TAG_PATTERN + "]";
|
||||
String valueRules = "Value at 'tags' failed to satisfy constraint: Map value must satisfy constraint: ["
|
||||
+ "Member must have length less than or equal to 256, "
|
||||
+ "Member must satisfy regular expression pattern: "
|
||||
+ BedrockAgentCoreTagFixtures.EXPECTED_TAG_PATTERN + "]";
|
||||
Map<String, String> tooMany = new LinkedHashMap<>();
|
||||
for (int i = 0; i < 51; i++) {
|
||||
tooMany.put("key-" + i, "value");
|
||||
}
|
||||
return Stream.of(
|
||||
Arguments.of(Map.of("", "value"), prefix + keyRules),
|
||||
Arguments.of(Map.of("k".repeat(129), "value"), prefix + keyRules),
|
||||
Arguments.of(Map.of("bad?key", "value"), prefix + keyRules),
|
||||
Arguments.of(Map.of("key", "v".repeat(257)), prefix + valueRules),
|
||||
Arguments.of(Map.of("key", "bad?value"), prefix + valueRules),
|
||||
Arguments.of(tooMany, prefix + "Value at 'tags' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 50"));
|
||||
}
|
||||
|
||||
private static void assertInvalid(JsonNode tags, String message) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> BedrockAgentCoreTagValidation.validateTags(tags));
|
||||
|
||||
+5
-1
@@ -906,7 +906,11 @@ class BedrockAgentCoreToolsIntegrationTest {
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("tag value does not satisfy AgentCore constraints"));
|
||||
.body("message", equalTo("1 validation error detected: Value at 'tags' "
|
||||
+ "failed to satisfy constraint: Map value must satisfy constraint: ["
|
||||
+ "Member must have length less than or equal to 256, "
|
||||
+ "Member must satisfy regular expression pattern: "
|
||||
+ BedrockAgentCoreTagFixtures.EXPECTED_TAG_PATTERN + "]"));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
|
||||
Reference in new issue
Block a user