From 24271ff5a67ac0c8f6d018e5aba026e2aac50795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= <92062874+matheusandre1@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:35:27 -0300 Subject: [PATCH] fix(apigateway): filter GetApiKeys by name and customerId (#9749) GetApiKeys ignored the name and customerId filters and returned every key. name now keeps the keys whose name starts with it and customerId the keys of that customer; the filtered keys are paged by limit and position as before. ApiGatewayRestApiPageCapTest and ApiGatewayApiKeyPageCapTest become one parameterized ApiGatewayPageCapTest over the collections paged through pageById. --- docs/services/api-gateway.md | 3 +- .../apigateway/ApiGatewayController.java | 5 +- .../apigateway/ApiGatewayService.java | 14 +- .../ApiGatewayApiKeyPageCapTest.java | 58 ------- ...ApiGatewayApiKeyPagingIntegrationTest.java | 49 ++++++ .../apigateway/ApiGatewayPageCapTest.java | 158 ++++++++++++++++++ .../ApiGatewayRestApiPageCapTest.java | 58 ------- 7 files changed, 224 insertions(+), 121 deletions(-) delete mode 100644 src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPageCapTest.java create mode 100644 src/test/java/io/floci/aws/services/apigateway/ApiGatewayPageCapTest.java delete mode 100644 src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPageCapTest.java diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index e39a9c28c8..1c34f9d72d 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -205,7 +205,8 @@ is a `BadRequestException`. `GetApiKeys` accepts `limit` and `position`, returns 25 keys by default and caps pages at 500. A nonterminal page includes `position`; the final page omits it. An unknown or blank `position` -is a `BadRequestException`. `includeValues` applies to every page. +is a `BadRequestException`. `includeValues` applies to every page. `name` keeps the keys whose +name starts with it and `customerId` the keys of that customer; both apply before paging. ### Base Path Mapping Pagination 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 0d5a909f28..ce851cb5db 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java @@ -826,11 +826,12 @@ public class ApiGatewayController { @Path("/apikeys") public Response getApiKeys(@Context HttpHeaders headers, @QueryParam("includeValues") boolean includeValues, + @QueryParam("name") String name, @QueryParam("customerId") String customerId, @QueryParam("limit") String limit, @QueryParam("position") String position, @Context UriInfo uriInfo) { String region = regionResolver.resolveRegion(headers); - PaginatedResult page = - service.getApiKeyPage(region, pageLimit(limit), requestedPosition(position, uriInfo)); + PaginatedResult page = service.getApiKeyPage(region, name, customerId, pageLimit(limit), + requestedPosition(position, uriInfo)); return pageResponse(page, key -> { ObjectNode node = toApiKeyNode(key); if (!includeValues) { 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 684828de2b..e6b179c20b 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -1616,8 +1616,18 @@ public class ApiGatewayService implements ResourceProvider, Resettable, PassedRo return apiKeyStore.scan(k -> k.startsWith(prefix)); } - public PaginatedResult getApiKeyPage(String region, Integer limit, String position) { - return pageById(getApiKeys(region), ApiKey::getId, limit, position); + /** + * One {@code GetApiKeys} page. {@code name} keeps the keys whose name starts with it and + * {@code customerId} the keys of that customer; the filtered keys are then paged. + */ + public PaginatedResult getApiKeyPage(String region, String name, String customerId, Integer limit, + String position) { + List keys = getApiKeys(region).stream() + .filter(key -> name == null || name.isEmpty() + || (key.getName() != null && key.getName().startsWith(name))) + .filter(key -> customerId == null || customerId.isEmpty() || customerId.equals(key.getCustomerId())) + .toList(); + return pageById(keys, ApiKey::getId, limit, position); } /** diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPageCapTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPageCapTest.java deleted file mode 100644 index 7a8840449f..0000000000 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPageCapTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package io.floci.aws.services.apigateway; - -import io.floci.aws.config.EmulatorConfig; -import io.floci.aws.config.TlsCertificateManager; -import io.floci.aws.core.common.PaginatedResult; -import io.floci.aws.core.common.RegionResolver; -import io.floci.aws.core.storage.AccountAwareStorageBackend; -import io.floci.aws.core.storage.StorageFactory; -import io.floci.aws.services.apigateway.model.ApiKey; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class ApiGatewayApiKeyPageCapTest { - - private static final String REGION = "us-east-1"; - - private ApiGatewayService service; - - @BeforeEach - void setUp() { - StorageFactory storageFactory = mock(StorageFactory.class); - when(storageFactory.create(anyString(), anyString(), any())) - .thenAnswer(invocation -> AccountAwareStorageBackend.inMemory("000000000000")); - service = new ApiGatewayService(storageFactory, mock(EmulatorConfig.class), - mock(TlsCertificateManager.class), new RegionResolver(REGION, "000000000000")); - for (int i = 0; i < 501; i++) { - service.createApiKey(REGION, Map.of("name", "capped-" + i)); - } - } - - @Test - void aLimitAboveTheCapReturnsFiveHundredAndAUsablePosition() { - PaginatedResult first = service.getApiKeyPage(REGION, 501, null); - assertEquals(500, first.items().size()); - assertNotNull(first.nextToken()); - - PaginatedResult last = service.getApiKeyPage(REGION, 501, first.nextToken()); - assertEquals(1, last.items().size()); - assertNull(last.nextToken()); - } - - @Test - void theLargestIntegerLimitIsCappedTheSameWay() { - PaginatedResult first = service.getApiKeyPage(REGION, Integer.MAX_VALUE, null); - assertEquals(500, first.items().size()); - assertNotNull(first.nextToken()); - } -} diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPagingIntegrationTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPagingIntegrationTest.java index 497fc65006..2049c1bafd 100644 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPagingIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayApiKeyPagingIntegrationTest.java @@ -127,6 +127,55 @@ class ApiGatewayApiKeyPagingIntegrationTest { } } + @Test + void nameKeepsTheKeysWhoseNameStartsWithIt() { + try (ApiGatewayClient client = client()) { + createKeys(client, 2); + String orders = client.createApiKey(request -> request.name("orders-key")).id(); + String ordersBatch = client.createApiKey(request -> request.name("orders-key-batch")).id(); + + GetApiKeysResponse page = client.getApiKeys(request -> request.nameQuery("orders-key")); + assertEquals(Set.of(orders, ordersBatch), + new HashSet<>(page.items().stream().map(ApiKey::id).toList())); + assertNull(page.position()); + + assertEquals(0, client.getApiKeys(request -> request.nameQuery("no-such-key")).items().size()); + } + } + + @Test + void customerIdKeepsTheKeysOfThatCustomer() { + try (ApiGatewayClient client = client()) { + createKeys(client, 2); + String first = client.createApiKey(request -> request.name("customer-key-1").customerId("customer-a")).id(); + String second = client.createApiKey(request -> request.name("customer-key-2").customerId("customer-a")).id(); + client.createApiKey(request -> request.name("customer-key-3").customerId("customer-b")); + + GetApiKeysResponse page = client.getApiKeys(request -> request.customerId("customer-a")); + assertEquals(Set.of(first, second), new HashSet<>(page.items().stream().map(ApiKey::id).toList())); + assertNull(page.position()); + } + } + + @Test + void filteredKeysArePagedToo() { + try (ApiGatewayClient client = client()) { + createKeys(client, 3); + Set matching = new HashSet<>(); + for (int i = 0; i < 3; i++) { + String name = "filtered-key-" + i; + matching.add(client.createApiKey(request -> request.name(name).customerId("customer-a")).id()); + } + client.createApiKey(request -> request.name("filtered-key-other").customerId("customer-b")); + + List seen = ApiGatewayPagingWalk.oneItemPerPageIds( + position -> client.getApiKeys(request -> request.nameQuery("filtered-key") + .customerId("customer-a").limit(1).position(position)), + GetApiKeysResponse::items, GetApiKeysResponse::position, ApiKey::id); + assertEquals(matching, new HashSet<>(seen)); + } + } + @Test void unknownPositionIsRejected() { try (ApiGatewayClient client = client()) { diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayPageCapTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayPageCapTest.java new file mode 100644 index 0000000000..0f2efe50f6 --- /dev/null +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayPageCapTest.java @@ -0,0 +1,158 @@ +package io.floci.aws.services.apigateway; + +import io.floci.aws.config.EmulatorConfig; +import io.floci.aws.config.TlsCertificateManager; +import io.floci.aws.core.common.PaginatedResult; +import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.core.storage.AccountAwareStorageBackend; +import io.floci.aws.core.storage.StorageFactory; +import org.junit.jupiter.api.Named; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** The 500-item page cap, on every collection that pages through {@code pageById}. */ +class ApiGatewayPageCapTest { + + private static final String REGION = "us-east-1"; + private static final int ITEMS = 501; + + /** One page of a collection that already holds {@link #ITEMS} items. */ + @FunctionalInterface + private interface Pager { + PaginatedResult page(Integer limit, String position); + } + + private static ApiGatewayService newService() { + StorageFactory storageFactory = mock(StorageFactory.class); + when(storageFactory.create(anyString(), anyString(), any())) + .thenAnswer(invocation -> AccountAwareStorageBackend.inMemory("000000000000")); + return new ApiGatewayService(storageFactory, mock(EmulatorConfig.class), + mock(TlsCertificateManager.class), new RegionResolver(REGION, "000000000000")); + } + + private static String newApi(ApiGatewayService service) { + return service.createRestApi(REGION, Map.of("name", "capped")).getId(); + } + + private static Pager restApis(ApiGatewayService service) { + for (int i = 0; i < ITEMS; i++) { + service.createRestApi(REGION, Map.of("name", "capped-" + i)); + } + return (limit, position) -> service.getRestApiPage(REGION, limit, position); + } + + private static Pager resources(ApiGatewayService service) { + String apiId = newApi(service); + String rootId = service.getResources(REGION, apiId).get(0).getId(); + for (int i = 0; service.getResources(REGION, apiId).size() < ITEMS; i++) { + service.createResource(REGION, apiId, rootId, Map.of("pathPart", "capped-" + i)); + } + return (limit, position) -> service.getResourcePage(REGION, apiId, limit, position); + } + + private static Pager deployments(ApiGatewayService service) { + String apiId = newApi(service); + for (int i = 0; i < ITEMS; i++) { + service.createDeployment(REGION, apiId, Map.of()); + } + return (limit, position) -> service.getDeploymentsPage(REGION, apiId, String.valueOf(limit), position); + } + + private static Pager authorizers(ApiGatewayService service) { + String apiId = newApi(service); + for (int i = 0; service.getAuthorizers(REGION, apiId).size() < ITEMS; i++) { + service.createAuthorizer(REGION, apiId, Map.of("name", "capped-" + i, "type", "TOKEN", + "identitySource", "method.request.header.Authorization")); + } + return (limit, position) -> service.getAuthorizerPage(REGION, apiId, limit, position); + } + + private static Pager apiKeys(ApiGatewayService service) { + for (int i = 0; i < ITEMS; i++) { + service.createApiKey(REGION, Map.of("name", "capped-" + i)); + } + return (limit, position) -> service.getApiKeyPage(REGION, null, null, limit, position); + } + + private static Pager usagePlans(ApiGatewayService service) { + for (int i = 0; i < ITEMS; i++) { + service.createUsagePlan(REGION, Map.of("name", "capped-" + i)); + } + return (limit, position) -> service.getUsagePlanPage(REGION, null, limit, position); + } + + private static Pager usagePlanKeys(ApiGatewayService service) { + String planId = service.createUsagePlan(REGION, Map.of("name", "capped")).getId(); + for (int i = 0; i < ITEMS; i++) { + String keyId = service.createApiKey(REGION, Map.of("name", "capped-" + i)).getId(); + service.createUsagePlanKey(REGION, planId, Map.of("keyId", keyId, "keyType", "API_KEY")); + } + return (limit, position) -> service.getUsagePlanKeyPage(REGION, planId, limit, position); + } + + private static Pager requestValidators(ApiGatewayService service) { + String apiId = newApi(service); + for (int i = 0; service.getRequestValidators(REGION, apiId).size() < ITEMS; i++) { + service.createRequestValidator(REGION, apiId, Map.of("name", "capped-" + i)); + } + return (limit, position) -> service.getRequestValidatorPage(REGION, apiId, limit, position); + } + + private static Pager vpcLinks(ApiGatewayService service) { + for (int i = 0; i < ITEMS; i++) { + service.createVpcLink(REGION, Map.of("name", "capped-" + i, + "targetArns", List.of("capped-target"))); + } + return (limit, position) -> service.getVpcLinkPage(REGION, limit, position); + } + + static Stream>> collections() { + return Stream.of( + Named.of("GetRestApis", ApiGatewayPageCapTest::restApis), + Named.of("GetResources", ApiGatewayPageCapTest::resources), + Named.of("GetDeployments", ApiGatewayPageCapTest::deployments), + Named.of("GetAuthorizers", ApiGatewayPageCapTest::authorizers), + Named.of("GetApiKeys", ApiGatewayPageCapTest::apiKeys), + Named.of("GetUsagePlans", ApiGatewayPageCapTest::usagePlans), + Named.of("GetUsagePlanKeys", ApiGatewayPageCapTest::usagePlanKeys), + Named.of("GetRequestValidators", ApiGatewayPageCapTest::requestValidators), + Named.of("GetVpcLinks", ApiGatewayPageCapTest::vpcLinks)); + } + + @ParameterizedTest + @MethodSource("collections") + void aLimitAboveTheCapReturnsFiveHundredAndAUsablePosition(Function collection) { + Pager pager = collection.apply(newService()); + + PaginatedResult first = pager.page(ITEMS, null); + assertEquals(500, first.items().size()); + assertNotNull(first.nextToken()); + + PaginatedResult last = pager.page(ITEMS, first.nextToken()); + assertEquals(1, last.items().size()); + assertNull(last.nextToken()); + } + + @ParameterizedTest + @MethodSource("collections") + void theLargestIntegerLimitIsCappedTheSameWay(Function collection) { + Pager pager = collection.apply(newService()); + + PaginatedResult first = pager.page(Integer.MAX_VALUE, null); + assertEquals(500, first.items().size()); + assertNotNull(first.nextToken()); + } +} diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPageCapTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPageCapTest.java deleted file mode 100644 index fc1e990a20..0000000000 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayRestApiPageCapTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package io.floci.aws.services.apigateway; - -import io.floci.aws.config.EmulatorConfig; -import io.floci.aws.config.TlsCertificateManager; -import io.floci.aws.core.common.PaginatedResult; -import io.floci.aws.core.common.RegionResolver; -import io.floci.aws.core.storage.AccountAwareStorageBackend; -import io.floci.aws.core.storage.StorageFactory; -import io.floci.aws.services.apigateway.model.RestApi; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class ApiGatewayRestApiPageCapTest { - - private static final String REGION = "us-east-1"; - - private ApiGatewayService service; - - @BeforeEach - void setUp() { - StorageFactory storageFactory = mock(StorageFactory.class); - when(storageFactory.create(anyString(), anyString(), any())) - .thenAnswer(invocation -> AccountAwareStorageBackend.inMemory("000000000000")); - service = new ApiGatewayService(storageFactory, mock(EmulatorConfig.class), - mock(TlsCertificateManager.class), new RegionResolver(REGION, "000000000000")); - for (int i = 0; i < 501; i++) { - service.createRestApi(REGION, Map.of("name", "capped-" + i)); - } - } - - @Test - void aLimitAboveTheCapReturnsFiveHundredAndAUsablePosition() { - PaginatedResult first = service.getRestApiPage(REGION, 501, null); - assertEquals(500, first.items().size()); - assertNotNull(first.nextToken()); - - PaginatedResult last = service.getRestApiPage(REGION, 501, first.nextToken()); - assertEquals(1, last.items().size()); - assertNull(last.nextToken()); - } - - @Test - void theLargestIntegerLimitIsCappedTheSameWay() { - PaginatedResult first = service.getRestApiPage(REGION, Integer.MAX_VALUE, null); - assertEquals(500, first.items().size()); - assertNotNull(first.nextToken()); - } -}