Light, fluffy, and always free - The AWS Local Emulator alternative
fix(emr-serverless): isolate applications by request region (#6078)
Jimmy Liu Jiae14e5433ab9c
diff --git a/docs/services/emr-serverless.md b/docs/services/emr-serverless.mdindex ccc902cbab..86c90e138e 100644--- a/docs/services/emr-serverless.md+++ b/docs/services/emr-serverless.md@@ -29,6 +29,9 @@ The tagging operations work on application ARNs, as `CreateApplication` returnsstores, so `GetApplication` always reports the current set. Following the EMR Serverless user guide, aresource holds at most 50 tags, and `aws:` in any case is reserved as the prefix of a tag key or value.+Applications, creation client tokens, and application tags are scoped to the request's AWS Region.+An application created in one Region is not returned by list, get, or tag requests signed for another.+## Limitations and Differences from AWS* **Jobs not implemented**: Floci supports the provisioning of the `Application` management plane (which satisfies tools like Terraform's `aws_emrserverless_application` resource). Executing actual Spark or Hive jobs against these applications via `StartJobRun` is not currently implemented.diff --git a/src/main/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessService.java b/src/main/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessService.javaindex acf369f9bd..487d245022 100644--- a/src/main/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessService.java+++ b/src/main/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessService.java@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.PaginatedResult;import io.github.hectorvent.floci.core.common.Pagination;+import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.RequestContext;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;@@ -40,13 +41,16 @@ public class EmrServerlessService {private final EmulatorConfig config;private final AccountAwareStorageBackend<Application> storage;+ private final RegionResolver regionResolver;@InjectRequestContext requestContext;@Inject- public EmrServerlessService(EmulatorConfig config, StorageFactory storageFactory) {+ public EmrServerlessService(EmulatorConfig config, StorageFactory storageFactory,+ RegionResolver regionResolver) {this.config = config;+ this.regionResolver = regionResolver;this.storage = storageFactory.create("emrserverless", "emr-serverless-applications.json",new TypeReference<Map<String, Application>>() {});}@@ -62,9 +66,10 @@ public class EmrServerlessService {throw new AwsException("ValidationException", "clientToken is required", 400);}+ String region = regionResolver.getRegion();if (request.getClientToken() != null) {for (Application existing : storage.scan(k -> true)) {- if (request.getClientToken().equals(existing.getClientToken())) {+ if (inRegion(existing, region) && request.getClientToken().equals(existing.getClientToken())) {return existing;}}@@ -104,11 +109,14 @@ public class EmrServerlessService {public Application getApplication(String applicationId) {return storage.get(applicationId)+ .filter(app -> inRegion(app, regionResolver.getRegion())).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Application " + applicationId + " not found", 404));}public PaginatedResult<ApplicationSummary> listApplications(ListApplicationsRequest request) {- List<Application> all = storage.scan(k -> true);+ String region = regionResolver.getRegion();+ List<Application> all = storage.scan(k -> true).stream()+ .filter(app -> inRegion(app, region)).collect(Collectors.toList());if (request.getStates() != null && !request.getStates().isEmpty()) {all = all.stream().filter(app -> request.getStates().contains(app.getState())).collect(Collectors.toList());}@@ -272,6 +280,9 @@ public class EmrServerlessService {if (!"emr-serverless".equals(parsed.service()) || !matcher.matches()) {throw new AwsException("ValidationException", "Invalid resourceArn: " + arn, 400);}+ if (!parsed.region().equals(regionResolver.getRegion())) {+ throw new AwsException("ResourceNotFoundException", "Resource " + arn + " not found", 404);+ }if (matcher.group(2) != null) {throw new AwsException("ResourceNotFoundException", "Job run " + matcher.group(2) + " not found", 404);}@@ -317,11 +328,23 @@ public class EmrServerlessService {}private String buildArn(String id) {- String region = requestContext != null && requestContext.getRegion() != null ? requestContext.getRegion() : config.defaultRegion();+ String region = regionResolver.getRegion();String accountId = requestContext != null && requestContext.getAccountId() != null ? requestContext.getAccountId() : config.defaultAccountId();return AwsArnUtils.Arn.of("emr-serverless", region, accountId, "/applications/" + id).toString();}+ private static boolean inRegion(Application app, String region) {+ if (app.getArn() == null) {+ return false;+ }+ try {+ return region.equals(AwsArnUtils.parse(app.getArn()).region());+ } catch (IllegalArgumentException ignored) {+ // A malformed stored ARN cannot establish Region ownership.+ return false;+ }+ }+private ApplicationSummary toSummary(Application app) {ApplicationSummary summary = new ApplicationSummary();summary.setId(app.getApplicationId());diff --git a/src/test/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessRegionIntegrationTest.javanew file mode 100644index 0000000000..a10fcff731--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/emrserverless/EmrServerlessRegionIntegrationTest.java@@ -0,0 +1,92 @@+package io.github.hectorvent.floci.services.emrserverless;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.Response;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import java.net.URLEncoder;+import java.nio.charset.StandardCharsets;+import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.hasItem;+import static org.hamcrest.Matchers.not;+import static org.hamcrest.Matchers.startsWith;++@QuarkusTest+class EmrServerlessRegionIntegrationTest {++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ private static RequestSpecification emr(String region) {+ return given()+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=TEST/20261020/" + region+ + "/emr-serverless/aws4_request, SignedHeaders=host;x-amz-date, Signature=test")+ .contentType("application/json")+ .urlEncodingEnabled(false);+ }++ private static String tagsPath(String arn) {+ return "/tags/" + URLEncoder.encode(arn, StandardCharsets.UTF_8);+ }++ @Test+ void applicationsAndClientTokensStayInTheirCreatingRegion() {+ String token = UUID.randomUUID().toString();+ String body = """+ {"name":"regional","releaseLabel":"emr-7.5.0","type":"SPARK",+ "clientToken":"%s","tags":{"team":"data"}}+ """.formatted(token);++ Response east = emr("us-east-1").body(body).when().post("/applications");+ east.then().statusCode(200).body("arn", startsWith("arn:aws:emr-serverless:us-east-1:"));+ String eastId = east.jsonPath().getString("applicationId");+ String eastArn = east.jsonPath().getString("arn");++ Response west = emr("eu-west-1").body(body).when().post("/applications");+ west.then().statusCode(200).body("arn", startsWith("arn:aws:emr-serverless:eu-west-1:"))+ .body("applicationId", not(equalTo(eastId)));+ String westId = west.jsonPath().getString("applicationId");+ String westArn = west.jsonPath().getString("arn");++ emr("us-east-1").body(body).when().post("/applications")+ .then().statusCode(200).body("applicationId", equalTo(eastId));+ emr("eu-west-1").body(body).when().post("/applications")+ .then().statusCode(200).body("applicationId", equalTo(westId));++ emr("us-east-1").when().get("/applications/" + eastId)+ .then().statusCode(200).body("application.arn", equalTo(eastArn));+ emr("eu-west-1").when().get("/applications/" + westId)+ .then().statusCode(200).body("application.arn", equalTo(westArn));+ emr("eu-west-1").when().get("/applications/" + eastId)+ .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));+ emr("us-east-1").when().get("/applications/" + westId)+ .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));++ emr("us-east-1").when().get("/applications")+ .then().statusCode(200)+ .body("applications.id", hasItem(eastId))+ .body("applications.id", not(hasItem(westId)));+ emr("eu-west-1").when().get("/applications")+ .then().statusCode(200)+ .body("applications.id", hasItem(westId))+ .body("applications.id", not(hasItem(eastId)));++ emr("us-east-1").when().get(tagsPath(eastArn))+ .then().statusCode(200).body("tags.team", equalTo("data"));+ emr("eu-west-1").when().get(tagsPath(eastArn))+ .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));+ emr("eu-west-1").body("{\"tags\":{\"team\":\"wrong-region\"}}")+ .when().post(tagsPath(eastArn))+ .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));+ emr("us-east-1").when().get(tagsPath(eastArn))+ .then().statusCode(200).body("tags.team", equalTo("data"));+ }+}