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
+27
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test;
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.testing.EmrServerlessRequests.tagsPath;
|
||||
import static io.floci.aws.testing.SignedRestRequests.denied;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -101,6 +102,32 @@ class EmrServerlessIamIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags EMR Serverless holds on the application a request names, as
|
||||
* TagResource leaves them, so an allow on one team's tag lets the caller read that team's application and not
|
||||
* another's.
|
||||
*/
|
||||
@Test
|
||||
void aResourceTagConditionReadsTheTagsOfTheApplication() throws Exception {
|
||||
String tagged = createApplication();
|
||||
String other = createApplication();
|
||||
send(admin, "POST", tagsPath(applicationArn(tagged)), "{\"tags\":{\"team\":\"data\"}}").then()
|
||||
.statusCode(200);
|
||||
send(admin, "POST", tagsPath(applicationArn(other)), "{\"tags\":{\"team\":\"web\"}}").then()
|
||||
.statusCode(200);
|
||||
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "emrs-iam-", "access",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"emr-serverless:GetApplication\",\"Resource\":\"*\","
|
||||
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}]}")) {
|
||||
send(user.signer("emr-serverless"), "GET", "/applications/" + tagged, null).then().statusCode(200);
|
||||
denied(send(user.signer("emr-serverless"), "GET", "/applications/" + other, null),
|
||||
"emr-serverless:GetApplication");
|
||||
} finally {
|
||||
send(admin, "DELETE", "/applications/" + tagged, null).then().statusCode(200);
|
||||
send(admin, "DELETE", "/applications/" + other, null).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
private static String allow(String actions, String resource) {
|
||||
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":" + actions
|
||||
+ ",\"Resource\":\"" + resource + "\"}]}";
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package io.floci.aws.services.emrserverless;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
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.emrserverless.model.Application;
|
||||
import io.floci.aws.services.emrserverless.model.CreateApplicationRequest;
|
||||
import io.floci.aws.services.emrserverless.model.InteractiveConfiguration;
|
||||
import io.floci.aws.services.emrserverless.model.JobDriver;
|
||||
import io.floci.aws.services.emrserverless.model.StartJobRunRequest;
|
||||
import io.floci.aws.services.emrserverless.model.StartSessionRequest;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import io.floci.aws.testing.MutableClock;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** IAM's lookup of the tags EMR Serverless holds on an application, a job run or a session. */
|
||||
class EmrServerlessStoredResourceArnsTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/emr-serverless-tags";
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||
private final RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT_ID);
|
||||
private final MutableClock clock = new MutableClock();
|
||||
private EmrServerlessService applications;
|
||||
private EmrServerlessJobRunService jobRuns;
|
||||
private EmrServerlessSessionService sessions;
|
||||
private EmrServerlessStoredResourceArns stored;
|
||||
|
||||
/**
|
||||
* The lookup reads the tags of the application, the job run or the session the ARN names, as the tag calls leave
|
||||
* them, and nothing for another account's, another application's, a deleted application's or an unknown ARN.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsAreTheTagsOfTheResourceTheArnNames() {
|
||||
build(new InMemoryStorageFactory(ACCOUNT_ID));
|
||||
Application app = applications.createApplication(application(Map.of("team", "web", "env", "dev")));
|
||||
String appArn = app.getArn();
|
||||
applications.tagResource(applications.resourceArn(appArn), Map.of("team", "data"));
|
||||
applications.untagResource(applications.resourceArn(appArn), List.of("env"));
|
||||
String runArn = jobRuns.startJobRun(app.getApplicationId(), jobRun(Map.of("team", "ops"))).getArn();
|
||||
StartSessionRequest session = new StartSessionRequest();
|
||||
session.setClientToken(UUID.randomUUID().toString());
|
||||
session.setExecutionRoleArn(ROLE);
|
||||
session.setTags(Map.of("team", "sre"));
|
||||
String sessionArn = sessions.startSession(app.getApplicationId(), session).getArn();
|
||||
Application deleted = applications.createApplication(application(Map.of("team", "data")));
|
||||
applications.deleteApplication(deleted.getApplicationId());
|
||||
|
||||
assertEquals("emr-serverless", stored.namespace());
|
||||
assertEquals(Map.of("team", "data"), stored.storedTags(appArn));
|
||||
assertEquals(Map.of("team", "ops"), stored.storedTags(runArn));
|
||||
assertEquals(Map.of("team", "sre"), stored.storedTags(sessionArn));
|
||||
assertTrue(stored.storedTags(appArn.replace(ACCOUNT_ID, "111122223333")).isEmpty());
|
||||
assertTrue(stored.storedTags(runArn.replace(app.getApplicationId(), deleted.getApplicationId())).isEmpty());
|
||||
assertTrue(stored.storedTags(deleted.getArn()).isEmpty());
|
||||
assertTrue(stored.storedTags(appArn + "/jobruns/00missing00").isEmpty());
|
||||
assertTrue(stored.storedTags("arn:aws:emr-containers:us-east-1:000000000000:/virtualclusters/abc").isEmpty());
|
||||
}
|
||||
|
||||
/** An application stored before storage was account-aware is read in place: the lookup migrates nothing. */
|
||||
@Test
|
||||
void storedTagsReadAPreAccountAwareApplicationInPlace() {
|
||||
InMemoryStorage<String, Application> raw = new InMemoryStorage<>();
|
||||
build(new InMemoryStorageFactory(ACCOUNT_ID).withStore("emr-serverless-applications.json", raw));
|
||||
Application legacy = new Application();
|
||||
legacy.setApplicationId("00legacy00");
|
||||
legacy.setArn("arn:aws:emr-serverless:us-east-1:000000000000:/applications/00legacy00");
|
||||
legacy.setState("CREATED");
|
||||
legacy.setTags(Map.of("team", "data"));
|
||||
raw.put("00legacy00", legacy);
|
||||
|
||||
assertEquals(Map.of("team", "data"), stored.storedTags(legacy.getArn()));
|
||||
assertEquals(Set.of("00legacy00"), raw.keys());
|
||||
}
|
||||
|
||||
private void build(InMemoryStorageFactory storage) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.defaultAccountId()).thenReturn(ACCOUNT_ID);
|
||||
applications = new EmrServerlessService(config, storage, regionResolver, clock);
|
||||
IamService iamService = mock(IamService.class);
|
||||
when(iamService.resolveCallerArn(any())).thenReturn(Optional.empty());
|
||||
jobRuns = new EmrServerlessJobRunService(AccountAwareStorageBackend.inMemory(ACCOUNT_ID), applications,
|
||||
iamService, regionResolver, null, objectMapper, 0, clock);
|
||||
sessions = new EmrServerlessSessionService(AccountAwareStorageBackend.inMemory(ACCOUNT_ID), applications,
|
||||
jobRuns, iamService, regionResolver, null, objectMapper, clock, "http://localhost:4566/");
|
||||
stored = new EmrServerlessStoredResourceArns(regionResolver, applications, jobRuns, sessions);
|
||||
}
|
||||
|
||||
private static CreateApplicationRequest application(Map<String, String> tags) {
|
||||
CreateApplicationRequest create = new CreateApplicationRequest();
|
||||
create.setName("tags");
|
||||
create.setReleaseLabel("emr-7.13.0");
|
||||
create.setType("SPARK");
|
||||
create.setClientToken(UUID.randomUUID().toString());
|
||||
InteractiveConfiguration interactive = new InteractiveConfiguration();
|
||||
interactive.setProperty("sessionEnabled", true);
|
||||
create.setInteractiveConfiguration(interactive);
|
||||
create.setTags(tags);
|
||||
return create;
|
||||
}
|
||||
|
||||
private static StartJobRunRequest jobRun(Map<String, String> tags) {
|
||||
JobDriver.SparkSubmit sparkSubmit = new JobDriver.SparkSubmit();
|
||||
sparkSubmit.setEntryPoint("s3://bucket/jobs/app.jar");
|
||||
JobDriver driver = new JobDriver();
|
||||
driver.setSparkSubmit(sparkSubmit);
|
||||
StartJobRunRequest request = new StartJobRunRequest();
|
||||
request.setClientToken(UUID.randomUUID().toString());
|
||||
request.setExecutionRoleArn(ROLE);
|
||||
request.setJobDriver(driver);
|
||||
request.setTags(tags);
|
||||
return request;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user