mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): page GetApiKeys by limit and position (#9748)
This commit is contained in:
1 parent
0bef961cf0
commit
00e4ceb1b2
9 files changed
+228
-14
No files matched your search
+1
-1
@@ -48,7 +48,7 @@ class ApiGatewayApiKeyCompatibilityTest {
|
||||
.includeValue(true));
|
||||
assertThat(readWithValue.value()).isEqualTo(value);
|
||||
|
||||
GetApiKeysResponse listed = apiGateway.getApiKeys();
|
||||
GetApiKeysResponse listed = apiGateway.getApiKeys(request -> request.limit(500));
|
||||
ApiKey listedKey = listed.items().stream()
|
||||
.filter(key -> created.id().equals(key.id()))
|
||||
.findFirst()
|
||||
|
||||
@@ -201,6 +201,12 @@ write REST API, stage, API key, usage plan and custom domain name tags through t
|
||||
A nonterminal page includes `position`; the final page omits it. An unknown or blank `position`
|
||||
is a `BadRequestException`.
|
||||
|
||||
### API Key Pagination
|
||||
|
||||
`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.
|
||||
|
||||
### Base Path Mapping Pagination
|
||||
|
||||
`GetBasePathMappings` accepts `limit` and `position`, returns 25 mappings by default,
|
||||
|
||||
@@ -825,19 +825,19 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/apikeys")
|
||||
public Response getApiKeys(@Context HttpHeaders headers,
|
||||
@QueryParam("includeValues") boolean includeValues) {
|
||||
@QueryParam("includeValues") boolean includeValues,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<ApiKey> keys = service.getApiKeys(region);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
keys.forEach(k -> {
|
||||
ObjectNode node = toApiKeyNode(k);
|
||||
PaginatedResult<ApiKey> page =
|
||||
service.getApiKeyPage(region, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, key -> {
|
||||
ObjectNode node = toApiKeyNode(key);
|
||||
if (!includeValues) {
|
||||
node.remove("value");
|
||||
}
|
||||
items.add(node);
|
||||
return node;
|
||||
});
|
||||
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -1616,6 +1616,10 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deleting a key detaches it from every usage plan, matching AWS. Usage plan keys hold their own
|
||||
* copy of the key value, so leaving the associations behind would keep a deleted key working as a
|
||||
|
||||
@@ -75,7 +75,7 @@ class ApiGatewayApiKeyIntegrationTest {
|
||||
@Test @Order(4)
|
||||
void listApiKeysIncludesTagsAndOmitsValueByDefault() {
|
||||
given()
|
||||
.when().get("/apikeys")
|
||||
.when().get("/apikeys?limit=500")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("item.find { it.id == '" + apiKeyId + "' }.createdDate", greaterThan(0))
|
||||
@@ -86,7 +86,7 @@ class ApiGatewayApiKeyIntegrationTest {
|
||||
.body("item.find { it.id == '" + apiKeyId + "' }.value", nullValue());
|
||||
|
||||
given()
|
||||
.when().get("/apikeys?includeValues=true")
|
||||
.when().get("/apikeys?includeValues=true&limit=500")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("item.find { it.id == '" + apiKeyId + "' }.value", notNullValue());
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.ApiKey;
|
||||
import software.amazon.awssdk.services.apigateway.model.BadRequestException;
|
||||
import software.amazon.awssdk.services.apigateway.model.GetApiKeysResponse;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* GetApiKeys honours {@code limit} and {@code position}. Runs in a region no other test uses so the
|
||||
* account-wide API key collection holds only this class's keys.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class ApiGatewayApiKeyPagingIntegrationTest {
|
||||
|
||||
private static final Region REGION = Region.of("ap-southeast-7");
|
||||
|
||||
private ApiGatewayClient client() {
|
||||
return AwsTestClients.create(ApiGatewayClient.builder(), "test", REGION, Apache5HttpClient.builder());
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void deleteRegionKeys() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
for (ApiKey key : client.getApiKeys(request -> request.limit(500)).items()) {
|
||||
client.deleteApiKey(request -> request.apiKey(key.id()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> createKeys(ApiGatewayClient client, int count) {
|
||||
Set<String> ids = new HashSet<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
String name = "paging-key-" + i;
|
||||
ids.add(client.createApiKey(request -> request.name(name).enabled(true)).id());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
@Test
|
||||
void limitOneReturnsPositionAndFollowsToTerminalPage() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
Set<String> created = createKeys(client, 3);
|
||||
|
||||
GetApiKeysResponse first = client.getApiKeys(request -> request.limit(1));
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
|
||||
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
|
||||
position -> client.getApiKeys(request -> request.limit(1).position(position)),
|
||||
GetApiKeysResponse::items, GetApiKeysResponse::position, ApiKey::id);
|
||||
assertEquals(created, new HashSet<>(seen));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedLimitReturnsFirstTwentyFiveKeys() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
createKeys(client, 27);
|
||||
|
||||
GetApiKeysResponse first = client.getApiKeys(request -> { });
|
||||
assertEquals(25, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
|
||||
GetApiKeysResponse second = client.getApiKeys(request -> request.position(first.position()));
|
||||
assertEquals(2, second.items().size());
|
||||
assertNull(second.position());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void limitLargerThanCollectionReturnsEverythingWithoutPosition() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
createKeys(client, 3);
|
||||
|
||||
GetApiKeysResponse page = client.getApiKeys(request -> request.limit(5));
|
||||
assertEquals(3, page.items().size());
|
||||
assertNull(page.position());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void includeValuesAppliesToEveryPage() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
createKeys(client, 2);
|
||||
|
||||
GetApiKeysResponse first = client.getApiKeys(request -> request.limit(1).includeValues(true));
|
||||
assertNotNull(first.items().get(0).value());
|
||||
GetApiKeysResponse second = client.getApiKeys(
|
||||
request -> request.limit(1).includeValues(true).position(first.position()));
|
||||
assertNotNull(second.items().get(0).value());
|
||||
|
||||
GetApiKeysResponse withoutValues = client.getApiKeys(request -> request.limit(1));
|
||||
assertNull(withoutValues.items().get(0).value());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherRegionsKeysAreNotPagedIn() {
|
||||
try (ApiGatewayClient client = client();
|
||||
ApiGatewayClient other = AwsTestClients.create(ApiGatewayClient.builder(), "test",
|
||||
Region.US_EAST_1, Apache5HttpClient.builder())) {
|
||||
Set<String> created = createKeys(client, 2);
|
||||
String otherKey = other.createApiKey(request -> request.name("paging-key-other-region")).id();
|
||||
try {
|
||||
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
|
||||
position -> client.getApiKeys(request -> request.limit(1).position(position)),
|
||||
GetApiKeysResponse::items, GetApiKeysResponse::position, ApiKey::id);
|
||||
assertEquals(created, new HashSet<>(seen));
|
||||
} finally {
|
||||
other.deleteApiKey(request -> request.apiKey(otherKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownPositionIsRejected() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
createKeys(client, 2);
|
||||
|
||||
assertThrows(BadRequestException.class,
|
||||
() -> client.getApiKeys(request -> request.limit(1).position("not-a-key")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void limitBelowOneIsRejected() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
assertThrows(BadRequestException.class, () -> client.getApiKeys(request -> request.limit(0)));
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -857,7 +857,7 @@ class ApiGatewayRequestAuthorizerIntegrationTest {
|
||||
.then().statusCode(201);
|
||||
|
||||
apiKeyKeyId = given()
|
||||
.when().get("/apikeys")
|
||||
.when().get("/apikeys?limit=500")
|
||||
.then().statusCode(200)
|
||||
.extract().path("item.find { it.name == 'test-key' }.id");
|
||||
|
||||
@@ -871,7 +871,7 @@ class ApiGatewayRequestAuthorizerIntegrationTest {
|
||||
.then().statusCode(201);
|
||||
|
||||
apiKeyDistinctKeyId = given()
|
||||
.when().get("/apikeys")
|
||||
.when().get("/apikeys?limit=500")
|
||||
.then().statusCode(200)
|
||||
.extract().path("item.find { it.name == 'test-key-distinct' }.id");
|
||||
assertNotEquals("my-distinct-api-key", apiKeyDistinctKeyId,
|
||||
|
||||
+1
-1
@@ -194,7 +194,7 @@ class ApiGatewayApiKeyCfnIntegrationTest {
|
||||
// The update creates a key in place of the deleted one before the secret fails it.
|
||||
cloudFormation(stack, "UpdateStack", ROLLBACK_TEMPLATE.formatted(properties, FAILING_RESOURCE), Map.of());
|
||||
describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE");
|
||||
given().when().get("/apikeys").then()
|
||||
given().when().get("/apikeys?limit=500").then()
|
||||
.statusCode(200)
|
||||
.body("item.name", not(hasItem(name)));
|
||||
|
||||
|
||||
Reference in new issue
Block a user