mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): page GetUsagePlanKeys by limit and position (#9594)
GetUsagePlanKeys ignored limit and position and returned every key attached to the plan in one response. It now pages with the same id-ordered pageById helper and pageResponse envelope GetUsagePlans and GetAuthorizers use: 25 keys by default, 500 at most, limit=0 rejected, position on nonterminal pages only, and an unknown position rejected with BadRequestException. GetUsage and the data plane keep the unpaged list. Closes #9062
This commit is contained in:
1 parent
05217485a9
commit
ab71c20cda
4 files changed
+150
-6
No files matched your search
@@ -255,6 +255,10 @@ stores its own copy of the key value, so without that sweep a deleted key would
|
||||
`GetUsagePlanKeys` and keep being recognised on the data plane. `DeleteUsagePlan` drops the plan's keys
|
||||
the same way, so a plan recreated with the same pinned id starts with none.
|
||||
|
||||
`GetUsagePlanKeys` accepts `limit` and `position`, returns 25 keys by default and caps pages at 500.
|
||||
A nonterminal page includes `position`; the final page omits it, and an unknown `position` is a
|
||||
`BadRequestException`.
|
||||
|
||||
`requestContext.identity.apiKey` is only populated when the `x-api-key` header matches a key that still
|
||||
exists and has `enabled` set to `true`, so disabling a key through `UpdateApiKey` takes effect
|
||||
immediately.
|
||||
|
||||
@@ -1027,13 +1027,12 @@ public class ApiGatewayController {
|
||||
|
||||
@GET
|
||||
@Path("/usageplans/{usagePlanId}/keys")
|
||||
public Response getUsagePlanKeys(@Context HttpHeaders headers, @PathParam("usagePlanId") String usagePlanId) {
|
||||
public Response getUsagePlanKeys(@Context HttpHeaders headers, @PathParam("usagePlanId") String usagePlanId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<UsagePlanKey> keys = service.getUsagePlanKeys(region, usagePlanId);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
keys.forEach(k -> items.add(toUsagePlanKeyNode(k)));
|
||||
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
PaginatedResult<UsagePlanKey> page =
|
||||
service.getUsagePlanKeyPage(region, usagePlanId, pageLimit(limit), position);
|
||||
return pageResponse(page, this::toUsagePlanKeyNode);
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -2156,6 +2156,11 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
|
||||
return usagePlanKeyStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
public PaginatedResult<UsagePlanKey> getUsagePlanKeyPage(String region, String usagePlanId, Integer limit,
|
||||
String position) {
|
||||
return pageById(getUsagePlanKeys(region, usagePlanId), UsagePlanKey::getId, limit, position);
|
||||
}
|
||||
|
||||
public void deleteUsagePlanKey(String region, String usagePlanId, String keyId) {
|
||||
getUsagePlanKey(region, usagePlanId, keyId);
|
||||
usagePlanKeyStore.delete(usagePlanKeyPathKey(region, usagePlanId, keyId));
|
||||
|
||||
+136
@@ -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.BadRequestException;
|
||||
import software.amazon.awssdk.services.apigateway.model.GetUsagePlanKeysResponse;
|
||||
import software.amazon.awssdk.services.apigateway.model.UsagePlanKey;
|
||||
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* GetUsagePlanKeys honours {@code limit} and {@code position}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class ApiGatewayUsagePlanKeyPagingIntegrationTest {
|
||||
|
||||
private ApiGatewayClient client() {
|
||||
return AwsTestClients.create(ApiGatewayClient.builder(), "test", Region.US_EAST_1,
|
||||
Apache5HttpClient.builder());
|
||||
}
|
||||
|
||||
private List<String> attachKeys(ApiGatewayClient client, String planId, String prefix, int count) {
|
||||
List<String> keyIds = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
String name = prefix + "-" + i;
|
||||
String keyId = client.createApiKey(request -> request.name(name).enabled(true)).id();
|
||||
keyIds.add(keyId);
|
||||
client.createUsagePlanKey(request -> request.usagePlanId(planId).keyId(keyId).keyType("API_KEY"));
|
||||
}
|
||||
return keyIds;
|
||||
}
|
||||
|
||||
private void cleanUp(ApiGatewayClient client, String planId, List<String> keyIds) {
|
||||
for (String keyId : keyIds) {
|
||||
client.deleteApiKey(request -> request.apiKey(keyId));
|
||||
}
|
||||
client.deleteUsagePlan(request -> request.usagePlanId(planId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void limitOneReturnsPositionAndFollowsToTerminalPage() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String planId = client.createUsagePlan(request -> request.name("plan-key-paging-walk")).id();
|
||||
List<String> keyIds = new ArrayList<>();
|
||||
try {
|
||||
keyIds.addAll(attachKeys(client, planId, "plan-key-walk", 3));
|
||||
|
||||
GetUsagePlanKeysResponse first = client.getUsagePlanKeys(request -> request.usagePlanId(planId)
|
||||
.limit(1));
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
UsagePlanKey sample = first.items().get(0);
|
||||
assertEquals("API_KEY", sample.type());
|
||||
assertNotNull(sample.value());
|
||||
assertTrue(sample.name().startsWith("plan-key-walk-"));
|
||||
|
||||
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
|
||||
position -> client.getUsagePlanKeys(
|
||||
request -> request.usagePlanId(planId).limit(1).position(position)),
|
||||
GetUsagePlanKeysResponse::items, GetUsagePlanKeysResponse::position, UsagePlanKey::id);
|
||||
assertEquals(3, seen.size());
|
||||
assertEquals(new HashSet<>(keyIds), new HashSet<>(seen));
|
||||
} finally {
|
||||
cleanUp(client, planId, keyIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedLimitReturnsFirstTwentyFiveKeys() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String planId = client.createUsagePlan(request -> request.name("plan-key-paging-default")).id();
|
||||
List<String> keyIds = new ArrayList<>();
|
||||
try {
|
||||
keyIds.addAll(attachKeys(client, planId, "plan-key-default", 26));
|
||||
|
||||
GetUsagePlanKeysResponse first = client.getUsagePlanKeys(request -> request.usagePlanId(planId));
|
||||
assertEquals(25, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
|
||||
GetUsagePlanKeysResponse second = client.getUsagePlanKeys(request -> request.usagePlanId(planId)
|
||||
.position(first.position()));
|
||||
assertEquals(1, second.items().size());
|
||||
assertNull(second.position());
|
||||
|
||||
Set<String> seen = new HashSet<>();
|
||||
first.items().forEach(key -> seen.add(key.id()));
|
||||
second.items().forEach(key -> seen.add(key.id()));
|
||||
assertEquals(new HashSet<>(keyIds), seen);
|
||||
} finally {
|
||||
cleanUp(client, planId, keyIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyPlanAndBadInput() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String planId = client.createUsagePlan(request -> request.name("plan-key-paging-errors")).id();
|
||||
List<String> keyIds = new ArrayList<>();
|
||||
try {
|
||||
GetUsagePlanKeysResponse empty = client.getUsagePlanKeys(request -> request.usagePlanId(planId)
|
||||
.limit(1));
|
||||
assertEquals(0, empty.items().size());
|
||||
assertNull(empty.position());
|
||||
|
||||
keyIds.addAll(attachKeys(client, planId, "plan-key-errors", 2));
|
||||
|
||||
GetUsagePlanKeysResponse all = client.getUsagePlanKeys(request -> request.usagePlanId(planId)
|
||||
.limit(2));
|
||||
assertEquals(2, all.items().size());
|
||||
assertNull(all.position());
|
||||
|
||||
assertThrows(BadRequestException.class,
|
||||
() -> client.getUsagePlanKeys(request -> request.usagePlanId(planId).limit(0)));
|
||||
assertThrows(BadRequestException.class,
|
||||
() -> client.getUsagePlanKeys(request -> request.usagePlanId(planId).position("missing")));
|
||||
} finally {
|
||||
cleanUp(client, planId, keyIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user