mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): reject a non-integer limit on GetUsage, GetAuthorizers and GetDocumentationParts (#8560)
* fix(apigateway): reject a non-integer limit on GetUsage, GetAuthorizers and GetDocumentationParts * test(apigateway): make the valid limit control page two authorizers
This commit is contained in:
1 parent
18d04ded17
commit
f733c91a74
3 files changed
+109
-7
No files matched your search
@@ -248,13 +248,18 @@ public final class Pagination {
|
||||
* should produce.
|
||||
*/
|
||||
public static Integer parseMaxResults(String value, String errorCode) {
|
||||
return parseMaxResults(value, "maxResults", errorCode);
|
||||
}
|
||||
|
||||
/** As above, naming {@code field} in the message for a page size that is not called {@code maxResults}. */
|
||||
public static Integer parseMaxResults(String value, String field, String errorCode) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException(errorCode, "maxResults must be an integer.", 400);
|
||||
throw new AwsException(errorCode, field + " must be an integer.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -275,13 +275,17 @@ public class ApiGatewayController {
|
||||
return Response.ok(toAuthorizerNode(service.getAuthorizer(region, apiId, authorizerId)).toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
private static Integer pageLimit(String limit) {
|
||||
return Pagination.parseMaxResults(limit, "limit", "BadRequestException");
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/authorizers")
|
||||
public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("limit") Integer limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
// Authorizer clashes with the imported API Gateway v2 model
|
||||
PaginatedResult<io.floci.aws.services.apigateway.model.Authorizer> page = service.getAuthorizerPage(region, apiId, limit, position);
|
||||
PaginatedResult<io.floci.aws.services.apigateway.model.Authorizer> page = service.getAuthorizerPage(region, apiId, pageLimit(limit), position);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
page.items().forEach(a -> items.add(toAuthorizerNode(a)));
|
||||
@@ -994,11 +998,11 @@ public class ApiGatewayController {
|
||||
@QueryParam("startDate") String startDate,
|
||||
@QueryParam("endDate") String endDate,
|
||||
@QueryParam("keyId") String keyId,
|
||||
@QueryParam("limit") Integer limit,
|
||||
@QueryParam("limit") String limit,
|
||||
@QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ApiGatewayService.UsageReport report =
|
||||
service.getUsage(region, usagePlanId, startDate, endDate, keyId, limit, position);
|
||||
service.getUsage(region, usagePlanId, startDate, endDate, keyId, pageLimit(limit), position);
|
||||
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
// The wire key is "values", not "items": the Usage shape models this map with
|
||||
@@ -1251,10 +1255,10 @@ public class ApiGatewayController {
|
||||
@QueryParam("type") String type, @QueryParam("name") String name,
|
||||
@QueryParam("path") String path,
|
||||
@QueryParam("locationStatus") String locationStatus,
|
||||
@QueryParam("limit") Integer limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<DocumentationPart> page = service.getDocumentationParts(region, apiId, type, name,
|
||||
path, locationStatus, limit, position);
|
||||
path, locationStatus, pageLimit(limit), position);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
page.items().forEach(part -> items.add(objectMapper.valueToTree(part)));
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
/**
|
||||
* A page size the SDK's typed setter cannot produce, so these are raw requests: a non-numeric or
|
||||
* out-of-range {@code limit} must reach the service and come back as a {@code BadRequestException}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class ApiGatewayMalformedPageLimitIntegrationTest {
|
||||
|
||||
private static final String[] MALFORMED = {"abc", "1.5", "99999999999"};
|
||||
|
||||
private static String createRestApi() {
|
||||
return given().contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"malformed-page-limit\"}")
|
||||
.when().post("/restapis")
|
||||
.then().statusCode(201)
|
||||
.extract().path("id");
|
||||
}
|
||||
|
||||
private static String createUsagePlan() {
|
||||
return given().contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"malformed-page-limit\"}")
|
||||
.when().post("/usageplans")
|
||||
.then().statusCode(201)
|
||||
.extract().path("id");
|
||||
}
|
||||
|
||||
private static void assertRejected(String path) {
|
||||
for (String limit : MALFORMED) {
|
||||
given().when().get(path + (path.contains("?") ? "&" : "?") + "limit=" + limit)
|
||||
.then().statusCode(400)
|
||||
.header("x-amzn-ErrorType", containsString("BadRequestException"))
|
||||
.body("message", containsString("limit must be an integer"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAuthorizersRejectsAMalformedLimit() {
|
||||
String apiId = createRestApi();
|
||||
|
||||
assertRejected("/restapis/" + apiId + "/authorizers");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDocumentationPartsRejectsAMalformedLimit() {
|
||||
String apiId = createRestApi();
|
||||
|
||||
assertRejected("/restapis/" + apiId + "/documentation/parts");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUsageRejectsAMalformedLimit() {
|
||||
String planId = createUsagePlan();
|
||||
|
||||
assertRejected("/usageplans/" + planId + "/usage?startDate=2026-09-01&endDate=2026-09-02");
|
||||
}
|
||||
|
||||
private static void createAuthorizer(String apiId, String name) {
|
||||
given().contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"" + name + "\",\"type\":\"TOKEN\","
|
||||
+ "\"authorizerUri\":\"arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/"
|
||||
+ "arn:aws:lambda:us-east-1:000000000000:function:f/invocations\","
|
||||
+ "\"identitySource\":\"method.request.header.Authorization\"}")
|
||||
.when().post("/restapis/" + apiId + "/authorizers")
|
||||
.then().statusCode(201);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIntegerLimitStillPagesAndABlankLimitUsesTheDefault() {
|
||||
String apiId = createRestApi();
|
||||
createAuthorizer(apiId, "first");
|
||||
createAuthorizer(apiId, "second");
|
||||
|
||||
given().when().get("/restapis/" + apiId + "/authorizers?limit=1")
|
||||
.then().statusCode(200)
|
||||
.body("item", hasSize(1))
|
||||
.body("position", notNullValue());
|
||||
given().when().get("/restapis/" + apiId + "/authorizers?limit=")
|
||||
.then().statusCode(200)
|
||||
.body("item", hasSize(2))
|
||||
.body("position", nullValue());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user