diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonTimestamp.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonTimestamp.java new file mode 100644 index 0000000000..9729528483 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonTimestamp.java @@ -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)); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/emr/EmrHandler.java b/src/main/java/io/github/hectorvent/floci/services/emr/EmrHandler.java index b2eca51da7..62efcfa447 100644 --- a/src/main/java/io/github/hectorvent/floci/services/emr/EmrHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/emr/EmrHandler.java @@ -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 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(); 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 bfe955869f..519da79ebb 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 @@ -126,7 +126,8 @@ public class EmrService { return requireCluster(id); } - public List listClusters(String region, List states) { + public List listClusters(String region, List states, + Instant createdAfter, Instant createdBefore) { List 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; diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java index 6fa73fdb8c..8f9b4a82e5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessAdminJsonHandler.java @@ -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"); diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java index d77c796fd0..6349883c3e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessJsonHandler.java @@ -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 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"); diff --git a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java index 867e194785..4a4610a692 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshiftserverless/ServerlessSupport.java @@ -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 stringList(JsonNode request, String field) { JsonNode node = request.path(field); if (node.isMissingNode() || node.isNull()) { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsJsonTimestampTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsJsonTimestampTest.java new file mode 100644 index 0000000000..316e72d231 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsJsonTimestampTest.java @@ -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()); + } +} 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 4941d5490f..1bab6fd970 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 @@ -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 diff --git a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java index c7ef603250..d5704c0383 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessIntegrationTest.java @@ -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)