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

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

EMR looked a cluster up by id alone, so DescribeCluster, AddJobFlowSteps,
the instance group and fleet operations, policies, tags, TerminateJobFlows
and the cluster-wide setters acted on a cluster created in another region;
only ListClusters filtered by region. Every lookup now goes through one
region-aware findCluster: a cluster from another region reads as an unknown
id (InvalidRequestException), and terminate and the setters skip it, as they
skip an unknown id. A cluster recorded without a region stays reachable.

* test(emr): check a cluster-wide setter from another region leaves the cluster unchanged
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-05 00:36:51 -04:00
1 parent e22e24a810
commit 0e89ab962a
2 files changed
+87 -5

No files matched your search

@@ -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);
});
@@ -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);
}
}