fix(emr-serverless): scope a job run's credentials by its execution IAM policy (#9190)

StartJobRun kept a run's executionIamPolicy only to give it back: its
credentials had every permission of its execution role, and the policy
was stored as sent, members it does not have included.

- A run's inline policy now scopes the session its containers get, as an
  assumed role's session policy does, so with IAM enforcement on "The
  resulting job IAM role permissions will be an intersection of this
  policy and the policy associated with your job execution role". Its
  policyArns are kept and given back but not applied, as Floci's IAM
  applies no managed session policy, an assumed role's included.
- The policy keeps only its two members. An inline policy outside 1 to
  2,048 characters, more than 10 policy ARNs, or one outside 20 to
  2,048 characters is refused with the framework's ValidationException,
  and either member of another JSON type with SerializationException.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-22 05:02:40 -04:00
1 parent a1f3379198
commit d63ad7c226
8 files changed
+191 -5

No files matched your search

@@ -34,8 +34,10 @@ import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -1200,6 +1202,53 @@ class EmrServerlessJobRunServiceTest {
immediate.getJobRun(applicationId, stored, null).getTotalResourceUtilization().get("vCPUHour"));
}
@Test
void aRunsCredentialsAreScopedByItsInlinePolicyAndItKeepsOnlyThePolicysMembers() {
FakeRunner runner = new FakeRunner();
EmrServerlessJobRunService service = dockerService(runner);
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}";
String readOnly = "arn:aws:iam::aws:policy/ReadOnlyAccess";
StartJobRunRequest scoped = request(null, null);
scoped.setExecutionIamPolicy(Map.of("policy", policy, "policyArns", List.of(readOnly), "extra", "dropped"));
String scopedRun = service.startJobRun(applicationId, scoped).getJobRunId();
runNext();
service.startJobRun(applicationId, request(null, null));
runNext();
assertEquals(Arrays.asList(policy, null), runner.sessionPolicies);
assertEquals(Map.of("policy", policy, "policyArns", List.of(readOnly)),
service.getJobRun(applicationId, scopedRun, null).getExecutionIamPolicy());
}
@Test
void anExecutionIamPolicyOutsideTheModelsBoundsIsRefused() {
EmrServerlessJobRunService service = service(0);
List<String> elevenArns = new ArrayList<>();
for (int i = 0; i < 11; i++) {
elevenArns.add("arn:aws:iam::aws:policy/Policy" + i);
}
Map<Map<String, Object>, String> refusals = new LinkedHashMap<>();
refusals.put(Map.of("policy", ""), "'executionIamPolicy.policy'");
refusals.put(Map.of("policy", "x".repeat(2049)), "'executionIamPolicy.policy'");
refusals.put(Map.of("policyArns", elevenArns), "'executionIamPolicy.policyArns'");
refusals.put(Map.of("policyArns", List.of("arn:aws:iam::1:p/x")), "'executionIamPolicy.policyArns.1.member'");
for (Map.Entry<Map<String, Object>, String> refusal : refusals.entrySet()) {
StartJobRunRequest refused = request(null, null);
refused.setExecutionIamPolicy(refusal.getKey());
AwsException error = assertThrows(AwsException.class, () -> service.startJobRun(applicationId, refused));
assertEquals("ValidationException", error.getErrorCode());
assertTrue(error.getMessage().contains(refusal.getValue()), error.getMessage());
}
for (Map<String, Object> malformed : List.<Map<String, Object>>of(Map.of("policy", 1),
Map.of("policyArns", "arn:aws:iam::aws:policy/ReadOnlyAccess"))) {
StartJobRunRequest refused = request(null, null);
refused.setExecutionIamPolicy(malformed);
assertEquals("SerializationException", assertThrows(AwsException.class,
() -> service.startJobRun(applicationId, refused)).getErrorCode());
}
}
@Test
void eachAttemptIsLaunchedWhileItsApplicationCannotBeDeleted() {
FakeRunner runner = new FakeRunner();
@@ -1906,6 +1955,8 @@ class EmrServerlessJobRunServiceTest {
final Set<String> stopped = new HashSet<>();
final Set<String> interrupted = new HashSet<>();
final List<String> driversStarted = new ArrayList<>();
// The session policy each run's credentials were scoped by, as its driver started.
final List<String> sessionPolicies = new ArrayList<>();
// What progress.cancelled() said once the driver ran, and again after whileRunning.
final List<Boolean> cancelledAsRunning = new ArrayList<>();
Outcome outcome = Outcome.exitedWith(0);
@@ -1935,6 +1986,7 @@ class EmrServerlessJobRunServiceTest {
resolved(progress, WorkerType.EXECUTOR, job.executors().image(), executorsResolve);
}
driversStarted.add(job.runId());
sessionPolicies.add(progress.sessionPolicy());
progress.started();
cancelledAsRunning.add(progress.cancelled());
whileRunning.run();
@@ -2769,6 +2769,23 @@ class IamServiceTest {
assertTrue(sessions.getForAccount(accountId, accessKeyId).isEmpty());
}
@Test
void anExecutionRoleSessionWithAnInlinePolicyIsScopedByItAsAnAssumedRolesIs() {
String accountId = "222233334444";
String accessKeyId = "ASIAEXECUTIONSCOPED";
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}";
AccountAwareStorageBackend<SessionCredential> sessions = new AccountAwareStorageBackend<>(
new InMemoryStorage<>(), null, "000000000000");
IamService service = iamService(false, new InMemoryStorage<>(), sessions);
service.registerLambdaExecutionRoleSession(accountId, accessKeyId, "secret", "token",
"arn:aws:iam::222233334444:role/JobRole", "run-1", null, policy);
assertEquals(policy, sessions.getForAccount(accountId, accessKeyId).orElseThrow().getSessionPolicyDocument());
assertEquals(policy, service.resolveCallerContext(accessKeyId).sessionPolicyDocument());
}
@Test
void temporarySessionUsesExplicitAccountNamespace() {
String accountId = "222233334444";