mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 08:31:53 -05:00
fix(apigateway): refuse an empty or blank position on every paged collection (#9625)
The empty or blank position check lived in getRestApiPage alone, so GetRestApis refused position= while the other collections paged by pageById served their first page for it. The rule now lives in pageById, so GetRestApis, GetResources, GetDeployments, GetAuthorizers, GetRequestValidators, GetModels, GetDocumentationParts, GetUsagePlans, GetUsagePlanKeys, GetBasePathMappings and GetVpcLinks all answer BadRequestException: Invalid position parameter, and the controller reads an explicitly empty position from the URI for each of them through one requestedPosition helper.
This commit is contained in:
1 parent
5746133527
commit
4e2bd570b5
3 files changed
+78
-34
No files matched your search
@@ -281,6 +281,11 @@ public class ApiGatewayController {
|
||||
return Pagination.parseMaxResults(limit, "limit", "BadRequestException");
|
||||
}
|
||||
|
||||
/** An empty query value binds as null, so an explicitly empty {@code position} is read from the URI. */
|
||||
private static String requestedPosition(String position, UriInfo uriInfo) {
|
||||
return position == null && uriInfo.getQueryParameters().containsKey("position") ? "" : position;
|
||||
}
|
||||
|
||||
private <T> Response pageResponse(PaginatedResult<T> page, Function<T, JsonNode> toNode) {
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
@@ -294,10 +299,12 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/authorizers")
|
||||
public Response getAuthorizers(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
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, pageLimit(limit), position);
|
||||
PaginatedResult<io.floci.aws.services.apigateway.model.Authorizer> page =
|
||||
service.getAuthorizerPage(region, apiId, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, this::toAuthorizerNode);
|
||||
}
|
||||
|
||||
@@ -438,10 +445,7 @@ public class ApiGatewayController {
|
||||
public Response getRestApis(@Context HttpHeaders headers, @Context UriInfo uriInfo,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
// An empty query value binds as null, so presence is read from the URI.
|
||||
String requestedPosition = position == null && uriInfo.getQueryParameters().containsKey("position")
|
||||
? "" : position;
|
||||
return pageResponse(service.getRestApiPage(region, pageLimit(limit), requestedPosition),
|
||||
return pageResponse(service.getRestApiPage(region, pageLimit(limit), requestedPosition(position, uriInfo)),
|
||||
api -> toApiNode(region, api));
|
||||
}
|
||||
|
||||
@@ -475,10 +479,11 @@ public class ApiGatewayController {
|
||||
@Path("/restapis/{apiId}/resources")
|
||||
public Response getResources(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("embed") List<String> embed,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
boolean embedMethods = embed != null && embed.contains("methods");
|
||||
return pageResponse(service.getResourcePage(region, apiId, pageLimit(limit), position),
|
||||
return pageResponse(service.getResourcePage(region, apiId, pageLimit(limit), requestedPosition(position, uriInfo)),
|
||||
resource -> toResourceNode(resource, embedMethods));
|
||||
}
|
||||
|
||||
@@ -661,9 +666,10 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/deployments")
|
||||
public Response getDeployments(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
return pageResponse(service.getDeploymentsPage(region, apiId, limit, position), this::toDeploymentNode);
|
||||
return pageResponse(service.getDeploymentsPage(region, apiId, limit, requestedPosition(position, uriInfo)), this::toDeploymentNode);
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -908,9 +914,10 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/usageplans")
|
||||
public Response getUsagePlans(@Context HttpHeaders headers, @QueryParam("keyId") String keyId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<UsagePlan> page = service.getUsagePlanPage(region, keyId, pageLimit(limit), position);
|
||||
PaginatedResult<UsagePlan> page = service.getUsagePlanPage(region, keyId, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, p -> toUsagePlanNode(p, false));
|
||||
}
|
||||
|
||||
@@ -1029,10 +1036,11 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/usageplans/{usagePlanId}/keys")
|
||||
public Response getUsagePlanKeys(@Context HttpHeaders headers, @PathParam("usagePlanId") String usagePlanId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<UsagePlanKey> page =
|
||||
service.getUsagePlanKeyPage(region, usagePlanId, pageLimit(limit), position);
|
||||
service.getUsagePlanKeyPage(region, usagePlanId, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, this::toUsagePlanKeyNode);
|
||||
}
|
||||
|
||||
@@ -1135,9 +1143,11 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/requestvalidators")
|
||||
public Response getRequestValidators(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<RequestValidator> page = service.getRequestValidatorPage(region, apiId, pageLimit(limit), position);
|
||||
PaginatedResult<RequestValidator> page = service.getRequestValidatorPage(region, apiId, pageLimit(limit),
|
||||
requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, this::toRequestValidatorNode);
|
||||
}
|
||||
|
||||
@@ -1191,11 +1201,12 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/models")
|
||||
public Response getModels(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
// Model clashes with the imported API Gateway v2 model
|
||||
PaginatedResult<io.floci.aws.services.apigateway.model.Model> page =
|
||||
service.getModelPage(region, apiId, pageLimit(limit), position);
|
||||
service.getModelPage(region, apiId, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, this::toModelNode);
|
||||
}
|
||||
|
||||
@@ -1261,10 +1272,11 @@ public class ApiGatewayController {
|
||||
@QueryParam("type") String type, @QueryParam("name") String name,
|
||||
@QueryParam("path") String path,
|
||||
@QueryParam("locationStatus") String locationStatus,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<DocumentationPart> page = service.getDocumentationParts(region, apiId, type, name,
|
||||
path, locationStatus, pageLimit(limit), position);
|
||||
path, locationStatus, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, part -> objectMapper.valueToTree(part));
|
||||
}
|
||||
|
||||
@@ -1373,9 +1385,11 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/domainnames/{domainName}/basepathmappings")
|
||||
public Response getBasePathMappings(@Context HttpHeaders headers, @PathParam("domainName") String domainName,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<BasePathMapping> page = service.getBasePathMappingsPage(region, domainName, limit, position);
|
||||
PaginatedResult<BasePathMapping> page = service.getBasePathMappingsPage(region, domainName, limit,
|
||||
requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, this::toMappingNode);
|
||||
}
|
||||
|
||||
@@ -1650,11 +1664,12 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/vpclinks")
|
||||
public Response getV1VpcLinks(@Context HttpHeaders headers,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
// VpcLink clashes with the imported API Gateway v2 model
|
||||
PaginatedResult<io.floci.aws.services.apigateway.model.VpcLink> page =
|
||||
service.getVpcLinkPage(region, pageLimit(limit), position);
|
||||
service.getVpcLinkPage(region, pageLimit(limit), requestedPosition(position, uriInfo));
|
||||
return pageResponse(page, this::toV1VpcLinkNode);
|
||||
}
|
||||
|
||||
|
||||
@@ -527,11 +527,7 @@ public class ApiGatewayService implements ResourceProvider, Resettable, PassedRo
|
||||
return apiStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
/** AWS answers an explicitly empty {@code position}, and Floci a blank one, with a BadRequestException instead of the first page. */
|
||||
public PaginatedResult<RestApi> getRestApiPage(String region, Integer limit, String position) {
|
||||
if (position != null && position.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "Invalid position parameter", 400);
|
||||
}
|
||||
return pageById(getRestApis(region), RestApi::getId, limit, position);
|
||||
}
|
||||
|
||||
@@ -1324,14 +1320,15 @@ public class ApiGatewayService implements ResourceProvider, Resettable, PassedRo
|
||||
|
||||
/**
|
||||
* One page of a list, sorted by id here. The token is the id of the last item returned, and
|
||||
* an unrecognised token is a {@code BadRequestException}, as on AWS.
|
||||
* an unrecognised token is a {@code BadRequestException}, as on AWS. So is an explicitly empty
|
||||
* {@code position}, which AWS refuses instead of serving the first page, and a blank one.
|
||||
*/
|
||||
private static <T> PaginatedResult<T> pageById(List<T> items, Function<T, String> idOf, Integer limit,
|
||||
String position) {
|
||||
List<T> sorted = items.stream().sorted(Comparator.comparing(idOf)).toList();
|
||||
int pageSize = resolvePageSize(limit);
|
||||
int start = 0;
|
||||
if (position != null && !position.isBlank()) {
|
||||
if (position != null) {
|
||||
while (start < sorted.size() && !position.equals(idOf.apply(sorted.get(start)))) {
|
||||
start++;
|
||||
}
|
||||
|
||||
+36
-4
@@ -65,13 +65,45 @@ class ApiGatewayRestApiPagingIntegrationTest {
|
||||
|
||||
assertThrows(BadRequestException.class,
|
||||
() -> client.getRestApis(r -> r.limit(1).position("missing")));
|
||||
given().queryParam("position", "").when().get("/restapis").then().statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
given().queryParam("position", " ").when().get("/restapis").then().statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
} finally {
|
||||
created.forEach(id -> client.deleteRestApi(r -> r.restApiId(id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyCollectionRefusesAnEmptyOrBlankPosition() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String apiId = client.createRestApi(r -> r.name("BlankPositionApi")).id();
|
||||
String planId = client.createUsagePlan(r -> r.name("BlankPositionPlan")).id();
|
||||
String domainName = "blank-position.example.com";
|
||||
client.createDomainName(r -> r.domainName(domainName));
|
||||
try {
|
||||
List<String> collections = List.of(
|
||||
"/restapis",
|
||||
"/restapis/" + apiId + "/resources",
|
||||
"/restapis/" + apiId + "/deployments",
|
||||
"/restapis/" + apiId + "/authorizers",
|
||||
"/restapis/" + apiId + "/requestvalidators",
|
||||
"/restapis/" + apiId + "/models",
|
||||
"/restapis/" + apiId + "/documentation/parts",
|
||||
"/usageplans",
|
||||
"/usageplans/" + planId + "/keys",
|
||||
"/domainnames/" + domainName + "/basepathmappings",
|
||||
"/vpclinks");
|
||||
for (String collection : collections) {
|
||||
for (String position : List.of("", " ")) {
|
||||
given().queryParam("position", position).when().get(collection).then().statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("Invalid position parameter"));
|
||||
}
|
||||
given().when().get(collection).then().statusCode(200);
|
||||
}
|
||||
} finally {
|
||||
client.deleteDomainName(r -> r.domainName(domainName));
|
||||
client.deleteUsagePlan(r -> r.usagePlanId(planId));
|
||||
client.deleteRestApi(r -> r.restApiId(apiId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user