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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 22:42:22 -04:00
1 parent 1d581eb131
commit 3313a5a9f0
6 files changed
+103 -9

No files matched your search

+2 -2
View File
@@ -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
@@ -22,10 +22,11 @@ import java.util.Optional;
* <p>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
@@ -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<String, String> tags) {
run(() -> {
EmrCluster cluster = requireCluster(resourceId);
rejectTerminatedTagging(cluster);
cluster.getTags().putAll(tags);
Map<String, String> 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<String> keys) {
run(() -> {
EmrCluster cluster = requireCluster(resourceId);
rejectTerminatedTagging(cluster);
keys.forEach(cluster.getTags()::remove);
Map<String, String> 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<String, String> 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<String, String> 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 ────────────────────────────
@@ -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<EmrErrorDetail> errorDetails = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
private List<EmrStep> steps = new ArrayList<>();
private List<EmrInstanceGroup> instanceGroups = new ArrayList<>();
private List<EmrInstanceFleet> instanceFleets = new ArrayList<>();
@@ -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<EmrCluster> clusters = fixture.storage.create("emr", "emr-clusters.json",
new TypeReference<Map<String, EmrCluster>>() {});
Map<String, String> held = clusters.get(cluster.getId()).orElseThrow().getTags();
service.addTags(cluster.getId(), Map.of("env", "prod"));
assertEquals(Map.of("team", "data"), held);
assertEquals(Map.of("team", "data", "env", "prod"), service.storedTags(arn));
assertEquals(Map.of(), service.storedTags(elsewhere));
assertEquals(Map.of(), service.storedTags(arn + "/session/s-1"));
assertEquals(Map.of(), service.storedTags(arn.replace("arn:aws:", "arn:aws-cn:")));
EmrCluster regionless = clusters.get(cluster.getId()).orElseThrow();
regionless.setRegion(null);
clusters.put(cluster.getId(), regionless);
assertEquals(Map.of("team", "data", "env", "prod"), service.storedTags(elsewhere));
}
@Test
void aClusterIsStartingThenBootstrappingUntilItIsReady() {
EmrServiceFixture fixture = new EmrServiceFixture(10, 0, 0);
@@ -1732,6 +1732,31 @@ class IamRequestResourceEnforcementIntegrationTest {
"elasticmapreduce:AddTags");
}
/**
* An {@code aws:ResourceTag} condition reads the tags EMR holds on the cluster a request names, so an allow on one
* team's tag lets the caller describe that team's cluster and not another's.
*/
@Test
void anEmrResourceTagConditionReadsTheTagsOfTheCluster() {
String tagged = emrClusterId(emrCluster("res-emr-tag"), "data");
String other = emrClusterId(emrCluster("res-emr-tag"), "web");
User user = user("res-emr-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"elasticmapreduce:DescribeCluster\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
emr(user, "DescribeCluster", "{\"ClusterId\":\"" + tagged + "\"}").statusCode(200);
refusedJson(emr(user, "DescribeCluster", "{\"ClusterId\":\"" + other + "\"}"),
"elasticmapreduce:DescribeCluster");
}
/** The id of the cluster {@code arn} names, tagged with the team by an administrator. */
private static String emrClusterId(String arn, String team) {
String id = arn.substring(arn.lastIndexOf('/') + 1);
adminJson("elasticmapreduce", "ElasticMapReduce.AddTags").body("{\"ResourceId\":\"" + id
+ "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}").post("/").then().statusCode(200);
return id;
}
/**
* A Rekognition stream processor is checked on the ARN its {@code Name} gives, and a moderation analysis on the
* custom model whose ARN its {@code ProjectVersion} gives. Floci serves no stream processor and no custom model,