mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(bedrock): share tag key and value validation (#8811)
This commit is contained in:
1 parent
e6f4758bc1
commit
5b9709677e
9 files changed
+126
-22
No files matched your search
@@ -0,0 +1,22 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Key and value constraints shared by Bedrock and Bedrock AgentCore tags. */
|
||||
public final class BedrockTagKeyValueRules {
|
||||
|
||||
private static final int KEY_MAX = 128;
|
||||
private static final int VALUE_MAX = 256;
|
||||
private static final Pattern TAG_PATTERN = Pattern.compile("[a-zA-Z0-9\\s._:/=+@-]*");
|
||||
|
||||
private BedrockTagKeyValueRules() {
|
||||
}
|
||||
|
||||
public static boolean validKey(String key) {
|
||||
return !key.isEmpty() && key.length() <= KEY_MAX && TAG_PATTERN.matcher(key).matches();
|
||||
}
|
||||
|
||||
public static boolean validValue(String value) {
|
||||
return value.length() <= VALUE_MAX && TAG_PATTERN.matcher(value).matches();
|
||||
}
|
||||
}
|
||||
@@ -415,12 +415,14 @@ public class BedrockController {
|
||||
}
|
||||
|
||||
private static Map<String, String> applicationProfileTags(JsonNode tags) {
|
||||
Map<String, String> parsed = BedrockService.parseTagList(tags);
|
||||
BedrockTagValidator.validate(tags, "key", "value", "ValidationException", "tags");
|
||||
return BedrockService.parseTagList(tags);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
private static Map<String, String> bedrockTagResourceTags(JsonNode tags) {
|
||||
Map<String, String> parsed = BedrockService.parseTagList(tags);
|
||||
BedrockTagValidator.validateRequired(tags, "key", "value", "ValidationException", "tags");
|
||||
return BedrockService.parseTagList(tags);
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
@@ -1150,10 +1150,7 @@ public class BedrockService implements ResourceProvider {
|
||||
if (tagsNode == null || !tagsNode.isArray()) {
|
||||
return tags;
|
||||
}
|
||||
if (tagsNode.size() > TAG_LIST_MAX) {
|
||||
throw new AwsException("ValidationException",
|
||||
"tags must have at most " + TAG_LIST_MAX + " items.", 400);
|
||||
}
|
||||
validateTagListSize(tagsNode, "ValidationException", "tags");
|
||||
for (JsonNode tag : tagsNode) {
|
||||
JsonNode key = tag.get("key");
|
||||
JsonNode value = tag.get("value");
|
||||
@@ -1164,6 +1161,13 @@ public class BedrockService implements ResourceProvider {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public static void validateTagListSize(JsonNode tagsNode, String errorCode, String propertyName) {
|
||||
if (tagsNode.size() > TAG_LIST_MAX) {
|
||||
throw new AwsException(errorCode,
|
||||
propertyName + " must have at most " + TAG_LIST_MAX + " items.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException tooManyTags(String resourceArn, int total) {
|
||||
return new AwsException("TooManyTagsException",
|
||||
"Resource " + resourceArn + " would hold " + total + " tags, over the limit of "
|
||||
|
||||
@@ -2,18 +2,11 @@ package io.floci.aws.services.bedrock;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import io.floci.aws.core.common.BedrockTagKeyValueRules;
|
||||
|
||||
/** Validates the Bedrock TagList shape. */
|
||||
public final class BedrockTagValidator {
|
||||
|
||||
private static final int TAG_LIST_MAX = 200;
|
||||
private static final int TAG_KEY_MIN = 1;
|
||||
private static final int TAG_KEY_MAX = 128;
|
||||
private static final int TAG_VALUE_MAX = 256;
|
||||
private static final Pattern TAG_PATTERN = Pattern.compile("[a-zA-Z0-9\\s._:/=+@-]*");
|
||||
|
||||
private BedrockTagValidator() {
|
||||
}
|
||||
|
||||
@@ -33,9 +26,6 @@ public final class BedrockTagValidator {
|
||||
if (!tags.isArray()) {
|
||||
throw invalid(errorCode, propertyName + " must be an array.");
|
||||
}
|
||||
if (tags.size() > TAG_LIST_MAX) {
|
||||
throw invalid(errorCode, propertyName + " must have at most " + TAG_LIST_MAX + " items.");
|
||||
}
|
||||
for (JsonNode tag : tags) {
|
||||
JsonNode key = tag.path(keyProperty);
|
||||
JsonNode value = tag.path(valueProperty);
|
||||
@@ -45,11 +35,10 @@ public final class BedrockTagValidator {
|
||||
}
|
||||
String keyText = key.textValue();
|
||||
String valueText = value.textValue();
|
||||
if (keyText.length() < TAG_KEY_MIN || keyText.length() > TAG_KEY_MAX
|
||||
|| !TAG_PATTERN.matcher(keyText).matches()) {
|
||||
if (!BedrockTagKeyValueRules.validKey(keyText)) {
|
||||
throw invalid(errorCode, propertyName + " item " + keyProperty + " is invalid.");
|
||||
}
|
||||
if (valueText.length() > TAG_VALUE_MAX || !TAG_PATTERN.matcher(valueText).matches()) {
|
||||
if (!BedrockTagKeyValueRules.validValue(valueText)) {
|
||||
throw invalid(errorCode, propertyName + " item " + valueProperty + " is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -2,6 +2,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;
|
||||
|
||||
/**
|
||||
* AgentCore's tag key/value validation rules, shared by the service classes under this package
|
||||
@@ -24,14 +25,14 @@ final class BedrockAgentCoreTagValidation {
|
||||
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._:/=+@-]*")) {
|
||||
if (!BedrockTagKeyValueRules.validKey(key)) {
|
||||
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._:/=+@-]*")) {
|
||||
if (!BedrockTagKeyValueRules.validValue(value)) {
|
||||
throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400);
|
||||
}
|
||||
});
|
||||
|
||||
+1
@@ -103,6 +103,7 @@ public class BedrockCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (resolved == null || !resolved.isArray()) {
|
||||
throw new AwsException("ValidationError", TYPE + " Tags must be an array.", 400);
|
||||
}
|
||||
BedrockService.validateTagListSize(resolved, "ValidationError", TYPE + " Tags");
|
||||
BedrockTagValidator.validate(resolved, "Key", "Value", "ValidationError",
|
||||
TYPE + " Tags");
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
Reference in new issue
Block a user