fix(ssm): apply every DescribeParameters filter and page the results (#3646)

DescribeParameters honoured only Key=Name, Option=Equals and silently
dropped every other ParameterFilters entry. With no Name/Equals filter
left it returned every parameter in the region, so a Path, Type or
Name BeginsWith filter widened the result instead of narrowing it.

The filters now follow the ParameterStringFilter reference: Name, Type,
KeyId, Path, Tier, DataType and tag:<key>, with Equals and BeginsWith
(Name also Contains, Path OneLevel and Recursive). Filters combine with
AND and a filter's values with OR. The deprecated Filters shape matches
Name, Type and KeyId exactly. An unsupported key, option or value is
InvalidFilterKey, InvalidFilterOption or InvalidFilterValue instead of
being ignored, and MaxResults/NextToken page through the shared cursor.

Closes #3642
This commit is contained in:
Amogh Bhat Nagri authored and GitHub committed 2026-09-14 22:49:15 -04:00
1 parent 190c556940
commit ea88947865
5 files changed
+299 -18

No files matched your search

@@ -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));
}
}
@@ -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());
}
}