From 722472e76b4422e91962c3849abd9634232ce2bf Mon Sep 17 00:00:00 2001 From: Mohith Date: Mon, 16 Nov 2026 04:51:34 +0530 Subject: [PATCH] fix(apigateway): page GetAuthorizers with limit and position (#8495) API Gateway's GetAuthorizers now pages with limit and position instead of returning every authorizer at once: authorizers come back sorted by id, at most limit of them (25 by default, capped at 500, refused below 1), with position set to the id the next page starts after, and an unknown position is refused with BadRequestException as GetUsage and GetDocumentationParts refuse one. GetAuthorizers and GetDocumentationParts share one id-cursor page helper returning PaginatedResult, and the page-size rule all three paged calls use is named resolvePageSize. Closes #8439 --- .../apigateway/ApiGatewayController.java | 17 ++- .../apigateway/ApiGatewayService.java | 73 ++++++---- ...atewayAuthorizerPagingIntegrationTest.java | 136 ++++++++++++++++++ .../apigateway/ApiGatewayUsageLimitTest.java | 18 +-- 4 files changed, 200 insertions(+), 44 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/apigateway/ApiGatewayAuthorizerPagingIntegrationTest.java diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java index ea775cb981..850844ceb3 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java @@ -277,12 +277,17 @@ public class ApiGatewayController { @GET @Path("/restapis/{apiId}/authorizers") - public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId) { + public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId, + @QueryParam("limit") Integer limit, @QueryParam("position") String position) { String region = regionResolver.resolveRegion(headers); - List auths = service.getAuthorizers(region, apiId); + // Authorizer clashes with the imported API Gateway v2 model + PaginatedResult page = service.getAuthorizerPage(region, apiId, limit, position); ObjectNode root = objectMapper.createObjectNode(); ArrayNode items = root.putArray("item"); - auths.forEach(a -> items.add(toAuthorizerNode(a))); + page.items().forEach(a -> items.add(toAuthorizerNode(a))); + if (page.nextToken() != null) { + root.put("position", page.nextToken()); + } return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build(); } @@ -1248,13 +1253,13 @@ public class ApiGatewayController { @QueryParam("locationStatus") String locationStatus, @QueryParam("limit") Integer limit, @QueryParam("position") String position) { String region = regionResolver.resolveRegion(headers); - ApiGatewayService.DocumentationPartPage page = service.getDocumentationParts(region, apiId, type, name, + PaginatedResult page = service.getDocumentationParts(region, apiId, type, name, path, locationStatus, limit, position); ObjectNode root = objectMapper.createObjectNode(); ArrayNode items = root.putArray("item"); page.items().forEach(part -> items.add(objectMapper.valueToTree(part))); - if (page.position() != null) { - root.put("position", page.position()); + if (page.nextToken() != null) { + root.put("position", page.nextToken()); } return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build(); } diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java index 818451b313..18296b82fe 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -15,6 +15,7 @@ import io.floci.aws.core.common.AwsPartitions; import io.floci.aws.core.common.AwsRegionFacts; import io.floci.aws.core.common.AwsRegions; import io.floci.aws.core.common.CloudFrontEdgeDomain; +import io.floci.aws.core.common.PaginatedResult; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.ReservedTags; import io.floci.aws.core.common.Resettable; @@ -110,13 +111,13 @@ public class ApiGatewayService implements ResourceProvider, Resettable { private static final ObjectMapper JSON = new ObjectMapper(); - /** Documented default page size for GetUsage. */ - private static final int DEFAULT_USAGE_LIMIT = 25; + /** Documented default page size for the paged list calls. */ + private static final int DEFAULT_PAGE_SIZE = 25; /** * Documented maximum results per page. A larger {@code limit} is accepted, as real API Gateway * accepts one, but the page returned is still capped here. */ - private static final int MAX_USAGE_PAGE_SIZE = 500; + private static final int MAX_PAGE_SIZE = 500; private static final Logger LOG = Logger.getLogger(ApiGatewayService.class); @@ -1251,6 +1252,36 @@ public class ApiGatewayService implements ResourceProvider, Resettable { return authorizerStore.scan(k -> k.startsWith(prefix)); } + public PaginatedResult getAuthorizerPage(String region, String apiId, Integer limit, + String position) { + List authorizers = getAuthorizers(region, apiId).stream() + .sorted(Comparator.comparing(Authorizer::getId)) + .toList(); + return pageById(authorizers, Authorizer::getId, limit, position); + } + + /** + * One page of a list already sorted by id. The token is the id of the last item returned, and + * an unrecognised token is a {@code BadRequestException}, as on AWS. + */ + private static PaginatedResult pageById(List sorted, Function idOf, Integer limit, + String position) { + int pageSize = resolvePageSize(limit); + int start = 0; + if (position != null && !position.isBlank()) { + while (start < sorted.size() && !position.equals(idOf.apply(sorted.get(start)))) { + start++; + } + if (start == sorted.size()) { + throw new AwsException("BadRequestException", "Invalid position parameter", 400); + } + start++; + } + int end = Math.min(start + pageSize, sorted.size()); + String nextPosition = end < sorted.size() ? idOf.apply(sorted.get(end - 1)) : null; + return new PaginatedResult<>(List.copyOf(sorted.subList(start, end)), nextPosition); + } + public void deleteAuthorizer(String region, String apiId, String authorizerId) { getAuthorizer(region, apiId, authorizerId); authorizerStore.delete(authorizerKey(region, apiId, authorizerId)); @@ -1790,7 +1821,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable { if (end.isBefore(start)) { throw new AwsException("BadRequestException", "Usage end date must be after start date", 400); } - int pageSize = resolveUsageLimit(limit); + int pageSize = resolvePageSize(limit); List keys = getUsagePlanKeys(region, usagePlanId).stream() .filter(key -> keyId == null || keyId.isBlank() || keyId.equals(key.getId())) @@ -1848,9 +1879,11 @@ public class ApiGatewayService implements ResourceProvider, Resettable { } /** - * Resolves the effective page size, which is not the same thing as accepting the request. + * Resolves the effective page size of a paged list call ({@code GetUsage}, {@code GetAuthorizers}, + * {@code GetDocumentationParts}), which is not the same thing as accepting the request. * - *

Request acceptance and response page size are separate. Probed against real API Gateway, + *

Request acceptance and response page size are separate. The probe below was made against + * {@code GetUsage} on real API Gateway: * every {@code limit} from 500 up to {@link Integer#MAX_VALUE} is accepted without error, so * none is rejected here either. What that probe does not establish is that the service * ever returns more than 500 entries in one page, and the documented contract says 500 is the @@ -1861,14 +1894,14 @@ public class ApiGatewayService implements ResourceProvider, Resettable { * {@code limit=-1} with an {@code InternalFailure}, which is a fault rather than a contract, so * a page size below one is rejected as a bad request instead of reproducing a 500. */ - static int resolveUsageLimit(Integer limit) { + static int resolvePageSize(Integer limit) { if (limit == null) { - return DEFAULT_USAGE_LIMIT; + return DEFAULT_PAGE_SIZE; } if (limit < 1) { throw new AwsException("BadRequestException", "Invalid limit parameter", 400); } - return Math.min(limit, MAX_USAGE_PAGE_SIZE); + return Math.min(limit, MAX_PAGE_SIZE); } /** @@ -2443,9 +2476,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable { .orElseThrow(() -> new AwsException("NotFoundException", "Invalid documentation part id specified", 404)); } - public record DocumentationPartPage(List items, String position) {} - - public DocumentationPartPage getDocumentationParts(String region, String apiId, String type, String name, + public PaginatedResult getDocumentationParts(String region, String apiId, String type, String name, String path, String locationStatus, Integer limit, String position) { getRestApi(region, apiId); @@ -2469,10 +2500,6 @@ public class ApiGatewayService implements ResourceProvider, Resettable { modeledRequest.put("position", position); } ApiGatewayDocumentationRequestShapes.check("GetDocumentationParts", modeledRequest); - if (limit != null && limit < 1) { - throw new AwsException("BadRequestException", "Invalid limit parameter", 400); - } - int pageSize = limit == null ? 25 : Math.min(limit, 500); String prefix = region + "::" + apiId + "::"; List parts = documentationPartStore.scan(key -> key.startsWith(prefix)).stream() .filter(part -> type == null || type.equals(part.getLocation().get("type"))) @@ -2482,19 +2509,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable { .sorted(Comparator.comparing(DocumentationPart::getId)) .toList(); - int start = 0; - if (position != null && !position.isBlank()) { - while (start < parts.size() && !position.equals(parts.get(start).getId())) { - start++; - } - if (start == parts.size()) { - throw new AwsException("BadRequestException", "Invalid position parameter", 400); - } - start++; - } - int end = Math.min(start + pageSize, parts.size()); - String nextPosition = end < parts.size() ? parts.get(end - 1).getId() : null; - return new DocumentationPartPage(List.copyOf(parts.subList(start, end)), nextPosition); + return pageById(parts, DocumentationPart::getId, limit, position); } public synchronized DocumentationPart updateDocumentationPart(String region, String apiId, String partId, diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayAuthorizerPagingIntegrationTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayAuthorizerPagingIntegrationTest.java new file mode 100644 index 0000000000..90044a7307 --- /dev/null +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayAuthorizerPagingIntegrationTest.java @@ -0,0 +1,136 @@ +package io.floci.aws.services.apigateway; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.http.apache5.Apache5HttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.apigateway.ApiGatewayClient; +import software.amazon.awssdk.services.apigateway.model.Authorizer; +import software.amazon.awssdk.services.apigateway.model.AuthorizerType; +import software.amazon.awssdk.services.apigateway.model.BadRequestException; +import software.amazon.awssdk.services.apigateway.model.GetAuthorizersResponse; +import software.amazon.awssdk.services.apigateway.model.NotFoundException; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +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; + +@QuarkusTest +class ApiGatewayAuthorizerPagingIntegrationTest { + + private static final String AUTHORIZER_URI = "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/" + + "arn:aws:lambda:us-east-1:000000000000:function:page-authorizer/invocations"; + + private ApiGatewayClient client() { + return AwsTestClients.create(ApiGatewayClient.builder(), "test", Region.US_EAST_1, + Apache5HttpClient.builder()); + } + + private Set createAuthorizers(ApiGatewayClient client, String apiId, int count) { + Set ids = new HashSet<>(); + for (int i = 0; i < count; i++) { + String name = "page-authorizer-" + i; + ids.add(client.createAuthorizer(request -> request.restApiId(apiId).name(name) + .type(AuthorizerType.TOKEN).authorizerUri(AUTHORIZER_URI) + .identitySource("method.request.header.Authorization")).id()); + } + return ids; + } + + @Test + void getAuthorizersWithLimitReturnsPositionAndFollowsToTerminalPage() { + try (ApiGatewayClient client = client()) { + String apiId = client.createRestApi(request -> request.name("authorizer-paging-sdk")).id(); + try { + Set created = createAuthorizers(client, apiId, 3); + + GetAuthorizersResponse first = client.getAuthorizers(request -> request.restApiId(apiId).limit(1)); + assertEquals(1, first.items().size()); + assertNotNull(first.position()); + + List seen = new ArrayList<>(); + List cursors = new ArrayList<>(); + seen.add(first.items().get(0).id()); + String position = first.position(); + for (int page = 0; position != null && page < created.size(); page++) { + assertFalse(cursors.contains(position), "position repeated: " + position); + cursors.add(position); + String next = position; + GetAuthorizersResponse response = client.getAuthorizers(request -> request.restApiId(apiId) + .limit(1).position(next)); + assertEquals(1, response.items().size()); + assertFalse(seen.contains(response.items().get(0).id()), "authorizer repeated"); + seen.add(response.items().get(0).id()); + position = response.position(); + } + assertNull(position); + assertEquals(3, seen.size()); + assertEquals(created, new HashSet<>(seen)); + } finally { + client.deleteRestApi(request -> request.restApiId(apiId)); + } + } + } + + @Test + void getAuthorizersDefaultsToTwentyFiveAndKeepsAuthorizerFields() { + try (ApiGatewayClient client = client()) { + String apiId = client.createRestApi(request -> request.name("authorizer-default-page-sdk")).id(); + try { + createAuthorizers(client, apiId, 27); + + GetAuthorizersResponse first = client.getAuthorizers(request -> request.restApiId(apiId)); + assertEquals(25, first.items().size()); + assertNotNull(first.position()); + Authorizer sample = first.items().get(0); + assertEquals(AuthorizerType.TOKEN, sample.type()); + assertEquals(AUTHORIZER_URI, sample.authorizerUri()); + assertEquals("method.request.header.Authorization", sample.identitySource()); + + GetAuthorizersResponse second = client.getAuthorizers(request -> request.restApiId(apiId) + .position(first.position())); + assertEquals(2, second.items().size()); + assertNull(second.position()); + } finally { + client.deleteRestApi(request -> request.restApiId(apiId)); + } + } + } + + @Test + void getAuthorizersIsolatesParentsAndRejectsBadInput() { + try (ApiGatewayClient client = client()) { + String apiId = client.createRestApi(request -> request.name("authorizer-paging-a")).id(); + String otherId = client.createRestApi(request -> request.name("authorizer-paging-b")).id(); + try { + createAuthorizers(client, apiId, 2); + + GetAuthorizersResponse other = client.getAuthorizers(request -> request.restApiId(otherId)); + assertEquals(0, other.items().size()); + assertNull(other.position()); + + GetAuthorizersResponse all = client.getAuthorizers(request -> request.restApiId(apiId).limit(2)); + assertEquals(2, all.items().size()); + assertNull(all.position()); + + assertThrows(BadRequestException.class, + () -> client.getAuthorizers(request -> request.restApiId(apiId).limit(0))); + assertThrows(BadRequestException.class, + () -> client.getAuthorizers(request -> request.restApiId(apiId).position("missing"))); + assertThrows(NotFoundException.class, + () -> client.getAuthorizers(request -> request.restApiId("missingapi"))); + } finally { + client.deleteRestApi(request -> request.restApiId(apiId)); + client.deleteRestApi(request -> request.restApiId(otherId)); + } + } + } +} diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsageLimitTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsageLimitTest.java index 1152b20081..eb79422cb4 100644 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsageLimitTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsageLimitTest.java @@ -19,28 +19,28 @@ class ApiGatewayUsageLimitTest { @Test void anAbsentLimitUsesTheDocumentedDefault() { - assertEquals(25, ApiGatewayService.resolveUsageLimit(null)); + assertEquals(25, ApiGatewayService.resolvePageSize(null)); } @Test void aLimitWithinTheDocumentedMaximumIsUsedAsGiven() { - assertEquals(1, ApiGatewayService.resolveUsageLimit(1)); - assertEquals(100, ApiGatewayService.resolveUsageLimit(100)); - assertEquals(500, ApiGatewayService.resolveUsageLimit(500)); + assertEquals(1, ApiGatewayService.resolvePageSize(1)); + assertEquals(100, ApiGatewayService.resolvePageSize(100)); + assertEquals(500, ApiGatewayService.resolvePageSize(500)); } @Test void aLimitAboveTheMaximumIsAcceptedButThePageStaysCapped() { // Accepted, not rejected: real API Gateway accepts these. // Capped, not honoured verbatim: nothing shows it returning more than 500 in one page. - assertEquals(500, ApiGatewayService.resolveUsageLimit(501)); - assertEquals(500, ApiGatewayService.resolveUsageLimit(100_000)); - assertEquals(500, ApiGatewayService.resolveUsageLimit(Integer.MAX_VALUE)); + assertEquals(500, ApiGatewayService.resolvePageSize(501)); + assertEquals(500, ApiGatewayService.resolvePageSize(100_000)); + assertEquals(500, ApiGatewayService.resolvePageSize(Integer.MAX_VALUE)); } @Test void aLimitBelowOneIsRejected() { - assertThrows(AwsException.class, () -> ApiGatewayService.resolveUsageLimit(0)); - assertThrows(AwsException.class, () -> ApiGatewayService.resolveUsageLimit(-1)); + assertThrows(AwsException.class, () -> ApiGatewayService.resolvePageSize(0)); + assertThrows(AwsException.class, () -> ApiGatewayService.resolvePageSize(-1)); } }