Search in sources :

Example 1 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException 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)

Example 2 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.

the class CreateSecret method createNewSecret.

// snippet-start:[secretsmanager.java2.create_secret.main]
public static String createNewSecret(SecretsManagerClient secretsClient, String secretName, String secretValue) {
    try {
        CreateSecretRequest secretRequest = CreateSecretRequest.builder().name(secretName).description("This secret was created by the AWS Secret Manager Java API").secretString(secretValue).build();
        CreateSecretResponse secretResponse = secretsClient.createSecret(secretRequest);
        return secretResponse.arn();
    } catch (SecretsManagerException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) CreateSecretRequest(software.amazon.awssdk.services.secretsmanager.model.CreateSecretRequest) CreateSecretResponse(software.amazon.awssdk.services.secretsmanager.model.CreateSecretResponse)

Example 3 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.

the class DescribeSecret method describeGivenSecret.

// snippet-start:[secretsmanager.java2.describe_secret.main]
public static void describeGivenSecret(SecretsManagerClient secretsClient, String secretName) {
    try {
        DescribeSecretRequest secretRequest = DescribeSecretRequest.builder().secretId(secretName).build();
        DescribeSecretResponse secretResponse = secretsClient.describeSecret(secretRequest);
        Instant lastChangedDate = secretResponse.lastChangedDate();
        // Convert the Instant to readable date
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.US).withZone(ZoneId.systemDefault());
        formatter.format(lastChangedDate);
        System.out.println("The date of the last change to " + secretResponse.name() + " is " + lastChangedDate);
    } catch (SecretsManagerException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) Instant(java.time.Instant) DescribeSecretRequest(software.amazon.awssdk.services.secretsmanager.model.DescribeSecretRequest) DateTimeFormatter(java.time.format.DateTimeFormatter) DescribeSecretResponse(software.amazon.awssdk.services.secretsmanager.model.DescribeSecretResponse)

Example 4 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.

the class UpdateSecret method updateMySecret.

// snippet-start:[secretsmanager.java2.update_secret.main]
public static void updateMySecret(SecretsManagerClient secretsClient, String secretName, String secretValue) {
    try {
        UpdateSecretRequest secretRequest = UpdateSecretRequest.builder().secretId(secretName).secretString(secretValue).build();
        secretsClient.updateSecret(secretRequest);
    } catch (SecretsManagerException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) UpdateSecretRequest(software.amazon.awssdk.services.secretsmanager.model.UpdateSecretRequest)

Example 5 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project aws-doc-sdk-examples by awsdocs.

the class GetSecretValue method getValue.

// snippet-start:[secretsmanager.java2.get_secret.main]
public static void getValue(SecretsManagerClient secretsClient, String secretName) {
    try {
        GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
        GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
        String secret = valueResponse.secretString();
        System.out.println(secret);
    } catch (SecretsManagerException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse)

Aggregations

SecretsManagerException (software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException)13 GetSecretValueRequest (software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest)4 GetSecretValueResponse (software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 SecretsManagerClient (software.amazon.awssdk.services.secretsmanager.SecretsManagerClient)2 ListSecretsResponse (software.amazon.awssdk.services.secretsmanager.model.ListSecretsResponse)2 SecretListEntry (software.amazon.awssdk.services.secretsmanager.model.SecretListEntry)2 UpdateSecretRequest (software.amazon.awssdk.services.secretsmanager.model.UpdateSecretRequest)2 NonNull (io.micronaut.core.annotation.NonNull)1 Instant (java.time.Instant)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RetryPolicy (net.jodah.failsafe.RetryPolicy)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1 CreateSecretRequest (software.amazon.awssdk.services.secretsmanager.model.CreateSecretRequest)1 CreateSecretResponse (software.amazon.awssdk.services.secretsmanager.model.CreateSecretResponse)1 DeleteSecretRequest (software.amazon.awssdk.services.secretsmanager.model.DeleteSecretRequest)1