diff --git a/docs/services/iam.md b/docs/services/iam.md index 805ebb923d..a31594ef38 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -3457,8 +3457,9 @@ floci populates: DB instances, DB clusters, parameter groups and backups, CloudTrail for its trails, Budgets for its budgets and budget actions, AWS FIS for its experiment templates, experiments and actions, CodePipeline for its pipelines, webhooks and custom action types, DataSync for its agents, locations and tasks, Global Accelerator for its accelerators, listeners - and endpoint groups, and EMR Serverless for its applications, job runs and sessions. Tag keys differing only in case - are one condition key holding every value. + and endpoint groups, EMR Serverless for its applications, job runs and sessions, and EMR on EKS for its virtual + clusters, job runs, managed endpoints, job templates and security configurations. 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 diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java index 402ae51442..7816ca68ad 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -52,9 +52,10 @@ import java.util.Optional; * 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; 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} for the namespace a request or - * an ARN is of, so IAM never depends on that service directly.

+ * Serverless, for the tags of an application, a job run or a session; EMR on EKS, for the tags of a virtual cluster, a + * job run, a managed endpoint, a job template or a security configuration); looked up lazily by + * {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance} for + * the namespace a request or an ARN is of, so IAM never depends on that service directly.

* *

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 diff --git a/src/main/java/io/floci/aws/services/emrcontainers/EmrContainersService.java b/src/main/java/io/floci/aws/services/emrcontainers/EmrContainersService.java index dcf4a5f4e8..ee10769ed8 100644 --- a/src/main/java/io/floci/aws/services/emrcontainers/EmrContainersService.java +++ b/src/main/java/io/floci/aws/services/emrcontainers/EmrContainersService.java @@ -16,6 +16,7 @@ import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.RequestContext; import io.floci.aws.core.common.Resettable; import io.floci.aws.core.common.SmithyValidationErrors; +import io.floci.aws.core.common.StoredResourceArns; import io.floci.aws.core.storage.AccountAwareStorageBackend; import io.floci.aws.core.storage.StorageFactory; import io.floci.aws.services.eks.EksService; @@ -54,7 +55,7 @@ import java.util.regex.Pattern; * starts with {@code _} are Floci's own and never leave the service. */ @ApplicationScoped -public class EmrContainersService implements Resettable { +public class EmrContainersService implements Resettable, StoredResourceArns { static final String SERVICE = "emr-containers"; static final String VIRTUAL_CLUSTERS = "/virtualclusters/"; @@ -592,6 +593,55 @@ public class EmrContainersService implements Resettable { target.storage().put(resourceArn, updated); } + @Override + public String namespace() { + return "emr-containers"; + } + + /** A request names each resource by the id its ARN ends in: nothing to find. */ + @Override + public Optional find(String type, List names, String region) { + return Optional.empty(); + } + + /** + * The tags of the virtual cluster, job run, managed endpoint, job template or security configuration whose ARN is + * {@code arn}, in the request's account, where the handlers read: an ARN of another account names none. Each is + * stored under its ARN, so it is read in place under the ARN itself, a resource stored before storage was + * account-aware included, and matched on the ARN it records. Every write stores a new copy of the resource, so + * the read is of one whole set. + */ + @Override + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "emr-containers")) { + return Map.of(); + } + AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); + AccountAwareStorageBackend store = storeOf(parsed.resource()); + if (store == null || !parsed.accountId().equals(regionResolver.getAccountId())) { + return Map.of(); + } + return AccountAwareStorageBackend.readInAccount(store, parsed.accountId(), arn) + .filter(resource -> arn.equals(resource.path("arn").textValue())) + .map(EmrContainersService::tagMap) + .orElse(Map.of()); + } + + /** The store holding the kind of resource an ARN's resource part names, or null for none of them. */ + private AccountAwareStorageBackend storeOf(String resource) { + Matcher nested = NESTED_ARN.matcher(resource); + if (nested.matches()) { + return "jobruns".equals(nested.group(2)) ? jobRuns : endpoints; + } + if (VIRTUAL_CLUSTER_ARN.matcher(resource).matches()) { + return virtualClusters; + } + if (resource.startsWith(JOB_TEMPLATES)) { + return jobTemplates; + } + return resource.startsWith(SECURITY_CONFIGURATIONS) ? securityConfigurations : null; + } + @Override public synchronized void clear() { virtualClusters.clear(); diff --git a/src/test/java/io/floci/aws/services/emrcontainers/EmrContainersServiceTest.java b/src/test/java/io/floci/aws/services/emrcontainers/EmrContainersServiceTest.java index 96258a6269..b8dd9645b8 100644 --- a/src/test/java/io/floci/aws/services/emrcontainers/EmrContainersServiceTest.java +++ b/src/test/java/io/floci/aws/services/emrcontainers/EmrContainersServiceTest.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.storage.AccountAwareStorageBackend; +import io.floci.aws.core.storage.InMemoryStorage; import io.floci.aws.services.eks.EksService; import io.floci.aws.services.eks.model.AccessConfig; import io.floci.aws.services.eks.model.Cluster; @@ -21,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -279,6 +281,51 @@ class EmrContainersServiceTest { assertFalse(cleared.has("schedulerStatus")); } + /** + * IAM's lookup reads the tags of the virtual cluster, job run or job template the ARN names, as the tag calls + * leave them, and nothing for another account's, another Region's, an unknown or another service's ARN. + */ + @Test + void storedTagsAreTheTagsOfTheResourceTheArnNames() { + // A job run that takes an hour, so it is still running when it is tagged. + EmrContainersService service = service(3600); + String clusterId = virtualCluster(service, EKS_CLUSTER); + String clusterArn = service.describeVirtualCluster(clusterId).at("/virtualCluster/arn").textValue(); + service.tagResource(clusterArn, Map.of("team", "data", "env", "dev")); + service.untagResource(clusterArn, List.of("env")); + String runArn = startJobRun(service, clusterId).get("arn").textValue(); + service.tagResource(runArn, Map.of("team", "ops")); + String templateArn = service.createJobTemplate(json(""" + {"name": "tagged", "clientToken": "%s", "tags": {"team": "web"}, "jobTemplateData": { + "executionRoleArn": "%s", "releaseLabel": "%s", + "jobDriver": {"sparkSubmitJobDriver": {"entryPoint": "s3://bucket/pi.py"}}}} + """.formatted(UUID.randomUUID(), ROLE, RELEASE))).get("arn").textValue(); + + assertEquals("emr-containers", service.namespace()); + assertEquals(Map.of("team", "data"), service.storedTags(clusterArn)); + assertEquals(Map.of("team", "ops"), service.storedTags(runArn)); + assertEquals(Map.of("team", "web"), service.storedTags(templateArn)); + assertTrue(service.storedTags(clusterArn.replace(ACCOUNT_ID, "111122223333")).isEmpty()); + assertTrue(service.storedTags(clusterArn.replace(REGION, "us-west-2")).isEmpty()); + assertTrue(service.storedTags(clusterArn.replace(clusterId, "0000000000000000000000000")).isEmpty()); + assertTrue(service.storedTags("arn:aws:emr-serverless:us-east-1:000000000000:/applications/abc").isEmpty()); + } + + /** A virtual cluster stored before storage was account-aware is read in place: the lookup migrates nothing. */ + @Test + void storedTagsReadAPreAccountAwareVirtualClusterInPlace() { + InMemoryStorage raw = new InMemoryStorage<>(); + EmrContainersService service = service(0, REGION, List.of(new AccountAwareStorageBackend<>(raw, null, + ACCOUNT_ID), AccountAwareStorageBackend.inMemory(ACCOUNT_ID), AccountAwareStorageBackend.inMemory( + ACCOUNT_ID), AccountAwareStorageBackend.inMemory(ACCOUNT_ID), AccountAwareStorageBackend.inMemory( + ACCOUNT_ID))); + String arn = "arn:aws:emr-containers:us-east-1:000000000000:/virtualclusters/legacy0000000000000000000"; + raw.put(arn, json("{\"arn\": \"" + arn + "\", \"tags\": {\"team\": \"data\"}}")); + + assertEquals(Map.of("team", "data"), service.storedTags(arn)); + assertEquals(Set.of(arn), raw.keys()); + } + @Test void terminatingAVirtualClusterClearsItsTagsAndRefusesNewOnes() { EmrContainersService service = service(0); diff --git a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java index a82652891c..7908916dbe 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -1216,6 +1216,35 @@ class IamRestRouteEnforcementIntegrationTest { refused(request(user, "oam", "POST", "/GetSink", getSink), "oam:GetSink"); } + /** + * An {@code aws:ResourceTag} condition reads the tags EMR on EKS holds on the job template a request names, those + * it was created with (TagResource takes only virtual clusters, job runs and managed endpoints), so an allow on one + * team's tag lets the caller read that team's template and not another's. + */ + @Test + void anEmrContainersResourceTagConditionReadsTheTagsOfTheJobTemplate() { + String tagged = emrContainersJobTemplate("data"); + String other = emrContainersJobTemplate("web"); + String user = user("route-emrc-tags"); + + policy(user, onTeam("Allow", "emr-containers:DescribeJobTemplate", "data")); + request(user, "emr-containers", "GET", "/jobtemplates/" + tagged, null).statusCode(200); + refused(request(user, "emr-containers", "GET", "/jobtemplates/" + other, null), + "emr-containers:DescribeJobTemplate"); + } + + /** The id of an EMR on EKS job template an administrator creates, tagged with {@code team}, deleted after. */ + private String emrContainersJobTemplate(String team) { + String body = "{\"name\":\"route-template-" + suffix() + "\",\"clientToken\":\"" + suffix() + + "\",\"tags\":{\"team\":\"" + team + "\"},\"jobTemplateData\":{\"executionRoleArn\":\"arn:aws:iam::" + + ACCOUNT + ":role/emr-job\",\"releaseLabel\":\"emr-7.5.0-latest\",\"jobDriver\":" + + "{\"sparkSubmitJobDriver\":{\"entryPoint\":\"s3://bucket/pi.py\"}}}}"; + String id = adminOf("emr-containers").body(body).post("/jobtemplates").then().statusCode(200).extract() + .path("id"); + cleanup.register(() -> adminOf("emr-containers").delete("/jobtemplates/" + id).then().statusCode(200)); + return id; + } + /** A statement with {@code effect} on {@code action} for every resource whose {@code team} tag is the one given. */ private static String onTeam(String effect, String action, String team) { return "{\"Effect\":\"" + effect + "\",\"Action\":\"" + action + "\",\"Resource\":\"*\","