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

@@ -30,7 +30,8 @@ import java.util.Optional;
* or a change set; S3 Tables, for the table a request names by its table bucket, namespace and name, and the tags of a
* table bucket or a table; Lambda, for the layer version a layer version ARN selects, the image of a MicroVM and the
* execution of a callback, and the tags of a function, an event source mapping, a web function, a MicroVM image or a
* network connector; Glue, for the tags of a resource it tags); looked up lazily by {@code ResourceArnBuilder} and
* network connector; Glue, for the tags of a resource it tags; Step Functions, for the tags of a state machine, its
* versions and executions, and an activity); looked up lazily by {@code ResourceArnBuilder} and
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
* ARN is of, so IAM never depends on that service directly.</p>
*
@@ -1510,42 +1510,106 @@ public class StepFunctionsService implements Resettable, ResourceProvider, Store
throw new AwsException("ResourceNotFound", "Resource not found: " + arn, 400);
}
public void tagResource(String arn, Map<String, String> tags) {
/**
* Puts a new tag map in place of the state machine's or the activity's, never changing one in place, so a reader
* copying it, as {@link #storedTags} does on every request IAM checks, sees one whole set; under the service's lock
* the other state machine writes take, so an update copying the state machine does not lose a tag.
*/
public synchronized void tagResource(String arn, Map<String, String> tags) {
Optional<StateMachine> smOpt = stateMachineStore.get(arn);
if (smOpt.isPresent()) {
StateMachine sm = smOpt.get();
sm.getTags().putAll(tags);
Map<String, String> next = new HashMap<>(sm.getTags());
next.putAll(tags);
sm.setTags(next);
stateMachineStore.put(arn, sm);
return;
}
Optional<Activity> actOpt = activityStore.get(arn);
if (actOpt.isPresent()) {
Activity activity = actOpt.get();
activity.getTags().putAll(tags);
Map<String, String> next = new HashMap<>(activity.getTags());
next.putAll(tags);
activity.setTags(next);
activityStore.put(arn, activity);
return;
}
throw new AwsException("ResourceNotFound", "Resource not found: " + arn, 400);
}
public void untagResource(String arn, List<String> tagKeys) {
/** As {@link #tagResource}, without the keys. */
public synchronized void untagResource(String arn, List<String> tagKeys) {
Optional<StateMachine> smOpt = stateMachineStore.get(arn);
if (smOpt.isPresent()) {
StateMachine sm = smOpt.get();
tagKeys.forEach(sm.getTags()::remove);
Map<String, String> next = new HashMap<>(sm.getTags());
tagKeys.forEach(next::remove);
sm.setTags(next);
stateMachineStore.put(arn, sm);
return;
}
Optional<Activity> actOpt = activityStore.get(arn);
if (actOpt.isPresent()) {
Activity activity = actOpt.get();
tagKeys.forEach(activity.getTags()::remove);
Map<String, String> next = new HashMap<>(activity.getTags());
tagKeys.forEach(next::remove);
activity.setTags(next);
activityStore.put(arn, activity);
return;
}
throw new AwsException("ResourceNotFound", "Resource not found: " + arn, 400);
}
/**
* The tags of the state machine or the activity stored under {@code arn} in the account and Region it names, read
* in place and matched on the whole ARN; a version and an execution have their state machine's, as all versions
* of a state machine share its tags and its executions inherit them, and an alias has none. Empty for an ARN
* naming nothing stored.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "states")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String account = parsed.accountId();
String resource = parsed.resource();
if (resource.startsWith("activity:")) {
return AccountAwareStorageBackend.readInAccount(activityStore, account, arn)
.map(activity -> Map.copyOf(activity.getTags()))
.orElse(Map.of());
}
if (resource.startsWith("execution:")) {
return AccountAwareStorageBackend.readInAccount(executionStore, account, arn)
.map(execution -> stateMachineTags(account, execution.getStateMachineArn(), null))
.orElse(Map.of());
}
String[] stateMachine = resource.split(":", -1);
if (!"stateMachine".equals(stateMachine[0]) || stateMachine.length < 2 || stateMachine.length > 3) {
return Map.of();
}
if (stateMachine.length == 2) {
return stateMachineTags(account, arn, null);
}
return stateMachine[2].matches("[0-9]+")
? stateMachineTags(account, arn.substring(0, arn.lastIndexOf(':')), arn)
: Map.of();
}
/** The tags of the state machine stored under {@code arn} in the account, when it holds {@code version} if one. */
private Map<String, String> stateMachineTags(String account, String arn, String version) {
Optional<StateMachine> stored = AccountAwareStorageBackend.readInAccount(stateMachineStore, account, arn);
if (stored.isEmpty() || version != null && !holdsVersion(stored.get(), version)) {
return Map.of();
}
return Map.copyOf(stored.get().getTags());
}
private synchronized boolean holdsVersion(StateMachine stateMachine, String versionArn) {
return stateMachine.getVersions().stream()
.anyMatch(published -> versionArn.equals(published.getStateMachineVersionArn()));
}
public synchronized void replaceStateMachineTags(String arn, Map<String, String> tags) {
StateMachine current = stateMachineStore.get(arn)
.orElseThrow(() -> new AwsException(
@@ -13,7 +13,7 @@ public class Activity {
private String activityArn;
private String name;
private double creationDate;
private Map<String, String> tags = new HashMap<>();
private volatile Map<String, String> tags = new HashMap<>();
public Activity() {
this.creationDate = EpochSeconds.fromMillis(System.currentTimeMillis());
@@ -26,7 +26,7 @@ public class StateMachine {
private JsonNode loggingConfiguration;
private JsonNode tracingConfiguration;
private JsonNode encryptionConfiguration;
private Map<String, String> tags = new HashMap<>();
private volatile Map<String, String> tags = new HashMap<>();
private String creationVersionArn;
private String creationVersionDescription;
private int versionCounter = 0;