refactor(stepfunctions): strip a task's callback suffix in one place (#9809)

AslExecutor.executeTaskState and NestedTaskCredentials.validateTarget each
stripped .waitForTaskToken from a task resource. StatesIntegration now
holds the suffix with isCallback and withoutCallback, which both use.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-29 23:45:24 -04:00
1 parent 8e5725cc6f
commit 3e0385f91d
4 files changed
+33 -8

No files matched your search

@@ -976,10 +976,8 @@ public class AslExecutor {
JsonNode context, ObjectNode variables,
long executionDeadlineNanos) throws Exception {
String resource = stateDef.path("Resource").asText();
boolean isWaitForToken = resource.endsWith(".waitForTaskToken");
String effectiveResource = isWaitForToken
? resource.substring(0, resource.length() - ".waitForTaskToken".length())
: resource;
boolean isWaitForToken = StatesIntegration.isCallback(resource);
String effectiveResource = StatesIntegration.withoutCallback(resource);
boolean isActivity = isActivityArn(effectiveResource);
MockedTaskInvocation mockedInvocation = findMockedInvocation(context, stateName);
// A mocked task never calls the integrated service, so it neither registers a task token
@@ -21,7 +21,6 @@ import java.util.Set;
*/
@ApplicationScoped
public class NestedTaskCredentials {
private static final String WAIT_FOR_TASK_TOKEN = ".waitForTaskToken";
private final IamService iam;
private final EmulatorConfig config;
@@ -169,9 +168,9 @@ public class NestedTaskCredentials {
*/
static void validateTarget(String resource, JsonNode input, StateMachine machine, String taskAccount) {
// A callback task, optimized or AWS SDK, starts the child too once the executor strips .waitForTaskToken.
boolean callback = resource != null && resource.endsWith(WAIT_FOR_TASK_TOKEN);
StatesIntegration integration = StatesIntegration.parse(callback
? resource.substring(0, resource.length() - WAIT_FOR_TASK_TOKEN.length()) : resource).orElse(null);
boolean callback = StatesIntegration.isCallback(resource);
StatesIntegration integration = StatesIntegration.parse(StatesIntegration.withoutCallback(resource))
.orElse(null);
String target = input.path("StateMachineArn").asText(null);
if (!isNested(integration) || !AwsArnUtils.isArn(target)) {
return;
@@ -29,6 +29,19 @@ public record StatesIntegration(String partition, String service, String api, bo
private static final Pattern TAIL = Pattern.compile("^arn:" + AwsArnUtils.PARTITION_REGEX + ":states:::(.*)$");
/** The suffix of a callback task, which waits for a task token after calling its integration. */
public static final String WAIT_FOR_TASK_TOKEN = ".waitForTaskToken";
/** True when {@code resource}, an integration id or any other task resource, ends in the callback suffix. */
public static boolean isCallback(String resource) {
return resource != null && resource.endsWith(WAIT_FOR_TASK_TOKEN);
}
/** {@code resource} without its callback suffix, the integration the task calls; unchanged when it has none. */
public static String withoutCallback(String resource) {
return isCallback(resource) ? resource.substring(0, resource.length() - WAIT_FOR_TASK_TOKEN.length()) : resource;
}
public static Optional<StatesIntegration> parse(String resource) {
if (resource == null) {
return Optional.empty();
@@ -10,6 +10,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -88,4 +89,18 @@ class StatesIntegrationTest {
assertTrue(StatesIntegration.tail("arn:aws:states:us-east-1:000000000000:activity:work").isEmpty());
assertTrue(StatesIntegration.tail(null).isEmpty());
}
@Test
void theCallbackSuffixComesOffAnyResourceAndOnlyAtTheEnd() {
assertTrue(StatesIntegration.isCallback("arn:aws:states:::sqs:sendMessage.waitForTaskToken"));
assertEquals("arn:aws:states:::sqs:sendMessage",
StatesIntegration.withoutCallback("arn:aws:states:::sqs:sendMessage.waitForTaskToken"));
assertEquals("arn:aws-cn:states:::aws-sdk:sfn:startExecution",
StatesIntegration.withoutCallback("arn:aws-cn:states:::aws-sdk:sfn:startExecution.waitForTaskToken"));
assertEquals("arn:aws:states:::states:startExecution.sync:2",
StatesIntegration.withoutCallback("arn:aws:states:::states:startExecution.sync:2"));
assertFalse(StatesIntegration.isCallback("arn:aws:states:::lambda:invoke"));
assertFalse(StatesIntegration.isCallback(null));
assertNull(StatesIntegration.withoutCallback(null));
}
}