fix(emr): honor ListClusters creation time filters (#6123)

* fix(emr): honor ListClusters creation time filters

* fix(emr): preserve ListClusters timestamp precision

* fix(emr): share millisecond timestamp parsing
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-21 08:23:14 -04:00
1 parent 89317831bb
commit 7c0d592b85
9 files changed
+171 -20

No files matched your search

@@ -0,0 +1,27 @@
package io.github.hectorvent.floci.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import java.time.Instant;
/** Parses numeric epoch-second members in AWS JSON requests at millisecond precision. */
public final class AwsJsonTimestamp {
private AwsJsonTimestamp() {
}
public static Instant epochSeconds(JsonNode request, String field, String errorCode) {
JsonNode node = request.path(field);
if (node.isMissingNode() || node.isNull()) {
return null;
}
if (!node.isNumber()) {
throw new AwsException(errorCode, field + " must be a timestamp.", 400);
}
double epochMillis = node.asDouble() * 1000;
if (!Double.isFinite(epochMillis) || epochMillis < Long.MIN_VALUE || epochMillis >= 0x1.0p63) {
throw new AwsException(errorCode, field + " is outside the timestamp range.", 400);
}
return Instant.ofEpochMilli(Math.round(epochMillis));
}
}
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.AwsEndpoints;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsJson11Controller;
import io.github.hectorvent.floci.core.common.AwsJsonTimestamp;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.services.emr.model.EmrBlockPublicAccess;
@@ -167,9 +168,11 @@ public class EmrHandler {
private Response handleListClusters(JsonNode request, String region) {
List<String> states = stringList(request.path("ClusterStates"));
Instant createdAfter = AwsJsonTimestamp.epochSeconds(request, "CreatedAfter", "InvalidRequestException");
Instant createdBefore = AwsJsonTimestamp.epochSeconds(request, "CreatedBefore", "InvalidRequestException");
ObjectNode response = objectMapper.createObjectNode();
ArrayNode arr = response.putArray("Clusters");
for (EmrCluster c : service.listClusters(region, states)) {
for (EmrCluster c : service.listClusters(region, states, createdAfter, createdBefore)) {
arr.add(clusterSummaryNode(c));
}
return Response.ok(response).build();
@@ -126,7 +126,8 @@ public class EmrService {
return requireCluster(id);
}
public List<EmrCluster> listClusters(String region, List<String> states) {
public List<EmrCluster> listClusters(String region, List<String> states,
Instant createdAfter, Instant createdBefore) {
List<EmrCluster> result = new ArrayList<>();
for (EmrCluster c : clusterStore.scan(k -> true)) {
if (!region.equals(c.getRegion())) {
@@ -135,6 +136,13 @@ public class EmrService {
if (states != null && !states.isEmpty() && !states.contains(c.getState())) {
continue;
}
Instant createdAt = c.getCreationDateTime();
if (createdAfter != null && (createdAt == null || !createdAt.isAfter(createdAfter))) {
continue;
}
if (createdBefore != null && (createdAt == null || !createdAt.isBefore(createdBefore))) {
continue;
}
result.add(c);
}
return result;
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsJsonTimestamp;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.services.redshiftserverless.model.CustomDomainAssociation;
@@ -279,12 +280,12 @@ public class RedshiftServerlessAdminJsonHandler {
action.setRoleArn(ServerlessSupport.text(request, "roleArn"));
action.setDescription(ServerlessSupport.text(request, "scheduledActionDescription"));
action.setEnabled(ServerlessSupport.bool(request, "enabled"));
action.setStartTime(ServerlessSupport.epochSeconds(request, "startTime"));
action.setEndTime(ServerlessSupport.epochSeconds(request, "endTime"));
action.setStartTime(AwsJsonTimestamp.epochSeconds(request, "startTime", "ValidationException"));
action.setEndTime(AwsJsonTimestamp.epochSeconds(request, "endTime", "ValidationException"));
JsonNode schedule = ServerlessSupport.object(request, "schedule");
if (schedule != null) {
action.setScheduleSpecified(true);
action.setScheduleAt(ServerlessSupport.epochSeconds(schedule, "at"));
action.setScheduleAt(AwsJsonTimestamp.epochSeconds(schedule, "at", "ValidationException"));
action.setScheduleCron(ServerlessSupport.text(schedule, "cron"));
}
JsonNode target = ServerlessSupport.object(request, "targetAction");
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsJsonTimestamp;
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.services.redshift.RedshiftIamDbUserResolver;
@@ -254,7 +255,8 @@ public class RedshiftServerlessJsonHandler {
private Response handleListSnapshots(JsonNode request, String region) {
PaginatedResult<RedshiftServerlessSnapshot> page = service.listSnapshots(
ServerlessSupport.text(request, "namespaceName"), ServerlessSupport.text(request, "namespaceArn"), ServerlessSupport.text(request, "ownerAccount"),
ServerlessSupport.epochSeconds(request, "startTime"), ServerlessSupport.epochSeconds(request, "endTime"), region,
AwsJsonTimestamp.epochSeconds(request, "startTime", "ValidationException"),
AwsJsonTimestamp.epochSeconds(request, "endTime", "ValidationException"), region,
ServerlessSupport.maxResults(request), ServerlessSupport.text(request, "nextToken"));
ObjectNode response = objectMapper.createObjectNode();
ArrayNode items = response.putArray("snapshots");
@@ -6,7 +6,6 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.redshiftserverless.model.CustomDomainAssociation;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@@ -94,18 +93,6 @@ final class ServerlessSupport {
return node.asBoolean();
}
/** A timestamp member, which awsJson1.1 sends as epoch seconds unless the model says otherwise. */
static Instant epochSeconds(JsonNode request, String field) {
JsonNode node = request.path(field);
if (node.isMissingNode() || node.isNull()) {
return null;
}
if (!node.isNumber()) {
throw validation(field + " must be a timestamp.");
}
return Instant.ofEpochMilli(Math.round(node.asDouble() * 1000));
}
static List<String> stringList(JsonNode request, String field) {
JsonNode node = request.path(field);
if (node.isMissingNode() || node.isNull()) {
@@ -0,0 +1,53 @@
package io.github.hectorvent.floci.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
class AwsJsonTimestampTest {
private final ObjectMapper mapper = new ObjectMapper();
@Test
void epochSecondsRoundsNumericValuesToMilliseconds() throws Exception {
JsonNode positive = mapper.readTree("{\"time\":1761000000.1256}");
JsonNode negative = mapper.readTree("{\"time\":-0.0016}");
assertEquals(Instant.ofEpochMilli(1_761_000_000_126L),
AwsJsonTimestamp.epochSeconds(positive, "time", "InvalidRequestException"));
assertEquals(Instant.ofEpochMilli(-2),
AwsJsonTimestamp.epochSeconds(negative, "time", "ValidationException"));
}
@Test
void missingAndNullMembersRemainAbsent() {
ObjectNode request = mapper.createObjectNode();
assertNull(AwsJsonTimestamp.epochSeconds(request, "time", "ValidationException"));
request.putNull("time");
assertNull(AwsJsonTimestamp.epochSeconds(request, "time", "ValidationException"));
}
@Test
void invalidValuesUseTheRequestedAwsErrorCode() throws Exception {
JsonNode wrongType = mapper.readTree("{\"time\":\"invalid\"}");
JsonNode outOfRange = mapper.readTree("{\"time\":123456789012345678901234567890}");
AwsException emrError = assertThrows(AwsException.class,
() -> AwsJsonTimestamp.epochSeconds(wrongType, "time", "InvalidRequestException"));
assertEquals("InvalidRequestException", emrError.getErrorCode());
assertEquals(400, emrError.getHttpStatus());
AwsException redshiftError = assertThrows(AwsException.class,
() -> AwsJsonTimestamp.epochSeconds(outOfRange, "time", "ValidationException"));
assertEquals("ValidationException", redshiftError.getErrorCode());
assertEquals(400, redshiftError.getHttpStatus());
}
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.emr;
import io.github.hectorvent.floci.services.emr.model.EmrCluster;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
@@ -10,6 +11,9 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.math.BigDecimal;
import java.time.Instant;
import static io.github.hectorvent.floci.testing.EmrRequests.emr;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
@@ -17,6 +21,7 @@ import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
/**
@@ -79,10 +84,66 @@ class EmrIntegrationTest {
@Test
@Order(3)
void listClustersFilteredByState() {
void listClustersFilteredByStateAndCreationTime() {
emr("ListClusters", "{\"ClusterStates\":[\"WAITING\"]}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
emr("ListClusters", "{\"CreatedAfter\":4102444800}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
emr("ListClusters", "{\"CreatedBefore\":946684800}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
emr("ListClusters", "{\"ClusterStates\":[\"WAITING\"],\"CreatedAfter\":946684800,"
+ "\"CreatedBefore\":4102444800}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
Instant createdAt = emrService.describeCluster(clusterId).getCreationDateTime();
BigDecimal epochSeconds = BigDecimal.valueOf(createdAt.getEpochSecond())
.add(BigDecimal.valueOf(createdAt.getNano(), 9));
String shortlyBefore = epochSeconds.subtract(new BigDecimal("0.1")).toPlainString();
String shortlyAfter = epochSeconds.add(new BigDecimal("0.1")).toPlainString();
emr("ListClusters", "{\"CreatedAfter\":" + shortlyAfter + "}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
emr("ListClusters", "{\"CreatedBefore\":" + shortlyBefore + "}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
emr("ListClusters", "{\"CreatedAfter\":" + shortlyBefore + ",\"CreatedBefore\":" + shortlyAfter + "}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
emr("ListClusters", "{\"ClusterStates\":[\"TERMINATED\"],\"CreatedAfter\":" + shortlyBefore + "}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
EmrCluster cluster = emrService.describeCluster(clusterId);
Instant originalCreationTime = cluster.getCreationDateTime();
cluster.setCreationDateTime(Instant.ofEpochMilli(1_761_000_000_000L));
try {
emr("ListClusters", "{\"CreatedBefore\":1761000000.001}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
emr("ListClusters", "{\"CreatedAfter\":1760999999.999}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }.Name", equalTo("floci-emr"));
emr("ListClusters", "{\"CreatedBefore\":1761000000.000000001}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
emr("ListClusters", "{\"CreatedAfter\":1760999999.999999999}")
.then().statusCode(200)
.body("Clusters.find { it.Id == '" + clusterId + "' }", nullValue());
} finally {
cluster.setCreationDateTime(originalCreationTime);
}
emr("ListClusters", "{\"CreatedAfter\":\"invalid\"}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
emr("ListClusters", "{\"CreatedBefore\":123456789012345678901234567890}")
.then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
}
@Test
@@ -312,6 +312,15 @@ class RedshiftServerlessIntegrationTest {
call("ListSnapshots", "{\"startTime\":4102444800}")
.statusCode(200)
.body("snapshots", hasSize(0));
call("ListSnapshots", "{\"startTime\":4102444800.125}")
.statusCode(200)
.body("snapshots", hasSize(0));
call("ListSnapshots", "{\"startTime\":\"invalid\"}")
.statusCode(400)
.body("__type", equalTo("ValidationException"));
call("ListSnapshots", "{\"startTime\":123456789012345678901234567890}")
.statusCode(400)
.body("__type", equalTo("ValidationException"));
call("RestoreFromSnapshot", "{\"namespaceName\":\"snap-life-ns\",\"workgroupName\":\"snap-life-wg\","
+ "\"snapshotName\":\"snap-life\",\"snapshotArn\":\"" + snapshotArn + "\"}")
.statusCode(400)