From ddad6ddeb91e785b5e48eee74188d9e46535040e Mon Sep 17 00:00:00 2001 From: Jimmy Liu Jia <91264074+JimmyLiuJia@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:22:27 +0800 Subject: [PATCH] fix(cloudwatch): paginate ListDashboards results (#7789) * fix(cloudwatch): paginate ListDashboards results * fix(cloudwatch): reject empty ListDashboards tokens --- .../CloudWatchDashboardsService.java | 15 +++ .../metrics/CloudWatchMetricsJsonHandler.java | 8 +- .../CloudWatchMetricsQueryHandler.java | 9 +- .../CloudWatchDashboardsIntegrationTest.java | 109 +++++++++++++++++- 4 files changed, 131 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsService.java b/src/main/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsService.java index fee2ffc7bd..ab528d1c49 100644 --- a/src/main/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsService.java +++ b/src/main/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsService.java @@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.ObjectReader; import io.floci.aws.core.common.AwsArnUtils; import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.AwsRegions; +import io.floci.aws.core.common.PaginatedResult; +import io.floci.aws.core.common.Pagination; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.storage.StorageBackend; import io.floci.aws.core.storage.StorageFactory; @@ -142,6 +144,19 @@ public class CloudWatchDashboardsService { return all; } + public PaginatedResult listDashboardsPage(String dashboardNamePrefix, + String nextToken, String region) { + if (nextToken != null && nextToken.isEmpty()) { + throw new AwsException("InvalidParameterValue", "Invalid NextToken.", 400); + } + String prefix = dashboardNamePrefix == null || dashboardNamePrefix.isBlank() + ? "" : dashboardNamePrefix; + String scope = "cloudwatch-dashboards:" + regionResolver.getAccountId() + ":" + region + ":" + prefix; + return Pagination.paginate(listDashboards(prefix, region), Dashboard::getDashboardName, + 1000, nextToken, scope, + token -> new AwsException("InvalidParameterValue", "Invalid NextToken.", 400)); + } + /** * Deletes as many of the named dashboards as it can, then reports the first name that was not * there. AWS documents this as best effort, verbatim: "If there is an error during this call, diff --git a/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsJsonHandler.java b/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsJsonHandler.java index 8573aa355c..9811c47632 100644 --- a/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsJsonHandler.java +++ b/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsJsonHandler.java @@ -621,17 +621,21 @@ public class CloudWatchMetricsJsonHandler { private Response handleListDashboards(JsonNode request, String region) { String prefix = request.has("DashboardNamePrefix") ? request.path("DashboardNamePrefix").asText() : null; - List dashboards = dashboardsService.listDashboards(prefix, region); + PaginatedResult page = dashboardsService.listDashboardsPage(prefix, + Pagination.parseNextToken(request, "NextToken", "InvalidParameterValue"), region); ObjectNode response = objectMapper.createObjectNode(); ArrayNode entries = response.putArray("DashboardEntries"); - for (Dashboard d : dashboards) { + for (Dashboard d : page.items()) { ObjectNode node = entries.addObject(); node.put("DashboardName", d.getDashboardName()); node.put("DashboardArn", d.getDashboardArn()); node.put("LastModified", d.getLastModified()); node.put("Size", d.getSize()); } + if (page.nextToken() != null) { + response.put("NextToken", page.nextToken()); + } return Response.ok(response).build(); } diff --git a/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java b/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java index 45c8601dd8..e13948be9e 100644 --- a/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java +++ b/src/main/java/io/floci/aws/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java @@ -456,12 +456,12 @@ public class CloudWatchMetricsQueryHandler { } private Response handleListDashboards(MultivaluedMap params, String region) { - List dashboards = - dashboardsService.listDashboards(params.getFirst("DashboardNamePrefix"), region); + PaginatedResult page = dashboardsService.listDashboardsPage( + params.getFirst("DashboardNamePrefix"), params.getFirst("NextToken"), region); DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT; XmlBuilder xml = new XmlBuilder().start("DashboardEntries"); - for (Dashboard d : dashboards) { + for (Dashboard d : page.items()) { xml.start("member") .elem("DashboardName", d.getDashboardName()) .elem("DashboardArn", d.getDashboardArn()) @@ -470,6 +470,9 @@ public class CloudWatchMetricsQueryHandler { .end("member"); } xml.end("DashboardEntries"); + if (page.nextToken() != null) { + xml.elem("NextToken", page.nextToken()); + } return Response.ok(AwsQueryResponse.envelope("ListDashboards", null, xml.build())).build(); } diff --git a/src/test/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsIntegrationTest.java index ad3bbb8b60..00a41c0c32 100644 --- a/src/test/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudwatch/dashboards/CloudWatchDashboardsIntegrationTest.java @@ -5,15 +5,23 @@ import io.restassured.RestAssured; import io.restassured.config.EncoderConfig; import io.restassured.http.ContentType; import io.restassured.parsing.Parser; +import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; +import jakarta.inject.Inject; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * The dashboard operations reach the emulator over the Query protocol - that is what the @@ -23,6 +31,9 @@ import static org.hamcrest.Matchers.not; @QuarkusTest class CloudWatchDashboardsIntegrationTest { + @Inject + CloudWatchDashboardsService dashboardsService; + private static final String CW_SCOPE = "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/monitoring/aws4_request"; @@ -43,6 +54,20 @@ class CloudWatchDashboardsIntegrationTest { .body(body); } + private static RequestSpecification queryListDashboards(String prefix, String nextToken) { + RequestSpecification request = given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CW_SCOPE) + .formParam("Action", "ListDashboards"); + if (prefix != null) { + request.formParam("DashboardNamePrefix", prefix); + } + if (nextToken != null) { + request.formParam("NextToken", nextToken); + } + return request; + } + private static final String BODY = "{\"widgets\":[{\"type\":\"metric\",\"properties\":{\"title\":\"a > b\"}}]}"; @@ -98,11 +123,7 @@ class CloudWatchDashboardsIntegrationTest { putDashboard("prefixed-two", BODY); putDashboard("unprefixed", BODY); - given() - .contentType("application/x-www-form-urlencoded") - .header("Authorization", CW_SCOPE) - .formParam("Action", "ListDashboards") - .formParam("DashboardNamePrefix", "prefixed-") + queryListDashboards("prefixed-", null) .when() .post("/") .then() @@ -113,6 +134,84 @@ class CloudWatchDashboardsIntegrationTest { not(hasItem("unprefixed"))); } + @Test + void listDashboardsReturnsAtMostOneThousandEntriesAndAContinuationToken() { + List names = new ArrayList<>(); + try { + for (int i = 0; i < 1001; i++) { + String name = "pagination-issue-" + i; + dashboardsService.putDashboard(name, BODY, "us-east-1"); + names.add(name); + } + + Response response = json("ListDashboards", "{\"DashboardNamePrefix\":\"pagination-issue-\"}") + .when().post("/"); + response.then().statusCode(200); + assertEquals(1000, response.jsonPath().getList("DashboardEntries").size()); + String nextToken = response.jsonPath().getString("NextToken"); + assertNotNull(nextToken); + + Response queryFirstPage = queryListDashboards("pagination-issue-", null) + .when().post("/"); + queryFirstPage.then().statusCode(200); + assertEquals(1000, queryFirstPage.xmlPath().getList( + "ListDashboardsResponse.ListDashboardsResult.DashboardEntries.member").size()); + assertNotNull(queryFirstPage.xmlPath().getString( + "ListDashboardsResponse.ListDashboardsResult.NextToken")); + + Response secondPage = json("ListDashboards", + "{\"DashboardNamePrefix\":\"pagination-issue-\",\"NextToken\":\"" + nextToken + "\"}") + .when().post("/"); + secondPage.then().statusCode(200); + assertEquals(1, secondPage.jsonPath().getList("DashboardEntries").size()); + assertNull(secondPage.jsonPath().getString("NextToken")); + + queryListDashboards("pagination-issue-", nextToken) + .when().post("/") + .then().statusCode(200) + .body("ListDashboardsResponse.ListDashboardsResult.DashboardEntries.member.DashboardName", + equalTo(secondPage.jsonPath().getString("DashboardEntries[0].DashboardName"))); + + json("ListDashboards", "{\"DashboardNamePrefix\":\"other-\",\"NextToken\":\"" + + nextToken + "\"}") + .when().post("/") + .then().statusCode(400); + json("ListDashboards", "{\"NextToken\":\"not-a-token\"}") + .when().post("/") + .then().statusCode(400); + json("ListDashboards", "{\"DashboardNamePrefix\":\"pagination-issue-\",\"NextToken\":\"" + + nextToken + "\"}") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=210987654321/20260205/us-east-1/monitoring/aws4_request") + .when().post("/") + .then().statusCode(400); + json("ListDashboards", "{\"DashboardNamePrefix\":\"pagination-issue-\",\"NextToken\":\"" + + nextToken + "\"}") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=test/20260205/us-west-2/monitoring/aws4_request") + .when().post("/") + .then().statusCode(400); + queryListDashboards(null, "not-a-token") + .when().post("/") + .then().statusCode(400); + } finally { + dashboardsService.deleteDashboards(names, "us-east-1"); + } + } + + @Test + void listDashboardsRejectsExplicitlyEmptyContinuationTokens() { + json("ListDashboards", "{\"NextToken\":\"\"}") + .when().post("/") + .then().statusCode(400) + .body("__type", equalTo("InvalidParameterValue")); + + queryListDashboards(null, "") + .when().post("/") + .then().statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue")); + } + @Test void deleteDashboardsRemovesEveryNamedDashboard() { putDashboard("delete-a", BODY);