Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(emr): keep cluster operations in the cluster's region (#5097)

Hector Ventura0e89ab962a83
diff --git a/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java b/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java
index d705af98eb..42c065e871 100644
--- a/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/emr/EmrService.java
@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
/**
@@ -130,7 +131,7 @@ public class EmrService {
// ValidationException if any were termination protected.
boolean anyProtected = false;
for (String id : ids) {
- EmrCluster cluster = clusterStore.get(id).orElse(null);
+ EmrCluster cluster = findCluster(id).orElse(null);
if (cluster == null) {
continue;
}
@@ -184,8 +185,7 @@ public class EmrService {
// ──────────────────────────── Steps ────────────────────────────
public synchronized List<String> addJobFlowSteps(String clusterId, List<EmrStep> steps) {
- EmrCluster cluster = clusterStore.get(clusterId).orElseThrow(() -> new AwsException(
- "InvalidRequestException", "Cluster id '" + clusterId + "' is not valid.", 400));
+ EmrCluster cluster = requireCluster(clusterId);
List<String> ids = new ArrayList<>();
for (EmrStep step : steps) {
initStep(step);
@@ -648,13 +648,22 @@ public class EmrService {
if (id == null) {
throw new AwsException("InvalidRequestException", "ClusterId is required.", 400);
}
- return clusterStore.get(id).orElseThrow(() -> new AwsException(
+ return findCluster(id).orElseThrow(() -> new AwsException(
"InvalidRequestException", "Cluster id '" + id + "' is not valid.", 400));
}
+ /**
+ * Clusters are stored by id for every region; a request sees only its own region's, and one from
+ * another region reads as an unknown id. A cluster recorded without a region stays reachable.
+ */
+ private Optional<EmrCluster> findCluster(String id) {
+ String region = regionResolver.getRegion();
+ return clusterStore.get(id).filter(c -> c.getRegion() == null || c.getRegion().equals(region));
+ }
+
private void mutateClusters(List<String> ids, java.util.function.Consumer<EmrCluster> mutation) {
for (String id : ids) {
- clusterStore.get(id).ifPresent(c -> {
+ findCluster(id).ifPresent(c -> {
mutation.accept(c);
clusterStore.put(id, c);
});
diff --git a/src/test/java/io/github/hectorvent/floci/services/emr/EmrClusterRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/emr/EmrClusterRegionIntegrationTest.java
new file mode 100644
index 0000000000..b71fb5ed73
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/emr/EmrClusterRegionIntegrationTest.java
@@ -0,0 +1,73 @@
+package io.github.hectorvent.floci.services.emr;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * Clusters are regional: a request signed for another region sees none of a cluster's operations
+ * work on it, as for an unknown id, and cannot terminate it.
+ */
+@QuarkusTest
+class EmrClusterRegionIntegrationTest {
+
+ private static final String CT = "application/x-amz-json-1.1";
+ private static final String PREFIX = "ElasticMapReduce.";
+ private static final String HOME = "eu-west-1";
+ private static final String OTHER = "us-west-2";
+
+ @BeforeAll
+ static void configure() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ private static Response call(String region, String action, String body) {
+ return given().contentType(CT)
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region
+ + "/elasticmapreduce/aws4_request, SignedHeaders=host, Signature=abc")
+ .header("X-Amz-Target", PREFIX + action)
+ .body(body).when().post("/");
+ }
+
+ @Test
+ void clusterOperationsStayInTheClustersRegion() {
+ String clusterId = call(HOME, "RunJobFlow",
+ "{\"Name\":\"region-emr\",\"ReleaseLabel\":\"emr-7.5.0\","
+ + "\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true,"
+ + "\"InstanceGroups\":[{\"Name\":\"master\",\"InstanceRole\":\"MASTER\","
+ + "\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}]}}")
+ .then().statusCode(200)
+ .extract().path("JobFlowId");
+ String byId = "{\"ClusterId\":\"" + clusterId + "\"}";
+
+ call(OTHER, "DescribeCluster", byId)
+ .then().statusCode(400)
+ .body("__type", equalTo("InvalidRequestException"));
+ call(OTHER, "AddJobFlowSteps", "{\"JobFlowId\":\"" + clusterId + "\",\"Steps\":[{\"Name\":\"s\","
+ + "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\",\"Args\":[\"echo\"]}}]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("InvalidRequestException"));
+ call(OTHER, "ListClusters", "{}")
+ .then().statusCode(200)
+ .body("Clusters.Id", not(hasItem(clusterId)));
+ call(OTHER, "SetTerminationProtection",
+ "{\"JobFlowIds\":[\"" + clusterId + "\"],\"TerminationProtected\":true}")
+ .then().statusCode(200);
+ call(OTHER, "TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}")
+ .then().statusCode(200);
+ call(HOME, "DescribeCluster", byId)
+ .then().statusCode(200)
+ .body("Cluster.Status.State", equalTo("WAITING"))
+ .body("Cluster.TerminationProtected", equalTo(false));
+
+ call(HOME, "TerminateJobFlows", "{\"JobFlowIds\":[\"" + clusterId + "\"]}")
+ .then().statusCode(200);
+ }
+}