Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Allen Sanborn25a63eef9e56
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SsmTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SsmTest.java
index cc80d5f9ba..16b69d0af2 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SsmTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SsmTest.java
@@ -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());
+ }
+ }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java
index 663611ebf7..ec8e2ee173 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java
@@ -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();
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java
index 904f6a6c38..24ea4f09ab 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java
@@ -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";
diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmParameterPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmParameterPaginationIntegrationTest.java
new file mode 100644
index 0000000000..94e5e111bb
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmParameterPaginationIntegrationTest.java
@@ -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");
+ }
+}