Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ssm): apply every DescribeParameters filter and page the results (#3646)
Amogh Bhat Nagriea88947865cd
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.javaindex 4643262618..f561dc892e 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@@ -2,11 +2,14 @@ package io.github.hectorvent.floci.services.ssm;import io.github.hectorvent.floci.core.common.AwsErrorResponse;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.PaginatedResult;+import io.github.hectorvent.floci.core.common.Pagination;import io.github.hectorvent.floci.services.ssm.model.Command;import io.github.hectorvent.floci.services.ssm.model.CommandInvocation;import io.github.hectorvent.floci.services.ssm.model.InstanceInformation;import io.github.hectorvent.floci.services.ssm.model.Parameter;import io.github.hectorvent.floci.services.ssm.model.ParameterHistory;+import io.github.hectorvent.floci.services.ssm.model.ParameterStringFilter;import io.github.hectorvent.floci.services.ssm.model.PatchBaselineIdentity;import io.github.hectorvent.floci.services.ssm.model.ServiceSetting;import io.github.hectorvent.floci.services.ssm.model.SsmAssociation;@@ -221,23 +224,38 @@ public class SsmJsonHandler {return Response.ok(response).build();}+ private static final int DESCRIBE_PARAMETERS_MAX_RESULTS = 50;+private Response handleDescribeParameters(JsonNode request, String region) {- List<String> nameFilters = new ArrayList<>();- JsonNode filters = request.path("ParameterFilters");- if (filters.isArray()) {- for (JsonNode f : filters) {- String key = f.path("Key").asText("");- String option = f.path("Option").asText("Equals");- if ("Name".equals(key) && "Equals".equals(option)) {- f.path("Values").forEach(v -> nameFilters.add(v.asText()));- }+ List<ParameterStringFilter> filters = new ArrayList<>();+ for (JsonNode f : request.path("ParameterFilters")) {+ filters.add(new ParameterStringFilter(f.path("Key").asText(null),+ f.path("Option").asText(null), textValues(f.path("Values"))));+ }+ // The deprecated Filters shape has no option; each of its keys matches exactly.+ for (JsonNode f : request.path("Filters")) {+ String key = f.path("Key").asText(null);+ if (!"Name".equals(key) && !"Type".equals(key) && !"KeyId".equals(key)) {+ throw new AwsException("InvalidFilterKey", "The specified key isn't valid.", 400);}+ 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 new AwsException("ValidationException",+ "1 validation error detected: Value '" + maxResults + "' at 'maxResults' failed to "+ + "satisfy constraint: Member must have value between 1 and "+ + DESCRIBE_PARAMETERS_MAX_RESULTS, 400);}- List<Parameter> params = ssmService.describeParameters(nameFilters, region);+ PaginatedResult<Parameter> page = Pagination.paginate(+ ssmService.describeParameters(filters, region), Parameter::getName,+ maxResults, request.path("NextToken").asText(null),+ DESCRIBE_PARAMETERS_MAX_RESULTS, "InvalidNextToken");ObjectNode response = objectMapper.createObjectNode();ArrayNode parametersArray = objectMapper.createArrayNode();- for (Parameter p : params) {+ for (Parameter p : page.items()) {ObjectNode node = objectMapper.createObjectNode();node.put("Name", p.getName());node.put("Type", p.getType());@@ -250,9 +268,18 @@ public class SsmJsonHandler {parametersArray.add(node);}response.set("Parameters", parametersArray);+ if (page.nextToken() != null) {+ response.put("NextToken", page.nextToken());+ }return Response.ok(response).build();}+ private static List<String> textValues(JsonNode values) {+ List<String> result = new ArrayList<>();+ values.forEach(v -> result.add(v.asText()));+ return result;+ }+private Response handleDescribePatchBaselines(JsonNode request, String region) {Map<String, List<String>> filters = new HashMap<>();JsonNode filtersNode = request.path("Filters");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.javaindex c04db36240..7acc838456 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@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.ec2.Ec2ImageCatalog;import io.github.hectorvent.floci.services.ssm.model.Parameter;import io.github.hectorvent.floci.services.ssm.model.ParameterHistory;+import io.github.hectorvent.floci.services.ssm.model.ParameterStringFilter;import io.github.hectorvent.floci.services.ssm.model.PatchBaselineIdentity;import io.github.hectorvent.floci.services.ssm.model.ServiceSetting;import io.github.hectorvent.floci.services.ssm.model.SsmAssociation;@@ -33,6 +34,10 @@ public class SsmService implements ResourceProvider {private static final Logger LOG = Logger.getLogger(SsmService.class);private static final String PUBLIC_PARAMETER_PREFIX = "/aws/service/";+ // Label is a ParameterStringFilter key too, but only GetParametersByPath accepts it.+ private static final Set<String> DESCRIBE_PARAMETERS_FILTER_KEYS =+ Set.of("Name", "Type", "KeyId", "Path", "Tier", "DataType");+ private static final String DEFAULT_SSM_KEY_ID = "alias/aws/ssm";/*** Account-default values for the service settings floci models. AWS rejects@@ -329,13 +334,77 @@ public class SsmService implements ResourceProvider {return describeParameters(List.of(), region);}- public List<Parameter> describeParameters(List<String> nameFilters, String region) {+ /**+ * Lists the region's own parameters that pass every filter: filters combine with AND, the+ * values of one filter with OR. A key or option AWS rejects is an error rather than being+ * skipped, because dropping a filter silently widens the result.+ */+ public List<Parameter> describeParameters(List<ParameterStringFilter> filters, String region) {+ filters.forEach(SsmService::validateDescribeParametersFilter);String prefix = region + "::";- return parameterStore.scan(key -> {- if (!key.startsWith(prefix)) return false;- if (nameFilters.isEmpty()) return true;- String name = key.substring(prefix.length());- return nameFilters.contains(name);+ return parameterStore.scan(key -> key.startsWith(prefix)).stream()+ .filter(p -> filters.stream().allMatch(f -> matchesParameterFilter(p, f)))+ .toList();+ }++ private static void validateDescribeParametersFilter(ParameterStringFilter filter) {+ String key = filter.key();+ boolean tag = key != null && key.startsWith("tag:") && key.length() > "tag:".length();+ // Set.of rejects a null lookup, so an absent key is checked before the membership test.+ if (!tag && (key == null || !DESCRIBE_PARAMETERS_FILTER_KEYS.contains(key))) {+ throw new AwsException("InvalidFilterKey", "The specified key isn't valid.", 400);+ }+ if (filter.option() != null) {+ Set<String> options = "Path".equals(key) ? Set.of("Recursive", "OneLevel")+ : "Name".equals(key) ? Set.of("Equals", "BeginsWith", "Contains")+ : Set.of("Equals", "BeginsWith");+ if (!options.contains(filter.option())) {+ throw new AwsException("InvalidFilterOption",+ "The specified filter option isn't valid. Valid options are Equals and BeginsWith. "+ + "For Path filter, valid options are Recursive and OneLevel.", 400);+ }+ }+ // Every key but a tag needs at least one value, and a path value must be absolute.+ boolean invalidValues = !tag && filter.values().isEmpty()+ || "Path".equals(key) && filter.values().stream().anyMatch(v -> !v.startsWith("/"));+ if (invalidValues) {+ throw new AwsException("InvalidFilterValue",+ "The filter value isn't valid. Verify the value and try again.", 400);+ }+ }++ private static boolean matchesParameterFilter(Parameter parameter, ParameterStringFilter filter) {+ String key = filter.key();+ if (key.startsWith("tag:")) {+ String tagValue = parameter.getTags() == null ? null+ : parameter.getTags().get(key.substring("tag:".length()));+ // A tag filter without values matches any parameter carrying the tag key.+ return tagValue != null && (filter.values().isEmpty() || matchesAny(tagValue, filter));+ }+ if ("Path".equals(key)) {+ boolean recursive = "Recursive".equals(filter.option());+ return filter.values().stream().anyMatch(path ->+ underPath(parameter.getName(), path.endsWith("/") ? path : path + "/", recursive));+ }+ String actual = switch (key) {+ case "Name" -> parameter.getName();+ case "Type" -> parameter.getType();+ case "DataType" -> parameter.getDataType();+ // Floci stores neither a tier nor a customer key, so every parameter is Standard and+ // a SecureString is encrypted with the AWS managed key, as AWS defaults them.+ case "Tier" -> "Standard";+ case "KeyId" -> "SecureString".equals(parameter.getType()) ? DEFAULT_SSM_KEY_ID : null;+ default -> null;+ };+ return actual != null && matchesAny(actual, filter);+ }++ private static boolean matchesAny(String actual, ParameterStringFilter filter) {+ String option = filter.option() == null ? "Equals" : filter.option();+ return filter.values().stream().anyMatch(value -> switch (option) {+ case "BeginsWith" -> actual.startsWith(value);+ case "Contains" -> actual.contains(value);+ default -> actual.equals(value);});}diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/model/ParameterStringFilter.java b/src/main/java/io/github/hectorvent/floci/services/ssm/model/ParameterStringFilter.javanew file mode 100644index 0000000000..db9eaa0f83--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/model/ParameterStringFilter.java@@ -0,0 +1,10 @@+package io.github.hectorvent.floci.services.ssm.model;++import java.util.List;++/**+ * One entry of {@code DescribeParameters}' {@code ParameterFilters} (or a translated legacy+ * {@code Filters} entry). A null {@code option} means the caller omitted it.+ */+public record ParameterStringFilter(String key, String option, List<String> values) {+}diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.javaindex beee61f832..0fd219ce51 100644--- a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java@@ -1510,4 +1510,104 @@ class SsmIntegrationTest {.statusCode(400).body("__type", equalTo("ValidationException"));}++ @Test+ @Order(20)+ void describeParametersAppliesParameterFilters() {+ putFilterFixture("/dpf/prod/db", "String", "");+ putFilterFixture("/dpf/prod/api/key", "SecureString", "");+ putFilterFixture("/dpf/dev/db", "String", ", \"Tags\": [{\"Key\": \"Team\", \"Value\": \"core\"}]");++ describeParameters("""+ { "ParameterFilters": [{ "Key": "Path", "Values": ["/dpf/prod"] }] }+ """)+ .body("Parameters.Name", contains("/dpf/prod/db"));++ describeParameters("""+ { "ParameterFilters": [{ "Key": "Path", "Option": "Recursive", "Values": ["/dpf/prod"] }] }+ """)+ .body("Parameters.Name", containsInAnyOrder("/dpf/prod/db", "/dpf/prod/api/key"));++ describeParameters("""+ { "ParameterFilters": [+ { "Key": "Name", "Option": "BeginsWith", "Values": ["/dpf/"] },+ { "Key": "Type", "Values": ["SecureString"] }+ ] }+ """)+ .body("Parameters.Name", contains("/dpf/prod/api/key"));++ describeParameters("""+ { "ParameterFilters": [{ "Key": "tag:Team", "Values": ["core"] }] }+ """)+ .body("Parameters.Name", contains("/dpf/dev/db"));++ describeParameters("""+ { "Filters": [{ "Key": "Name", "Values": ["/dpf/dev/db"] }] }+ """)+ .body("Parameters.Name", contains("/dpf/dev/db"));+ }++ @Test+ @Order(21)+ void describeParametersPagesWithMaxResultsAndNextToken() {+ String filter = "\"ParameterFilters\": [{ \"Key\": \"Name\", \"Option\": \"BeginsWith\", \"Values\": [\"/dpf/\"] }]";++ String token = describeParameters("{ " + filter + ", \"MaxResults\": 2 }")+ .body("Parameters.Name", contains("/dpf/dev/db", "/dpf/prod/api/key"))+ .body("NextToken", notNullValue())+ .extract().path("NextToken");++ describeParameters("{ " + filter + ", \"MaxResults\": 2, \"NextToken\": \"" + token + "\" }")+ .body("Parameters.Name", contains("/dpf/prod/db"))+ .body("NextToken", nullValue());+ }++ @Test+ @Order(22)+ void describeParametersRejectsUnsupportedFilters() {+ describeParametersError("""+ { "ParameterFilters": [{ "Key": "Label", "Values": ["prod"] }] }+ """, "InvalidFilterKey");+ describeParametersError("""+ { "ParameterFilters": [{ "Key": "Type", "Option": "Contains", "Values": ["String"] }] }+ """, "InvalidFilterOption");+ describeParametersError("""+ { "ParameterFilters": [{ "Key": "Path", "Values": ["dpf"] }] }+ """, "InvalidFilterValue");+ describeParametersError("{ \"MaxResults\": 51 }", "ValidationException");+ }++ private void putFilterFixture(String name, String type, String extra) {+ given()+ .header("X-Amz-Target", "AmazonSSM.PutParameter")+ .contentType(SSM_CONTENT_TYPE)+ .body("{ \"Name\": \"" + name + "\", \"Value\": \"v\", \"Type\": \"" + type + "\"" + extra + " }")+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ private io.restassured.response.ValidatableResponse describeParameters(String body) {+ return given()+ .header("X-Amz-Target", "AmazonSSM.DescribeParameters")+ .contentType(SSM_CONTENT_TYPE)+ .body(body)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ private void describeParametersError(String body, String errorType) {+ given()+ .header("X-Amz-Target", "AmazonSSM.DescribeParameters")+ .contentType(SSM_CONTENT_TYPE)+ .body(body)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo(errorType));+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServiceTest.javaindex 3641b4c864..3875d4daa1 100644--- a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServiceTest.java@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.core.storage.StorageBackend;import io.github.hectorvent.floci.services.ssm.model.Parameter;import io.github.hectorvent.floci.services.ssm.model.ParameterHistory;+import io.github.hectorvent.floci.services.ssm.model.ParameterStringFilter;import io.github.hectorvent.floci.services.ssm.model.ServiceSetting;import io.github.hectorvent.floci.services.ssm.model.SsmAssociation;import io.github.hectorvent.floci.services.ssm.model.SsmDocument;@@ -912,4 +913,78 @@ class SsmServiceTest {ssmService.putParameter("/app/desc-test", "val2", "String", null, true, null, region);assertNull(ssmService.getParameter("/app/desc-test", region).getDescription());}++ @Test+ void describeParametersWithoutFiltersListsOnlyTheRegion() {+ ssmService.putParameter("/a", "v", "String", null, false, "us-east-1");+ ssmService.putParameter("/b", "v", "String", null, false, "eu-west-1");++ assertEquals(List.of("/a"), describedNames(List.of(), "us-east-1"));+ }++ @Test+ void describeParametersPathDefaultsToOneLevel() {+ String region = "us-east-1";+ ssmService.putParameter("/app/a", "v", "String", null, false, region);+ ssmService.putParameter("/app/nested/b", "v", "String", null, false, region);+ ssmService.putParameter("/application/c", "v", "String", null, false, region);++ assertEquals(Set.of("/app/a"),+ Set.copyOf(describedNames(List.of(filter("Path", null, "/app/")), region)));+ assertEquals(Set.of("/app/a", "/app/nested/b"),+ Set.copyOf(describedNames(List.of(filter("Path", "Recursive", "/app")), region)));+ }++ @Test+ void describeParametersMatchesNameOptionsAndOrsValues() {+ String region = "us-east-1";+ ssmService.putParameter("/svc/orders/url", "v", "String", null, false, region);+ ssmService.putParameter("/svc/users/url", "v", "String", null, false, region);+ ssmService.putParameter("/other", "v", "String", null, false, region);++ assertEquals(Set.of("/svc/orders/url", "/svc/users/url"),+ Set.copyOf(describedNames(List.of(filter("Name", "Contains", "/url")), region)));+ assertEquals(Set.of("/svc/orders/url", "/other"),+ Set.copyOf(describedNames(List.of(filter("Name", "Equals", "/svc/orders/url", "/other")), region)));+ }++ @Test+ void describeParametersMatchesTypeKeyIdTierDataTypeAndTags() {+ String region = "us-east-1";+ ssmService.putParameter("/plain", "v", "String", null, false, region);+ ssmService.putParameter("/secret", "v", "SecureString", null, false, Map.of("Env", "prod"), region);++ assertEquals(List.of("/secret"), describedNames(List.of(filter("Type", null, "SecureString")), region));+ assertEquals(List.of("/secret"), describedNames(List.of(filter("KeyId", null, "alias/aws/ssm")), region));+ assertEquals(2, describedNames(List.of(filter("Tier", null, "Standard")), region).size());+ assertEquals(2, describedNames(List.of(filter("DataType", null, "text")), region).size());+ assertEquals(List.of("/secret"), describedNames(List.of(filter("tag:Env", "BeginsWith", "pr")), region));+ assertEquals(List.of("/secret"),+ describedNames(List.of(new ParameterStringFilter("tag:Env", null, List.of())), region));+ assertTrue(describedNames(List.of(filter("tag:Env", null, "dev")), region).isEmpty());+ }++ @Test+ void describeParametersRejectsInvalidFilters() {+ assertFilterError("InvalidFilterKey", filter("Label", null, "prod"));+ assertFilterError("InvalidFilterKey", filter("tag:", null, "x"));+ assertFilterError("InvalidFilterOption", filter("Tier", "Contains", "Standard"));+ assertFilterError("InvalidFilterOption", filter("Path", "Equals", "/app"));+ assertFilterError("InvalidFilterValue", filter("Path", null, "app"));+ assertFilterError("InvalidFilterValue", new ParameterStringFilter("Type", null, List.of()));+ }++ private List<String> describedNames(List<ParameterStringFilter> filters, String region) {+ return ssmService.describeParameters(filters, region).stream().map(Parameter::getName).toList();+ }++ private static ParameterStringFilter filter(String key, String option, String... values) {+ return new ParameterStringFilter(key, option, List.of(values));+ }++ private void assertFilterError(String errorCode, ParameterStringFilter filter) {+ AwsException ex = assertThrows(AwsException.class, () ->+ ssmService.describeParameters(List.of(filter), "us-east-1"));+ assertEquals(errorCode, ex.getErrorCode());+ }}