mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
a1f3379198
commit
d63ad7c226
8 files changed
+191
-5
No files matched your search
@@ -100,6 +100,13 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
static final String STATE_QUEUED = "QUEUED";
|
||||
private static final String MODE_BATCH = "BATCH";
|
||||
private static final String MODE_STREAMING = "STREAMING";
|
||||
// JobRunExecutionIamPolicy's members, and the model's bounds on them: PolicyDocument's and Arn's lengths, and
|
||||
// PolicyArnList's size.
|
||||
private static final String INLINE_POLICY = "policy";
|
||||
private static final String POLICY_ARNS = "policyArns";
|
||||
private static final int MAX_POLICY_LENGTH = 2048;
|
||||
private static final int MIN_ARN_LENGTH = 20;
|
||||
private static final int MAX_POLICY_ARNS = 10;
|
||||
// CancelJobRun's grace period: its default for a streaming run, and its range.
|
||||
private static final int DEFAULT_STREAMING_GRACE_SECONDS = 120;
|
||||
private static final int MIN_GRACE_SECONDS = 15;
|
||||
@@ -595,7 +602,7 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
private static void requested(JobRun run, StartJobRunRequest request) {
|
||||
run.setName(request.getName());
|
||||
run.setExecutionRole(request.getExecutionRoleArn());
|
||||
run.setExecutionIamPolicy(request.getExecutionIamPolicy());
|
||||
run.setExecutionIamPolicy(executionIamPolicy(request.getExecutionIamPolicy()));
|
||||
run.setConfigurationOverrides(request.getConfigurationOverrides());
|
||||
run.setJobDriver(request.getJobDriver());
|
||||
run.setTags(request.getTags() != null ? new LinkedHashMap<>(request.getTags()) : new LinkedHashMap<>());
|
||||
@@ -606,6 +613,61 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
run.setFlexExecution(flexExecution(request.getFlexExecution()));
|
||||
}
|
||||
|
||||
// StartJobRun's executionIamPolicy as the model's JobRunExecutionIamPolicy holds it: its policy and policyArns.
|
||||
private static Map<String, Object> executionIamPolicy(Map<String, Object> given) {
|
||||
if (given == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> policy = new LinkedHashMap<>();
|
||||
for (String member : List.of(INLINE_POLICY, POLICY_ARNS)) {
|
||||
if (given.get(member) != null) {
|
||||
policy.put(member, given.get(member));
|
||||
}
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
|
||||
/**
|
||||
* What is wrong with StartJobRun's executionIamPolicy, against the model's constraints: an inline policy of 1 to
|
||||
* 2,048 characters, and at most 10 policy ARNs of 20 to 2,048 characters each. A member of another JSON type is
|
||||
* refused with SerializationException, as retryPolicy's are.
|
||||
*/
|
||||
private static List<String> executionIamPolicyViolations(Map<String, Object> given) {
|
||||
List<String> violations = new ArrayList<>();
|
||||
Object policy = given != null ? given.get(INLINE_POLICY) : null;
|
||||
Object arns = given != null ? given.get(POLICY_ARNS) : null;
|
||||
if (policy != null && !(policy instanceof String)) {
|
||||
throw new AwsException("SerializationException", "executionIamPolicy.policy must be a string", 400);
|
||||
}
|
||||
if (arns != null && !(arns instanceof List<?> list && list.stream().allMatch(String.class::isInstance))) {
|
||||
throw new AwsException("SerializationException",
|
||||
"executionIamPolicy.policyArns must be a list of strings", 400);
|
||||
}
|
||||
if (policy instanceof String document) {
|
||||
String violation = SmithyValidationErrors.Violations.WITHOUT_VALUES.lengthOutOfRange(
|
||||
"executionIamPolicy.policy", document, 1, MAX_POLICY_LENGTH);
|
||||
if (violation != null) {
|
||||
violations.add(violation);
|
||||
}
|
||||
}
|
||||
if (arns instanceof List<?> list) {
|
||||
String size = SmithyValidationErrors.sizeOutOfRange("executionIamPolicy.policyArns", list.size(), 0,
|
||||
MAX_POLICY_ARNS);
|
||||
if (size != null) {
|
||||
violations.add(size);
|
||||
}
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
String violation = SmithyValidationErrors.lengthOutOfRange(
|
||||
"executionIamPolicy.policyArns." + (i + 1) + ".member", (String) list.get(i), MIN_ARN_LENGTH,
|
||||
MAX_POLICY_LENGTH);
|
||||
if (violation != null) {
|
||||
violations.add(violation);
|
||||
}
|
||||
}
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
// StartJobRun's flexExecution as the model's FlexExecution holds it: its one member, enabled, where it is given.
|
||||
private static Map<String, Object> flexExecution(Map<String, Object> given) {
|
||||
if (given == null) {
|
||||
@@ -1142,6 +1204,18 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* The inline policy of a run's executionIamPolicy, which scopes its credentials: "The resulting job IAM role
|
||||
* permissions will be an intersection of this policy and the policy associated with your job execution role".
|
||||
* Null where it has none. Its policyArns are not applied, as Floci's IAM applies no managed session policy, an
|
||||
* assumed role's included.
|
||||
*/
|
||||
private synchronized String inlinePolicy(String jobRunId) {
|
||||
return store.get(jobRunId).map(record -> record.getJobRun().getExecutionIamPolicy())
|
||||
.map(policy -> policy.get(INLINE_POLICY)).filter(String.class::isInstance).map(String.class::cast)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
// A run cancelled with a grace period that has not ended, whose driver may still end by itself.
|
||||
private boolean shuttingDown(JobRunRecord record) {
|
||||
return STATE_CANCELLING.equals(record.getJobRun().getState()) && record.getShutdownDeadline() != null
|
||||
@@ -1794,7 +1868,7 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
if (flex != null && !(flex instanceof Boolean)) {
|
||||
throw new AwsException("SerializationException", "flexExecution.enabled must be a boolean", 400);
|
||||
}
|
||||
List<String> violations = new ArrayList<>();
|
||||
List<String> violations = executionIamPolicyViolations(request.getExecutionIamPolicy());
|
||||
if (request.getClientToken() == null) {
|
||||
violations.add(SmithyValidationErrors.notNull("clientToken"));
|
||||
}
|
||||
@@ -1991,5 +2065,10 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
RequestScopes.runAs(job.accountId(), job.region(),
|
||||
() -> keepExecutorLifetimes(job.runId(), launchedIn, lifetimes));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sessionPolicy() {
|
||||
return RequestScopes.callAs(job.accountId(), job.region(), () -> inlinePolicy(job.runId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -540,7 +540,8 @@ public class EmrServerlessSparkRunner implements SparkJobRunner, ContainerTeardo
|
||||
if (progress.cancelled()) {
|
||||
return Outcome.halted();
|
||||
}
|
||||
credentials = roleCredentials.forRole(job.executionRole(), job.accountId(), job.runId());
|
||||
credentials = roleCredentials.forRole(job.executionRole(), job.accountId(), job.runId(),
|
||||
progress.sessionPolicy());
|
||||
Map<String, String> labels = new LinkedHashMap<>(ContainerStorageHelper.resourceIdentityLabels(
|
||||
job.service(), job.runId(), job.accountId(), job.region()));
|
||||
// The deployment that started the driver, which removeLeftoverDrivers() goes by.
|
||||
|
||||
@@ -103,6 +103,14 @@ public interface SparkJobRunner {
|
||||
*/
|
||||
default void executorsRan(List<Duration> lifetimes) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The inline session policy the run's credentials are scoped by, so that its permissions are what its
|
||||
* execution role and the policy both allow; null for none.
|
||||
*/
|
||||
default String sessionPolicy() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** What a run's container runs as: its driver, or one of its executors. */
|
||||
|
||||
@@ -5558,6 +5558,18 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void registerLambdaExecutionRoleSession(String accountId, String sessionAccessKeyId,
|
||||
String secretAccessKey, String sessionToken, String roleArn,
|
||||
String roleSessionName, String assumedRoleId) {
|
||||
registerLambdaExecutionRoleSession(accountId, sessionAccessKeyId, secretAccessKey, sessionToken, roleArn,
|
||||
roleSessionName, assumedRoleId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a non-expiring execution-role session with its session token, assumed role ID and an inline session
|
||||
* policy, which scopes it as an assumed role's session policy does; null for none.
|
||||
*/
|
||||
public void registerLambdaExecutionRoleSession(String accountId, String sessionAccessKeyId,
|
||||
String secretAccessKey, String sessionToken, String roleArn,
|
||||
String roleSessionName, String assumedRoleId,
|
||||
String sessionPolicyDocument) {
|
||||
if (accountId == null || accountId.isBlank()) {
|
||||
throw new IllegalArgumentException("Lambda function account ID must not be blank");
|
||||
}
|
||||
@@ -5566,6 +5578,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
session.setLambdaExecutionRole(true);
|
||||
session.setRoleSessionName(roleSessionName);
|
||||
session.setAssumedRoleId(assumedRoleId);
|
||||
session.setSessionPolicyDocument(sessionPolicyDocument);
|
||||
issuedNow(session);
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
aware.putForAccount(accountId, sessionAccessKeyId, session);
|
||||
|
||||
+11
-1
@@ -58,6 +58,16 @@ public class LambdaExecutionRoleCredentials {
|
||||
|
||||
/** A session for {@code roleArn} assumed on behalf of {@code functionName} in {@code functionAccountId}. */
|
||||
public Optional<SessionCreds> forRole(String roleArn, String functionAccountId, String functionName) {
|
||||
return forRole(roleArn, functionAccountId, functionName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A session for {@code roleArn} assumed on behalf of {@code functionName} in {@code functionAccountId}, its
|
||||
* permissions scoped by the inline {@code sessionPolicy} as an assumed role's are by the one it is assumed with;
|
||||
* none where it is null.
|
||||
*/
|
||||
public Optional<SessionCreds> forRole(String roleArn, String functionAccountId, String functionName,
|
||||
String sessionPolicy) {
|
||||
if (roleArn == null || roleArn.isBlank()
|
||||
|| functionAccountId == null || functionAccountId.isBlank()) {
|
||||
return Optional.empty();
|
||||
@@ -90,7 +100,7 @@ public class LambdaExecutionRoleCredentials {
|
||||
String assumedRoleId = role.get().getRoleId() != null ? role.get().getRoleId() + ":" + functionName : null;
|
||||
iamService.registerLambdaExecutionRoleSession(
|
||||
functionAccountId, credentials.accessKeyId(), credentials.secretAccessKey(),
|
||||
credentials.sessionToken(), roleArn, functionName, assumedRoleId);
|
||||
credentials.sessionToken(), roleArn, functionName, assumedRoleId, sessionPolicy);
|
||||
return Optional.of(credentials);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user