mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(emr-containers): give resource-tag conditions the tags of the resource a request names (#8875)
The generated request resources give EMR on EKS's virtualCluster, jobRun, managedEndpoint, jobTemplate and securityConfiguration types the aws:ResourceTag/ key, but no service answered the emr-containers namespace's stored tags, so a condition on their tags saw none. EmrContainersService now answers it: storedTags reads the resource stored under its ARN in the store its kind names, in the request's account, where the handlers read, in place, and matches the ARN it records. Every write already stores a new copy, so no lock change.
This commit is contained in:
1 parent
81c5ecf777
commit
7b750b8455
5 files changed
+134
-6
No files matched your search
@@ -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<String, ObjectNode> 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);
|
||||
|
||||
@@ -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\":\"*\","
|
||||
|
||||
Reference in new issue
Block a user