From 3313a5a9f0d9402aac04c3ddf212c68c8e27ab93 Mon Sep 17 00:00:00 2001
From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
Date: Mon, 16 Nov 2026 22:42:22 -0400
Subject: [PATCH] fix(emr): give resource-tag conditions the tags of the
cluster a request names (#8698)
The generated request resources give EMR's cluster type the
aws:ResourceTag/ and elasticmapreduce:ResourceTag/ keys, but EmrService
answered no tags, so a condition on a cluster's tags saw none.
EmrService.storedTags now reads the cluster stored under the ARN's id
in the account it names, in place, in the ARN's Region or recorded with
none, as the tag handlers find it, the ARN matched whole. AddTags and
RemoveTags now put a new map in place of the held one under the
service's lock instead of editing it in place, and the field is
volatile.
---
docs/services/iam.md | 4 +-
.../aws/core/common/StoredResourceArns.java | 9 ++--
.../io/floci/aws/services/emr/EmrService.java | 42 ++++++++++++++++++-
.../aws/services/emr/model/EmrCluster.java | 2 +-
.../aws/services/emr/EmrServiceTest.java | 30 +++++++++++++
...estResourceEnforcementIntegrationTest.java | 25 +++++++++++
6 files changed, 103 insertions(+), 9 deletions(-)
diff --git a/docs/services/iam.md b/docs/services/iam.md
index cc4bb9a142..d8299600bf 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -3191,8 +3191,8 @@ floci populates:
projects and report groups, Athena for its workgroups and data catalogs, Redshift Serverless for its namespaces,
workgroups and snapshots, DynamoDB for its tables and their indexes (the table's tags), ECR for its
repositories, Redshift for its clusters, snapshots, parameter groups, subnet groups, snapshot copy grants,
- snapshot schedules, event subscriptions and integrations, and SSM for its parameters, documents and associations.
- Tag keys differing only in case are one condition key holding every value.
+ snapshot schedules, event subscriptions and integrations, SSM for its parameters, documents and associations, and
+ EMR for its clusters. Tag keys differing only in case are one condition key holding every value.
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
index 062004965c..a6e6fc9a53 100644
--- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
+++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
@@ -22,10 +22,11 @@ import java.util.Optional;
*
Implemented by the service that owns such resources or whose requests name them (EventBridge, for its
* connections and API destinations; Application Auto Scaling, for its scalable targets; Redshift Data, for the
* Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment Cryptography, for
- * the key an alias names; EMR, for the clusters of its instance groups; IAM Identity Center, for the accounts its
- * permission sets are provisioned to; Resource Explorer, for its default view; OAM and RAM, for the sinks,
- * invitations and shares of other accounts they act on; Secrets Manager, for the secret a name or an ARN names,
- * random suffix included, and its tags; EventBridge Scheduler, for its default schedule group and its groups' tags;
+ * the key an alias names; EMR, for the clusters of its instance groups and a tag call's resource and the tags of a
+ * cluster; IAM Identity Center, for the accounts its permission sets are provisioned to; Resource Explorer, for its
+ * default view; OAM and RAM, for the sinks, invitations and shares of other accounts they act on; Secrets Manager,
+ * for the secret a name or an ARN names, random suffix included, and its tags; EventBridge Scheduler, for its
+ * default schedule group and its groups' tags;
* CloudFormation, for the stack or the stack set a name names, whose id it minted, and the tags of a stack, a stack set
* 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
diff --git a/src/main/java/io/floci/aws/services/emr/EmrService.java b/src/main/java/io/floci/aws/services/emr/EmrService.java
index 0ffb7ec8df..6e6eae0069 100644
--- a/src/main/java/io/floci/aws/services/emr/EmrService.java
+++ b/src/main/java/io/floci/aws/services/emr/EmrService.java
@@ -107,6 +107,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
private static final String RUNNER_DOCKER = "docker";
// The service a step's driver belongs to, which names it and labels it.
private static final String SERVICE = "emr";
+ // The resource of a cluster's ARN, before its id.
+ private static final String CLUSTER_RESOURCE = "cluster/";
// The reason of a cluster a failed bootstrap action ended, as Terraform reported a real one's
// (hashicorp/terraform-provider-aws#4378) and the Management Guide's VPC errors page quotes it.
private static final String BOOTSTRAP_ACTION_FAILED =
@@ -1019,24 +1021,60 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
// ──────────────────────────── Tags ────────────────────────────
+ /** Puts a new map, the held tags with {@code tags} added, in place of the cluster's, so a lookup reads it whole. */
public void addTags(String resourceId, Map tags) {
run(() -> {
EmrCluster cluster = requireCluster(resourceId);
rejectTerminatedTagging(cluster);
- cluster.getTags().putAll(tags);
+ Map updated = new LinkedHashMap<>(cluster.getTags());
+ updated.putAll(tags);
+ cluster.setTags(updated);
clusterStore.put(resourceId, cluster);
});
}
+ /** Puts a new map, the held tags without {@code keys}, in place of the cluster's, so a lookup reads it whole. */
public void removeTags(String resourceId, List keys) {
run(() -> {
EmrCluster cluster = requireCluster(resourceId);
rejectTerminatedTagging(cluster);
- keys.forEach(cluster.getTags()::remove);
+ Map updated = new LinkedHashMap<>(cluster.getTags());
+ keys.forEach(updated::remove);
+ cluster.setTags(updated);
clusterStore.put(resourceId, cluster);
});
}
+ /**
+ * The tags held on the cluster stored under {@code arn}, in the account it names, read in place: the cluster of
+ * that id in the ARN's Region, or one recorded with no Region, as the tag handlers find it; empty for an ARN
+ * naming nothing stored or naming anything but a cluster.
+ */
+ @Override
+ public Map storedTags(String arn) {
+ if (!AwsArnUtils.isArnFor(arn, "elasticmapreduce")) {
+ return Map.of();
+ }
+ AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
+ String resource = parsed.resource();
+ if (!resource.startsWith(CLUSTER_RESOURCE) || resource.indexOf('/', CLUSTER_RESOURCE.length()) >= 0
+ || !arn.equals(AwsArnUtils.Arn.of("elasticmapreduce", parsed.region(), parsed.accountId(), resource)
+ .toString())) {
+ return Map.of();
+ }
+ Map held = new LinkedHashMap<>();
+ AccountAwareStorageBackend.readInAccount(clusterStore, parsed.accountId(),
+ resource.substring(CLUSTER_RESOURCE.length()))
+ .filter(cluster -> cluster.getRegion() == null || cluster.getRegion().equals(parsed.region()))
+ .map(EmrCluster::getTags)
+ .ifPresent(tags -> tags.forEach((key, value) -> {
+ if (key != null && value != null) {
+ held.put(key, value);
+ }
+ }));
+ return held;
+ }
+
// ──────────────────────────── Helpers ────────────────────────────
// ──────────────────────────── Scaling and termination policies ────────────────────────────
diff --git a/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java b/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java
index e895f5dc3c..81c1f9ff15 100644
--- a/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java
+++ b/src/main/java/io/floci/aws/services/emr/model/EmrCluster.java
@@ -74,7 +74,7 @@ public class EmrCluster {
private Instant terminatingDateTime;
// Status.ErrorDetails: the errors that ended the cluster, empty unless one of those with an ErrorDetail code did.
private List errorDetails = new ArrayList<>();
- private Map tags = new LinkedHashMap<>();
+ private volatile Map tags = new LinkedHashMap<>();
private List steps = new ArrayList<>();
private List instanceGroups = new ArrayList<>();
private List instanceFleets = new ArrayList<>();
diff --git a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java
index 66946a324c..8be1bbcd39 100644
--- a/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java
+++ b/src/test/java/io/floci/aws/services/emr/EmrServiceTest.java
@@ -73,6 +73,36 @@ class EmrServiceTest {
assertEquals(List.of(cluster.getClusterArn()), service.through("ResourceId", cluster.getId(), "eu-west-1"));
}
+ /**
+ * A resource-tag condition reads the tags of the cluster the ARN names, in its account and Region, or of one stored
+ * with no Region, as the tag handlers find it; tagging puts a new map in place of the held one.
+ */
+ @Test
+ void storedTagsAreTheTagsOfTheClusterTheArnNamesAndTaggingPutsANewMap() {
+ EmrServiceFixture fixture = new EmrServiceFixture(0, 0, 0);
+ EmrService service = fixture.service;
+ EmrCluster cluster = service.runJobFlow(cluster("tagged", true), REGION);
+ String arn = cluster.getClusterArn();
+ String elsewhere = arn.replace(":" + REGION + ":", ":eu-west-1:");
+ service.addTags(cluster.getId(), Map.of("team", "data"));
+ AccountAwareStorageBackend clusters = fixture.storage.create("emr", "emr-clusters.json",
+ new TypeReference