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.
This commit is contained in:
Matheus André authored and GitHub committed 2026-11-29 11:35:27 -04:00
1 parent 1c84d5eccf
commit 24271ff5a6
7 files changed
+224 -121

No files matched your search

+2 -1
View File
@@ -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
@@ -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<ApiKey> page =
service.getApiKeyPage(region, pageLimit(limit), requestedPosition(position, uriInfo));
PaginatedResult<ApiKey> page = service.getApiKeyPage(region, name, customerId, pageLimit(limit),
requestedPosition(position, uriInfo));
return pageResponse(page, key -> {
ObjectNode node = toApiKeyNode(key);
if (!includeValues) {
@@ -1616,8 +1616,18 @@ public class ApiGatewayService implements ResourceProvider, Resettable, PassedRo
return apiKeyStore.scan(k -> k.startsWith(prefix));
}
public PaginatedResult<ApiKey> 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<ApiKey> getApiKeyPage(String region, String name, String customerId, Integer limit,
String position) {
List<ApiKey> 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);
}
/**
@@ -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<ApiKey> first = service.getApiKeyPage(REGION, 501, null);
assertEquals(500, first.items().size());
assertNotNull(first.nextToken());
PaginatedResult<ApiKey> last = service.getApiKeyPage(REGION, 501, first.nextToken());
assertEquals(1, last.items().size());
assertNull(last.nextToken());
}
@Test
void theLargestIntegerLimitIsCappedTheSameWay() {
PaginatedResult<ApiKey> first = service.getApiKeyPage(REGION, Integer.MAX_VALUE, null);
assertEquals(500, first.items().size());
assertNotNull(first.nextToken());
}
}
@@ -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<String> 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<String> 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()) {
@@ -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<Named<Function<ApiGatewayService, Pager>>> 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<ApiGatewayService, Pager> 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<ApiGatewayService, Pager> collection) {
Pager pager = collection.apply(newService());
PaginatedResult<?> first = pager.page(Integer.MAX_VALUE, null);
assertEquals(500, first.items().size());
assertNotNull(first.nextToken());
}
}
@@ -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<RestApi> first = service.getRestApiPage(REGION, 501, null);
assertEquals(500, first.items().size());
assertNotNull(first.nextToken());
PaginatedResult<RestApi> last = service.getRestApiPage(REGION, 501, first.nextToken());
assertEquals(1, last.items().size());
assertNull(last.nextToken());
}
@Test
void theLargestIntegerLimitIsCappedTheSameWay() {
PaginatedResult<RestApi> first = service.getRestApiPage(REGION, Integer.MAX_VALUE, null);
assertEquals(500, first.items().size());
assertNotNull(first.nextToken());
}
}