mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 02:28:08 -05:00
fix(stepfunctions): authorize nested task credentials (#6084)
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
83647aa05d
commit
0750128966
8 files changed
+739
-15
No files matched your search
@@ -268,6 +268,7 @@ public class AslExecutor {
|
||||
private final WebClient webClient;
|
||||
private final ApiGatewayTaskInvoker apiGatewayTaskInvoker;
|
||||
private final EmulatorConfig config;
|
||||
private final NestedTaskCredentials nestedTaskCredentials;
|
||||
private final CustomResourceLiveness customResourceLiveness;
|
||||
private final Clock clock;
|
||||
private final Sleeper sleeper;
|
||||
@@ -295,15 +296,15 @@ public class AslExecutor {
|
||||
ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator,
|
||||
Instance<StepFunctionsService> sfnService, EmulatorConfig config, Vertx vertx,
|
||||
CustomResourceLiveness customResourceLiveness, RedshiftDataJsonHandler redshiftDataHandler,
|
||||
ServiceRegistry serviceRegistry, LambdaTaskAuthorizer lambdaTaskAuthorizer,
|
||||
ApiGatewayTaskInvoker apiGatewayTaskInvoker) {
|
||||
ServiceRegistry serviceRegistry, NestedTaskCredentials nestedTaskCredentials,
|
||||
LambdaTaskAuthorizer lambdaTaskAuthorizer, ApiGatewayTaskInvoker apiGatewayTaskInvoker) {
|
||||
this(lambdaExecutor, targetResolver, dynamoDb, dynamoDbJsonHandler,
|
||||
sqsJsonHandler, snsJsonHandler, cloudFormationHandler,
|
||||
ec2Service, s3Service, ecsService, ecsJsonHandler,
|
||||
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
|
||||
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
|
||||
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, redshiftDataHandler, serviceRegistry,
|
||||
lambdaTaskAuthorizer, null, null, apiGatewayTaskInvoker);
|
||||
nestedTaskCredentials, lambdaTaskAuthorizer, null, null, apiGatewayTaskInvoker);
|
||||
}
|
||||
|
||||
AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver,
|
||||
@@ -319,9 +320,11 @@ public class AslExecutor {
|
||||
CustomResourceLiveness customResourceLiveness,
|
||||
Clock clock, Sleeper sleeper, Integer maxWaitSecondsOverride,
|
||||
RedshiftDataJsonHandler redshiftDataHandler, ServiceRegistry serviceRegistry,
|
||||
LambdaTaskAuthorizer lambdaTaskAuthorizer, Duration expressMaxExecutionTimeOverride,
|
||||
LongSupplier nanoTimeOverride, ApiGatewayTaskInvoker apiGatewayTaskInvoker) {
|
||||
NestedTaskCredentials nestedTaskCredentials, LambdaTaskAuthorizer lambdaTaskAuthorizer,
|
||||
Duration expressMaxExecutionTimeOverride, LongSupplier nanoTimeOverride,
|
||||
ApiGatewayTaskInvoker apiGatewayTaskInvoker) {
|
||||
this.apiGatewayTaskInvoker = apiGatewayTaskInvoker;
|
||||
this.nestedTaskCredentials = nestedTaskCredentials;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.lambdaTaskAuthorizer = lambdaTaskAuthorizer;
|
||||
this.customResourceLiveness = customResourceLiveness;
|
||||
@@ -383,7 +386,7 @@ public class AslExecutor {
|
||||
ec2Service, s3Service, ecsService, ecsJsonHandler,
|
||||
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
|
||||
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
|
||||
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null, null, null, null, null);
|
||||
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
// Kept for tests that do not exercise the Redshift Data API integration.
|
||||
@@ -404,7 +407,7 @@ public class AslExecutor {
|
||||
ec2Service, s3Service, ecsService, ecsJsonHandler,
|
||||
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
|
||||
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
|
||||
clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null, null);
|
||||
clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
AslExecutor(LambdaExecutorService lambdaExecutor, LambdaFunctionStore functionStore,
|
||||
@@ -909,10 +912,11 @@ public class AslExecutor {
|
||||
}
|
||||
|
||||
JsonNode effectiveInput;
|
||||
// The role a task's Credentials name is read the way its Parameters or Arguments are, and
|
||||
// only under IAM enforcement, the one place it is used.
|
||||
// Read Credentials for IAM checks and nested account routing.
|
||||
boolean nestedTask = NestedTaskCredentials.isNested(StatesIntegration.parse(resource).orElse(null));
|
||||
JsonNode credentials = null;
|
||||
boolean readCredentials = stateDef.has("Credentials") && iamEnforcementEnabled();
|
||||
boolean readCredentials = stateDef.has("Credentials")
|
||||
&& (iamEnforcementEnabled() || (mockedInvocation == null && nestedTask));
|
||||
if (jsonata) {
|
||||
effectiveInput = input;
|
||||
if (stateDef.has("Arguments")) {
|
||||
@@ -946,13 +950,25 @@ public class AslExecutor {
|
||||
addTaskScheduledEvent(chain, profile, stateDef, effectiveInput, sm);
|
||||
addTaskStartedEvent(chain, profile);
|
||||
try {
|
||||
String taskAccount = null;
|
||||
if (mockedInvocation == null && credentials != null && nestedTask) {
|
||||
taskAccount = nestedTaskCredentials.account(credentials, resource, effectiveInput, sm);
|
||||
}
|
||||
if (mockedInvocation == null) {
|
||||
NestedTaskCredentials.validateTargetAccount(resource, effectiveInput, sm, taskAccount);
|
||||
}
|
||||
JsonNode resourceInput = effectiveInput;
|
||||
String resourceToken = taskToken;
|
||||
taskResult = mockedInvocation != null
|
||||
? mockedTaskResult(mockedInvocation.steps(), stateName, mockedInvocation.responseIndex())
|
||||
: invokeResource(effectiveResource, effectiveInput, sm, taskToken,
|
||||
: RequestScopes.callAsChecked(taskAccount, () -> invokeResource(
|
||||
effectiveResource, resourceInput, sm, resourceToken,
|
||||
context.path("Execution").path("Id").asText(null),
|
||||
executionDeadlineNanos, taskDeadlineNanos,
|
||||
jsonata ? null : stateDef.path("Parameters"), credentialsRoleArn,
|
||||
jobResponse -> addTaskSubmittedEvent(chain, profile, jobResponse));
|
||||
jobResponse -> RequestScopes.runAs(
|
||||
AwsArnUtils.parse(sm.getStateMachineArn()).accountId(),
|
||||
() -> addTaskSubmittedEvent(chain, profile, jobResponse))));
|
||||
if (tokenFuture != null) {
|
||||
taskResult = awaitToken(tokenFuture, stateDef, taskToken, executionDeadlineNanos,
|
||||
taskDeadlineNanos);
|
||||
@@ -5932,7 +5948,8 @@ public class AslExecutor {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return abortedByCaller(sfnService.get().describeExecution(executionArn));
|
||||
return RequestScopes.callAs(AwsArnUtils.parse(executionArn).accountId(),
|
||||
() -> abortedByCaller(sfnService.get().describeExecution(executionArn)));
|
||||
} catch (AwsException e) {
|
||||
LOG.warnv("Execution {0} vanished while a .sync task was waiting on its job; ending the wait ({1})",
|
||||
executionArn, e.getMessage());
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class NestedTaskCredentials {
|
||||
private final IamService iam;
|
||||
private final EmulatorConfig config;
|
||||
private final LambdaTaskAuthorizer authorizer;
|
||||
|
||||
@Inject
|
||||
public NestedTaskCredentials(IamService iam, EmulatorConfig config, LambdaTaskAuthorizer authorizer) {
|
||||
this.iam = iam;
|
||||
this.config = config;
|
||||
this.authorizer = authorizer;
|
||||
}
|
||||
|
||||
String account(JsonNode credentials, String resource, JsonNode input, StateMachine machine) {
|
||||
StatesIntegration integration = StatesIntegration.parse(resource).orElse(null);
|
||||
if (!isNested(integration)) {
|
||||
throw new AslExecutor.FailStateException("States.Runtime",
|
||||
"Task Credentials account routing is supported only for nested executions");
|
||||
}
|
||||
if (!credentials.isObject() || credentials.size() != 1 || !credentials.path("RoleArn").isTextual()) {
|
||||
throw new AslExecutor.FailStateException("States.Runtime",
|
||||
"Task Credentials must resolve to an object containing RoleArn");
|
||||
}
|
||||
String roleArn = credentials.path("RoleArn").asText();
|
||||
AwsArnUtils.Arn role;
|
||||
try {
|
||||
role = AwsArnUtils.parse(roleArn);
|
||||
} catch (IllegalArgumentException error) {
|
||||
throw new AslExecutor.FailStateException("States.Runtime", "Task Credentials RoleArn is invalid");
|
||||
}
|
||||
AwsArnUtils.Arn owner = AwsArnUtils.parse(machine.getStateMachineArn());
|
||||
if (!"iam".equals(role.service()) || !role.region().isEmpty()
|
||||
|| !role.resource().startsWith("role/") || !role.accountId().matches("\\d{12}")
|
||||
|| !role.partition().equals(owner.partition())) {
|
||||
throw new AslExecutor.FailStateException("States.Runtime",
|
||||
"Task Credentials must name an IAM role in the execution partition");
|
||||
}
|
||||
String name = role.resource().substring(role.resource().lastIndexOf('/') + 1);
|
||||
if (iam.findRole(role.accountId(), name).filter(found -> IamService.roleArnMatches(roleArn, found)).isEmpty()) {
|
||||
throw assumptionDenied(machine, roleArn);
|
||||
}
|
||||
if (config.services().iam().enforcementEnabled()) {
|
||||
if (authorizer.assumeRefusal(machine.getRoleArn(), roleArn, machine.getStateMachineArn()).isPresent()) {
|
||||
throw assumptionDenied(machine, roleArn);
|
||||
}
|
||||
validateTargetAccount(resource, input, machine, role.accountId());
|
||||
String target = input.path("StateMachineArn").asText(null);
|
||||
if (AwsArnUtils.isArn(target)) {
|
||||
String action = integration.isSdk("sfn", "startSyncExecution")
|
||||
? "states:StartSyncExecution" : "states:StartExecution";
|
||||
Optional<String> refusal = authorizer.refusal(roleArn, action, target);
|
||||
if (refusal.isPresent()) {
|
||||
throw accessDenied(integration, refusal.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
return role.accountId();
|
||||
}
|
||||
|
||||
private static AslExecutor.FailStateException assumptionDenied(StateMachine machine, String roleArn) {
|
||||
return new AslExecutor.FailStateException("States.TaskFailed",
|
||||
"The role " + machine.getRoleArn() + " is not authorized to assume the task state's role, "
|
||||
+ roleArn + ".");
|
||||
}
|
||||
|
||||
static void validateTargetAccount(String resource, JsonNode input, StateMachine machine, String taskAccount) {
|
||||
StatesIntegration integration = StatesIntegration.parse(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())) {
|
||||
throw accessDenied(integration, "Not authorized to access this resource: " + target);
|
||||
}
|
||||
}
|
||||
|
||||
private static AslExecutor.FailStateException accessDenied(StatesIntegration integration, String cause) {
|
||||
String error = integration.isAnySuffix("states", "startExecution")
|
||||
? "StepFunctions.AWSStepFunctionsException" : "Sfn.SfnException";
|
||||
return new AslExecutor.FailStateException(error, cause);
|
||||
}
|
||||
|
||||
static boolean isNested(StatesIntegration integration) {
|
||||
return integration != null
|
||||
&& ((integration.isAnySuffix("states", "startExecution")
|
||||
&& Set.of("", ".sync", ".sync:2").contains(integration.suffix()))
|
||||
|| integration.isSdk("sfn", "startExecution")
|
||||
|| integration.isSdk("sfn", "startSyncExecution"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user