fix(athena): paginate ListWorkGroups (#6519)

* fix(athena): paginate ListWorkGroups

* fix(athena): use stable workgroup pagination cursors
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-25 13:23:36 -04:00
1 parent 57889bbb45
commit 57257e198f
3 files changed
+238 -1

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.athena;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.services.athena.model.CreateWorkGroupRequest;
import io.github.hectorvent.floci.services.athena.model.QueryExecution;
import io.github.hectorvent.floci.services.athena.model.QueryExecutionContext;
@@ -73,7 +74,21 @@ public class AthenaJsonHandler {
String name = request.has("WorkGroup") ? request.get("WorkGroup").asText() : "primary";
yield Response.ok(Map.of("WorkGroup", athenaService.getWorkGroup(name, region))).build();
}
case "ListWorkGroups" -> Response.ok(Map.of("WorkGroups", athenaService.listWorkGroups(region))).build();
case "ListWorkGroups" -> {
JsonNode maxResults = request.get("MaxResults");
if (maxResults != null && (!maxResults.isIntegralNumber() || !maxResults.canConvertToInt())) {
throw new AwsException("InvalidRequestException", "Invalid MaxResults", 400);
}
int pageSize = maxResults == null ? 50 : maxResults.asInt();
String nextToken = request.hasNonNull("NextToken") ? request.get("NextToken").asText() : null;
PaginatedResult<Map<String, Object>> page = athenaService.listWorkGroupsPage(region, pageSize, nextToken);
Map<String, Object> response = new LinkedHashMap<>();
response.put("WorkGroups", page.items());
if (page.nextToken() != null) {
response.put("NextToken", page.nextToken());
}
yield Response.ok(response).build();
}
case "CreateWorkGroup" -> {
CreateWorkGroupRequest createRequest = mapper.treeToValue(request, CreateWorkGroupRequest.class);
athenaService.createWorkGroup(createRequest, region);
@@ -5,6 +5,8 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.CsvParser;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.athena.model.CreateWorkGroupConfigurationRequest;
@@ -363,6 +365,20 @@ public class AthenaService {
return workGroups;
}
public PaginatedResult<Map<String, Object>> listWorkGroupsPage(String region, int maxResults, String nextToken) {
if (maxResults < 1 || maxResults > 50) {
throw new AwsException("InvalidRequestException", "MaxResults must be between 1 and 50", 400);
}
if (nextToken != null && nextToken.isEmpty()) {
throw new AwsException("InvalidRequestException", "Invalid NextToken", 400);
}
return Pagination.paginate(listWorkGroups(region), workGroup -> {
String name = (String) workGroup.get("Name");
return DEFAULT_WORKGROUP.equals(name) ? "0" : "1" + name;
}, maxResults, nextToken, "ListWorkGroups:" + region,
token -> new AwsException("InvalidRequestException", "Invalid NextToken", 400));
}
/**
* The account's built-in Glue catalog first, then the catalogs registered through
* {@code CreateDataCatalog}, sorted by name.
@@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@QuarkusTest
class AthenaCreateWorkGroupIntegrationTest {
@@ -101,6 +103,210 @@ class AthenaCreateWorkGroupIntegrationTest {
.body("WorkGroups.State", contains("ENABLED", "ENABLED"));
}
@Test
void listWorkGroupsPaginatesWithNextToken() {
String authorization = "AWS4-HMAC-SHA256 Credential=test/20261025/ap-south-2/athena/aws4_request";
for (String name : new String[] {"pagination-one", "pagination-two"}) {
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.CreateWorkGroup")
.contentType(CONTENT_TYPE)
.body("{\"Name\":\"" + name + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
}
String token = given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":1}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("primary"))
.body("NextToken", notNullValue())
.extract().path("NextToken");
token = given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":1,\"NextToken\":\"" + token + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("pagination-one"))
.body("NextToken", notNullValue())
.extract().path("NextToken");
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":1,\"NextToken\":\"" + token + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("pagination-two"))
.body("NextToken", nullValue());
}
@Test
void listWorkGroupsDoesNotRepeatEntriesInsertedBeforeCursor() {
String authorization = "AWS4-HMAC-SHA256 Credential=test/20261025/eu-central-2/athena/aws4_request";
for (String name : new String[] {"cursor-b", "cursor-c"}) {
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.CreateWorkGroup")
.contentType(CONTENT_TYPE)
.body("{\"Name\":\"" + name + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
}
String token = given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":2}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("primary", "cursor-b"))
.extract().path("NextToken");
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.CreateWorkGroup")
.contentType(CONTENT_TYPE)
.body("{\"Name\":\"cursor-a\"}")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":2,\"NextToken\":\"" + token + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("cursor-c"))
.body("NextToken", nullValue());
}
@Test
void listWorkGroupsDoesNotSkipEntriesAfterDeletion() {
String authorization = "AWS4-HMAC-SHA256 Credential=test/20261025/sa-east-1/athena/aws4_request";
for (String name : new String[] {"deleted-a", "deleted-b"}) {
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.CreateWorkGroup")
.contentType(CONTENT_TYPE)
.body("{\"Name\":\"" + name + "\"}")
.when()
.post("/")
.then()
.statusCode(200);
}
String token = given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":2}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("primary", "deleted-a"))
.extract().path("NextToken");
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.DeleteWorkGroup")
.contentType(CONTENT_TYPE)
.body("{\"WorkGroup\":\"deleted-a\"}")
.when()
.post("/")
.then()
.statusCode(200);
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":2,\"NextToken\":\"" + token + "\"}")
.when()
.post("/")
.then()
.statusCode(200)
.body("WorkGroups.Name", contains("deleted-b"))
.body("NextToken", nullValue());
}
@Test
void listWorkGroupsRejectsInvalidPageSizes() {
for (String maxResults : new String[] {"0", "-1", "51", "1.5", "2147483648", "\"1\""}) {
given()
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":" + maxResults + "}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
}
}
@Test
void listWorkGroupsRejectsMalformedAndCrossRegionTokens() {
String authorization = "AWS4-HMAC-SHA256 Credential=test/20261025/eu-west-3/athena/aws4_request";
given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.CreateWorkGroup")
.contentType(CONTENT_TYPE)
.body("{\"Name\":\"token-source\"}")
.when().post("/").then().statusCode(200);
String token = given()
.header("Authorization", authorization)
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":1}")
.when()
.post("/")
.then()
.statusCode(200)
.body("NextToken", notNullValue())
.extract().path("NextToken");
for (String invalidToken : new String[] {"", "not-a-token!", token}) {
given()
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261025/ap-northeast-3/athena/aws4_request")
.header("X-Amz-Target", "AmazonAthena.ListWorkGroups")
.contentType(CONTENT_TYPE)
.body("{\"MaxResults\":1,\"NextToken\":\"" + invalidToken + "\"}")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
}
}
@Test
void createWorkGroupRejectsPrimaryWorkGroup() {
given()