Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(ssm): honor path parameter type filters (#6183)

Aniket Shuklaa9ceee1de61f
diff --git a/docs/services/ssm.md b/docs/services/ssm.md
index ac6f30c8b8..1df2df14cd 100644
--- a/docs/services/ssm.md
+++ b/docs/services/ssm.md
@@ -25,6 +25,13 @@
| `DescribePatchBaselines` | List AWS-owned predefined patch baselines (filter by `OWNER`, `OPERATING_SYSTEM`, `NAME_PREFIX`) |
| `GetDefaultPatchBaseline` | Get the default patch baseline id for an operating system |
+`GetParametersByPath` supports `Type` and `KeyId` entries in `ParameterFilters`, with `Equals`
+(the default) or `BeginsWith`. Values within one filter combine with OR; multiple
+filters combine with AND. Filtering also applies to public AMI parameters and
+respects `Path`, `Recursive`, and region selection. Unknown or missing keys and keys
+not allowed for this action are rejected with `InvalidFilterKey`.
+`Label` filters accept only `Equals`, but label matching and pagination are not currently modeled.
+
### Run Command
| Action | Description |
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 0b53d57921..4a1d433479 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
@@ -232,7 +232,7 @@ public class SsmJsonHandler {
String path = request.path("Path").asText();
boolean recursive = request.path("Recursive").asBoolean(false);
- List<Parameter> params = ssmService.getParametersByPath(path, recursive, region);
+ List<Parameter> params = ssmService.getParametersByPath(path, recursive, parameterFilters(request), region);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode parametersArray = objectMapper.createArrayNode();
@@ -302,11 +302,7 @@ public class SsmJsonHandler {
private static final int DESCRIBE_PARAMETERS_MAX_RESULTS = 50;
private Response handleDescribeParameters(JsonNode request, String region) {
- 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"))));
- }
+ List<ParameterStringFilter> filters = parameterFilters(request);
// 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);
@@ -371,6 +367,15 @@ public class SsmJsonHandler {
return text.isEmpty() ? null : text;
}
+ private static List<ParameterStringFilter> parameterFilters(JsonNode request) {
+ List<ParameterStringFilter> filters = new ArrayList<>();
+ for (JsonNode filter : request.path("ParameterFilters")) {
+ filters.add(new ParameterStringFilter(filter.path("Key").asText(null),
+ filter.path("Option").asText(null), textValues(filter.path("Values"))));
+ }
+ return filters;
+ }
+
private static List<String> textValues(JsonNode values) {
List<String> result = new ArrayList<>();
values.forEach(v -> result.add(v.asText()));
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 3c29deb7d7..904f6a6c38 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
@@ -56,6 +56,7 @@ public class SsmService implements ResourceProvider {
// 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 Set<String> GET_PARAMETERS_BY_PATH_FILTER_KEYS = Set.of("Type", "KeyId", "Label");
private static final String DEFAULT_SSM_KEY_ID = "alias/aws/ssm";
private static final int STANDARD_TIER_MAX_VALUE_BYTES = 4096;
private static final Set<String> PARAMETER_TIERS = Set.of("Standard", "Advanced", "Intelligent-Tiering");
@@ -561,6 +562,15 @@ public class SsmService implements ResourceProvider {
});
}
+ public List<Parameter> getParametersByPath(String path, boolean recursive,
+ List<ParameterStringFilter> filters, String region) {
+ filters.forEach(SsmService::validateGetParametersByPathFilter);
+ List<ParameterStringFilter> matchingFilters = filters.stream().filter(filter -> !"Label".equals(filter.key())).toList();
+ return getParametersByPath(path, recursive, region).stream()
+ .filter(parameter -> matchingFilters.stream().allMatch(filter -> matchesParameterFilter(parameter, filter)))
+ .toList();
+ }
+
public List<Parameter> getParametersByPath(String path, boolean recursive, String region) {
String normalizedPath = path.endsWith("/") ? path : path + "/";
String prefix = region + "::";
@@ -663,9 +673,7 @@ public class SsmService implements ResourceProvider {
: "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);
+ throw invalidFilterOption();
}
}
// Every key but a tag needs at least one value, and a path value must be absolute.
@@ -677,6 +685,30 @@ public class SsmService implements ResourceProvider {
}
}
+ private static void validateGetParametersByPathFilter(ParameterStringFilter filter) {
+ String key = filter.key();
+ if (key == null || !GET_PARAMETERS_BY_PATH_FILTER_KEYS.contains(key)) {
+ throw new AwsException("InvalidFilterKey", "The specified key isn't valid.", 400);
+ }
+ if (!"Label".equals(key)) {
+ validateDescribeParametersFilter(filter);
+ return;
+ }
+ if (filter.option() != null && !"Equals".equals(filter.option())) {
+ throw invalidFilterOption();
+ }
+ if (filter.values().isEmpty()) {
+ throw new AwsException("InvalidFilterValue",
+ "The filter value isn't valid. Verify the value and try again.", 400);
+ }
+ }
+
+ private static AwsException invalidFilterOption() {
+ return 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);
+ }
+
private static boolean matchesParameterFilter(Parameter parameter, ParameterStringFilter filter) {
String key = filter.key();
if (key.startsWith("tag:")) {
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.java
index db9eaa0f83..bf139148f6 100644
--- 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.java
@@ -3,8 +3,8 @@ 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.
+ * One {@code ParameterFilters} entry for a parameter listing (or a translated legacy
+ * {@code DescribeParameters.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/SsmPathTypeFilterIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmPathTypeFilterIntegrationTest.java
new file mode 100644
index 0000000000..637995908c
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmPathTypeFilterIntegrationTest.java
@@ -0,0 +1,210 @@
+package io.github.hectorvent.floci.services.ssm;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.stream.Stream;
+
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class SsmPathTypeFilterIntegrationTest {
+
+ private static final String REGION = "eu-west-3";
+ private final List<OwnedParameter> owned = new ArrayList<>();
+ private String path;
+
+ @BeforeAll
+ static void configureContentTypes() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ @BeforeEach
+ void createParameters() {
+ path = "/type-filter-" + UUID.randomUUID();
+ put("text", "String", REGION);
+ put("list", "StringList", REGION);
+ put("secret", "SecureString", REGION);
+ put("nested/text", "String", REGION);
+ put("foreign", "StringList", "ap-southeast-2");
+ }
+
+ @AfterEach
+ void deleteOwnedParameters() {
+ for (OwnedParameter parameter : owned) {
+ ssm("DeleteParameter", Map.of("Name", parameter.name()), parameter.region()).then().statusCode(200);
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("typeFilters")
+ void appliesTypeFiltersWithinTheRequestedPathAndRegion(List<Map<String, Object>> filters, List<String> expected) {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path, "ParameterFilters", filters), REGION)
+ .then().statusCode(200).extract().response();
+ assertNames(response, expected);
+ }
+
+ static Stream<Arguments> typeFilters() {
+ return Stream.of(
+ Arguments.of(List.of(filter(null, "StringList")), List.of("list")),
+ Arguments.of(List.of(filter("Equals", "String")), List.of("text")),
+ Arguments.of(List.of(filter("BeginsWith", "String")), List.of("list", "text")),
+ Arguments.of(List.of(filter("Equals", "StringList", "SecureString")), List.of("list", "secret")),
+ Arguments.of(List.of(filter("BeginsWith", "String"), filter("Equals", "StringList")), List.of("list")),
+ Arguments.of(List.of(filter("Equals", "String"), filter("Equals", "StringList")), List.of()));
+ }
+
+ @Test
+ void preservesUnfilteredOneLevelResults() {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path), REGION)
+ .then().statusCode(200).extract().response();
+ assertNames(response, List.of("list", "secret", "text"));
+ }
+
+ @Test
+ void combinesTypeWithRecursivePathSelection() {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path + "/", "Recursive", true,
+ "ParameterFilters", List.of(filter(null, "String"))), REGION)
+ .then().statusCode(200).extract().response();
+ assertNames(response, List.of("nested/text", "text"));
+ }
+
+ @Test
+ void rejectsAnInvalidTypeOptionEvenForAnEmptyPath() {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path + "/empty",
+ "ParameterFilters", List.of(filter("Contains", "String"))), REGION)
+ .then().statusCode(400).extract().response();
+ assertTrue(response.jsonPath().getString("__type").endsWith("InvalidFilterOption"));
+ }
+
+ @Test
+ void rejectsEmptyTypeValuesEvenForAnEmptyPath() {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path + "/empty",
+ "ParameterFilters", List.of(filter(null))), REGION)
+ .then().statusCode(400).extract().response();
+ assertTrue(response.jsonPath().getString("__type").endsWith("InvalidFilterValue"));
+ }
+
+ @ParameterizedTest
+ @MethodSource("keyIdFilters")
+ void appliesKeyIdAndCombinedFilters(List<Map<String, Object>> filters, List<String> expected) {
+ putKeyedParameter("key-one", "alias/key-one");
+ putKeyedParameter("key-two", "alias/key-two");
+ Response response = ssm("GetParametersByPath", Map.of("Path", path, "ParameterFilters", filters), REGION)
+ .then().statusCode(200).extract().response();
+ assertNames(response, expected);
+ }
+
+ static Stream<Arguments> keyIdFilters() {
+ return Stream.of(
+ Arguments.of(List.of(filterForKey("KeyId", null, "alias/aws/ssm")), List.of("secret")),
+ Arguments.of(List.of(filterForKey("KeyId", "Equals", "alias/key-one")), List.of("key-one")),
+ Arguments.of(List.of(filterForKey("KeyId", "BeginsWith", "alias/key-")), List.of("key-one", "key-two")),
+ Arguments.of(List.of(filterForKey("KeyId", null, "alias/aws/ssm", "alias/key-two")),
+ List.of("secret", "key-two")),
+ Arguments.of(List.of(filter(null, "String"), filterForKey("KeyId", null, "alias/key-one")), List.of()),
+ Arguments.of(List.of(filter(null, "SecureString"), filterForKey("KeyId", null, "alias/key-one")),
+ List.of("key-one")));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"Name", "Path", "Tier", "DataType", "tag:env", "Unknown", "missing", "null"})
+ void rejectsInvalidFilterKeysBeforeReturningPathResults(String key) {
+ Map<String, Object> invalidFilter = new HashMap<>();
+ if (!"missing".equals(key)) {
+ invalidFilter.put("Key", "null".equals(key) ? null : key);
+ }
+ invalidFilter.put("Values", List.of("text"));
+ for (String requestedPath : List.of(path, path + "/empty")) {
+ Response response = ssm("GetParametersByPath", Map.of("Path", requestedPath,
+ "ParameterFilters", List.of(invalidFilter)), REGION)
+ .then().statusCode(400).extract().response();
+ assertTrue(response.jsonPath().getString("__type").endsWith("InvalidFilterKey"));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"KeyId", "Label"})
+ void rejectsInvalidOptionsForOtherAllowedKeys(String key) {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path + "/empty",
+ "ParameterFilters", List.of(filterForKey(key, "Contains", "value"))), REGION)
+ .then().statusCode(400).extract().response();
+ assertTrue(response.jsonPath().getString("__type").endsWith("InvalidFilterOption"));
+ assertEquals("The specified filter option isn't valid. Valid options are Equals and BeginsWith. "
+ + "For Path filter, valid options are Recursive and OneLevel.",
+ response.jsonPath().getString("message"));
+ }
+
+ @Test
+ void rejectsBeginsWithForLabelFilters() {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path + "/empty",
+ "ParameterFilters", List.of(filterForKey("Label", "BeginsWith", "value"))), REGION)
+ .then().statusCode(400).extract().response();
+ assertTrue(response.jsonPath().getString("__type").endsWith("InvalidFilterOption"));
+ assertEquals("The specified filter option isn't valid. Valid options are Equals and BeginsWith. "
+ + "For Path filter, valid options are Recursive and OneLevel.",
+ response.jsonPath().getString("message"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"KeyId", "Label"})
+ void rejectsEmptyValuesForOtherAllowedKeys(String key) {
+ Response response = ssm("GetParametersByPath", Map.of("Path", path + "/empty",
+ "ParameterFilters", List.of(filterForKey(key, null))), REGION)
+ .then().statusCode(400).extract().response();
+ assertTrue(response.jsonPath().getString("__type").endsWith("InvalidFilterValue"));
+ }
+
+ private void putKeyedParameter(String suffix, String keyId) {
+ String name = path + "/" + suffix;
+ ssm("PutParameter", Map.of("Name", name, "Type", "SecureString", "Value", "value", "KeyId", keyId), REGION)
+ .then().statusCode(200);
+ owned.add(new OwnedParameter(name, REGION));
+ }
+
+ private static Map<String, Object> filter(String option, String... values) {
+ return filterForKey("Type", option, values);
+ }
+
+ private static Map<String, Object> filterForKey(String key, String option, String... values) {
+ return option == null ? Map.of("Key", key, "Values", List.of(values))
+ : Map.of("Key", key, "Option", option, "Values", List.of(values));
+ }
+
+ private void put(String suffix, String type, String region) {
+ String name = path + "/" + suffix;
+ ssm("PutParameter", Map.of("Name", name, "Type", type, "Value", "a,b"), region).then().statusCode(200);
+ owned.add(new OwnedParameter(name, region));
+ }
+
+ private void assertNames(Response response, List<String> suffixes) {
+ List<String> names = response.jsonPath().getList("Parameters.Name", String.class);
+ assertEquals(suffixes.stream().map(suffix -> path + "/" + suffix).sorted().toList(),
+ names.stream().sorted().toList());
+ }
+
+ private Response ssm(String action, Map<String, ?> request, String region) {
+ return given().contentType("application/x-amz-json-1.1")
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261022/" + region + "/ssm/aws4_request")
+ .header("X-Amz-Target", "AmazonSSM." + action).body(request).post("/");
+ }
+
+ private record OwnedParameter(String name, String region) {
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServicePublicAmiParameterTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServicePublicAmiParameterTest.java
index 95293453cc..7f5ba9a47e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServicePublicAmiParameterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmServicePublicAmiParameterTest.java
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
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.ParameterStringFilter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -110,6 +111,20 @@ class SsmServicePublicAmiParameterTest {
assertEquals(32, ssmService.getParametersByPath("/aws/service", true, REGION).size());
}
+ @Test
+ void appliesPathTypeFiltersToPublicParameters() {
+ assertEquals(32, ssmService.getParametersByPath("/aws/service", true,
+ List.of(new ParameterStringFilter("Type", null, List.of("String"))), REGION).size());
+ assertTrue(ssmService.getParametersByPath("/aws/service", true,
+ List.of(new ParameterStringFilter("Type", null, List.of("StringList"))), REGION).isEmpty());
+ }
+
+ @Test
+ void excludesPublicParametersFromKeyIdFilteredPaths() {
+ assertTrue(ssmService.getParametersByPath("/aws/service", true,
+ List.of(new ParameterStringFilter("KeyId", null, List.of("alias/aws/ssm"))), REGION).isEmpty());
+ }
+
@Test
void resolvesEksOptimizedAmiParametersAcrossArchitecturesAndVersions() {
String x86Param = "/aws/service/eks/optimized-ami/1.31/amazon-linux-2023/x86_64/standard/recommended/image_id";