From a1ebc10bd104cde5958a363f4e46aee5d179bd7c Mon Sep 17 00:00:00 2001 From: Aniket Shukla Date: Mon, 26 Oct 2026 19:32:04 +0530 Subject: [PATCH] fix(cloudmap): paginate namespace service and operation lists (#6629) * fix(cloudmap): paginate namespace service and operation lists * refactor(core): share bounded pagination validation * refactor(cloudmap): reuse shared pagination checks for cloud map and bcm --- docs/services/cloudmap.md | 13 + pom.xml | 6 + .../floci/core/common/Pagination.java | 41 +++ .../BcmDataExportsJsonHandler.java | 31 +- .../bcmdataexports/BcmDataExportsService.java | 22 +- .../services/cloudmap/CloudMapHandler.java | 32 +- .../services/cloudmap/CloudMapService.java | 40 +++ .../floci/core/common/PaginationTest.java | 78 ++++ .../CloudMapPaginationIntegrationTest.java | 337 ++++++++++++++++++ 9 files changed, 553 insertions(+), 47 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudmap/CloudMapPaginationIntegrationTest.java diff --git a/docs/services/cloudmap.md b/docs/services/cloudmap.md index 26a5c25237..3528632abd 100644 --- a/docs/services/cloudmap.md +++ b/docs/services/cloudmap.md @@ -17,6 +17,19 @@ hosted zones are created: public and private DNS namespaces are assigned a synthetic `HostedZoneId` so SDK and CLI clients can exercise the full Cloud Map control flow locally. +`ListNamespaces`, `ListServices`, and `ListOperations` accept `MaxResults` from +1 to 100 (default 100) and an opaque `NextToken`. Final and empty collections +omit the token. Tokens are scoped to the action, account, region, and effective +filters; changing the page size while continuing is supported. Invalid or +oversized tokens (over 4096 characters) return `InvalidInput`. + +As in AWS, filters apply after selecting each page, so a filtered page can be +empty and still include `NextToken`. Continue until the token is absent. +Namespace filters remain unsupported. Service filtering uses the first +`NAMESPACE_ID` value. Operation filters match values for `STATUS`, `TYPE`, +`NAMESPACE_ID`, and `SERVICE_ID`; filter conditions and unknown names are +ignored. These existing filter limitations are unchanged. + ### DNS resolution A DNS namespace is answerable, not just storable. Floci's embedded DNS server diff --git a/pom.xml b/pom.xml index 83fe5b954c..493dfbce64 100644 --- a/pom.xml +++ b/pom.xml @@ -396,6 +396,12 @@ ${aws.sdk.version} test + + software.amazon.awssdk + servicediscovery + ${aws.sdk.version} + test + software.amazon.awssdk redshift diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java index 9f42d4918a..16edc56049 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java @@ -1,5 +1,7 @@ package io.github.hectorvent.floci.core.common; +import com.fasterxml.jackson.databind.JsonNode; + import java.util.Comparator; import java.util.List; import java.util.function.Function; @@ -71,6 +73,45 @@ public final class Pagination { return slice(all, cursorOf, limit, decodeCursor(nextToken, prefix, invalidToken), prefix); } + /** Validates service-specific bounds before applying the existing namespaced cursor format. */ + public static PaginatedResult paginate(List all, Function cursorOf, + Integer maxResults, String nextToken, + int defaultPageSize, int maxResultsLimit, int maxTokenLength, + String errorCode, String namespace) { + if (maxResults != null && (maxResults < 1 || maxResults > maxResultsLimit)) { + throw new AwsException(errorCode, "Page size must be between 1 and " + maxResultsLimit + ".", 400); + } + if (nextToken != null && nextToken.length() > maxTokenLength) { + throw new AwsException(errorCode, "Pagination token must not exceed " + maxTokenLength + " characters.", 400); + } + return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace, + token -> new AwsException(errorCode, "Invalid pagination token.", 400)); + } + + /** Reads an optional JSON page size without coercing strings, fractions, or overflowing integers. */ + public static Integer parseMaxResults(JsonNode request, String field, String errorCode) { + JsonNode value = request.get(field); + if (value == null || value.isNull()) { + return null; + } + if (!value.isIntegralNumber() || !value.canConvertToInt()) { + throw new AwsException(errorCode, field + " must be an integer.", 400); + } + return value.intValue(); + } + + /** Reads an optional JSON cursor; bounds and scope are checked by the namespaced pager. */ + public static String parseNextToken(JsonNode request, String field, String errorCode) { + JsonNode value = request.get(field); + if (value == null || value.isNull()) { + return null; + } + if (!value.isTextual()) { + throw new AwsException(errorCode, field + " must be a string.", 400); + } + return value.textValue(); + } + private static PaginatedResult slice(List all, Function cursorOf, int limit, String after, String tokenPrefix) { List sorted = all.stream().sorted(Comparator.comparing(cursorOf)).collect(Collectors.toList()); diff --git a/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsJsonHandler.java index e14ff389c5..a247d5bd1b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsJsonHandler.java @@ -7,6 +7,7 @@ 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.PaginatedResult; +import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.services.bcmdataexports.model.DataQuery; import io.github.hectorvent.floci.services.bcmdataexports.model.DestinationConfiguration; import io.github.hectorvent.floci.services.bcmdataexports.model.Export; @@ -86,7 +87,9 @@ public class BcmDataExportsJsonHandler { } private Response handleList(JsonNode request) { - PaginatedResult page = service.listExports(maxResults(request), nextToken(request)); + PaginatedResult page = service.listExports( + Pagination.parseMaxResults(request, "MaxResults", "ValidationException"), + Pagination.parseNextToken(request, "NextToken", "ValidationException")); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Exports"); for (Export export : page.items()) { @@ -122,7 +125,9 @@ public class BcmDataExportsJsonHandler { private Response handleListExecutions(JsonNode request) { String arn = stringOrNull(request, "ExportArn"); - PaginatedResult page = service.listExecutions(arn, maxResults(request), nextToken(request)); + PaginatedResult page = service.listExecutions(arn, + Pagination.parseMaxResults(request, "MaxResults", "ValidationException"), + Pagination.parseNextToken(request, "NextToken", "ValidationException")); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Executions"); for (ExportExecution exec : page.items()) { @@ -134,28 +139,6 @@ public class BcmDataExportsJsonHandler { return Response.ok(response).build(); } - private static Integer maxResults(JsonNode request) { - JsonNode value = request.get("MaxResults"); - if (value == null || value.isNull()) { - return null; - } - if (!value.isIntegralNumber() || !value.canConvertToInt()) { - throw new AwsException("ValidationException", "MaxResults must be an integer between 1 and 300.", 400); - } - return value.intValue(); - } - - private static String nextToken(JsonNode request) { - JsonNode value = request.get("NextToken"); - if (value == null || value.isNull()) { - return null; - } - if (!value.isTextual()) { - throw new AwsException("ValidationException", "NextToken must be a string.", 400); - } - return value.textValue(); - } - private Response handleGetExecution(JsonNode request) { String arn = stringOrNull(request, "ExportArn"); String executionId = stringOrNull(request, "ExecutionId"); diff --git a/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java b/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java index 6510923f0f..6950999238 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java @@ -26,7 +26,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; -import java.util.function.Function; import java.util.regex.Pattern; /** @@ -118,7 +117,8 @@ public class BcmDataExportsService { /** Returns one account-scoped page for {@code ListExports}. */ public PaginatedResult listExports(Integer maxResults, String nextToken) { - return paginate(exportStore.scan(key -> true), Export::getExportArn, maxResults, nextToken, + return Pagination.paginate(exportStore.scan(key -> true), Export::getExportArn, maxResults, nextToken, + MAX_PAGE_SIZE, MAX_PAGE_SIZE, 8192, "ValidationException", "ListExports:" + regionResolver.getAccountId()); } @@ -199,21 +199,9 @@ public class BcmDataExportsService { "Export " + exportArn + " not found.", 400); } String prefix = exportArn + "::"; - return paginate(executionStore.scan(key -> key.startsWith(prefix)), ExportExecution::getExecutionId, - maxResults, nextToken, "ListExecutions:" + regionResolver.getAccountId() + ":" + exportArn); - } - - private static PaginatedResult paginate(List items, Function cursorOf, - Integer maxResults, String nextToken, String namespace) { - int limit = maxResults != null ? maxResults : MAX_PAGE_SIZE; - if (limit < 1 || limit > MAX_PAGE_SIZE) { - throw new AwsException("ValidationException", "MaxResults must be between 1 and 300.", 400); - } - if (nextToken != null && nextToken.length() > 8192) { - throw new AwsException("ValidationException", "NextToken must not exceed 8192 characters.", 400); - } - return Pagination.paginate(items, cursorOf, limit, nextToken, namespace, - token -> new AwsException("ValidationException", "Invalid NextToken.", 400)); + return Pagination.paginate(executionStore.scan(key -> key.startsWith(prefix)), ExportExecution::getExecutionId, + maxResults, nextToken, MAX_PAGE_SIZE, MAX_PAGE_SIZE, 8192, "ValidationException", + "ListExecutions:" + regionResolver.getAccountId() + ":" + exportArn); } /** {@code GetExecution} — returns one execution record. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapHandler.java index 2835eacf45..7b8972ad14 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapHandler.java @@ -7,6 +7,8 @@ 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.AwsJson11Controller; +import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.services.cloudmap.model.Instance; import io.github.hectorvent.floci.services.cloudmap.model.Namespace; import io.github.hectorvent.floci.services.cloudmap.model.Operation; @@ -53,7 +55,7 @@ public class CloudMapHandler { text(request, "Name"), text(request, "Vpc"), text(request, "CreatorRequestId"), text(request, "Description"), parseTags(request), region)); case "GetNamespace" -> handleGetNamespace(request); - case "ListNamespaces" -> handleListNamespaces(region); + case "ListNamespaces" -> handleListNamespaces(request, region); case "DeleteNamespace" -> opResponse(service.deleteNamespace(text(request, "Id"), region)); case "GetOperation" -> handleGetOperation(request); case "ListOperations" -> handleListOperations(request, region); @@ -100,12 +102,18 @@ public class CloudMapHandler { return Response.ok(response).build(); } - private Response handleListNamespaces(String region) { + private Response handleListNamespaces(JsonNode request, String region) { + PaginatedResult page = service.listNamespaces(region, + Pagination.parseMaxResults(request, "MaxResults", "InvalidInput"), + Pagination.parseNextToken(request, "NextToken", "InvalidInput")); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Namespaces"); - for (Namespace ns : service.listNamespaces(region)) { + for (Namespace ns : page.items()) { arr.add(buildNamespaceNode(ns)); } + if (page.nextToken() != null) { + response.put("NextToken", page.nextToken()); + } return Response.ok(response).build(); } @@ -120,14 +128,20 @@ public class CloudMapHandler { private Response handleListOperations(JsonNode request, String region) { Map> filters = parseFilters(request); + PaginatedResult page = service.listOperations( + region, filters, Pagination.parseMaxResults(request, "MaxResults", "InvalidInput"), + Pagination.parseNextToken(request, "NextToken", "InvalidInput")); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Operations"); - for (Operation op : service.listOperations(region, filters)) { + for (Operation op : page.items()) { ObjectNode node = objectMapper.createObjectNode(); node.put("Id", op.getId()); node.put("Status", op.getStatus()); arr.add(node); } + if (page.nextToken() != null) { + response.put("NextToken", page.nextToken()); + } return Response.ok(response).build(); } @@ -155,11 +169,17 @@ public class CloudMapHandler { Map> filters = parseFilters(request); String namespaceId = filters.containsKey("NAMESPACE_ID") && !filters.get("NAMESPACE_ID").isEmpty() ? filters.get("NAMESPACE_ID").get(0) : null; + PaginatedResult page = service.listServices( + region, namespaceId, Pagination.parseMaxResults(request, "MaxResults", "InvalidInput"), + Pagination.parseNextToken(request, "NextToken", "InvalidInput")); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("Services"); - for (Service s : service.listServices(region, namespaceId)) { + for (Service s : page.items()) { arr.add(buildServiceSummaryNode(s)); } + if (page.nextToken() != null) { + response.put("NextToken", page.nextToken()); + } return Response.ok(response).build(); } @@ -426,4 +446,4 @@ public class CloudMapHandler { LOG.warnv("Failed to parse stored {0} JSON; field omitted. Value: {1}", field, rawJson); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java b/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java index bed7eaa3b8..a2348f5ab7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java @@ -9,6 +9,9 @@ import com.fasterxml.jackson.databind.node.MissingNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.Digests; +import io.github.hectorvent.floci.core.common.PaginatedResult; +import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.dns.DnsAnswer; import io.github.hectorvent.floci.core.common.dns.DnsRecord; @@ -35,11 +38,13 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.TreeMap; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import java.util.function.Function; /** * Cloud Map (AWS service discovery) business logic. Mirrors the EventBridge service @@ -166,6 +171,11 @@ public class CloudMapService { return result; } + public PaginatedResult listNamespaces(String region, Integer maxResults, String nextToken) { + return paginate(listNamespaces(region), Namespace::getId, maxResults, nextToken, + "ListNamespaces", region, null); + } + public void updateNamespace(String id, String description) { Namespace ns = requireNamespace(id); ns.setDescription(description); @@ -256,6 +266,16 @@ public class CloudMapService { return result; } + public PaginatedResult listServices(String region, String namespaceIdFilter, + Integer maxResults, String nextToken) { + PaginatedResult page = paginate(listServices(region, null), Service::getId, + maxResults, nextToken, "ListServices", region, namespaceIdFilter); + // AWS applies filters after selecting the page, so an empty page can still have a token. + List items = page.items().stream() + .filter(s -> namespaceIdFilter == null || namespaceIdFilter.equals(s.getNamespaceId())).toList(); + return new PaginatedResult<>(items, page.nextToken()); + } + /** * Only the TTLs of the existing DNS records can change, as on AWS: any other DnsConfig change * (record types or count, RoutingPolicy), or a DnsConfig for a service without one, is rejected. @@ -728,6 +748,26 @@ public class CloudMapService { return result; } + public PaginatedResult listOperations(String region, Map> filters, + Integer maxResults, String nextToken) { + Map> normalized = new TreeMap<>(); + if (filters != null) { + filters.forEach((name, values) -> normalized.put(name, values.stream().distinct().sorted().toList())); + } + PaginatedResult page = paginate(listOperations(region, null), Operation::getId, + maxResults, nextToken, "ListOperations", region, normalized); + List items = page.items().stream().filter(op -> matchesOperationFilters(op, filters)).toList(); + return new PaginatedResult<>(items, page.nextToken()); + } + + private PaginatedResult paginate(List items, Function cursorOf, + Integer maxResults, String nextToken, + String action, String region, Object query) { + String namespace = action + ":" + regionResolver.getAccountId() + ":" + region + ":" + + Digests.sha256Hex(objectMapper.valueToTree(query).toString()); + return Pagination.paginate(items, cursorOf, maxResults, nextToken, 100, 100, 4096, "InvalidInput", namespace); + } + private boolean matchesOperationFilters(Operation op, Map> filters) { if (filters == null || filters.isEmpty()) { return true; diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java index 7611732f3f..19354d5c6e 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java @@ -1,6 +1,8 @@ package io.github.hectorvent.floci.core.common; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.nio.charset.StandardCharsets; import java.util.Base64; @@ -19,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class PaginationTest { + private static final ObjectMapper MAPPER = new ObjectMapper(); private static final List ITEMS = List.of("c", "a", "b"); private static final AwsException INVALID = new AwsException("BadRequestException", "bad", 400); private static final Function INVALID_TOKEN = token -> INVALID; @@ -114,6 +117,81 @@ class PaginationTest { assertFalse(Pagination.tokenStartsWith("", "")); } + @Test + void boundedNamespacedPaging_usesTheDefaultAndCanChangePageSize() { + PaginatedResult first = bounded(null, null); + assertEquals(List.of("a", "b"), first.items()); + PaginatedResult last = bounded(3, first.nextToken()); + assertEquals(List.of("c"), last.items()); + assertNull(last.nextToken()); + } + + @Test + void boundedNamespacedPaging_refusesOutOfRangeSizes() { + for (int size : new int[] {-1, 0, 4}) { + assertInvalid(() -> bounded(size, null)); + } + } + + @Test + void boundedNamespacedPaging_refusesLongTokensEvenWhenTheirScopeIsValid() { + String boundary = Base64.getUrlEncoder().withoutPadding() + .encodeToString(("ns:" + "z".repeat(21)).getBytes(StandardCharsets.UTF_8)); + assertEquals(32, boundary.length()); + assertTrue(bounded(1, boundary).items().isEmpty()); + String token = Base64.getUrlEncoder().withoutPadding() + .encodeToString(("ns:" + "a".repeat(100)).getBytes(StandardCharsets.UTF_8)); + assertInvalid(() -> bounded(1, token)); + } + + @Test + void boundedNamespacedPaging_refusesMalformedAndForeignTokens() { + assertInvalid(() -> bounded(1, "!!!")); + assertInvalid(() -> bounded(1, page(1, null, "other").nextToken())); + } + + @Test + void jsonPageSize_preservesOmittedNullZeroAndIntegerValues() throws Exception { + for (String field : List.of("MaxResults", "PageSize")) { + assertNull(Pagination.parseMaxResults(MAPPER.readTree("{}"), field, "InvalidInput")); + assertNull(Pagination.parseMaxResults(MAPPER.readTree("{\"" + field + "\":null}"), field, "InvalidInput")); + assertEquals(0, Pagination.parseMaxResults(MAPPER.readTree("{\"" + field + "\":0}"), field, "InvalidInput")); + assertEquals(2, Pagination.parseMaxResults(MAPPER.readTree("{\"" + field + "\":2}"), field, "InvalidInput")); + } + } + + @Test + void jsonPageSize_refusesOverflowAndCoercibleNonIntegers() throws Exception { + for (String value : List.of("4294967297", "1.5", "\"1\"", "{}", "[]", "true")) { + assertInvalid(() -> Pagination.parseMaxResults(MAPPER.readTree("{\"MaxResults\":" + value + "}"), + "MaxResults", "InvalidInput")); + } + } + + @Test + void jsonToken_preservesNullEmptyAndTextButRefusesOtherShapes() throws Exception { + for (String field : List.of("NextToken", "PageToken")) { + assertNull(Pagination.parseNextToken(MAPPER.readTree("{}"), field, "InvalidInput")); + assertNull(Pagination.parseNextToken(MAPPER.readTree("{\"" + field + "\":null}"), field, "InvalidInput")); + assertEquals("", Pagination.parseNextToken(MAPPER.readTree("{\"" + field + "\":\"\"}"), field, "InvalidInput")); + assertEquals("token", Pagination.parseNextToken(MAPPER.readTree("{\"" + field + "\":\"token\"}"), field, "InvalidInput")); + for (String value : List.of("1", "true", "{}", "[]")) { + assertInvalid(() -> Pagination.parseNextToken(MAPPER.readTree("{\"" + field + "\":" + value + "}"), + field, "InvalidInput")); + } + } + } + + private static PaginatedResult bounded(Integer size, String token) { + return Pagination.paginate(ITEMS, Function.identity(), size, token, 2, 3, 32, "InvalidInput", "ns"); + } + + private static void assertInvalid(Executable call) { + AwsException error = assertThrows(AwsException.class, call); + assertEquals("InvalidInput", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + } + private static PaginatedResult page(int limit, String token, String namespace) { return Pagination.paginate(ITEMS, Function.identity(), limit, token, namespace, INVALID_TOKEN); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudmap/CloudMapPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudmap/CloudMapPaginationIntegrationTest.java new file mode 100644 index 0000000000..7ec2c4a439 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudmap/CloudMapPaginationIntegrationTest.java @@ -0,0 +1,337 @@ +package io.github.hectorvent.floci.services.cloudmap; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.cloudmap.model.Namespace; +import io.github.hectorvent.floci.services.cloudmap.model.Operation; +import io.github.hectorvent.floci.services.cloudmap.model.Service; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.awscore.exception.AwsServiceException; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.servicediscovery.ServiceDiscoveryClient; +import software.amazon.awssdk.services.servicediscovery.model.ListNamespacesResponse; +import software.amazon.awssdk.services.servicediscovery.model.ListOperationsResponse; +import software.amazon.awssdk.services.servicediscovery.model.ListServicesResponse; +import software.amazon.awssdk.services.servicediscovery.model.OperationFilter; +import software.amazon.awssdk.services.servicediscovery.model.ServiceFilter; + +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class CloudMapPaginationIntegrationTest { + + private static final String ACCOUNT = "741660100001"; + private static final String OTHER_ACCOUNT = "741660100002"; + private static final Region REGION = Region.US_EAST_1; + private static final List ACTIONS = List.of("ListNamespaces", "ListServices", "ListOperations"); + + @Inject + StorageFactory storageFactory; + + @TestHTTPResource + URI endpoint; + + private AccountAwareStorageBackend namespaces; + private AccountAwareStorageBackend services; + private AccountAwareStorageBackend operations; + private final List created = new ArrayList<>(); + private ServiceDiscoveryClient client; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @BeforeEach + void setUp() { + namespaces = storageFactory.create("cloudmap", "cloudmap-namespaces.json", + new TypeReference>() {}); + services = storageFactory.create("cloudmap", "cloudmap-services.json", + new TypeReference>() {}); + operations = storageFactory.create("cloudmap", "cloudmap-operations.json", + new TypeReference>() {}); + client = client(ACCOUNT, REGION); + } + + @AfterEach + void cleanUp() { + client.close(); + for (StoredResources item : created) { + namespaces.deleteForAccount(item.account(), item.namespace()); + services.deleteForAccount(item.account(), item.service()); + operations.deleteForAccount(item.account(), item.operation()); + } + } + + @ParameterizedTest + @ValueSource(strings = {"ListNamespaces", "ListServices", "ListOperations"}) + void sdkPaginatorsRoundTripEveryItemExactlyOnce(String action) { + seedThree(); + Page first = page(client, action, 1, null); + assertEquals(List.of(prefix(action) + "a"), first.ids()); + assertNotNull(first.token()); + List expected = List.of(prefix(action) + "a", prefix(action) + "b", prefix(action) + "c"); + List actual = switch (action) { + case "ListNamespaces" -> client.listNamespacesPaginator(request -> request.maxResults(1)).stream() + .flatMap(response -> response.namespaces().stream()).map(item -> item.id()).toList(); + case "ListServices" -> client.listServicesPaginator(request -> request.maxResults(1)).stream() + .flatMap(response -> response.services().stream()).map(item -> item.id()).toList(); + case "ListOperations" -> client.listOperationsPaginator(request -> request.maxResults(1)).stream() + .flatMap(response -> response.operations().stream()).map(item -> item.id()).toList(); + default -> throw new IllegalArgumentException(action); + }; + assertEquals(expected, actual); + Page last = page(client, action, 100, first.token()); + assertEquals(expected.subList(1, 3), last.ids()); + assertNull(last.token()); + } + + @Test + void allThreeActionsDefaultTo100AndAcceptTheMaximumPageSize() { + for (int i = 0; i < 101; i++) { + seed(ACCOUNT, REGION, "item" + i, "ns-item" + i, "SUCCESS"); + } + for (String action : ACTIONS) { + Page first = page(client, action, null, null); + assertEquals(100, first.ids().size()); + assertNotNull(first.token()); + Page last = page(client, action, 100, first.token()); + assertEquals(1, last.ids().size()); + assertNull(last.token()); + } + } + + @ParameterizedTest + @ValueSource(ints = {-1, 0, 101}) + void sdkRejectsOutOfRangeLimits(int limit) { + for (String action : ACTIONS) { + invalid(() -> page(client, action, limit, null)); + } + } + + @ParameterizedTest + @ValueSource(strings = {"4294967297", "1.5", "\"1\"", "{}", "[]"}) + void malformedLimitsAreRejectedBeforeCoercion(String value) { + for (String action : ACTIONS) { + raw(action, "{\"MaxResults\":" + value + "}", 400); + } + } + + @ParameterizedTest + @ValueSource(strings = {"\"%%%%\"", "17", "{}", "[]"}) + void invalidTokenShapesAreRejected(String value) { + for (String action : ACTIONS) { + raw(action, "{\"NextToken\":" + value + "}", 400); + } + } + + @ParameterizedTest + @ValueSource(strings = {"ListNamespaces", "ListServices", "ListOperations"}) + void oversizedTokensAreRejectedEvenWithAValidDecodedScope(String action) { + seedThree(); + String token = page(client, action, 1, null).token(); + assertNotNull(token); + String cursor = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8); + String oversized = Base64.getUrlEncoder().withoutPadding() + .encodeToString((cursor + "x".repeat(4096)).getBytes(StandardCharsets.UTF_8)); + assertTrue(oversized.length() > 4096); + invalid(() -> page(client, action, 1, oversized)); + } + + @ParameterizedTest + @ValueSource(strings = {"ListNamespaces", "ListServices", "ListOperations"}) + void tokensCannotCrossAccountRegionOrAction(String action) { + seedThree(); + String token = page(client, action, 1, null).token(); + assertNotNull(token); + seed(OTHER_ACCOUNT, REGION, "a", "ns-a", "SUCCESS"); + seed(ACCOUNT, Region.EU_WEST_1, "z", "ns-z", "SUCCESS"); + try (ServiceDiscoveryClient otherAccount = client(OTHER_ACCOUNT, REGION); + ServiceDiscoveryClient otherRegion = client(ACCOUNT, Region.EU_WEST_1)) { + invalid(() -> page(otherAccount, action, 1, token)); + invalid(() -> page(otherRegion, action, 1, token)); + } + String otherAction = action.equals("ListNamespaces") ? "ListServices" : "ListNamespaces"; + invalid(() -> page(client, otherAction, 1, token)); + } + + @ParameterizedTest + @ValueSource(strings = {"ListNamespaces", "ListServices", "ListOperations"}) + void deletedCursorItemsDoNotPreventResuming(String action) { + seedThree(); + Page first = page(client, action, 1, null); + assertNotNull(first.token()); + switch (action) { + case "ListNamespaces" -> namespaces.deleteForAccount(ACCOUNT, "ns-a"); + case "ListServices" -> services.deleteForAccount(ACCOUNT, "srv-a"); + case "ListOperations" -> operations.deleteForAccount(ACCOUNT, "op-a"); + default -> throw new IllegalArgumentException(action); + } + assertEquals(List.of(prefix(action) + "b", prefix(action) + "c"), page(client, action, 100, first.token()).ids()); + } + + @Test + void serviceFilteringCanReturnAnEmptyPageWithAContinuationToken() { + seedThree(); + ServiceFilter filter = ServiceFilter.builder().name("NAMESPACE_ID").values("ns-b").condition("EQ").build(); + ListServicesResponse first = client.listServices(request -> request.maxResults(1).filters(filter)); + assertTrue(first.services().isEmpty()); + assertNotNull(first.nextToken()); + assertEquals(List.of("srv-b", "srv-c"), client.listServicesPaginator(request -> request.maxResults(1).filters(filter)) + .stream().flatMap(response -> response.services().stream()).map(item -> item.id()).toList()); + ServiceFilter changed = ServiceFilter.builder().name("NAMESPACE_ID").values("ns-a").condition("EQ").build(); + invalid(() -> client.listServices(request -> request.maxResults(1).filters(changed).nextToken(first.nextToken()))); + } + + @Test + void operationFilteringCanReturnAnEmptyPageWithAContinuationToken() { + seedThree(); + OperationFilter filter = OperationFilter.builder().name("STATUS").values("SUCCESS").condition("IN").build(); + ListOperationsResponse first = client.listOperations(request -> request.maxResults(1).filters(filter)); + assertTrue(first.operations().isEmpty()); + assertNotNull(first.nextToken()); + assertEquals(List.of("op-b", "op-c"), client.listOperationsPaginator(request -> request.maxResults(1).filters(filter)) + .stream().flatMap(response -> response.operations().stream()).map(item -> item.id()).toList()); + invalid(() -> client.listOperations(request -> request.nextToken(first.nextToken()))); + } + + @Test + void equivalentOperationFilterOrderCanResumeButAnEmptyValuedFilterIsDifferent() { + seedThree(); + OperationFilter status = OperationFilter.builder().name("STATUS").values("SUCCESS", "PENDING").condition("IN").build(); + OperationFilter type = OperationFilter.builder().name("TYPE").values("CREATE_NAMESPACE").condition("EQ").build(); + String token = client.listOperations(request -> request.maxResults(1).filters(status, type)).nextToken(); + assertNotNull(token); + OperationFilter reordered = OperationFilter.builder().name("STATUS").values("PENDING", "SUCCESS").condition("IN").build(); + assertEquals(List.of("op-b", "op-c"), client.listOperations(request -> request.filters(type, reordered).nextToken(token)) + .operations().stream().map(item -> item.id()).toList()); + String unfiltered = client.listOperations(request -> request.maxResults(1)).nextToken(); + OperationFilter empty = OperationFilter.builder().name("STATUS").values(List.of()).build(); + invalid(() -> client.listOperations(request -> request.filters(empty).nextToken(unfiltered))); + } + + @Test + void emptyAndFinalPagesOmitNextToken() { + for (String action : ACTIONS) { + assertFalse(raw(action, "{\"NextToken\":\"\"}", 200).containsKey("NextToken")); + } + seed(ACCOUNT, REGION, "a", "ns-a", "SUCCESS"); + for (String action : ACTIONS) { + assertFalse(raw(action, "{}", 200).containsKey("NextToken")); + } + } + + private Page page(ServiceDiscoveryClient sdk, String action, Integer limit, String token) { + return switch (action) { + case "ListNamespaces" -> { + ListNamespacesResponse response = sdk.listNamespaces(request -> request.maxResults(limit).nextToken(token)); + yield new Page(response.namespaces().stream().map(item -> item.id()).toList(), response.nextToken()); + } + case "ListServices" -> { + ListServicesResponse response = sdk.listServices(request -> request.maxResults(limit).nextToken(token)); + yield new Page(response.services().stream().map(item -> item.id()).toList(), response.nextToken()); + } + case "ListOperations" -> { + ListOperationsResponse response = sdk.listOperations(request -> request.maxResults(limit).nextToken(token)); + yield new Page(response.operations().stream().map(item -> item.id()).toList(), response.nextToken()); + } + default -> throw new IllegalArgumentException(action); + }; + } + + private void invalid(Runnable call) { + AwsServiceException error = assertThrows(AwsServiceException.class, call::run); + assertEquals(400, error.statusCode()); + assertEquals("InvalidInput", error.awsErrorDetails().errorCode()); + } + + private Map raw(String action, String body, int status) { + Map response = given().contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "Route53AutoNaming_v20170314." + action) + .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20260101/us-east-1/servicediscovery/aws4_request") + .body(body).when().post("/").then().statusCode(status).extract().jsonPath().getMap(""); + if (status == 400) { + assertEquals("InvalidInput", response.get("__type")); + } + return response; + } + + private void seedThree() { + seed(ACCOUNT, REGION, "a", "ns-a", "PENDING"); + seed(ACCOUNT, REGION, "b", "ns-b", "SUCCESS"); + seed(ACCOUNT, REGION, "c", "ns-b", "SUCCESS"); + } + + private void seed(String account, Region region, String suffix, String serviceNamespace, String status) { + Namespace namespace = new Namespace(); + namespace.setId("ns-" + suffix); + namespace.setName("namespace-" + suffix); + namespace.setArn("arn:aws:servicediscovery:" + region.id() + ":" + account + ":namespace/" + namespace.getId()); + namespace.setType("HTTP"); + namespace.setRegion(region.id()); + namespace.setCreateDate(Instant.EPOCH); + namespaces.putForAccount(account, namespace.getId(), namespace); + Service service = new Service(); + service.setId("srv-" + suffix); + service.setName("service-" + suffix); + service.setArn("arn:aws:servicediscovery:" + region.id() + ":" + account + ":service/" + service.getId()); + service.setNamespaceId(serviceNamespace); + service.setType("HTTP"); + service.setRegion(region.id()); + service.setCreateDate(Instant.EPOCH); + services.putForAccount(account, service.getId(), service); + Operation operation = new Operation(); + operation.setId("op-" + suffix); + operation.setType("CREATE_NAMESPACE"); + operation.setStatus(status); + operation.setRegion(region.id()); + operation.setCreateDate(Instant.EPOCH); + operation.setUpdateDate(Instant.EPOCH); + operation.getTargets().put("NAMESPACE", namespace.getId()); + operations.putForAccount(account, operation.getId(), operation); + created.add(new StoredResources(account, namespace.getId(), service.getId(), operation.getId())); + } + + private ServiceDiscoveryClient client(String account, Region region) { + return ServiceDiscoveryClient.builder().endpointOverride(endpoint).region(region) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test"))).build(); + } + + private String prefix(String action) { + return switch (action) { + case "ListNamespaces" -> "ns-"; + case "ListServices" -> "srv-"; + case "ListOperations" -> "op-"; + default -> throw new IllegalArgumentException(action); + }; + } + + private record Page(List ids, String token) {} + private record StoredResources(String account, String namespace, String service, String operation) {} +}