fix(emr): give resource-tag conditions the tags of the session a request names (#8878)

The generated request resources give EMR's session type the
aws:ResourceTag/ and elasticmapreduce:ResourceTag/ keys, but
EmrService.storedTags answered clusters, Studios and notebook executions
and nothing for a session's ARN, so a condition on a session's tags saw
none. It now reads the session stored under the id the ARN ends in, in
the account the ARN names, in place, matched on the whole ARN, giving
the tags it started with.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 12:21:41 -04:00
1 parent 0842636ed9
commit 5548bb78eb
6 files changed
+97 -11

No files matched your search

@@ -311,6 +311,11 @@ final class EmrServiceFixture {
startupDelaySeconds, stepDurationSeconds, terminationDelaySeconds);
}
/** The service as configured by default, every duration 0, on {@code storage}, so a test can seed its stores. */
static EmrServiceFixture on(InMemoryStorageFactory storage) {
return new EmrServiceFixture(storage, new MutableClock(), null, null, null, null, null, null, 0, 0, 0);
}
/** The service in the {@code docker} runner mode, every duration 0, its steps run by {@code runner}. */
static EmrServiceFixture docker(SparkJobRunner runner, IamService iam) {
return docker(runner, null, iam);
@@ -5,15 +5,18 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.SparkConnectEndpoint;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.emr.model.EmrCluster;
import io.floci.aws.services.emr.model.EmrInstanceGroup;
import io.floci.aws.services.emr.model.EmrSession;
import io.floci.aws.testing.InMemoryStorageFactory;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static io.floci.aws.services.emr.EmrServiceFixture.REGION;
@@ -53,6 +56,40 @@ class EmrSessionsTest {
assertEquals("notebook", read.getName());
}
/**
* IAM's lookup reads the tags a session started with, by the ARN it records, and nothing for an unknown session,
* a session named under another cluster, or another account's ARN.
*/
@Test
void storedTagsAreTheTagsTheSessionStartedWith() {
String clusterId = sessionCluster();
EmrSession started = service.startSession(clusterId,
new EmrSessions.Request("tagged", null, null, null, null, null, Map.of("team", "data")));
String arn = started.getArn();
assertEquals(Map.of("team", "data"), service.storedTags(arn));
assertTrue(service.storedTags(arn.replace(started.getId(), "is-UNKNOWN000000")).isEmpty());
assertTrue(service.storedTags(arn.replace(clusterId, "j-OTHERCLUSTER")).isEmpty());
assertTrue(service.storedTags(arn.replace("000000000000", "111122223333")).isEmpty());
}
/** A session stored before storage was account-aware is read in place: the lookup migrates nothing. */
@Test
void storedTagsReadAPreAccountAwareSessionInPlace() {
InMemoryStorage<String, EmrSession> raw = new InMemoryStorage<>();
EmrService legacyState = EmrServiceFixture.on(new InMemoryStorageFactory("000000000000")
.withStore("emr-sessions.json", raw)).service;
EmrSession legacy = new EmrSession();
legacy.setId("is-LEGACY000000");
legacy.setClusterId("j-LEGACY");
legacy.setArn("arn:aws:elasticmapreduce:us-east-1:000000000000:cluster/j-LEGACY/session/is-LEGACY000000");
legacy.setTags(Map.of("team", "data"));
raw.put("is-LEGACY000000", legacy);
assertEquals(Map.of("team", "data"), legacyState.storedTags(legacy.getArn()));
assertEquals(Set.of("is-LEGACY000000"), raw.keys());
}
@Test
void anIdleSessionEndsOnceItsIdleTimeoutHasPassed() {
String clusterId = sessionCluster();
@@ -2156,6 +2156,40 @@ class IamRequestResourceEnforcementIntegrationTest {
"elasticmapreduce:DescribeCluster");
}
/**
* An {@code aws:ResourceTag} condition reads the tags EMR holds on the session a request names, those it started
* with, so an allow on one team's tag lets the caller read that team's session and not another's.
*/
@Test
void anEmrResourceTagConditionReadsTheTagsOfTheSession() {
String clusterId = adminJson("elasticmapreduce", "ElasticMapReduce.RunJobFlow")
.body("{\"Name\":\"res-emr-sessions\",\"ReleaseLabel\":\"emr-spark-8.0.0\",\"Applications\":[{\"Name\":"
+ "\"Spark\"}],\"SessionEnabled\":true,\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,"
+ "\"InstanceGroups\":[{\"Name\":\"primary\",\"InstanceRole\":\"MASTER\",\"InstanceType\":"
+ "\"m5.xlarge\",\"InstanceCount\":1}]}}")
.post("/").then().statusCode(200).extract().path("JobFlowId");
cleanup.register(() -> adminJson("elasticmapreduce", "ElasticMapReduce.TerminateJobFlows")
.body("{\"JobFlowIds\":[\"" + clusterId + "\"]}").post("/"));
String tagged = emrSession(clusterId, "data");
String other = emrSession(clusterId, "web");
User user = user("res-emr-session-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"elasticmapreduce:GetSession\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
emr(user, "GetSession", "{\"ClusterId\":\"" + clusterId + "\",\"SessionId\":\"" + tagged + "\"}")
.statusCode(200);
refusedJson(emr(user, "GetSession", "{\"ClusterId\":\"" + clusterId + "\",\"SessionId\":\"" + other + "\"}"),
"elasticmapreduce:GetSession");
}
/** The id of a session an administrator starts on the cluster, tagged with the team. */
private static String emrSession(String clusterId, String team) {
return adminJson("elasticmapreduce", "ElasticMapReduce.StartSession")
.body("{\"ClusterId\":\"" + clusterId + "\",\"Name\":\"res-" + team + "\",\"Tags\":[{\"Key\":\"team\","
+ "\"Value\":\"" + team + "\"}]}")
.post("/").then().statusCode(200).extract().path("Id");
}
/** 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);