mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(stepfunctions): evaluate the execution role on the remaining task integrations (#7724)
* feat(stepfunctions): evaluate the execution role on the remaining task integrations * fix(stepfunctions): check a Secrets Manager task from the generated request resources
This commit is contained in:
1 parent
558453df20
commit
1b8e4fa503
12 files changed
+1729
-36
No files matched your search
@@ -23,6 +23,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -44,6 +45,26 @@ public class CloudFormationQueryHandler {
|
||||
this.stackSetService = stackSetService;
|
||||
}
|
||||
|
||||
/** The id of the stack a stack name or id names, as a request naming it finds it. */
|
||||
public Optional<String> stackId(String stackNameOrId, String region) {
|
||||
return cfnService.stackId(stackNameOrId, region);
|
||||
}
|
||||
|
||||
/** The id of the stack holding the change set {@code changeSetArn} names. */
|
||||
public Optional<String> stackIdOfChangeSet(String changeSetArn, String region) {
|
||||
return cfnService.stackIdOfChangeSet(changeSetArn, region);
|
||||
}
|
||||
|
||||
/** The id of the stack that owns the resource {@code physicalResourceId} names. */
|
||||
public Optional<String> stackIdOfResource(String physicalResourceId, String region) {
|
||||
return cfnService.stackIdOfResource(physicalResourceId, region);
|
||||
}
|
||||
|
||||
/** The id of the stack set of this name. */
|
||||
public Optional<String> stackSetId(String stackSetName) {
|
||||
return stackSetService.stackSetId(stackSetName);
|
||||
}
|
||||
|
||||
public Response handle(String action, MultivaluedMap<String, String> params, String region) {
|
||||
return switch (action) {
|
||||
case "DescribeStacks" -> describeStacks(params, region);
|
||||
|
||||
@@ -398,6 +398,32 @@ public class CloudFormationService implements ResourceProvider {
|
||||
|
||||
// ── DescribeStacks ────────────────────────────────────────────────────────
|
||||
|
||||
/** The id of the stack a stack name or id names, as a request describing it finds it, reading only. */
|
||||
public Optional<String> stackId(String stackNameOrId, String region) {
|
||||
return Optional.ofNullable(resolveStackForDescribe(stackNameOrId, region)).map(Stack::getStackId);
|
||||
}
|
||||
|
||||
/** The id of the stack holding the change set {@code changeSetArn} names, reading only. */
|
||||
public Optional<String> stackIdOfChangeSet(String changeSetArn, String region) {
|
||||
try {
|
||||
return Optional.ofNullable(findStackForChangeSetArn(changeSetArn, region, currentAccount()))
|
||||
.map(Stack::getStackId);
|
||||
} catch (AwsException expected) {
|
||||
LOG.debugv("The change set ARN {0} names no change set: {1}", changeSetArn, expected.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/** The id of the stack that owns the resource {@code physicalResourceId} names, reading only. */
|
||||
public Optional<String> stackIdOfResource(String physicalResourceId, String region) {
|
||||
try {
|
||||
return Optional.of(stackContaining(physicalResourceId, region).getStackId());
|
||||
} catch (AwsException expected) {
|
||||
LOG.debugv("No stack owns the resource {0}: {1}", physicalResourceId, expected.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Stack> describeStacks(String stackName, String region) {
|
||||
return describeStacks(stackName, region, currentAccount());
|
||||
}
|
||||
|
||||
@@ -204,6 +204,11 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
|
||||
organizationsAccess.put(ORGANIZATIONS_ACCESS_KEY, "ENABLED");
|
||||
}
|
||||
|
||||
/** The id of the stack set of this name, reading only. */
|
||||
public Optional<String> stackSetId(String name) {
|
||||
return stackSets.get(name).map(StackSet::getStackSetId);
|
||||
}
|
||||
|
||||
public StackSet describeStackSet(String name) {
|
||||
return getStackSetOrThrow(name);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.floci.aws.core.common.AwsErrorResponse;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.EpochSeconds;
|
||||
import io.floci.aws.core.common.IamEnforcementFilter;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.secretsmanager.model.Secret;
|
||||
import io.floci.aws.services.secretsmanager.model.SecretVersion;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -48,6 +49,11 @@ public class SecretsManagerJsonHandler {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
/** The secrets the service finds, random suffix included, for a caller naming the resources of a request. */
|
||||
public StoredResourceArns storedResourceArns() {
|
||||
return service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request that carries no caller credential of its own, such as an API Gateway
|
||||
* service integration. Checks that authorize the caller for a second resource have no caller to
|
||||
|
||||
@@ -1260,9 +1260,15 @@ public class AslExecutor {
|
||||
/** Integrations {@link TaskPermissions} does not map stay unchecked, Lambda and API Gateway check their own. */
|
||||
private void authorizeTask(StatesIntegration integration, JsonNode input, StateMachine sm, JsonNode credentials,
|
||||
String taskToken) {
|
||||
// NestedTaskCredentials has decided this one. Its callback form does not reach it, so it is decided here.
|
||||
if (credentials != null && taskToken == null && NestedTaskCredentials.isNested(integration)) {
|
||||
return;
|
||||
}
|
||||
Optional<TaskPermissions> required = TaskPermissions.of(integration, input, sm,
|
||||
redshiftDataHandler == null ? null : redshiftDataHandler.storedResourceArns(), ecsService,
|
||||
taskToken != null);
|
||||
taskToken != null, new TaskPermissions.ControlServices(cloudFormationHandler,
|
||||
sdkManagementTaskInvoker == null ? null : sdkManagementTaskInvoker.secretsStoredResourceArns(),
|
||||
sfnService == null || !sfnService.isResolvable() ? null : sfnService.get()));
|
||||
if (required.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -1270,7 +1276,7 @@ public class AslExecutor {
|
||||
NestedTaskCredentials.validateShape(credentials);
|
||||
}
|
||||
lambdaTaskAuthorizer.authorize(sm, credentials == null ? null : credentials.path("RoleArn").asText(),
|
||||
required.get().deniedError(), required.get().permissions());
|
||||
required.get().deniedError(), required.get().permissions(), required.get().checksExecutionRole());
|
||||
}
|
||||
|
||||
private boolean iamEnforcementEnabled() {
|
||||
|
||||
@@ -98,14 +98,27 @@ public class LambdaTaskAuthorizer {
|
||||
* {@code States.TaskFailed} for a refused assumption, and {@code deniedError} for a refused permission
|
||||
*/
|
||||
void authorize(StateMachine machine, String credentialsRoleArn, String deniedError, List<Permission> permissions) {
|
||||
authorize(machine, credentialsRoleArn, deniedError, permissions, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #authorize(StateMachine, String, String, List)}.
|
||||
*
|
||||
* @param checkExecutionRole false for an integration whose tasks do not refuse an execution role for not existing
|
||||
* or not trusting Step Functions: a nested execution, which only needs the permissions
|
||||
*/
|
||||
void authorize(StateMachine machine, String credentialsRoleArn, String deniedError, List<Permission> permissions,
|
||||
boolean checkExecutionRole) {
|
||||
AwsArnUtils.Arn owner = AwsArnUtils.parse(machine.getStateMachineArn());
|
||||
IamRole executionRole = role(machine.getRoleArn()).orElseThrow(
|
||||
() -> new AslExecutor.FailStateException("States.Permissions",
|
||||
"The execution role " + machine.getRoleArn() + " does not exist."));
|
||||
if (!trustEvaluator.allowsService(executionRole.getAssumeRolePolicyDocument(), ServicePrincipals.of("states"),
|
||||
machine.getStateMachineArn(), owner.accountId())) {
|
||||
throw new AslExecutor.FailStateException("States.Permissions",
|
||||
"The execution role " + machine.getRoleArn() + " does not trust Step Functions.");
|
||||
if (checkExecutionRole) {
|
||||
IamRole executionRole = role(machine.getRoleArn()).orElseThrow(
|
||||
() -> new AslExecutor.FailStateException("States.Permissions",
|
||||
"The execution role " + machine.getRoleArn() + " does not exist."));
|
||||
if (!trustEvaluator.allowsService(executionRole.getAssumeRolePolicyDocument(),
|
||||
ServicePrincipals.of("states"), machine.getStateMachineArn(), owner.accountId())) {
|
||||
throw new AslExecutor.FailStateException("States.Permissions",
|
||||
"The execution role " + machine.getRoleArn() + " does not trust Step Functions.");
|
||||
}
|
||||
}
|
||||
String effectiveRoleArn = machine.getRoleArn();
|
||||
if (credentialsRoleArn != null) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.ServiceRegistry;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.ecs.EcsJsonHandler;
|
||||
import io.floci.aws.services.lambda.LambdaController;
|
||||
import io.floci.aws.services.secretsmanager.SecretsManagerJsonHandler;
|
||||
@@ -54,6 +55,11 @@ public class SdkManagementTaskInvoker {
|
||||
};
|
||||
}
|
||||
|
||||
/** The secrets Secrets Manager finds, for the request of a {@code DescribeSecret} task. */
|
||||
StoredResourceArns secretsStoredResourceArns() {
|
||||
return secrets.storedResourceArns();
|
||||
}
|
||||
|
||||
JsonNode invoke(StatesIntegration integration, JsonNode input, String region) throws Exception {
|
||||
String prefix = switch (integration.service()) {
|
||||
case "ecs" -> "Ecs";
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RequestChecks;
|
||||
import io.floci.aws.core.common.RequestResources;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.cloudformation.CloudFormationQueryHandler;
|
||||
import io.floci.aws.services.dynamodb.DynamoDbPartiQLParser;
|
||||
import io.floci.aws.services.dynamodb.DynamoDbTableNames;
|
||||
import io.floci.aws.services.eventbridge.EventBridgeService;
|
||||
@@ -21,6 +22,7 @@ import io.floci.aws.services.stepfunctions.model.StateMachine;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -31,16 +33,14 @@ import java.util.Set;
|
||||
* What a task's call to its integrated service needs from IAM. A request that names no resource
|
||||
* needs no permission here: the integrated service refuses it with its own validation error.
|
||||
*
|
||||
* @param deniedError the error a refused permission fails the task with: the integration's prefix and
|
||||
* the exception the integrated service's SDK raises for an access denial
|
||||
* @param permissions the actions the call performs
|
||||
* @param deniedError the error a refused permission fails the task with: the integration's prefix
|
||||
* and the exception the integrated service's SDK raises for an access denial
|
||||
* @param permissions the actions the call performs
|
||||
* @param checksExecutionRole whether the task also needs its execution role to exist and to trust Step Functions,
|
||||
* which a nested execution does not, on the maintainer's decision to leave nested and
|
||||
* Lambda tasks as they are
|
||||
*/
|
||||
record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
|
||||
/** The permissions of an integration whose refusal is its prefix and {@code AccessDeniedException}. */
|
||||
static TaskPermissions accessDenied(String errorPrefix, List<Permission> permissions) {
|
||||
return new TaskPermissions(errorPrefix + "AccessDeniedException", permissions);
|
||||
}
|
||||
record TaskPermissions(String deniedError, List<Permission> permissions, boolean checksExecutionRole) {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(TaskPermissions.class);
|
||||
private static final String ANY_RESOURCE = "*";
|
||||
@@ -52,6 +52,8 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
private static final String ECS_SDK_PREFIX = "Ecs.";
|
||||
private static final String EMR_PREFIX = "EMR.";
|
||||
private static final String EMR_SERVERLESS_PREFIX = "EmrServerless.";
|
||||
private static final String LAMBDA_PREFIX = "Lambda.";
|
||||
private static final String SECRETS_MANAGER_PREFIX = "SecretsManager.";
|
||||
private static final Set<String> DYNAMODB_OPTIMIZED =
|
||||
Set.of("putItem", "getItem", "deleteItem", "updateItem", "scan");
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
@@ -67,6 +69,48 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
"cancelStep", List.of("CancelSteps"),
|
||||
"modifyInstanceFleetByName", List.of("ListInstanceFleets", "ModifyInstanceFleet"),
|
||||
"modifyInstanceGroupByName", List.of("ListInstanceGroups", "ModifyInstanceGroups"));
|
||||
private static final String SCHEDULER_PREFIX = "Scheduler.";
|
||||
private static final String CLOUDFORMATION_PREFIX = "CloudFormation.";
|
||||
private static final String HTTP_PREFIX = "States.Http.";
|
||||
private static final String SFN_SDK_PREFIX = "Sfn.";
|
||||
private static final String NESTED_PREFIX = "StepFunctions.";
|
||||
private static final Set<String> SCHEDULER = Set.of("createSchedule", "updateSchedule", "deleteSchedule");
|
||||
private static final Set<String> SFN_SDK = Set.of("startExecution", "startSyncExecution", "sendTaskSuccess",
|
||||
"sendTaskFailure", "describeMapRun");
|
||||
private static final Set<String> CFN_ANY_RESOURCE = Set.of("ValidateTemplate", "ListStacks", "ListExports",
|
||||
"DescribeOrganizationsAccess", "ActivateOrganizationsAccess", "CreateStackSet", "ListStackSets");
|
||||
private static final Set<String> CFN_OPTIONAL_STACK = Set.of("DescribeStacks", "GetTemplateSummary");
|
||||
private static final Set<String> CFN_CREATES_STACK = Set.of("CreateStack", "CreateChangeSet");
|
||||
private static final Set<String> CFN_NAMES_CHANGE_SET =
|
||||
Set.of("DescribeChangeSet", "ExecuteChangeSet", "DeleteChangeSet");
|
||||
private static final Set<String> CFN_STACK = Set.of("UpdateStack", "DeleteStack", "ContinueUpdateRollback",
|
||||
"UpdateTerminationProtection", "ListChangeSets", "DescribeStackEvents", "DescribeStackResources",
|
||||
"ListStackResources", "DescribeStackResource", "GetTemplate", "SetStackPolicy", "GetStackPolicy");
|
||||
private static final Set<String> CFN_STACK_SET = Set.of("DescribeStackSet", "UpdateStackSet", "DeleteStackSet",
|
||||
"CreateStackInstances", "ListStackInstances", "DescribeStackInstance", "DeleteStackInstances",
|
||||
"ListStackSetOperations", "DescribeStackSetOperation", "ListStackSetAutoDeploymentTargets");
|
||||
|
||||
TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
this(deniedError, permissions, true);
|
||||
}
|
||||
|
||||
/** The permissions of an integration whose refusal is its prefix and {@code AccessDeniedException}. */
|
||||
static TaskPermissions accessDenied(String errorPrefix, List<Permission> permissions) {
|
||||
return new TaskPermissions(errorPrefix + "AccessDeniedException", permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* What finds the resources of a control task's request, reading only.
|
||||
*
|
||||
* @param cloudFormation the stack or stack set of a CloudFormation request; without it such a request names nothing
|
||||
* @param secrets the secret a Secrets Manager request names, random suffix included; without it a secret is
|
||||
* the ARN its format gives the name
|
||||
* @param states the state machine or activity whose task a callback's token names; without it a token
|
||||
* names nothing
|
||||
*/
|
||||
record ControlServices(CloudFormationQueryHandler cloudFormation, StoredResourceArns secrets,
|
||||
StoredResourceArns states) {
|
||||
}
|
||||
|
||||
/** The permissions of a mapped integration, or nothing for one that is not checked. */
|
||||
static Optional<TaskPermissions> of(StatesIntegration integration, JsonNode input, StateMachine machine) {
|
||||
@@ -96,6 +140,49 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
*/
|
||||
static Optional<TaskPermissions> of(StatesIntegration integration, JsonNode input, StateMachine machine,
|
||||
StoredResourceArns redshiftData, StoredResourceArns ecs, boolean callback) {
|
||||
return of(integration, input, machine, redshiftData, ecs, callback, new ControlServices(null, null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* The permissions of a mapped integration, or nothing for one that is not checked.
|
||||
*
|
||||
* @param redshiftData finds the workgroup or the session target a Data API request names; without it a
|
||||
* workgroup is any workgroup and a session names nothing
|
||||
* @param ecs finds the task definition revision a reference resolves to and the default cluster, reading
|
||||
* only; without it a family names any revision and a request naming no cluster any cluster
|
||||
* @param callback whether the state's resource ended in {@code .waitForTaskToken}, which {@code integration}
|
||||
* no longer shows: EMR and EMR Serverless have no such form, so theirs stays unsupported
|
||||
* @param control finds what a control task's request names
|
||||
*/
|
||||
static Optional<TaskPermissions> of(StatesIntegration integration, JsonNode input, StateMachine machine,
|
||||
StoredResourceArns redshiftData, StoredResourceArns ecs, boolean callback,
|
||||
ControlServices control) {
|
||||
if (integration.isSdkService("scheduler") && SCHEDULER.contains(integration.api())) {
|
||||
return Optional.of(scheduler(integration.api(), input, machine));
|
||||
}
|
||||
if (integration.isSdkService("cloudformation") && servedByCloudFormation(integration.api())) {
|
||||
return Optional.of(cloudFormation(AslExecutor.capitalizeFirst(integration.api()), input, machine,
|
||||
control.cloudFormation()));
|
||||
}
|
||||
if (integration.is("http", "invoke")) {
|
||||
return Optional.of(httpInvoke(input, machine));
|
||||
}
|
||||
if (integration.isSdk("ec2", "describeRegions")) {
|
||||
return Optional.of(ec2DescribeRegions(input, machine));
|
||||
}
|
||||
if (integration.isSdkService("lambda") && SdkManagementTaskInvoker.supports(integration)) {
|
||||
return Optional.of(lambdaSdk(integration.api(), input, machine));
|
||||
}
|
||||
if (integration.isSdkService("secretsmanager") && SdkManagementTaskInvoker.supports(integration)) {
|
||||
return Optional.of(accessDenied(SECRETS_MANAGER_PREFIX,
|
||||
requestPermissions("secretsmanager", "DescribeSecret", input, machine, control.secrets())));
|
||||
}
|
||||
if (integration.isSdkService("sfn") && SFN_SDK.contains(integration.api())) {
|
||||
return Optional.of(sfnSdk(AslExecutor.capitalizeFirst(integration.api()), input, machine, control.states()));
|
||||
}
|
||||
if (integration.isAnySuffix("states", "startExecution")) {
|
||||
return Optional.of(nestedStartExecution(input, machine));
|
||||
}
|
||||
if (integration.isAnySuffix("ecs", "runTask")
|
||||
&& (integration.suffix().isEmpty() || ".sync".equals(integration.suffix()))) {
|
||||
return Optional.of(accessDenied(ECS_OPTIMIZED_PREFIX, ecs("RunTask", input, machine, ecs)));
|
||||
@@ -335,9 +422,19 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
*/
|
||||
private static List<Permission> ecs(String operation, JsonNode input, StateMachine machine,
|
||||
StoredResourceArns ecs) {
|
||||
String namedAs = "ecs:" + operation;
|
||||
String action = IamAuthorizedActions.actionFor("ecs", operation);
|
||||
JsonNode request = AslExecutor.recaseKeys(MAPPER, input, false);
|
||||
return requestPermissions("ecs", operation, AslExecutor.recaseKeys(MAPPER, input, false), machine, ecs);
|
||||
}
|
||||
|
||||
/**
|
||||
* The check the vendored request resources give the same call made directly, in the state machine's partition,
|
||||
* Region and account: the operation's action on each resource the request names, {@code stored} finding the ones
|
||||
* only its service knows, or on any resource for an operation that acts on no resource type. A request that is no
|
||||
* object, or an operation the table does not read, needs nothing here: the service answers it.
|
||||
*/
|
||||
private static List<Permission> requestPermissions(String namespace, String operation, JsonNode request,
|
||||
StateMachine machine, StoredResourceArns stored) {
|
||||
String namedAs = namespace + ":" + operation;
|
||||
String action = IamAuthorizedActions.actionFor(namespace, operation);
|
||||
if (!request.isObject() || !RequestResources.reads(namedAs)) {
|
||||
return List.of();
|
||||
}
|
||||
@@ -345,7 +442,7 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
return List.of(new Permission(action, ANY_RESOURCE));
|
||||
}
|
||||
AwsArnUtils.Arn owner = AwsArnUtils.parse(machine.getStateMachineArn());
|
||||
return RequestResources.ofBody(namedAs, request, owner.partition(), owner.region(), owner.accountId(), ecs)
|
||||
return RequestResources.ofBody(namedAs, request, owner.partition(), owner.region(), owner.accountId(), stored)
|
||||
.stream().map(resource -> new Permission(action, resource)).toList();
|
||||
}
|
||||
|
||||
@@ -377,6 +474,14 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
return accessDenied(EMR_PREFIX, permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DescribeRegions}, checked as the vendored request resources check the same call made directly: it acts
|
||||
* on no resource type, so on any resource.
|
||||
*/
|
||||
private static TaskPermissions ec2DescribeRegions(JsonNode input, StateMachine machine) {
|
||||
return accessDenied("Ec2.", requestPermissions("ec2", "DescribeRegions", input, machine, null));
|
||||
}
|
||||
|
||||
/** The members of the EMR request the integration sends that name the cluster. */
|
||||
private static JsonNode emrRequest(String operation, String clusterId) {
|
||||
return switch (operation) {
|
||||
@@ -409,6 +514,163 @@ record TaskPermissions(String deniedError, List<Permission> permissions) {
|
||||
.map(check -> new Permission(check.action(), check.resource(), check.context())).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* The action on the schedule its name and group give, read as the integration reads them, a group left out being
|
||||
* {@code default}. A request naming no schedule is refused by Scheduler.
|
||||
*/
|
||||
private static TaskPermissions scheduler(String api, JsonNode input, StateMachine machine) {
|
||||
String name = text(input.path("Name"));
|
||||
if (name == null) {
|
||||
return accessDenied(SCHEDULER_PREFIX, List.of());
|
||||
}
|
||||
String group = text(input.path("GroupName"));
|
||||
return accessDenied(SCHEDULER_PREFIX, List.of(new Permission(
|
||||
"scheduler:" + AslExecutor.capitalizeFirst(api),
|
||||
arn(machine, "scheduler", "schedule/" + (group == null ? "default" : group) + "/" + name))));
|
||||
}
|
||||
|
||||
/** Every action the CloudFormation handler serves; any other is its own {@code UnknownAction}. */
|
||||
private static boolean servedByCloudFormation(String api) {
|
||||
String operation = AslExecutor.capitalizeFirst(api);
|
||||
return CFN_ANY_RESOURCE.contains(operation) || CFN_OPTIONAL_STACK.contains(operation)
|
||||
|| CFN_CREATES_STACK.contains(operation) || CFN_NAMES_CHANGE_SET.contains(operation)
|
||||
|| CFN_STACK.contains(operation) || CFN_STACK_SET.contains(operation);
|
||||
}
|
||||
|
||||
/**
|
||||
* The action of the API's own name, on the stack or stack set the request reaches under the id CloudFormation
|
||||
* holds for it, in the service reference's resource types, so a name and the id it stands for are the same
|
||||
* resource. A stack not there yet has its id open for the APIs that create it; for any other API a stack or
|
||||
* stack set CloudFormation does not have names nothing, so CloudFormation answers it.
|
||||
*/
|
||||
private static TaskPermissions cloudFormation(String operation, JsonNode input, StateMachine machine,
|
||||
CloudFormationQueryHandler stored) {
|
||||
String action = "cloudformation:" + operation;
|
||||
if (CFN_ANY_RESOURCE.contains(operation)) {
|
||||
return accessDenied(CLOUDFORMATION_PREFIX, List.of(new Permission(action, ANY_RESOURCE)));
|
||||
}
|
||||
String resource;
|
||||
if (CFN_STACK_SET.contains(operation)) {
|
||||
resource = cloudFormationStackSet(input, machine, stored);
|
||||
} else if (CFN_OPTIONAL_STACK.contains(operation) && text(input.path("StackName")) == null) {
|
||||
resource = ANY_RESOURCE;
|
||||
} else {
|
||||
resource = cloudFormationStack(operation, input, machine, stored);
|
||||
}
|
||||
return accessDenied(CLOUDFORMATION_PREFIX,
|
||||
resource == null ? List.of() : List.of(new Permission(action, resource)));
|
||||
}
|
||||
|
||||
private static String cloudFormationStackSet(JsonNode input, StateMachine machine,
|
||||
CloudFormationQueryHandler stored) {
|
||||
String name = text(input.path("StackSetName"));
|
||||
if (name == null || stored == null) {
|
||||
return null;
|
||||
}
|
||||
return stored.stackSetId(name).map(id -> arn(machine, "cloudformation", "stackset/" + id)).orElse(null);
|
||||
}
|
||||
|
||||
/** The stack as each API finds it: by name or id, or else by the change set or the resource the API can name it by. */
|
||||
private static String cloudFormationStack(String operation, JsonNode input, StateMachine machine,
|
||||
CloudFormationQueryHandler stored) {
|
||||
String stack = text(input.path("StackName"));
|
||||
String region = regionOf(machine);
|
||||
if (stored == null) {
|
||||
return null;
|
||||
}
|
||||
if (stack != null) {
|
||||
Optional<String> id = stored.stackId(stack, region);
|
||||
boolean opensId = CFN_CREATES_STACK.contains(operation) && !AwsArnUtils.looksLikeArn(stack);
|
||||
return id.orElseGet(() -> opensId ? arn(machine, "cloudformation", "stack/" + stack + "/*") : null);
|
||||
}
|
||||
String changeSet = text(input.path("ChangeSetName"));
|
||||
if (CFN_NAMES_CHANGE_SET.contains(operation) && AwsArnUtils.looksLikeArn(changeSet)) {
|
||||
return stored.stackIdOfChangeSet(changeSet, region).orElse(null);
|
||||
}
|
||||
String resource = text(input.path("PhysicalResourceId"));
|
||||
if ("DescribeStackResources".equals(operation) && resource != null) {
|
||||
return stored.stackIdOfResource(resource, region).orElse(null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code states:InvokeHTTPEndpoint} on the state machine, the resource the Developer Guide's policy scopes it to:
|
||||
* a role allowed it on {@code *}, as the CDK grants it, is allowed it there too. The two condition keys the guide
|
||||
* and the CDK write are given. The connection's own permissions, {@code events:RetrieveConnectionCredentials}
|
||||
* and its secret's, are not evaluated, although the task reads the connection to authorize its request: a role
|
||||
* lacking them succeeds here and fails on AWS. A request naming no endpoint is refused by the integration.
|
||||
*/
|
||||
private static TaskPermissions httpInvoke(JsonNode input, StateMachine machine) {
|
||||
String endpoint = text(input.path("ApiEndpoint"));
|
||||
if (endpoint == null) {
|
||||
return accessDenied(HTTP_PREFIX, List.of());
|
||||
}
|
||||
Map<String, List<String>> keys = new LinkedHashMap<>();
|
||||
keys.put("states:HTTPEndpoint", List.of(endpoint));
|
||||
String method = text(input.path("Method"));
|
||||
if (method != null) {
|
||||
keys.put("states:HTTPMethod", List.of(method));
|
||||
}
|
||||
return accessDenied(HTTP_PREFIX, List.of(new Permission("states:InvokeHTTPEndpoint",
|
||||
machine.getStateMachineArn(), keys)));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code ListEventSourceMappings} has no resource type in the service reference, and
|
||||
* {@code UpdateEventSourceMapping} acts on the mapping its {@code Uuid} names; a request naming none is refused
|
||||
* by the integration.
|
||||
*/
|
||||
private static TaskPermissions lambdaSdk(String api, JsonNode input, StateMachine machine) {
|
||||
String action = "lambda:" + AslExecutor.capitalizeFirst(api);
|
||||
if ("listEventSourceMappings".equals(api)) {
|
||||
return accessDenied(LAMBDA_PREFIX, List.of(new Permission(action, ANY_RESOURCE)));
|
||||
}
|
||||
String uuid = text(input.path("Uuid"));
|
||||
return accessDenied(LAMBDA_PREFIX, uuid == null ? List.of()
|
||||
: List.of(new Permission(action, arn(machine, "lambda", "event-source-mapping:" + uuid))));
|
||||
}
|
||||
|
||||
/**
|
||||
* The check a direct request of the operation gets, on what it names: the state machine it starts, the Map Run it
|
||||
* describes, or the activity or state machine waiting on the callback's token. A token naming no task, or anything
|
||||
* that is not the ARN the request needs, names nothing, so Step Functions answers it.
|
||||
*/
|
||||
private static TaskPermissions sfnSdk(String operation, JsonNode input, StateMachine machine,
|
||||
StoredResourceArns stored) {
|
||||
return accessDenied(SFN_SDK_PREFIX, statesPermissions(operation, input, machine, stored));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code states:StartExecution} on the child, without the execution role checks the other integrations make, which
|
||||
* a nested execution has never had. A request naming no state machine is refused by the integration.
|
||||
*/
|
||||
private static TaskPermissions nestedStartExecution(JsonNode input, StateMachine machine) {
|
||||
return new TaskPermissions(NESTED_PREFIX + "AccessDeniedException",
|
||||
statesPermissions("StartExecution", input, machine, null), false);
|
||||
}
|
||||
|
||||
private static List<Permission> statesPermissions(String operation, JsonNode input, StateMachine machine,
|
||||
StoredResourceArns stored) {
|
||||
try {
|
||||
return requestPermissions("states", operation, AslExecutor.recaseKeys(MAPPER, input, false), machine,
|
||||
stored);
|
||||
} catch (AwsException expected) {
|
||||
LOG.debugv("The {0} request names no task: {1}", operation, expected.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
/** The ARN of a resource of the state machine's partition, Region and account. */
|
||||
private static String arn(StateMachine machine, String service, String resource) {
|
||||
AwsArnUtils.Arn owner = AwsArnUtils.parse(machine.getStateMachineArn());
|
||||
return new AwsArnUtils.Arn(owner.partition(), service, owner.region(), owner.accountId(), resource).toString();
|
||||
}
|
||||
|
||||
private static String regionOf(StateMachine machine) {
|
||||
return AwsArnUtils.parse(machine.getStateMachineArn()).region();
|
||||
}
|
||||
|
||||
/** The value when it is the non-blank string an id has to be, as the integrations require of one. */
|
||||
private static String text(JsonNode value) {
|
||||
return value.isTextual() && !value.asText().isBlank() ? value.asText() : null;
|
||||
|
||||
Reference in new issue
Block a user