Search in sources :

Example 1 with ListSecretsRequest

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);
}
Also used : HashMap(java.util.HashMap) SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) ListSecretsRequest(software.amazon.awssdk.services.secretsmanager.model.ListSecretsRequest) ListSecretsResponse(software.amazon.awssdk.services.secretsmanager.model.ListSecretsResponse) SecretListEntry(software.amazon.awssdk.services.secretsmanager.model.SecretListEntry) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 NonNull (io.micronaut.core.annotation.NonNull)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ListSecretsRequest (software.amazon.awssdk.services.secretsmanager.model.ListSecretsRequest)1 ListSecretsResponse (software.amazon.awssdk.services.secretsmanager.model.ListSecretsResponse)1 SecretListEntry (software.amazon.awssdk.services.secretsmanager.model.SecretListEntry)1 SecretsManagerException (software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException)1