mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(emr-serverless): give resource-tag conditions the tags of the application, job run or session a request names (#8853)
The generated request resources give EMR Serverless's application, jobRun and session types the aws:ResourceTag/ key, but no service answered the emr-serverless namespace's stored tags, so a condition on their tags saw none. EmrServerlessStoredResourceArns now answers it for the three services that hold them: storedTags reads the application, job run or session by its id, in the request's account, where the tag handlers read, in place, and matches it on its application and on the whole ARN; a deleted application's resources name nothing. The tags fields are now volatile.
This commit is contained in:
1 parent
84e60f73a8
commit
a72aedfbb0
11 files changed
+281
-10
No files matched your search
@@ -51,10 +51,10 @@ import java.util.Optional;
|
||||
* tags of a DB instance, a DB cluster, a parameter group or a backup; CloudTrail, for the tags of a trail; Budgets, for
|
||||
* the tags of a budget or a budget action; AWS FIS, for the tags of an experiment template, an experiment or an action;
|
||||
* CodePipeline, for the tags of a pipeline, a webhook or a custom action type; DataSync, for the tags of an agent, a
|
||||
* location or a task; Global Accelerator, for the tags of an accelerator, a listener or an endpoint group); looked up
|
||||
* lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
* location or a task; Global Accelerator, for the tags of an accelerator, a listener or an endpoint group; EMR
|
||||
* Serverless, for the tags of an application, a job run or a session); looked up lazily by {@code ResourceArnBuilder}
|
||||
* and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or
|
||||
* an ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -48,6 +48,7 @@ import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -480,6 +481,11 @@ public class EmrServerlessJobRunService implements Resettable {
|
||||
store.put(record.getJobRun().getJobRunId(), record);
|
||||
}
|
||||
|
||||
/** The job run {@code jobRunId} of {@code account} as stored, read in place, for IAM's lookup of stored tags. */
|
||||
Optional<JobRun> storedRun(String account, String jobRunId) {
|
||||
return AccountAwareStorageBackend.readInAccount(store, account, jobRunId).map(JobRunRecord::getJobRun);
|
||||
}
|
||||
|
||||
/** GetJobRun's {@code attempt}, an integer in the query string. */
|
||||
static Integer parseAttempt(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
|
||||
@@ -82,7 +82,8 @@ public class EmrServerlessService {
|
||||
Set.of("RepositoryNotFoundException", "ImageNotFoundException", "ServerException");
|
||||
// A release label's major and minor versions.
|
||||
private static final Pattern RELEASE_VERSION = Pattern.compile("emr-(\\d{1,6})\\.(\\d{1,6})(?:\\..*)?");
|
||||
private static final Pattern RESOURCE =
|
||||
/** A tagging ARN's resource: an application's id, then {@code jobruns} or {@code sessions} and its id. */
|
||||
static final Pattern RESOURCE =
|
||||
Pattern.compile("/applications/([0-9a-zA-Z]+)(?:/(jobruns|sessions)/([0-9a-zA-Z]+))?");
|
||||
private static final List<String> APPLICATION_STATES =
|
||||
List.of("CREATING", "CREATED", "STARTING", "STARTED", "STOPPING", "STOPPED", "TERMINATED");
|
||||
@@ -569,6 +570,16 @@ public class EmrServerlessService {
|
||||
session ? matcher.group(3) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The application {@code applicationId} of {@code account} as stored, read in place, a record stored before storage
|
||||
* was account-aware included, unless it was deleted, which leaves it TERMINATED: what IAM's lookup of stored tags
|
||||
* reads, never under this service's lock, a tag call giving the application a new map.
|
||||
*/
|
||||
Optional<Application> storedLive(String account, String applicationId) {
|
||||
return AccountAwareStorageBackend.readInAccount(storage, account, applicationId)
|
||||
.filter(app -> !"TERMINATED".equals(app.getState()));
|
||||
}
|
||||
|
||||
/** Whether the application exists and has not been deleted, which leaves it TERMINATED. */
|
||||
public synchronized boolean isLive(String applicationId) {
|
||||
return live(applicationId).isPresent();
|
||||
|
||||
@@ -368,6 +368,11 @@ public class EmrServerlessSessionService implements SparkConnectTargets, Resetta
|
||||
store.put(record.getSession().getSessionId(), record);
|
||||
}
|
||||
|
||||
/** The session {@code sessionId} of {@code account} as stored, read in place, for IAM's lookup of stored tags. */
|
||||
Optional<Session> storedSession(String account, String sessionId) {
|
||||
return AccountAwareStorageBackend.readInAccount(store, account, sessionId).map(SessionRecord::getSession);
|
||||
}
|
||||
|
||||
private static void validate(StartSessionRequest request) {
|
||||
List<String> violations = new ArrayList<>();
|
||||
if (request.getClientToken() == null) {
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package io.floci.aws.services.emrserverless;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.emrserverless.model.Application;
|
||||
import io.floci.aws.services.emrserverless.model.JobRun;
|
||||
import io.floci.aws.services.emrserverless.model.Session;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
/**
|
||||
* What IAM asks EMR Serverless of the resources a request names: the tags of an application, a job run or a session,
|
||||
* which three services hold, so one answer stands for the namespace.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EmrServerlessStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private static final String NAMESPACE = "emr-serverless";
|
||||
|
||||
private final RegionResolver regionResolver;
|
||||
private final EmrServerlessService applications;
|
||||
private final EmrServerlessJobRunService jobRuns;
|
||||
private final EmrServerlessSessionService sessions;
|
||||
|
||||
@Inject
|
||||
public EmrServerlessStoredResourceArns(RegionResolver regionResolver, EmrServerlessService applications,
|
||||
EmrServerlessJobRunService jobRuns, EmrServerlessSessionService sessions) {
|
||||
this.regionResolver = regionResolver;
|
||||
this.applications = applications;
|
||||
this.jobRuns = jobRuns;
|
||||
this.sessions = sessions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
/** A request names an application, a job run or a session by the id its ARN ends in: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the application, the job run or the session whose ARN is {@code arn}, in the request's account,
|
||||
* where the tag handlers read: an ARN of another account names none, and neither does one of a deleted
|
||||
* application, nor a job run's or a session's under it. Each is read in place by its id, a record stored before
|
||||
* storage was account-aware included, and matched on its application and on the whole ARN it records. Tag calls
|
||||
* give the resource a new map, so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, NAMESPACE)) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = regionResolver.getAccountId();
|
||||
Matcher matcher = EmrServerlessService.RESOURCE.matcher(parsed.resource());
|
||||
if (!parsed.accountId().equals(account) || !matcher.matches()) {
|
||||
return Map.of();
|
||||
}
|
||||
String applicationId = matcher.group(1);
|
||||
Optional<Application> application = applications.storedLive(account, applicationId);
|
||||
if (application.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<String, String> tags;
|
||||
if (matcher.group(2) == null) {
|
||||
tags = application.filter(app -> arn.equals(app.getArn())).map(Application::getTags).orElse(null);
|
||||
} else if ("sessions".equals(matcher.group(2))) {
|
||||
tags = sessions.storedSession(account, matcher.group(3))
|
||||
.filter(session -> applicationId.equals(session.getApplicationId()) && arn.equals(session.getArn()))
|
||||
.map(Session::getTags).orElse(null);
|
||||
} else {
|
||||
tags = jobRuns.storedRun(account, matcher.group(3))
|
||||
.filter(run -> applicationId.equals(run.getApplicationId()) && arn.equals(run.getArn()))
|
||||
.map(JobRun::getTags).orElse(null);
|
||||
}
|
||||
return tags == null ? Map.of() : new LinkedHashMap<>(tags);
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class Application {
|
||||
private Instant createdAt;
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
private Instant updatedAt;
|
||||
private Map<String, String> tags;
|
||||
private volatile Map<String, String> tags;
|
||||
private String architecture;
|
||||
private Map<String, InitialCapacityConfig> initialCapacity;
|
||||
private MaximumCapacity maximumCapacity;
|
||||
|
||||
@@ -27,7 +27,7 @@ public class JobRun {
|
||||
private String releaseLabel;
|
||||
private Map<String, Object> configurationOverrides;
|
||||
private JobDriver jobDriver;
|
||||
private Map<String, String> tags;
|
||||
private volatile Map<String, String> tags;
|
||||
private NetworkConfiguration networkConfiguration;
|
||||
private Integer totalExecutionDurationSeconds;
|
||||
private Long executionTimeoutMinutes;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class Session implements SparkConnectSession {
|
||||
private Map<String, Object> configurationOverrides;
|
||||
private NetworkConfiguration networkConfiguration;
|
||||
private Long idleTimeoutMinutes;
|
||||
private Map<String, String> tags;
|
||||
private volatile Map<String, String> tags;
|
||||
private Long totalExecutionDurationSeconds;
|
||||
|
||||
public String getApplicationId() { return applicationId; }
|
||||
|
||||
Reference in new issue
Block a user