fix(secretsmanager): split the "all" filter value into words (#8388)

This commit is contained in:
Mahmoud Khalifa authored and GitHub committed 2026-11-14 23:33:40 -04:00
1 parent 6971626d82
commit d6cf46ba3d
3 files changed
+123 -6

No files matched your search

@@ -67,6 +67,8 @@ public class SecretsManagerService implements ResourceProvider, StoredResourceAr
private static final String AWS_MANAGED_KEY = "aws/secretsmanager";
private static final String PENDING_DELETION_STATE = "PendingDeletion";
private static final Pattern SECRET_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9/_+=.@-]+");
private static final Pattern WORD_BOUNDARY = Pattern.compile(
"[^\\p{Alnum}]+|(?<=\\p{Lower})(?=\\p{Upper})|(?<=\\p{Alpha})(?=\\p{Digit})");
public static final String SCHEDULED_DELETION_ERROR_MESSAGE =
"You can't perform this operation on the secret because it was marked for deletion.";
@@ -826,17 +828,45 @@ public class SecretsManagerService implements ResourceProvider, StoredResourceAr
case "owning-service" -> secret.getOwningService() != null
&& secret.getOwningService().startsWith(targetVal);
case "all" -> {
String lowerVal = targetVal.toLowerCase();
boolean nameMatch = secret.getName() != null && secret.getName().toLowerCase().startsWith(lowerVal);
boolean descMatch = secret.getDescription() != null && secret.getDescription().toLowerCase().startsWith(lowerVal);
boolean tagKeyMatch = secret.getTags() != null && secret.getTags().stream().anyMatch(t -> t.key() != null && t.key().toLowerCase().startsWith(lowerVal));
boolean tagValueMatch = secret.getTags() != null && secret.getTags().stream().anyMatch(t -> t.value() != null && t.value().toLowerCase().startsWith(lowerVal));
yield nameMatch || descMatch || tagKeyMatch || tagValueMatch;
List<String> words = splitWords(targetVal);
if (words.isEmpty()) {
// A value with no words is a prefix of every name, as it was before word splitting.
yield secret.getName() != null;
}
yield matchesAnyWord(secret.getName(), words)
|| matchesAnyWord(secret.getDescription(), words)
|| (secret.getTags() != null && secret.getTags().stream()
.anyMatch(t -> matchesAnyWord(t.key(), words) || matchesAnyWord(t.value(), words)));
}
default -> false;
};
}
private static List<String> splitWords(String text) {
List<String> words = new ArrayList<>();
for (String word : WORD_BOUNDARY.split(text)) {
if (!word.isEmpty()) {
words.add(word.toLowerCase());
}
}
return words;
}
private static boolean matchesAnyWord(String attribute, List<String> filterWords) {
if (attribute == null) {
return false;
}
List<String> attributeWords = splitWords(attribute);
for (String filterWord : filterWords) {
for (String attributeWord : attributeWords) {
if (attributeWord.startsWith(filterWord)) {
return true;
}
}
}
return false;
}
public record Filter(String key, List<String> values) {}
/**
@@ -1887,4 +1887,22 @@ class SecretsManagerJsonHandlerTest {
() -> handler.handle("CreateSecret", req, REGION));
assertThat(ex.getErrorCode(), is("InvalidParameterException"));
}
@Test
void listSecretsAllFilterSplitsValueIntoWords() {
ObjectNode createReq = MAPPER.createObjectNode();
createReq.put("Name", "database");
createReq.put("Description", "production credentials");
handler.handle("CreateSecret", createReq, REGION);
ObjectNode otherReq = MAPPER.createObjectNode();
otherReq.put("Name", "unrelated");
handler.handle("CreateSecret", otherReq, REGION);
ObjectNode listReq = MAPPER.createObjectNode();
listReq.putArray("Filters").addObject().put("Key", "all").putArray("Values").add("database production");
ObjectNode listed = (ObjectNode) handler.handle("ListSecrets", listReq, REGION).getEntity();
assertThat(listed.get("SecretList").size(), is(1));
assertThat(listed.get("SecretList").get(0).get("Name").asText(), is("database"));
}
}
@@ -1801,4 +1801,73 @@ class SecretsManagerServiceTest {
assertEquals(List.of("AWSCURRENT"), after.getVersions().get(current).getVersionStages());
assertEquals(current, after.getCurrentVersionId());
}
@Test
void listSecretsAllFilterSplitsValueIntoWords() {
service.createSecret("database", "v", null, "production credentials", null, null, REGION);
service.createSecret("unrelated", "v", null, "nothing here", null, null, REGION);
List<SecretsManagerService.Filter> filters =
List.of(new SecretsManagerService.Filter("all", List.of("Database PRODUCTION")));
List<Secret> found = service.listSecrets(REGION, filters);
assertEquals(List.of("database"), found.stream().map(Secret::getName).toList());
}
@Test
void listSecretsAllFilterMatchesWordPrefixInTagsAndNegates() {
service.createSecret("alpha", "v", null, null, null, List.of(new Secret.Tag("Team", "Platform")), REGION);
service.createSecret("beta", "v", null, null, null, null, REGION);
List<Secret> tagged = service.listSecrets(REGION,
List.of(new SecretsManagerService.Filter("all", List.of("zzz plat"))));
List<Secret> others = service.listSecrets(REGION,
List.of(new SecretsManagerService.Filter("all", List.of("!plat zzz"))));
assertEquals(List.of("alpha"), tagged.stream().map(Secret::getName).toList());
assertEquals(List.of("beta"), others.stream().map(Secret::getName).toList());
}
@Test
void batchGetSecretValueByFiltersAllFilterSplitsValueIntoWords() {
service.createSecret("database", "v1", null, "production credentials", null, null, REGION);
service.createSecret("unrelated", "v2", null, "nothing here", null, null, REGION);
List<SecretsManagerService.BatchSecretValue> found = service.batchGetSecretValueByFilters(
List.of(new SecretsManagerService.Filter("all", List.of("database production"))), REGION);
assertEquals(List.of("database"), found.stream().map(SecretsManagerService.BatchSecretValue::name).toList());
}
@Test
void allFilterKeepsANumberFollowedByLettersAsOneWord() {
service.createSecret("db2prod", "v1", null, null, null, null, REGION);
service.createSecret("prod2db", "v2", null, null, null, null, REGION);
List<Secret> found = service.listSecrets(REGION,
List.of(new SecretsManagerService.Filter("all", List.of("prod"))));
assertEquals(List.of("prod2db"), found.stream().map(Secret::getName).toList());
}
@Test
void allFilterWithEmptyValueMatchesEverySecretAndItsNegationMatchesNone() {
service.createSecret("alpha", "v1", null, null, null, null, REGION);
service.createSecret("beta", "v2", null, "some description", null, null, REGION);
List<Secret> listedEmpty = service.listSecrets(REGION,
List.of(new SecretsManagerService.Filter("all", List.of(""))));
List<Secret> listedNegated = service.listSecrets(REGION,
List.of(new SecretsManagerService.Filter("all", List.of("!"))));
List<SecretsManagerService.BatchSecretValue> batchEmpty = service.batchGetSecretValueByFilters(
List.of(new SecretsManagerService.Filter("all", List.of(""))), REGION);
List<SecretsManagerService.BatchSecretValue> batchNegated = service.batchGetSecretValueByFilters(
List.of(new SecretsManagerService.Filter("all", List.of("!"))), REGION);
assertEquals(List.of("alpha", "beta"), listedEmpty.stream().map(Secret::getName).sorted().toList());
assertEquals(List.of(), listedNegated.stream().map(Secret::getName).toList());
assertEquals(List.of("alpha", "beta"),
batchEmpty.stream().map(SecretsManagerService.BatchSecretValue::name).sorted().toList());
assertEquals(List.of(), batchNegated.stream().map(SecretsManagerService.BatchSecretValue::name).toList());
}
}