fix(ssm): page GetParametersByPath and GetParameterHistory with MaxResults and NextToken (#6268)

* fix(ssm): page GetParametersByPath and GetParameterHistory with MaxResults and NextToken

Both actions ignored MaxResults and NextToken and returned every item.
Page them with Pagination.paginate, as DescribeParameters already does.

MaxResults is validated against the SSM model: 1-10 for
GetParametersByPath and 1-50 for GetParameterHistory, rejected with
ValidationException like DescribeParameters. A bad NextToken is
InvalidNextToken. Omitting MaxResults still returns everything, so
callers that never follow NextToken keep working.

History is paged by a zero-padded version cursor so version 10 sorts
after version 9.

Closes #6254

* fix(ssm): reject wrongly typed MaxResults and NextToken when paging parameters

A string MaxResults or a numeric NextToken was coerced instead of failing
to deserialize. Read both through the SSM deserialization helpers so they
fail with SerializationException, as other SSM fields do.

* fix(ssm): refuse a parameter page token minted for another listing

GetParametersByPath and GetParameterHistory tokens were bare cursors, so a
token from another operation, path or parameter was read as a position
in this listing and could skip results or return an empty page. Bind
each token to its operation, region and selection (path and Recursive,
or parameter name) and answer a mismatch with InvalidNextToken.

* test(ssm): follow GetParametersByPath and GetParameterHistory pages with SDK paginators

* fix(ssm): read pagination fields before listing parameters or their history

GetParameterHistory looked the parameter up before reading MaxResults and
NextToken, so a missing parameter answered ParameterNotFound for a request
whose pagination fields failed to deserialize or validate. Read and
validate both fields first in GetParameterHistory and GetParametersByPath.

* fix(ssm): refuse path and history page tokens in DescribeParameters

DescribeParameters decodes its token as a bare name cursor, so a
GetParametersByPath or GetParameterHistory token was read as a name and
could return a truncated or empty page. Refuse a token that decodes to
either namespace with InvalidNextToken. Its own tokens are unaffected:
the namespaces contain a colon, which AWS does not allow in a name.

* fix(ssm): bind parameter page tokens to the requesting account

Parameter storage is partitioned by account, but GetParametersByPath and
GetParameterHistory tokens named only the region and selection, so a token
minted in one account was accepted in another and skipped its entries.
Add the request account to both token namespaces.

* style(ssm): keep each javadoc on the method it describes

* fix(ssm): page DescribeParameters through the shared paging helpers

DescribeParameters repeated the MaxResults range check word for word and
decoded its token a second time to refuse GetParametersByPath and
GetParameterHistory tokens. Route it through pageRequest and page under a
namespace of its own, keeping its default page of 50. Its tokens are now
bound to the account and region like the other two listings, and a
wrongly typed MaxResults or NextToken fails to deserialize as theirs do.

Also cover resuming GetParametersByPath after the parameter that ended
the previous page is deleted.

---------

Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
Allen SanbornandPriamo authored and GitHub committed 2026-10-24 14:27:22 -04:00
1 parent 8d25cd91a6
commit 25a63eef9e
4 files changed
+361 -16

No files matched your search

@@ -42,6 +42,7 @@ import software.amazon.awssdk.services.ssm.model.UnlabelParameterVersionResponse
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.assertj.core.api.Assertions.*;
@@ -398,4 +399,32 @@ class SsmTest {
ssm.deleteParameter(DeleteParameterRequest.builder().name(name).build());
}
}
@Test
void paginatorsFollowNextTokenForPathAndHistory() {
String path = "/sdk-test/paging-" + UUID.randomUUID();
List<String> names = List.of(path + "/p1", path + "/p2", path + "/p3", path + "/p4", path + "/p5");
try {
names.forEach(name -> ssm.putParameter(PutParameterRequest.builder()
.name(name).value("v1").type(ParameterType.STRING).overwrite(true).build()));
for (int version = 2; version <= 3; version++) {
ssm.putParameter(PutParameterRequest.builder()
.name(names.get(0)).value("v" + version).type(ParameterType.STRING).overwrite(true).build());
}
List<GetParametersByPathResponse> pathPages = ssm.getParametersByPathPaginator(
GetParametersByPathRequest.builder().path(path).maxResults(2).build()).stream().toList();
assertThat(pathPages).hasSize(3).allSatisfy(page -> assertThat(page.parameters()).hasSizeLessThanOrEqualTo(2));
assertThat(pathPages.stream().flatMap(page -> page.parameters().stream()).map(p -> p.name()).toList())
.containsExactlyInAnyOrderElementsOf(names);
List<GetParameterHistoryResponse> historyPages = ssm.getParameterHistoryPaginator(
GetParameterHistoryRequest.builder().name(names.get(0)).maxResults(1).build()).stream().toList();
assertThat(historyPages).hasSize(3).allSatisfy(page -> assertThat(page.parameters()).hasSize(1));
assertThat(historyPages.stream().flatMap(page -> page.parameters().stream()).map(p -> p.version())
.toList()).containsExactly(1L, 2L, 3L);
} finally {
ssm.deleteParameters(DeleteParametersRequest.builder().names(names).build());
}
}
}
@@ -36,6 +36,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import static io.github.hectorvent.floci.services.ssm.SsmJsonDeserialization.serializationException;
@@ -229,6 +230,7 @@ public class SsmJsonHandler {
}
private Response handleGetParametersByPath(JsonNode request, String region) {
PageRequest pageRequest = pageRequest(request, GET_PARAMETERS_BY_PATH_MAX_RESULTS);
String path = request.path("Path").asText();
boolean recursive = request.path("Recursive").asBoolean(false);
@@ -236,10 +238,14 @@ public class SsmJsonHandler {
ObjectNode response = objectMapper.createObjectNode();
ArrayNode parametersArray = objectMapper.createArrayNode();
for (Parameter p : params) {
PaginatedResult<Parameter> page = page(pageRequest, params, Parameter::getName, ALL_ITEMS,
GET_PARAMETERS_BY_PATH_TOKEN + ssmService.accountId() + ":" + region + ":" + recursive + ":"
+ (path.endsWith("/") ? path : path + "/"));
for (Parameter p : page.items()) {
parametersArray.add(parameterToNode(p));
}
response.set("Parameters", parametersArray);
putNextToken(response, page);
return Response.ok(response).build();
}
@@ -273,12 +279,17 @@ public class SsmJsonHandler {
}
private Response handleGetParameterHistory(JsonNode request, String region) {
PageRequest pageRequest = pageRequest(request, GET_PARAMETER_HISTORY_MAX_RESULTS);
String name = request.path("Name").asText();
List<ParameterHistory> history = ssmService.getParameterHistory(name, region);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode historyArray = objectMapper.createArrayNode();
for (ParameterHistory h : history) {
// Zero-padded so the cursor's string order is version order ("10" would sort before "9").
PaginatedResult<ParameterHistory> page = page(pageRequest, history,
h -> String.format("%019d", h.getVersion()), ALL_ITEMS,
GET_PARAMETER_HISTORY_TOKEN + ssmService.accountId() + ":" + region + ":" + name);
for (ParameterHistory h : page.items()) {
ObjectNode node = objectMapper.createObjectNode();
node.put("Name", h.getName());
node.put("Version", h.getVersion());
@@ -296,10 +307,57 @@ public class SsmJsonHandler {
historyArray.add(node);
}
response.set("Parameters", historyArray);
putNextToken(response, page);
return Response.ok(response).build();
}
private static final int DESCRIBE_PARAMETERS_MAX_RESULTS = 50;
private static final int GET_PARAMETERS_BY_PATH_MAX_RESULTS = 10;
private static final int GET_PARAMETER_HISTORY_MAX_RESULTS = 50;
private static final int ALL_ITEMS = Integer.MAX_VALUE;
private static final String DESCRIBE_PARAMETERS_TOKEN = "DescribeParameters:";
private static final String GET_PARAMETERS_BY_PATH_TOKEN = "GetParametersByPath:";
private static final String GET_PARAMETER_HISTORY_TOKEN = "GetParameterHistory:";
private record PageRequest(Integer maxResults, String nextToken) {
}
/**
* Reads {@code MaxResults}/{@code NextToken} before the operation runs, so a malformed field is
* reported as such rather than masked by, say, {@code ParameterNotFound}. A non-numeric
* {@code MaxResults} or non-string {@code NextToken} fails to deserialize, as on AWS.
*/
private static PageRequest pageRequest(JsonNode request, int maxPage) {
Integer maxResults = request.hasNonNull("MaxResults")
? SsmJsonDeserialization.optionalInteger(request.path("MaxResults"), 0) : null;
if (maxResults != null && (maxResults < 1 || maxResults > maxPage)) {
throw SmithyValidationErrors.validationException(
"Value '" + maxResults + "' at 'maxResults' failed to "
+ "satisfy constraint: Member must have value between 1 and " + maxPage);
}
return new PageRequest(maxResults, optionalString(request, "NextToken"));
}
/**
* Pages a listing, returning {@code defaultPage} items when {@code MaxResults} is omitted.
* GetParametersByPath and GetParameterHistory pass {@link #ALL_ITEMS} rather than AWS's single
* page, so callers that never follow {@code NextToken} still see every item. A token carries
* {@code namespace}, so one minted for another operation, account, region, path or parameter is refused
* instead of read as a position here.
*/
private static <T> PaginatedResult<T> page(PageRequest pageRequest, List<T> items,
Function<T, String> cursorOf, int defaultPage, String namespace) {
Integer maxResults = pageRequest.maxResults();
return Pagination.paginate(items, cursorOf, maxResults != null ? maxResults : defaultPage,
pageRequest.nextToken(), namespace,
token -> new AwsException("InvalidNextToken", "The specified token isn't valid.", 400));
}
private static void putNextToken(ObjectNode response, PaginatedResult<?> page) {
if (page.nextToken() != null) {
response.put("NextToken", page.nextToken());
}
}
private Response handleDescribeParameters(JsonNode request, String region) {
List<ParameterStringFilter> filters = parameterFilters(request);
@@ -312,17 +370,10 @@ public class SsmJsonHandler {
filters.add(new ParameterStringFilter(key, "Equals", textValues(f.path("Values"))));
}
Integer maxResults = request.hasNonNull("MaxResults") ? request.path("MaxResults").asInt() : null;
if (maxResults != null && (maxResults < 1 || maxResults > DESCRIBE_PARAMETERS_MAX_RESULTS)) {
throw SmithyValidationErrors.validationException(
"Value '" + maxResults + "' at 'maxResults' failed to "
+ "satisfy constraint: Member must have value between 1 and "
+ DESCRIBE_PARAMETERS_MAX_RESULTS);
}
PaginatedResult<Parameter> page = Pagination.paginate(
ssmService.describeParameters(filters, region), Parameter::getName,
maxResults, request.path("NextToken").asText(null),
DESCRIBE_PARAMETERS_MAX_RESULTS, "InvalidNextToken");
PageRequest pageRequest = pageRequest(request, DESCRIBE_PARAMETERS_MAX_RESULTS);
PaginatedResult<Parameter> page = page(pageRequest, ssmService.describeParameters(filters, region),
Parameter::getName, DESCRIBE_PARAMETERS_MAX_RESULTS,
DESCRIBE_PARAMETERS_TOKEN + ssmService.accountId() + ":" + region);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode parametersArray = objectMapper.createArrayNode();
@@ -356,9 +407,7 @@ public class SsmJsonHandler {
parametersArray.add(node);
}
response.set("Parameters", parametersArray);
if (page.nextToken() != null) {
response.put("NextToken", page.nextToken());
}
putNextToken(response, page);
return Response.ok(response).build();
}
@@ -733,6 +733,11 @@ public class SsmService implements ResourceProvider {
return actual != null && matchesAny(actual, filter);
}
/** The account the current request acts in; parameter storage is partitioned by it. */
String accountId() {
return regionResolver.getAccountId();
}
/** Parameters stored before Floci kept a tier are Standard, as AWS defaults them. */
static String tierOf(Parameter parameter) {
return parameter.getTier() != null ? parameter.getTier() : "Standard";
@@ -0,0 +1,262 @@
package io.github.hectorvent.floci.services.ssm;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@QuarkusTest
class SsmParameterPaginationIntegrationTest {
private static final String SSM_CONTENT_TYPE = "application/x-amz-json-1.1";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void getParametersByPathPagesWithMaxResultsAndNextToken() {
String path = "/pagination-" + UUID.randomUUID();
List<String> names = IntStream.rangeClosed(1, 5).mapToObj(i -> path + "/p" + i).toList();
names.forEach(name -> put(name, "v"));
List<String> seen = new ArrayList<>();
List<Integer> pageSizes = new ArrayList<>();
String token = null;
do {
Map<String, Object> request = new HashMap<>(Map.of("Path", path, "MaxResults", 2));
if (token != null) {
request.put("NextToken", token);
}
JsonPath page = ok("GetParametersByPath", request);
List<String> pageNames = page.getList("Parameters.Name", String.class);
pageSizes.add(pageNames.size());
seen.addAll(pageNames);
token = page.getString("NextToken");
} while (token != null);
assertEquals(List.of(2, 2, 1), pageSizes);
assertEquals(names, seen);
}
@Test
void getParametersByPathResumesAfterTheLastItemOfAPageIsDeleted() {
String path = "/pagination-" + UUID.randomUUID();
IntStream.rangeClosed(1, 4).forEach(i -> put(path + "/p" + i, "v"));
String token = ok("GetParametersByPath", Map.of("Path", path, "MaxResults", 2)).getString("NextToken");
ok("DeleteParameter", Map.of("Name", path + "/p2"));
assertEquals(List.of(path + "/p3", path + "/p4"), ok("GetParametersByPath",
Map.of("Path", path, "MaxResults", 2, "NextToken", token)).getList("Parameters.Name", String.class));
}
@Test
void getParametersByPathWithoutMaxResultsReturnsEverything() {
String path = "/pagination-" + UUID.randomUUID();
IntStream.rangeClosed(1, 12).forEach(i -> put(path + "/p" + i, "v"));
JsonPath response = ok("GetParametersByPath", Map.of("Path", path));
assertEquals(12, response.getList("Parameters").size());
assertNull(response.getString("NextToken"));
}
@Test
void getParametersByPathRejectsOutOfRangeMaxResultsAndBadTokens() {
error("GetParametersByPath", Map.of("Path", "/pagination", "MaxResults", 0), "ValidationException");
error("GetParametersByPath", Map.of("Path", "/pagination", "MaxResults", 11), "ValidationException");
error("GetParametersByPath", Map.of("Path", "/pagination", "NextToken", "%%%"), "InvalidNextToken");
}
@Test
void getParameterHistoryPagesInVersionOrder() {
String name = "/pagination-" + UUID.randomUUID() + "/history";
// The test profile keeps the last five versions (7 to 11), so a cursor compared as text
// would put versions 10 and 11 before 7.
IntStream.rangeClosed(1, 11).forEach(i -> put(name, "v" + i));
List<Long> versions = new ArrayList<>();
List<Integer> pageSizes = new ArrayList<>();
String token = null;
do {
Map<String, Object> request = new HashMap<>(Map.of("Name", name, "MaxResults", 2));
if (token != null) {
request.put("NextToken", token);
}
JsonPath page = ok("GetParameterHistory", request);
List<Long> pageVersions = page.getList("Parameters.Version", Long.class);
pageSizes.add(pageVersions.size());
versions.addAll(pageVersions);
token = page.getString("NextToken");
} while (token != null);
assertEquals(List.of(2, 2, 1), pageSizes);
assertEquals(LongStream.rangeClosed(7, 11).boxed().toList(), versions);
JsonPath all = ok("GetParameterHistory", Map.of("Name", name));
assertEquals(5, all.getList("Parameters").size());
assertNull(all.getString("NextToken"));
}
@Test
void getParameterHistoryRejectsOutOfRangeMaxResults() {
String name = "/pagination-" + UUID.randomUUID() + "/history";
put(name, "v");
error("GetParameterHistory", Map.of("Name", name, "MaxResults", 0), "ValidationException");
error("GetParameterHistory", Map.of("Name", name, "MaxResults", 51), "ValidationException");
error("GetParameterHistory", Map.of("Name", name, "NextToken", "%%%"), "InvalidNextToken");
}
@Test
void refusesATokenMintedForAnotherListing() {
String root = "/pagination-" + UUID.randomUUID();
IntStream.rangeClosed(1, 3).forEach(i -> {
put(root + "/a/p" + i, "v");
put(root + "/b/p" + i, "v");
});
String first = root + "/a/p1";
String second = root + "/b/p1";
put(first, "v2");
put(second, "v2");
String pathToken = ok("GetParametersByPath", Map.of("Path", root + "/a", "MaxResults", 1))
.getString("NextToken");
String historyToken = ok("GetParameterHistory", Map.of("Name", first, "MaxResults", 1))
.getString("NextToken");
String describeToken = ok("DescribeParameters", Map.of("ParameterFilters", nameBeginsWith(root),
"MaxResults", 1)).getString("NextToken");
error("GetParametersByPath", Map.of("Path", root + "/b", "NextToken", pathToken), "InvalidNextToken");
error("GetParametersByPath", Map.of("Path", root + "/a", "Recursive", true, "NextToken", pathToken),
"InvalidNextToken");
error("GetParametersByPath", Map.of("Path", root + "/a", "NextToken", historyToken), "InvalidNextToken");
error("GetParameterHistory", Map.of("Name", second, "NextToken", historyToken), "InvalidNextToken");
error("GetParameterHistory", Map.of("Name", first, "NextToken", pathToken), "InvalidNextToken");
error("DescribeParameters", Map.of("NextToken", pathToken), "InvalidNextToken");
error("DescribeParameters", Map.of("NextToken", historyToken), "InvalidNextToken");
error("GetParametersByPath", Map.of("Path", root + "/a", "NextToken", describeToken), "InvalidNextToken");
error("GetParameterHistory", Map.of("Name", first, "NextToken", describeToken), "InvalidNextToken");
// The token still resumes the listing it came from, with or without a trailing slash.
assertEquals(List.of(root + "/a/p2"), ok("GetParametersByPath",
Map.of("Path", root + "/a/", "MaxResults", 1, "NextToken", pathToken))
.getList("Parameters.Name", String.class));
}
@Test
void refusesATokenMintedInAnotherAccount() {
String issuer = "000000000301";
String other = "000000000302";
String path = "/pagination-" + UUID.randomUUID();
for (String account : List.of(issuer, other)) {
IntStream.rangeClosed(1, 3).forEach(i -> put(account, path + "/p" + i, "v"));
put(account, path + "/p1", "v2");
}
String pathToken = ok(issuer, "GetParametersByPath", Map.of("Path", path, "MaxResults", 1))
.getString("NextToken");
String historyToken = ok(issuer, "GetParameterHistory", Map.of("Name", path + "/p1", "MaxResults", 1))
.getString("NextToken");
String describeToken = ok(issuer, "DescribeParameters", Map.of("ParameterFilters", nameBeginsWith(path),
"MaxResults", 1)).getString("NextToken");
error(other, "GetParametersByPath", Map.of("Path", path, "MaxResults", 1, "NextToken", pathToken),
"InvalidNextToken");
error(other, "GetParameterHistory", Map.of("Name", path + "/p1", "MaxResults", 1, "NextToken", historyToken),
"InvalidNextToken");
error(other, "DescribeParameters", Map.of("ParameterFilters", nameBeginsWith(path), "MaxResults", 1,
"NextToken", describeToken), "InvalidNextToken");
assertEquals(List.of(path + "/p2"), ok(issuer, "GetParametersByPath",
Map.of("Path", path, "MaxResults", 1, "NextToken", pathToken)).getList("Parameters.Name", String.class));
assertEquals(List.of(2L), ok(issuer, "GetParameterHistory",
Map.of("Name", path + "/p1", "MaxResults", 1, "NextToken", historyToken))
.getList("Parameters.Version", Long.class));
assertEquals(List.of(path + "/p2"), ok(issuer, "DescribeParameters",
Map.of("ParameterFilters", nameBeginsWith(path), "MaxResults", 1, "NextToken", describeToken))
.getList("Parameters.Name", String.class));
}
@Test
void rejectsWronglyTypedPaginationFields() {
String name = "/pagination-" + UUID.randomUUID() + "/history";
put(name, "v");
error("GetParametersByPath", Map.of("Path", "/pagination", "MaxResults", "2"), "SerializationException");
error("GetParametersByPath", Map.of("Path", "/pagination", "NextToken", 5), "SerializationException");
error("GetParameterHistory", Map.of("Name", name, "MaxResults", "2"), "SerializationException");
error("GetParameterHistory", Map.of("Name", name, "NextToken", 5), "SerializationException");
error("DescribeParameters", Map.of("MaxResults", "2"), "SerializationException");
error("DescribeParameters", Map.of("NextToken", 5), "SerializationException");
}
@Test
void reportsMalformedPaginationFieldsBeforeAMissingParameter() {
String missing = "/pagination-" + UUID.randomUUID() + "/missing";
error("GetParameterHistory", Map.of("Name", missing, "MaxResults", "2"), "SerializationException");
error("GetParameterHistory", Map.of("Name", missing, "NextToken", 5), "SerializationException");
error("GetParameterHistory", Map.of("Name", missing, "MaxResults", 51), "ValidationException");
}
private static List<Map<String, Object>> nameBeginsWith(String prefix) {
return List.of(Map.of("Key", "Name", "Option", "BeginsWith", "Values", List.of(prefix)));
}
private void put(String name, String value) {
put(null, name, value);
}
private void put(String account, String name, String value) {
ok(account, "PutParameter", Map.of("Name", name, "Value", value, "Type", "String", "Overwrite", true));
}
private JsonPath ok(String action, Map<String, ?> body) {
return ok(null, action, body);
}
private JsonPath ok(String account, String action, Map<String, ?> body) {
return request(account, action, body)
.when()
.post("/")
.then()
.statusCode(200)
.extract().jsonPath();
}
private void error(String action, Map<String, ?> body, String errorType) {
error(null, action, body, errorType);
}
private void error(String account, String action, Map<String, ?> body, String errorType) {
request(account, action, body)
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo(errorType));
}
/** A null account sends no Authorization header, so the request acts in the default account. */
private static RequestSpecification request(String account, String action, Map<String, ?> body) {
RequestSpecification spec = given()
.header("X-Amz-Target", "AmazonSSM." + action)
.contentType(SSM_CONTENT_TYPE)
.body(body);
return account == null ? spec : spec.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account
+ "/20260215/us-east-1/ssm/aws4_request, SignedHeaders=host, Signature=abc");
}
}