From 87749e46002d56bd2738fa464d13f2ebd06f42f3 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sun, 5 Jul 2026 09:48:34 -0500 Subject: [PATCH] fix(emr): return AWS client-error codes instead of 500 InternalServerError (#1731) * fix(emr): return AWS client-error codes instead of 500 InternalServerError Three EMR paths returned 500 InternalServerError for pure client errors, which SDK clients treat as retryable server faults. Verified against the botocore model and moto: RunJobFlow with a missing Name now returns 400 ValidationException in the AWS validation-message format, AddJobFlowSteps with an unknown cluster returns 400 InvalidRequestException with the 'Cluster id ... is not valid.' message already used by DescribeCluster, and TerminateJobFlows now matches AWS semantics by terminating the unprotected clusters in the request before failing with 400 ValidationException when any cluster is termination protected. * fix(emr): reject only a missing Name and pin the validation message Review follow-up: the blank-name branch produced a message claiming the value was null, and rejecting an explicit empty string was itself stricter than AWS. The Name member is REQUIRED with a len=[0..256] constraint, so real EMR accepts an empty string and framework validation only rejects null. Restrict the check to null, pin the validation message in the missing-Name test, and add a test that an empty Name creates a cluster. --- .../floci/services/emr/EmrService.java | 21 +++++-- .../services/emr/EmrIntegrationTest.java | 61 +++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) 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 58fd0c5c48..44c953ca92 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 @@ -50,8 +50,12 @@ public class EmrService { // ──────────────────────────── Cluster lifecycle ──────────────────────────── public EmrCluster runJobFlow(EmrCluster cluster, String region) { - if (cluster.getName() == null || cluster.getName().isBlank()) { - throw new AwsException("InternalServerError", "Name is required.", 500); + // Only a missing Name fails AWS's framework validation: the member is REQUIRED but its + // constraint is len=[0..256], so an explicit empty string is accepted by real EMR. + if (cluster.getName() == null) { + throw new AwsException("ValidationException", + "1 validation error detected: Value null at 'name' failed to satisfy constraint: " + + "Member must not be null", 400); } String id = "j-" + randomId(13); cluster.setId(id); @@ -104,14 +108,17 @@ public class EmrService { } public void terminateJobFlows(List ids) { + // AWS terminates the unprotected clusters in the request and then fails it with a + // ValidationException if any were termination protected. + boolean anyProtected = false; for (String id : ids) { EmrCluster cluster = clusterStore.get(id).orElse(null); if (cluster == null) { continue; } if (cluster.isTerminationProtected()) { - throw new AwsException("InternalServerError", - "Cluster " + id + " is protected from termination.", 500); + anyProtected = true; + continue; } if ("TERMINATED".equals(cluster.getState())) { continue; @@ -122,6 +129,10 @@ public class EmrService { cluster.setEndDateTime(Instant.now()); clusterStore.put(id, cluster); } + if (anyProtected) { + throw new AwsException("ValidationException", + "Could not shut down one or more job flows since they are termination protected.", 400); + } } public void setTerminationProtection(List ids, boolean value) { @@ -156,7 +167,7 @@ public class EmrService { public List addJobFlowSteps(String clusterId, List steps) { EmrCluster cluster = clusterStore.get(clusterId).orElseThrow(() -> new AwsException( - "InternalServerError", "Cluster " + clusterId + " does not exist.", 500)); + "InvalidRequestException", "Cluster id '" + clusterId + "' is not valid.", 400)); List ids = new ArrayList<>(); for (EmrStep step : steps) { initStep(step); diff --git a/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java index 389b392f8f..c684d29ce6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/emr/EmrIntegrationTest.java @@ -184,4 +184,65 @@ class EmrIntegrationTest { .then().statusCode(400) .body("__type", equalTo("InvalidRequestException")); } + + @Test + @Order(14) + void runJobFlowWithoutNameIsValidationException() { + call("RunJobFlow", "{\"ReleaseLabel\":\"emr-7.5.0\",\"Instances\":{}}") + .then().statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo( + "1 validation error detected: Value null at 'name' failed to satisfy constraint: " + + "Member must not be null")); + } + + @Test + @Order(15) + void runJobFlowWithEmptyNameIsAccepted() { + // The Name member is REQUIRED but its model constraint is len=[0..256], so real EMR + // accepts an explicit empty string; only a missing Name fails framework validation. + call("RunJobFlow", "{\"Name\":\"\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}") + .then().statusCode(200) + .body("JobFlowId", startsWith("j-")); + } + + @Test + @Order(16) + void addStepsToUnknownClusterIsInvalidRequest() { + call("AddJobFlowSteps", + "{\"JobFlowId\":\"j-DOESNOTEXIST0\",\"Steps\":[{\"Name\":\"s\"," + + "\"HadoopJarStep\":{\"Jar\":\"command-runner.jar\"}}]}") + .then().statusCode(400) + .body("__type", equalTo("InvalidRequestException")) + .body("message", equalTo("Cluster id 'j-DOESNOTEXIST0' is not valid.")); + } + + @Test + @Order(17) + void terminationProtectionIsValidationExceptionAndUnprotectedStillTerminate() { + String protectedId = call("RunJobFlow", + "{\"Name\":\"protected\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}") + .jsonPath().getString("JobFlowId"); + String unprotectedId = call("RunJobFlow", + "{\"Name\":\"unprotected\",\"Instances\":{\"KeepJobFlowAliveWhenNoSteps\":true}}") + .jsonPath().getString("JobFlowId"); + call("SetTerminationProtection", + "{\"JobFlowIds\":[\"" + protectedId + "\"],\"TerminationProtected\":true}") + .then().statusCode(200); + + // AWS terminates the unprotected clusters in the request and then fails it. + call("TerminateJobFlows", + "{\"JobFlowIds\":[\"" + unprotectedId + "\",\"" + protectedId + "\"]}") + .then().statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo( + "Could not shut down one or more job flows since they are termination protected.")); + + call("DescribeCluster", "{\"ClusterId\":\"" + unprotectedId + "\"}") + .then().statusCode(200) + .body("Cluster.Status.State", equalTo("TERMINATED")); + call("DescribeCluster", "{\"ClusterId\":\"" + protectedId + "\"}") + .then().statusCode(200) + .body("Cluster.Status.State", equalTo("WAITING")); + } }