mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudwatch): paginate ListDashboards results (#7789)
* fix(cloudwatch): paginate ListDashboards results * fix(cloudwatch): reject empty ListDashboards tokens
This commit is contained in:
1 parent
c9fc00a8d5
commit
ddad6ddeb9
4 files changed
+131
-10
No files matched your search
+15
@@ -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<Dashboard> 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,
|
||||
|
||||
+6
-2
@@ -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<Dashboard> dashboards = dashboardsService.listDashboards(prefix, region);
|
||||
PaginatedResult<Dashboard> 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();
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -456,12 +456,12 @@ public class CloudWatchMetricsQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleListDashboards(MultivaluedMap<String, String> params, String region) {
|
||||
List<Dashboard> dashboards =
|
||||
dashboardsService.listDashboards(params.getFirst("DashboardNamePrefix"), region);
|
||||
PaginatedResult<Dashboard> 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();
|
||||
}
|
||||
|
||||
|
||||
+104
-5
@@ -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<String> 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);
|
||||
|
||||
Reference in new issue
Block a user