fix(apigateway): filter GetUsagePlans by keyId (#7655)

This commit is contained in:
Mario Toffia authored and GitHub committed 2026-11-07 09:52:39 -04:00
1 parent 062389b370
commit 413f490d8c
4 files changed
+61 -4

No files matched your search

+6 -2
View File
@@ -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
@@ -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<UsagePlan> plans = service.getUsagePlans(region);
List<UsagePlan> plans = service.getUsagePlans(region, keyId);
ObjectNode root = objectMapper.createObjectNode();
ArrayNode items = root.putArray("item");
plans.forEach(p -> items.add(toUsagePlanNode(p, false)));
@@ -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<UsagePlan> getUsagePlans(String region, String keyId) {
List<UsagePlan> 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);
@@ -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<String> usagePlanIds(ApiGatewayClient client, String keyId) {
return client.getUsagePlans(r -> r.keyId(keyId).limit(500)).items().stream().map(UsagePlan::id).toList();
}
}