mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sfn): validate Map numeric path fields (#3827)
* fix(sfn): validate Map numeric path fields * fix(sfn): enforce ItemBatcher byte limit * fix(sfn): import BigDecimal for map number validation
This commit is contained in:
1 parent
c0ed88d647
commit
76a0b865ca
2 files changed
+159
-2
No files matched your search
+77
-2
@@ -30,6 +30,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -2397,7 +2398,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
if ("Map".equals(stateType)) {
|
||||
validateMapConcurrency(statePath, stateDef, stateIsJsonata, errors);
|
||||
if (stateDef.has("ItemReader")) {
|
||||
validateItemReader(statePath, stateDef, errors);
|
||||
validateItemReader(statePath, stateDef, stateIsJsonata, errors);
|
||||
}
|
||||
if (stateDef.has("ResultWriter")) {
|
||||
validateResultWriter(statePath, stateDef.get("ResultWriter"), stateIsJsonata, errors);
|
||||
@@ -2605,6 +2606,73 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
+ " at " + statePath);
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode itemBatcher = stateDef.path("ItemBatcher");
|
||||
if (itemBatcher.isObject()) {
|
||||
validateMapNumberField(statePath + "/ItemBatcher", itemBatcher, jsonata,
|
||||
statePath, "MaxItemsPerBatch", "MaxItemsPerBatchPath", false, null, errors);
|
||||
validateMapNumberField(statePath + "/ItemBatcher", itemBatcher, jsonata,
|
||||
statePath, "MaxInputBytesPerBatch", "MaxInputBytesPerBatchPath", false, 262_144, errors);
|
||||
}
|
||||
|
||||
validateMapNumberField(statePath, stateDef, jsonata,
|
||||
statePath, "ToleratedFailureCount", "ToleratedFailureCountPath", false, null, errors);
|
||||
validateMapNumberField(statePath, stateDef, jsonata,
|
||||
statePath, "ToleratedFailurePercentage", "ToleratedFailurePercentagePath", true, 100, errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates one Map numeric field and its mutually-exclusive JSONPath spelling. AWS treats
|
||||
* these fields as JSONPath-only when the effective language is JSONata, while JSONata numeric
|
||||
* expressions are accepted in the literal spelling. The explicit markers preserve the exact
|
||||
* diagnostic location for nested ItemBatcher fields and messages that do not name a field.
|
||||
*/
|
||||
private static void validateMapNumberField(String containerPath, JsonNode container,
|
||||
boolean jsonata, String languageLocation,
|
||||
String literalField,
|
||||
String pathField, boolean decimal, Integer maximum,
|
||||
List<String> errors) {
|
||||
boolean hasLiteral = container.has(literalField);
|
||||
boolean hasPath = container.has(pathField);
|
||||
if (hasLiteral && hasPath) {
|
||||
errors.add(EXPLICIT_LOCATION_MARKER
|
||||
+ "There should only be one of the following fields: [" + literalField + ", "
|
||||
+ pathField + "]" + MARKER_PAYLOAD_SEPARATOR + containerPath + "/" + literalField);
|
||||
}
|
||||
|
||||
if (jsonata && hasPath) {
|
||||
errors.add(EXPLICIT_LOCATION_MARKER + "The QueryLanguage is set to 'JSONata', but field '"
|
||||
+ pathField + "' is only supported for the 'JSONPath' QueryLanguage"
|
||||
+ MARKER_PAYLOAD_SEPARATOR + languageLocation);
|
||||
}
|
||||
|
||||
if (hasLiteral) {
|
||||
JsonNode value = container.get(literalField);
|
||||
boolean expression = jsonata && value.isTextual()
|
||||
&& JsonataEvaluator.isExpression(value.asText());
|
||||
boolean numeric = decimal ? value.isNumber() : value.isIntegralNumber();
|
||||
if (!numeric && !expression) {
|
||||
errors.add(EXPLICIT_LOCATION_MARKER + "Expected value of type ["
|
||||
+ (decimal ? "NUMBER" : "INTEGER") + "]"
|
||||
+ MARKER_PAYLOAD_SEPARATOR + containerPath + "/" + literalField);
|
||||
} else if (numeric && value.decimalValue().signum() < 0) {
|
||||
errors.add(EXPLICIT_LOCATION_MARKER + "Minimum value is "
|
||||
+ (decimal ? "0.0" : "0") + MARKER_PAYLOAD_SEPARATOR
|
||||
+ containerPath + "/" + literalField);
|
||||
} else if (numeric && maximum != null
|
||||
&& value.decimalValue().compareTo(BigDecimal.valueOf(maximum)) > 0) {
|
||||
errors.add(EXPLICIT_LOCATION_MARKER + "Maximum value is " + maximum
|
||||
+ MARKER_PAYLOAD_SEPARATOR + containerPath + "/" + literalField);
|
||||
}
|
||||
}
|
||||
|
||||
if (!jsonata && hasPath) {
|
||||
JsonNode value = container.get(pathField);
|
||||
if (!value.isTextual() || !isReferencePath(value.asText())) {
|
||||
errors.add(EXPLICIT_LOCATION_MARKER + "Value is not a Reference Path"
|
||||
+ MARKER_PAYLOAD_SEPARATOR + containerPath + "/" + pathField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isReferencePath(String path) {
|
||||
@@ -2672,7 +2740,8 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void validateItemReader(String statePath, JsonNode stateDef, List<String> errors) {
|
||||
private void validateItemReader(String statePath, JsonNode stateDef, boolean jsonata,
|
||||
List<String> errors) {
|
||||
JsonNode itemReader = stateDef.get("ItemReader");
|
||||
String resource = itemReader.path("Resource").asText(null);
|
||||
if (resource != null && !ITEM_READER_RESOURCES.contains(resource)) {
|
||||
@@ -2687,6 +2756,12 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
+ "[MANIFEST, JSON, CSV, JSONL, PARQUET]"
|
||||
+ " at " + statePath + "/ItemReader/ReaderConfig/InputType");
|
||||
}
|
||||
|
||||
JsonNode readerConfig = itemReader.path("ReaderConfig");
|
||||
if (readerConfig.isObject()) {
|
||||
validateMapNumberField(statePath + "/ItemReader/ReaderConfig", readerConfig, jsonata,
|
||||
statePath, "MaxItems", "MaxItemsPath", false, 100_000_000, errors);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateResultWriter(String statePath, JsonNode writer,
|
||||
|
||||
+82
@@ -425,6 +425,88 @@ class StepFunctionsValidateStateMachineDefinitionIntegrationTest {
|
||||
.body("diagnostics", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonataMapRejectsNestedNumberPathFields() {
|
||||
String def = """
|
||||
{"QueryLanguage":"JSONata","StartAt":"M","States":{"M":{"Type":"Map",
|
||||
"ItemBatcher":{"MaxItemsPerBatchPath":"$.batch"},
|
||||
"ToleratedFailurePercentagePath":"$.failure",
|
||||
"ItemReader":{"ReaderConfig":{"MaxItemsPath":"$.max"}},
|
||||
"ItemProcessor":{"StartAt":"P","States":{"P":{"Type":"Pass","End":true}}},
|
||||
"End":true}}}
|
||||
""";
|
||||
|
||||
validateDefinition(def)
|
||||
.then().statusCode(200)
|
||||
.body("result", equalTo("FAIL"))
|
||||
.body("diagnostics", hasSize(3))
|
||||
.body("diagnostics[0].message", containsString("MaxItemsPerBatchPath"))
|
||||
.body("diagnostics[0].location", equalTo("/States/M"))
|
||||
.body("diagnostics[1].message", containsString("ToleratedFailurePercentagePath"))
|
||||
.body("diagnostics[1].location", equalTo("/States/M"))
|
||||
.body("diagnostics[2].message", containsString("MaxItemsPath"))
|
||||
.body("diagnostics[2].location", equalTo("/States/M"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapNumberPathFieldsMustBeMutuallyExclusiveAndReferencePaths() {
|
||||
String def = mapDefinition("", "\"ItemBatcher\":{\"MaxItemsPerBatch\":2,"
|
||||
+ "\"MaxItemsPerBatchPath\":\"$.batch\"},"
|
||||
+ "\"ToleratedFailureCountPath\":\"$$.Execution.Input.count\",");
|
||||
|
||||
validateDefinition(def)
|
||||
.then().statusCode(200)
|
||||
.body("result", equalTo("FAIL"))
|
||||
.body("diagnostics", hasSize(2))
|
||||
.body("diagnostics[0].message", containsString("MaxItemsPerBatch"))
|
||||
.body("diagnostics[0].location", equalTo("/States/M/ItemBatcher/MaxItemsPerBatch"))
|
||||
.body("diagnostics[1].message", equalTo("Value is not a Reference Path"))
|
||||
.body("diagnostics[1].location", equalTo("/States/M/ToleratedFailureCountPath"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapNumberLiteralsEnforcePercentageAndReaderMaximums() {
|
||||
String def = mapDefinition("", "\"ItemBatcher\":{\"MaxInputBytesPerBatch\":262145},"
|
||||
+ "\"ToleratedFailurePercentage\":100.1,"
|
||||
+ "\"ItemReader\":{\"ReaderConfig\":{\"MaxItems\":100000001}},");
|
||||
|
||||
validateDefinition(def)
|
||||
.then().statusCode(200)
|
||||
.body("result", equalTo("FAIL"))
|
||||
.body("diagnostics", hasSize(3))
|
||||
.body("diagnostics[0].message", equalTo("Maximum value is 262144"))
|
||||
.body("diagnostics[0].location", equalTo("/States/M/ItemBatcher/MaxInputBytesPerBatch"))
|
||||
.body("diagnostics[1].message", equalTo("Maximum value is 100"))
|
||||
.body("diagnostics[1].location", equalTo("/States/M/ToleratedFailurePercentage"))
|
||||
.body("diagnostics[2].message", equalTo("Maximum value is 100000000"))
|
||||
.body("diagnostics[2].location", equalTo("/States/M/ItemReader/ReaderConfig/MaxItems"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapInputBytesAcceptsDocumentedMaximumAndReferencePath() {
|
||||
String maximum = mapDefinition("", "\"ItemBatcher\":{\"MaxInputBytesPerBatch\":262144},");
|
||||
validateDefinition(maximum)
|
||||
.then().statusCode(200)
|
||||
.body("result", equalTo("OK"))
|
||||
.body("diagnostics", hasSize(0));
|
||||
|
||||
String path = mapDefinition("", "\"ItemBatcher\":{\"MaxInputBytesPerBatchPath\":\"$.bytes\"},");
|
||||
validateDefinition(path)
|
||||
.then().statusCode(200)
|
||||
.body("result", equalTo("OK"))
|
||||
.body("diagnostics", hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStateMachineRejectsMapNumberPathInJsonata() {
|
||||
String def = mapDefinition("\"QueryLanguage\":\"JSONata\",",
|
||||
"\"ToleratedFailureCountPath\":\"$.count\",");
|
||||
|
||||
createStateMachine("map-number-validation-3733", def)
|
||||
.then().statusCode(400)
|
||||
.body("__type", containsString("InvalidDefinition"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedItemReaderResource_returnsFailWithSchemaError() {
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET)
|
||||
|
||||
Reference in new issue
Block a user