diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index f4b77cf8e4..80b62fb209 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -633,8 +633,12 @@ an offset inside it, a per-method throttle needs a plan throttle, and adding a s lists is a `ConflictException`. `AWS::ApiGateway::UsagePlan` maps its `ApiStages`, with each stage's `Throttle`, and its `Throttle` and `Quota` the same way, and an update replaces them. -Floci does not check that a stage's API, stage or method exists, does not filter `GetUsagePlans` by -`keyId`, and does not enforce throttles or quotas. +`GetUsagePlans` with a `keyId` returns only the plans that key is added to, and an empty list for a key +in no plan or an unknown key. A blank `keyId` lists every plan, as on AWS. Every matching plan comes +back in one page. + +Floci does not check that a stage's API, stage or method exists, and does not enforce throttles or +quotas. A `CreateUsagePlan` body member of the wrong JSON type is a `SerializationException` with API Gateway's deserializer message, before anything else is validated. The message names the Java class 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 59e2ae4862..6843d92480 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java @@ -895,9 +895,9 @@ public class ApiGatewayController { @GET @Path("/usageplans") - public Response getUsagePlans(@Context HttpHeaders headers) { + public Response getUsagePlans(@Context HttpHeaders headers, @QueryParam("keyId") String keyId) { String region = regionResolver.resolveRegion(headers); - List plans = service.getUsagePlans(region); + List plans = service.getUsagePlans(region, keyId); ObjectNode root = objectMapper.createObjectNode(); ArrayNode items = root.putArray("item"); plans.forEach(p -> items.add(toUsagePlanNode(p, false))); 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 ea24354e64..cb367a0650 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -1639,6 +1639,21 @@ public class ApiGatewayService implements ResourceProvider { return usagePlanStore.scan(k -> k.startsWith(prefix)); } + /** + * {@code GetUsagePlans} with its {@code keyId} filter, measured on API Gateway: only the plans + * the key is added to, an empty list for a key in no plan or no key at all, and every plan for a + * blank {@code keyId}. + */ + public List getUsagePlans(String region, String keyId) { + List plans = getUsagePlans(region); + if (keyId == null || keyId.isBlank()) { + return plans; + } + return plans.stream() + .filter(plan -> usagePlanKeyStore.get(usagePlanKeyPathKey(region, plan.getId(), keyId)).isPresent()) + .toList(); + } + public void deleteUsagePlan(String region, String usagePlanId) { synchronized (usagePlanLock) { getUsagePlan(region, usagePlanId); diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsagePlanSdkIntegrationTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsagePlanSdkIntegrationTest.java index b5d23c7a34..b71bd2f293 100644 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsagePlanSdkIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayUsagePlanSdkIntegrationTest.java @@ -19,8 +19,10 @@ import software.amazon.awssdk.services.apigateway.model.UsagePlan; import java.net.URI; import java.util.List; import java.util.Map; +import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; @QuarkusTest class ApiGatewayUsagePlanSdkIntegrationTest { @@ -68,4 +70,40 @@ class ApiGatewayUsagePlanSdkIntegrationTest { } } } + + // Measured on API Gateway: a key in no plan and an unknown key both answer an empty list, and an + // empty or whitespace-only keyId lists every plan. + @Test + void getUsagePlansFiltersByKeyId() { + try (ApiGatewayClient client = client()) { + String first = client.createUsagePlan(r -> r.name("key-filter-first")).id(); + String second = client.createUsagePlan(r -> r.name("key-filter-second")).id(); + String inPlan = client.createApiKey(r -> r.name("key-filter-in-plan")).id(); + String inNoPlan = client.createApiKey(r -> r.name("key-filter-in-no-plan")).id(); + try { + client.createUsagePlanKey(r -> r.usagePlanId(first).keyId(inPlan).keyType("API_KEY")); + + assertEquals(List.of(first), usagePlanIds(client, inPlan)); + assertEquals(List.of(), usagePlanIds(client, inNoPlan)); + assertEquals(List.of(), usagePlanIds(client, "missing")); + assertTrue(usagePlanIds(client, null).containsAll(List.of(first, second))); + assertTrue(usagePlanIds(client, " ").containsAll(List.of(first, second))); + + client.createUsagePlanKey(r -> r.usagePlanId(second).keyId(inPlan).keyType("API_KEY")); + assertEquals(Set.of(first, second), Set.copyOf(usagePlanIds(client, inPlan))); + + client.deleteUsagePlanKey(r -> r.usagePlanId(first).keyId(inPlan)); + assertEquals(List.of(second), usagePlanIds(client, inPlan)); + } finally { + client.deleteApiKey(r -> r.apiKey(inPlan)); + client.deleteApiKey(r -> r.apiKey(inNoPlan)); + client.deleteUsagePlan(r -> r.usagePlanId(first)); + client.deleteUsagePlan(r -> r.usagePlanId(second)); + } + } + } + + private static List usagePlanIds(ApiGatewayClient client, String keyId) { + return client.getUsagePlans(r -> r.keyId(keyId).limit(500)).items().stream().map(UsagePlan::id).toList(); + } }