fix(stepfunctions): give resource-tag conditions the tags of the state machine or activity a request names (#8589)

The generated request resources give Step Functions' state machine,
version, alias, execution and activity types aws:ResourceTag/ keys, but
StepFunctionsService did not answer storedTags, so a condition on their
tags saw none. It now reads the state machine or the activity stored
under the ARN in the account and Region it names, in place, matched on
the whole ARN; a version the state machine holds and an execution have
the state machine's tags, as all versions of a state machine share its
tags and its executions inherit them, and an alias has none. Tagging
and untagging now put a new tag map in place of the old one under the
service's lock, so the lookup always copies one whole set and an update
copying the state machine no longer loses a tag written at the same
time. iam.md also names EventBridge and RDS, which answer it too.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 10:49:38 -04:00
1 parent 276f2a92be
commit 1b54454b2d
7 files changed
+227 -14

No files matched your search

@@ -1860,6 +1860,62 @@ class IamRequestResourceEnforcementIntegrationTest {
}
/** The user's JSON-protocol request to the target, {@code <prefix>.<operation>}, with the body. */
/**
* An {@code aws:ResourceTag} condition reads a Step Functions state machine's tags for the state machine, its
* version and its execution, and an activity's own, so an allow on one team's tag lets the caller read that
* team's and not another's.
*/
@Test
void aStepFunctionsResourceTagConditionReadsTheStateMachineTagsAndTheActivityTags() {
String definition = "{\\\"StartAt\\\":\\\"Done\\\",\\\"States\\\":{\\\"Done\\\":{\\\"Type\\\":\\\"Pass\\\","
+ "\\\"End\\\":true}}}";
String tagged = stateMachine("res-tag-sm-" + suffix(), definition, "data");
String other = stateMachine("res-tag-sm-" + suffix(), definition, "web");
String version = sfn("PublishStateMachineVersion", "{\"stateMachineArn\":\"" + tagged + "\"}")
.statusCode(200).extract().path("stateMachineVersionArn");
String execution = sfn("StartExecution", "{\"stateMachineArn\":\"" + tagged + "\"}")
.statusCode(200).extract().path("executionArn");
String otherExecution = sfn("StartExecution", "{\"stateMachineArn\":\"" + other + "\"}")
.statusCode(200).extract().path("executionArn");
String activity = activity("res-tag-act-" + suffix(), "data");
String otherActivity = activity("res-tag-act-" + suffix(), "web");
User user = user("res-sfn-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":[\"states:DescribeStateMachine\",\"states:DescribeExecution\","
+ "\"states:DescribeActivity\"],\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
describe(user, "DescribeStateMachine", "stateMachineArn", tagged).statusCode(200);
describe(user, "DescribeStateMachine", "stateMachineArn", version).statusCode(200);
refusedJson(describe(user, "DescribeStateMachine", "stateMachineArn", other), "states:DescribeStateMachine");
describe(user, "DescribeExecution", "executionArn", execution).statusCode(200);
refusedJson(describe(user, "DescribeExecution", "executionArn", otherExecution), "states:DescribeExecution");
describe(user, "DescribeActivity", "activityArn", activity).statusCode(200);
refusedJson(describe(user, "DescribeActivity", "activityArn", otherActivity), "states:DescribeActivity");
}
private String stateMachine(String name, String definition, String team) {
String arn = sfn("CreateStateMachine", "{\"name\":\"" + name + "\",\"definition\":\"" + definition
+ "\",\"roleArn\":\"arn:aws:iam::" + ACCOUNT + ":role/sfn\",\"tags\":[{\"key\":\"team\",\"value\":\""
+ team + "\"}]}").statusCode(200).extract().path("stateMachineArn");
cleanup.register(() -> sfn("DeleteStateMachine", "{\"stateMachineArn\":\"" + arn + "\"}"));
return arn;
}
private String activity(String name, String team) {
String arn = sfn("CreateActivity", "{\"name\":\"" + name + "\",\"tags\":[{\"key\":\"team\",\"value\":\""
+ team + "\"}]}").statusCode(200).extract().path("activityArn");
cleanup.register(() -> sfn("DeleteActivity", "{\"activityArn\":\"" + arn + "\"}"));
return arn;
}
private static ValidatableResponse sfn(String operation, String body) {
return adminJson("states", "AWSStepFunctions." + operation, "1.0").body(body).post("/").then();
}
private static ValidatableResponse describe(User user, String operation, String member, String arn) {
return send(user, "states", "AWSStepFunctions." + operation, "1.0", "{\"" + member + "\":\"" + arn + "\"}");
}
/**
* A Glue table is checked on the account's catalog, its database and itself, the types AWS gives
* {@code glue:GetTable}, so a grant on all three allows it, the same grant refuses another table of the database,
@@ -0,0 +1,90 @@
package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.stepfunctions.model.Activity;
import io.floci.aws.services.stepfunctions.model.Execution;
import io.floci.aws.services.stepfunctions.model.MapRun;
import io.floci.aws.services.stepfunctions.model.RoutingConfiguration;
import io.floci.aws.services.stepfunctions.model.StateMachine;
import io.floci.aws.services.stepfunctions.model.StateMachineAlias;
import io.floci.aws.services.stepfunctions.model.StateMachineVersion;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
class StepFunctionsStoredTagsTest {
private static final String ACCOUNT = "000000000000";
private static final String REGION = "us-east-1";
private static final String DEFINITION = "{\"StartAt\":\"Done\",\"States\":{\"Done\":{\"Type\":\"Pass\",\"End\":true}}}";
private static final String ROLE = "arn:aws:iam::" + ACCOUNT + ":role/sfn";
private AccountAwareStorageBackend<Execution> executionStore;
private StepFunctionsService service;
@BeforeEach
void setUp() {
StorageFactory factory = mock(StorageFactory.class);
executionStore = AccountAwareStorageBackend.inMemory(ACCOUNT);
doReturn(AccountAwareStorageBackend.<StateMachine>inMemory(ACCOUNT))
.doReturn(executionStore)
.doReturn(AccountAwareStorageBackend.<Activity>inMemory(ACCOUNT))
.doReturn(AccountAwareStorageBackend.<MapRun>inMemory(ACCOUNT))
.doReturn(AccountAwareStorageBackend.<StateMachineAlias>inMemory(ACCOUNT))
.when(factory).create(anyString(), anyString(), any());
service = new StepFunctionsService(factory, new RegionResolver(REGION, ACCOUNT), null, new ObjectMapper(),
mock(SfnMockLoader.class), mock(StepFunctionsEventPublisher.class), Clock.systemUTC());
}
@Test
void aStateMachinesVersionsAndExecutionsHaveItsTagsAndAnAliasNone() {
String arn = service.createStateMachine("orders", DEFINITION, ROLE, "STANDARD", REGION,
Map.of("team", "data")).getStateMachineArn();
StateMachineVersion version = service.publishStateMachineVersion(arn, null, null);
service.createStateMachineAlias("live", null,
List.of(new RoutingConfiguration(version.getStateMachineVersionArn(), 100)));
Execution execution = new Execution();
execution.setExecutionArn("arn:aws:states:" + REGION + ":" + ACCOUNT + ":execution:orders:run-1");
execution.setStateMachineArn(arn);
executionStore.put(execution.getExecutionArn(), execution);
assertEquals(Map.of("team", "data"), service.storedTags(arn));
assertEquals(Map.of("team", "data"), service.storedTags(version.getStateMachineVersionArn()));
assertEquals(Map.of("team", "data"), service.storedTags(execution.getExecutionArn()));
assertEquals(Map.of(), service.storedTags(arn + ":live"));
assertEquals(Map.of(), service.storedTags(arn + ":7"));
assertEquals(Map.of(), service.storedTags(execution.getExecutionArn().replace("run-1", "run-2")));
assertEquals(Map.of(), service.storedTags(arn.replace(ACCOUNT, "111111111111")));
assertEquals(Map.of(), service.storedTags(arn.replace(REGION, "us-west-2")));
service.tagResource(arn, Map.of("stage", "prod"));
service.untagResource(arn, List.of("team"));
assertEquals(Map.of("stage", "prod"), service.storedTags(version.getStateMachineVersionArn()));
}
@Test
void anActivityHasItsOwnTagsAndTaggingPutsANewMapInPlaceOfTheHeldOne() {
String arn = service.createActivity("worker", REGION, Map.of("team", "data")).getActivityArn();
Map<String, String> held = service.listTags(arn);
service.tagResource(arn, Map.of("stage", "prod"));
service.untagResource(arn, List.of("team"));
assertEquals(Map.of("stage", "prod"), service.storedTags(arn));
assertEquals(Map.of("team", "data"), held);
assertEquals(Map.of(), service.storedTags(arn.replace("worker", "other")));
}
}