use of software.amazon.awssdk.services.secretsmanager.model.ListSecretsResponse 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);
}
use of software.amazon.awssdk.services.secretsmanager.model.ListSecretsResponse in project aws-doc-sdk-examples by awsdocs.
the class ListSecrets method listAllSecrets.
// snippet-start:[secretsmanager.java2.list_secrets.main]
public static void listAllSecrets(SecretsManagerClient secretsClient) {
try {
ListSecretsResponse secretsResponse = secretsClient.listSecrets();
List<SecretListEntry> secrets = secretsResponse.secretList();
for (SecretListEntry secret : secrets) {
System.out.println("The secret name is " + secret.name());
System.out.println("The secret descreiption is " + secret.description());
}
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations