fix(stepfunctions): check CloudFormation tasks from the generated request resources (#8487)

TaskPermissions decided a Step Functions aws-sdk:cloudformation task by six
hand-written operation sets and looked stacks up through four handler methods
of its own. With CloudFormation now read from iam-request-resources.json, a
task goes through the generic requestPermissions, as the same request made
directly is checked: CloudFormationStoredResourceArns, from the handler's
storedResourceArns(), finds the stack or the stack set a name names, and a
request naming none, whatever the task's input is, is checked on *, as the
direct request is. The operations checked are the ones the table reads; one
the handler does not serve is then its own UnknownAction.

So a change set named by its ARN alone, a describeStackResources by
PhysicalResourceId alone and a request with no StackName are checked on *,
a stack or stack set CloudFormation does not hold with its id open, and a
stack id it does not hold as given. The six sets, the arm's own lookups and
the handler and service methods only they used are gone.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 15:21:19 -04:00
1 parent c895a7f9ad
commit 8f538679e3
8 files changed
+139 -214

No files matched your search

@@ -2,6 +2,7 @@ package io.floci.aws.services.cloudformation;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.AwsQueryResponse;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.common.XmlBuilder;
import io.floci.aws.services.cloudformation.model.ChangeSet;
import io.floci.aws.services.cloudformation.model.Stack;
@@ -23,7 +24,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/**
@@ -45,24 +45,12 @@ 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);
/**
* What IAM asks CloudFormation of the resources a request names, the stack or the stack set a name names, reading
* only: for a state machine's task, which has no HTTP request of its own.
*/
public StoredResourceArns storedResourceArns() {
return new CloudFormationStoredResourceArns(cfnService, stackSetService);
}
public Response handle(String action, MultivaluedMap<String, String> params, String region) {
@@ -398,32 +398,6 @@ 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,11 +204,6 @@ 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);
}
@@ -1266,7 +1266,8 @@ public class AslExecutor {
}
Optional<TaskPermissions> required = TaskPermissions.of(integration, input, sm,
redshiftDataHandler == null ? null : redshiftDataHandler.storedResourceArns(), ecsService,
taskToken != null, new TaskPermissions.ControlServices(cloudFormationHandler,
taskToken != null, new TaskPermissions.ControlServices(
cloudFormationHandler == null ? null : cloudFormationHandler.storedResourceArns(),
sdkManagementTaskInvoker == null ? null : sdkManagementTaskInvoker.secretsStoredResourceArns(),
sfnService == null || !sfnService.isResolvable() ? null : sfnService.get()));
if (required.isEmpty()) {
@@ -8,7 +8,6 @@ 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;
@@ -77,18 +76,6 @@ record TaskPermissions(String deniedError, List<Permission> permissions, boolean
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);
@@ -102,13 +89,14 @@ record TaskPermissions(String deniedError, List<Permission> permissions, boolean
/**
* 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 cloudFormation the stack or stack set a CloudFormation request names by its name, whose id CloudFormation
* minted; without it one is the ARN its format gives the name, its id open
* @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,
record ControlServices(StoredResourceArns cloudFormation, StoredResourceArns secrets,
StoredResourceArns states) {
}
@@ -529,69 +517,25 @@ record TaskPermissions(String deniedError, List<Permission> permissions, boolean
arn(machine, "scheduler", "schedule/" + (group == null ? "default" : group) + "/" + name))));
}
/** Every action the CloudFormation handler serves; any other is its own {@code UnknownAction}. */
/**
* Every CloudFormation operation the vendored request resources read, which a direct request is checked as too;
* one the handler does not serve is then 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);
return RequestResources.reads("cloudformation:" + AslExecutor.capitalizeFirst(api));
}
/**
* 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.
* The check the vendored request resources give the same call made directly ({@link #requestPermissions}),
* {@code stored} finding the stack or the stack set a name names; on any resource when the request names none,
* whatever its input is, as a direct request naming none is checked.
*/
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;
StoredResourceArns stored) {
List<Permission> permissions = requestPermissions("cloudformation", operation, input, machine, stored);
return accessDenied(CLOUDFORMATION_PREFIX, permissions.isEmpty()
? List.of(new Permission(IamAuthorizedActions.actionFor("cloudformation", operation), ANY_RESOURCE))
: permissions);
}
/**
@@ -663,10 +607,6 @@ record TaskPermissions(String deniedError, List<Permission> permissions, boolean
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;