mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sfn): fail States.Runtime on an unresolvable JSONPath in a payload template (#3199)
A ".$" Parameters/ResultSelector/ItemSelector field whose JSONPath does not resolve against the input now fails the state with States.Runtime instead of silently resolving to null, matching real AWS. An out-of-range array index keeps resolving to null, the one leniency AWS actually has here. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * Fix #2521
This commit is contained in:
1 parent
5fd89e27bc
commit
618f52e758
3 files changed
+422
-2
No files matched your search
+158
@@ -0,0 +1,158 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* A {@code ".$"} payload template field (Parameters, ResultSelector, ItemSelector) whose JSONPath
|
||||
* does not resolve against the effective input fails the state with {@code States.Runtime},
|
||||
* matching real AWS and Step Functions Local 2.0.0, instead of silently resolving to null.
|
||||
* Fixes issue #2521.
|
||||
*
|
||||
* <p>AWS keeps one narrow leniency here: an out-of-range array index still resolves to null and
|
||||
* the execution keeps running (see {@link AslExecutorIntrinsicMissingArgumentTest}, which pins
|
||||
* the same path failing when passed to a {@code States.*} intrinsic instead).
|
||||
*/
|
||||
class AslExecutorUnresolvableJsonPathTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private AslExecutor newExecutor() {
|
||||
return new AslExecutor(null, null, null, null, null, null, null, null,
|
||||
null, null, null, null, null, mapper, null, null, null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unresolvableNestedPathFailsWithTheCauseAwsWrites() throws Exception {
|
||||
var failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> resolve("{\"missing.$\":\"$.nope.deep\"}", "{\"other\":1}"));
|
||||
|
||||
assertEquals("States.Runtime", failure.error);
|
||||
assertEquals("The JSONPath '$.nope.deep' specified for the field 'missing.$' could not "
|
||||
+ "be found in the input '{\"other\":1}'", failure.cause);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unresolvableTopLevelPathFails() throws Exception {
|
||||
var failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> resolve("{\"missing.$\":\"$.nope\"}", "{\"other\":1}"));
|
||||
|
||||
assertEquals("The JSONPath '$.nope' specified for the field 'missing.$' could not be "
|
||||
+ "found in the input '{\"other\":1}'", failure.cause);
|
||||
}
|
||||
|
||||
/** A field that resolves fine must be unaffected. */
|
||||
@Test
|
||||
void resolvablePathStillSucceeds() throws Exception {
|
||||
var resolved = resolve("{\"picked.$\":\"$.other\"}", "{\"other\":1}");
|
||||
|
||||
assertEquals(1, resolved.path("picked").asInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* An out-of-range array index is the one place AWS keeps the old null-and-succeed behavior;
|
||||
* verified against real AWS in us-east-1 (see the sibling comment in
|
||||
* {@link AslExecutorIntrinsicMissingArgumentTest#indexPastTheEndOfAnArrayFails}).
|
||||
*/
|
||||
@Test
|
||||
void outOfRangeArrayIndexStillResolvesToNull() throws Exception {
|
||||
var resolved = resolve("{\"idx.$\":\"$.items[5]\"}", "{\"items\":[1,2]}");
|
||||
|
||||
assertEquals("null", resolved.path("idx").toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* An all-digit index too large to fit in an {@code int} is still an out-of-range array access,
|
||||
* not a parse failure, so it must resolve to null the same as an ordinary out-of-range index.
|
||||
*/
|
||||
@Test
|
||||
void indexBeyondIntRangeStillResolvesToNull() throws Exception {
|
||||
var resolved = resolve("{\"idx.$\":\"$.items[99999999999999999999]\"}", "{\"items\":[1,2]}");
|
||||
|
||||
assertEquals("null", resolved.path("idx").toString());
|
||||
}
|
||||
|
||||
/** An explicit null value is present, not missing, so it must not fail. */
|
||||
@Test
|
||||
void explicitNullValueStillSucceeds() throws Exception {
|
||||
var resolved = resolve("{\"v.$\":\"$.nul\"}", "{\"nul\":null}");
|
||||
|
||||
assertEquals("null", resolved.path("v").toString());
|
||||
}
|
||||
|
||||
/** A miss at any nesting depth inside the template fails, not only at the top level. */
|
||||
@Test
|
||||
void unresolvablePathFailsAtAnyNestingDepth() throws Exception {
|
||||
var failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> resolve("{\"outer\":{\"missing.$\":\"$.a.b.c\"}}", "{\"a\":{\"x\":1}}"));
|
||||
|
||||
assertEquals("The JSONPath '$.a.b.c' specified for the field 'missing.$' could not be "
|
||||
+ "found in the input '{\"a\":{\"x\":1}}'", failure.cause);
|
||||
}
|
||||
|
||||
/** ResultSelector and ItemSelector run through the same resolver, so they fail the same way. */
|
||||
@Test
|
||||
void resultSelectorFailsOnAnUnresolvablePath() throws Exception {
|
||||
var failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> resolve("{\"missing.$\":\"$.nope\"}", "[{\"other\":1}]"));
|
||||
|
||||
assertEquals("The JSONPath '$.nope' specified for the field 'missing.$' could not be "
|
||||
+ "found in the input '[{\"other\":1}]'", failure.cause);
|
||||
}
|
||||
|
||||
/** Indexing through a value that is not an array or object fails, matching AWS. */
|
||||
@Test
|
||||
void navigatingThroughANonContainerValueFails() throws Exception {
|
||||
var failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> resolve("{\"v.$\":\"$.other.x\"}", "{\"other\":1}"));
|
||||
|
||||
assertEquals("States.Runtime", failure.error);
|
||||
assertEquals("The JSONPath '$.other.x' specified for the field 'v.$' could not be found "
|
||||
+ "in the input '{\"other\":1}'", failure.cause);
|
||||
}
|
||||
|
||||
/** A wildcard projection matching nothing is an empty array, not a failure. */
|
||||
@Test
|
||||
void wildcardMatchingNothingStaysAnEmptyArray() throws Exception {
|
||||
var resolved = resolve("{\"names.$\":\"$.items[*].name\"}", "{\"items\":[1,2]}");
|
||||
|
||||
assertEquals(0, resolved.path("names").size());
|
||||
}
|
||||
|
||||
/**
|
||||
* An unresolvable {@code $$.} Context Object reference fails too, and names the Context
|
||||
* Object as the input, since that is what the path was actually resolved against.
|
||||
*/
|
||||
@Test
|
||||
void unresolvableContextReferenceFailsAndNamesTheContextObject() throws Exception {
|
||||
var context = mapper.readTree("{\"State\":{\"Name\":\"P\"}}");
|
||||
var template = mapper.readTree("{\"missing.$\":\"$$.Nope.Deep\"}");
|
||||
|
||||
var failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> newExecutor().resolveParameters(template, mapper.readTree("{\"other\":1}"), context));
|
||||
|
||||
assertEquals("States.Runtime", failure.error);
|
||||
assertEquals("The JSONPath '$.Nope.Deep' specified for the field 'missing.$' could not "
|
||||
+ "be found in the input '{\"State\":{\"Name\":\"P\"}}'", failure.cause);
|
||||
}
|
||||
|
||||
/** A resolvable Context Object reference is unaffected. */
|
||||
@Test
|
||||
void resolvableContextReferenceStillSucceeds() throws Exception {
|
||||
var context = mapper.readTree("{\"Execution\":{\"Name\":\"exec1\"}}");
|
||||
var template = mapper.readTree("{\"name.$\":\"$$.Execution.Name\"}");
|
||||
|
||||
var resolved = newExecutor().resolveParameters(template, mapper.readTree("{}"), context);
|
||||
|
||||
assertEquals("exec1", resolved.path("name").asText());
|
||||
}
|
||||
|
||||
private JsonNode resolve(String template, String input) throws Exception {
|
||||
return newExecutor().resolveParameters(
|
||||
mapper.readTree(template), mapper.readTree(input), mapper.createObjectNode());
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* End to end coverage for issue #2521. A {@code ".$"} Parameters field whose JSONPath does not
|
||||
* resolve against the input used to resolve to null and the execution succeeded; real AWS and
|
||||
* Step Functions Local 2.0.0 fail the execution with {@code States.Runtime}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class StepFunctionsUnresolvableJsonPathIntegrationTest {
|
||||
|
||||
private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
/** The exact reproduction from issue #2521. */
|
||||
@Test
|
||||
void unresolvableParametersPathFailsTheExecution() throws Exception {
|
||||
var definition = """
|
||||
{"StartAt":"Pick","States":{
|
||||
"Pick":{"Type":"Pass","Parameters":{"missing.$":"$.nope.deep"},"End":true}}}
|
||||
""";
|
||||
|
||||
var describe = run("unresolvable-jsonpath", definition, "{\"other\":1}");
|
||||
|
||||
assertEquals("FAILED", describe.jsonPath().getString("status"), describe.body().asString());
|
||||
assertEquals("States.Runtime", describe.jsonPath().getString("error"));
|
||||
assertEquals("The JSONPath '$.nope.deep' specified for the field 'missing.$' could not "
|
||||
+ "be found in the input '{\"other\":1}'",
|
||||
describe.jsonPath().getString("cause"));
|
||||
}
|
||||
|
||||
/**
|
||||
* States.Runtime is never caught, matching the intrinsic-argument precedent. A Map carries
|
||||
* the Catch because AWS refuses it on a Pass.
|
||||
*/
|
||||
@Test
|
||||
void catchAllDoesNotSwallowTheFailure() throws Exception {
|
||||
var definition = """
|
||||
{"StartAt":"M","States":{
|
||||
"M":{"Type":"Map","ItemsPath":"$.items",
|
||||
"Parameters":{"missing.$":"$.nope"},
|
||||
"Iterator":{"StartAt":"P","States":{"P":{"Type":"Pass","End":true}}},
|
||||
"Catch":[{"ErrorEquals":["States.ALL"],"Next":"Caught"}],"End":true},
|
||||
"Caught":{"Type":"Pass","End":true}}}
|
||||
""";
|
||||
|
||||
var describe = run("unresolvable-jsonpath-catch-all", definition, "{\"items\":[1,2]}");
|
||||
|
||||
assertEquals("FAILED", describe.jsonPath().getString("status"), describe.body().asString());
|
||||
assertEquals("States.Runtime", describe.jsonPath().getString("error"));
|
||||
}
|
||||
|
||||
/** An out-of-range array index still resolves to null and the execution succeeds. */
|
||||
@Test
|
||||
void outOfRangeArrayIndexStillSucceeds() throws Exception {
|
||||
var definition = """
|
||||
{"StartAt":"Pick","States":{
|
||||
"Pick":{"Type":"Pass","Parameters":{"idx.$":"$.items[5]"},"End":true}}}
|
||||
""";
|
||||
|
||||
var describe = run("unresolvable-jsonpath-array-index", definition, "{\"items\":[1,2]}");
|
||||
|
||||
assertEquals("SUCCEEDED", describe.jsonPath().getString("status"), describe.body().asString());
|
||||
assertEquals("{\"idx\":null}", describe.jsonPath().getString("output"));
|
||||
}
|
||||
|
||||
/** ResultSelector runs through the same resolver and fails the same way. */
|
||||
@Test
|
||||
void unresolvableResultSelectorPathFailsTheExecution() throws Exception {
|
||||
var definition = """
|
||||
{"StartAt":"M","States":{
|
||||
"M":{"Type":"Map","ItemsPath":"$.items",
|
||||
"ItemProcessor":{"StartAt":"P","States":{"P":{"Type":"Pass","End":true}}},
|
||||
"ResultSelector":{"missing.$":"$.nope"},"End":true}}}
|
||||
""";
|
||||
|
||||
var describe = run("unresolvable-jsonpath-result-selector", definition, "{\"items\":[1,2]}");
|
||||
|
||||
assertEquals("FAILED", describe.jsonPath().getString("status"), describe.body().asString());
|
||||
assertEquals("States.Runtime", describe.jsonPath().getString("error"));
|
||||
assertEquals("The JSONPath '$.nope' specified for the field 'missing.$' could not be "
|
||||
+ "found in the input '[1,2]'",
|
||||
describe.jsonPath().getString("cause"));
|
||||
}
|
||||
|
||||
private Response run(String label, String definition, String input) throws InterruptedException {
|
||||
var smArn = create(label + "-" + System.currentTimeMillis(), definition);
|
||||
var execArn = start(smArn, input);
|
||||
for (var i = 0; i < 50; i++) {
|
||||
var resp = describe(execArn);
|
||||
var status = resp.jsonPath().getString("status");
|
||||
if (!"RUNNING".equals(status)) {
|
||||
return resp;
|
||||
}
|
||||
Thread.sleep(100);
|
||||
}
|
||||
fail("Execution did not complete");
|
||||
return null;
|
||||
}
|
||||
|
||||
private String create(String name, String definition) {
|
||||
var resp = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"name\":\"" + name + "\",\"definition\":" + quote(definition)
|
||||
+ ",\"roleArn\":\"" + ROLE_ARN + "\"}")
|
||||
.when().post("/");
|
||||
resp.then().statusCode(200);
|
||||
return resp.jsonPath().getString("stateMachineArn");
|
||||
}
|
||||
|
||||
private String start(String smArn, String input) {
|
||||
var resp = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StartExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"stateMachineArn\":\"" + smArn + "\",\"input\":" + quote(input) + "}")
|
||||
.when().post("/");
|
||||
resp.then().statusCode(200);
|
||||
return resp.jsonPath().getString("executionArn");
|
||||
}
|
||||
|
||||
private Response describe(String execArn) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"executionArn\":\"" + execArn + "\"}")
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static String quote(String raw) {
|
||||
return "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"")
|
||||
.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t") + "\"";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user