use of software.amazon.awssdk.services.secretsmanager.model.ListSecretsRequest in project micronaut-aws by micronaut-projects.
the class SecretsManagerKeyValueFetcher method keyValuesByPrefix.
@Override
@NonNull
public Optional<Map> keyValuesByPrefix(@NonNull String prefix) {
Map result = new HashMap<>();
try {
String nextToken = null;
do {
ListSecretsRequest.Builder builder = ListSecretsRequest.builder().nextToken(nextToken).filters(Filter.builder().key(FilterNameStringType.NAME).values(prefix).build());
if (nextToken != null) {
builder = builder.nextToken(nextToken);
}
ListSecretsRequest listSecretsRequest = builder.build();
ListSecretsResponse secretsResponse = secretsClient.listSecrets(listSecretsRequest);
List<SecretListEntry> secrets = secretsResponse.secretList();
if (LOG.isTraceEnabled()) {
if (secrets.isEmpty()) {
LOG.trace("zero secrets for prefix: {}", prefix);
} else {
LOG.trace("# {} secrets for prefix: {}", secrets.size(), prefix);
}
}
for (SecretListEntry secret : secrets) {
if (LOG.isTraceEnabled()) {
LOG.trace("Evaluating secret {}", secret.name());
}
Optional<String> secretValueOptional = fetchSecretValue(secretsClient, secret.name());
if (secretValueOptional.isPresent()) {
try {
result.putAll(objectMapper.readValue(secretValueOptional.get(), Map.class));
} catch (JsonProcessingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("could not read secret ({}) value from JSON to Map", secret.name());
}
}
}
}
nextToken = secretsResponse.nextToken();
} while (nextToken != null);
} catch (SecretsManagerException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("SecretsManagerException {}", e.awsErrorDetails().errorMessage());
}
return Optional.empty();
}
return result.isEmpty() ? Optional.empty() : Optional.of(result);
}
Aggregations