From 12a10d5319df40c6d34cd70855fdb09d4667e0f8 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Fri, 27 Nov 2026 14:42:06 -0600 Subject: [PATCH] fix(stepfunctions): fail a nested execution of a state machine in another region (#9604) A states:startExecution task (and its .sync forms), or an aws-sdk:sfn startExecution or startSyncExecution task, whose StateMachineArn names a state machine in another Region started it with an execution ARN in the parent's Region. AWS documents that "Cross-Region AWS SDK integration and cross-Region AWS resource access aren't available in Step Functions" (Developer Guide, "Accessing cross-account AWS resources in Step Functions"), so the task now fails before anything starts. AWS does not name the error; the task fails with the one it reports for a state machine of an account it cannot reach. The Region check sits in NestedTaskCredentials.validateTarget, beside the account check, and takes its error name from accessDenied. validateTarget strips .waitForTaskToken before it parses the resource, so every callback form, optimized or AWS SDK, gets the Region check too; the account check still skips callback tasks, as before. Signed-off-by: Hector Ventura --- .../services/stepfunctions/AslExecutor.java | 2 +- .../stepfunctions/NestedTaskCredentials.java | 27 ++++-- .../NestedTaskCredentialsTest.java | 23 ++++++ ...sNestedExecutionRegionIntegrationTest.java | 82 +++++++++++++++++++ 4 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsNestedExecutionRegionIntegrationTest.java diff --git a/src/main/java/io/floci/aws/services/stepfunctions/AslExecutor.java b/src/main/java/io/floci/aws/services/stepfunctions/AslExecutor.java index 141f2eb11e..2916a7105c 100644 --- a/src/main/java/io/floci/aws/services/stepfunctions/AslExecutor.java +++ b/src/main/java/io/floci/aws/services/stepfunctions/AslExecutor.java @@ -1050,7 +1050,7 @@ public class AslExecutor { taskAccount = nestedTaskCredentials.account(credentials, resource, effectiveInput, sm); } if (mockedInvocation == null) { - NestedTaskCredentials.validateTargetAccount(resource, effectiveInput, sm, taskAccount); + NestedTaskCredentials.validateTarget(resource, effectiveInput, sm, taskAccount); } JsonNode resourceInput = effectiveInput; String resourceToken = taskToken; diff --git a/src/main/java/io/floci/aws/services/stepfunctions/NestedTaskCredentials.java b/src/main/java/io/floci/aws/services/stepfunctions/NestedTaskCredentials.java index 31f0cf04d0..5f17172257 100644 --- a/src/main/java/io/floci/aws/services/stepfunctions/NestedTaskCredentials.java +++ b/src/main/java/io/floci/aws/services/stepfunctions/NestedTaskCredentials.java @@ -21,6 +21,8 @@ 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; private final LambdaTaskAuthorizer authorizer; @@ -84,7 +86,7 @@ public class NestedTaskCredentials { if (authorizer.assumeRefusal(machine.getRoleArn(), roleArn, machine.getStateMachineArn()).isPresent()) { throw assumptionDenied(machine, roleArn); } - validateTargetAccount(resource, input, machine, role.accountId()); + validateTarget(resource, input, machine, role.accountId()); String target = input.path("StateMachineArn").asText(null); if (AwsArnUtils.isArn(target)) { String action = integration.isSdk("sfn", "startSyncExecution") @@ -159,16 +161,31 @@ public class NestedTaskCredentials { + roleArn + "."); } - static void validateTargetAccount(String resource, JsonNode input, StateMachine machine, String taskAccount) { - StatesIntegration integration = StatesIntegration.parse(resource).orElse(null); + /** + * Fails a nested execution whose state machine the task cannot reach: one of another account than the task's, + * or one in another Region, since "Cross-Region AWS SDK integration and cross-Region AWS resource access aren't + * available in Step Functions" (Developer Guide, "Accessing cross-account AWS resources in Step Functions"). AWS + * names no error for the Region case, so both fail with the integration's access error. + */ + 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); String target = input.path("StateMachineArn").asText(null); if (!isNested(integration) || !AwsArnUtils.isArn(target)) { return; } - String account = taskAccount != null ? taskAccount : AwsArnUtils.parse(machine.getStateMachineArn()).accountId(); - if (!account.equals(AwsArnUtils.parse(target).accountId())) { + AwsArnUtils.Arn machineArn = AwsArnUtils.parse(machine.getStateMachineArn()); + AwsArnUtils.Arn targetArn = AwsArnUtils.parse(target); + String account = taskAccount != null ? taskAccount : machineArn.accountId(); + if (!callback && !account.equals(targetArn.accountId())) { throw accessDenied(integration, "Not authorized to access this resource: " + target); } + if (!targetArn.region().isEmpty() && !targetArn.region().equals(machineArn.region())) { + throw accessDenied(integration, "Cross-Region access is not available in Step Functions: " + + target + " is not in the execution's Region, " + machineArn.region()); + } } private static AslExecutor.FailStateException accessDenied(StatesIntegration integration, String cause) { diff --git a/src/test/java/io/floci/aws/services/stepfunctions/NestedTaskCredentialsTest.java b/src/test/java/io/floci/aws/services/stepfunctions/NestedTaskCredentialsTest.java index d1e1db7584..8124150419 100644 --- a/src/test/java/io/floci/aws/services/stepfunctions/NestedTaskCredentialsTest.java +++ b/src/test/java/io/floci/aws/services/stepfunctions/NestedTaskCredentialsTest.java @@ -66,4 +66,27 @@ class NestedTaskCredentialsTest { assertSame(machine, NestedTaskCredentials.inAccount(machine, null)); assertSame(machine, NestedTaskCredentials.inAccount(machine, "000000000111")); } + + @ParameterizedTest + @ValueSource(strings = {"arn:aws:states:::states:startExecution", "arn:aws:states:::states:startExecution.sync", + "arn:aws:states:::states:startExecution.sync:2", "arn:aws:states:::states:startExecution.waitForTaskToken", + "arn:aws:states:::aws-sdk:sfn:startExecution", "arn:aws:states:::aws-sdk:sfn:startSyncExecution", + "arn:aws:states:::aws-sdk:sfn:startExecution.waitForTaskToken", + "arn:aws:states:::aws-sdk:sfn:startSyncExecution.waitForTaskToken"}) + void aNestedExecutionOfAStateMachineInAnotherRegionFailsWithTheIntegrationsAccessError(String resource) { + StateMachine machine = new StateMachine(); + machine.setStateMachineArn("arn:aws:states:us-east-1:000000000111:stateMachine:parent"); + String sameRegion = "arn:aws:states:us-east-1:000000000111:stateMachine:child"; + String otherRegion = "arn:aws:states:eu-west-1:000000000111:stateMachine:child"; + + NestedTaskCredentials.validateTarget(resource, + new ObjectMapper().createObjectNode().put("StateMachineArn", sameRegion), machine, null); + AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class, + () -> NestedTaskCredentials.validateTarget(resource, + new ObjectMapper().createObjectNode().put("StateMachineArn", otherRegion), machine, null)); + + assertEquals(resource.contains("aws-sdk") ? "Sfn.SfnException" : "StepFunctions.AWSStepFunctionsException", + refused.error); + assertTrue(refused.cause.contains(otherRegion), refused.cause); + } } diff --git a/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsNestedExecutionRegionIntegrationTest.java b/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsNestedExecutionRegionIntegrationTest.java new file mode 100644 index 0000000000..53e9b0f8ea --- /dev/null +++ b/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsNestedExecutionRegionIntegrationTest.java @@ -0,0 +1,82 @@ +package io.floci.aws.services.stepfunctions; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.floci.aws.core.common.RequestScopes; +import io.floci.aws.services.stepfunctions.model.Execution; +import io.floci.aws.services.stepfunctions.model.StateMachine; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.time.Duration; +import java.util.Map; +import java.util.UUID; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * "Cross-Region AWS SDK integration and cross-Region AWS resource access aren't available in Step Functions" + * (Developer Guide, "Accessing cross-account AWS resources in Step Functions"), so a task naming a state machine of + * another Region starts nothing there or in its own Region. + */ +@QuarkusTest +class StepFunctionsNestedExecutionRegionIntegrationTest { + + private static final String ACCOUNT = "000000000739"; + private static final String PARENT_REGION = "us-east-1"; + private static final String CHILD_REGION = "eu-west-1"; + private static final String ROLE = "arn:aws:iam::" + ACCOUNT + ":role/test"; + private static final String PASS = """ + {"StartAt":"Done","States":{"Done":{"Type":"Pass","End":true}}} + """; + + @Inject + StepFunctionsService states; + + @Inject + ObjectMapper mapper; + + @ParameterizedTest + @CsvSource({"states:startExecution,StepFunctions.AWSStepFunctionsException,STANDARD", + "states:startExecution.sync,StepFunctions.AWSStepFunctionsException,STANDARD", + "states:startExecution.sync:2,StepFunctions.AWSStepFunctionsException,STANDARD", + "states:startExecution.waitForTaskToken,StepFunctions.AWSStepFunctionsException,STANDARD", + "aws-sdk:sfn:startExecution,Sfn.SfnException,STANDARD", + "aws-sdk:sfn:startSyncExecution,Sfn.SfnException,EXPRESS", + "aws-sdk:sfn:startExecution.waitForTaskToken,Sfn.SfnException,STANDARD", + "aws-sdk:sfn:startSyncExecution.waitForTaskToken,Sfn.SfnException,EXPRESS"}) + void aStateMachineInAnotherRegionFailsTheTaskAndStartsNothing(String integration, String error, String childType) + throws Exception { + String suffix = UUID.randomUUID().toString(); + StateMachine child = RequestScopes.callAs(ACCOUNT, CHILD_REGION, () -> states.createStateMachine( + "regional-child-" + suffix, PASS, ROLE, childType, CHILD_REGION, Map.of())); + String definition = mapper.writeValueAsString(Map.of("StartAt", "Child", "States", Map.of("Child", Map.of( + "Type", "Task", "Resource", "arn:aws:states:::" + integration, + "Parameters", Map.of("StateMachineArn", child.getStateMachineArn(), "Input", Map.of()), + "End", true)))); + StateMachine parent = RequestScopes.callAs(ACCOUNT, PARENT_REGION, () -> states.createStateMachine( + "regional-parent-" + suffix, definition, ROLE, "STANDARD", PARENT_REGION, Map.of())); + try { + Execution execution = RequestScopes.callAs(ACCOUNT, PARENT_REGION, + () -> states.startExecution(parent.getStateMachineArn(), null, "{}", PARENT_REGION)); + await().atMost(Duration.ofSeconds(10)).until(() -> RequestScopes.callAs(ACCOUNT, PARENT_REGION, + () -> !"RUNNING".equals(states.describeExecution(execution.getExecutionArn()).getStatus()))); + Execution result = RequestScopes.callAs(ACCOUNT, PARENT_REGION, + () -> states.describeExecution(execution.getExecutionArn())); + + assertEquals("FAILED", result.getStatus(), result.getCause()); + assertEquals(error, result.getError(), result.getCause()); + assertTrue(result.getCause().contains(child.getStateMachineArn()), result.getCause()); + for (String region : new String[] {PARENT_REGION, CHILD_REGION}) { + assertTrue(RequestScopes.callAs(ACCOUNT, region, + () -> states.listExecutions(child.getStateMachineArn())).isEmpty(), region); + } + } finally { + RequestScopes.runAs(ACCOUNT, PARENT_REGION, () -> states.deleteStateMachine(parent.getStateMachineArn())); + RequestScopes.runAs(ACCOUNT, CHILD_REGION, () -> states.deleteStateMachine(child.getStateMachineArn())); + } + } +}