Search in sources :

Example 11 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project thunder by RohanNagar.

the class SecretsManagerSecretProvider method lookup.

/**
 * Gets the secret value from AWS secrets manager. If there is an {@link SdkClientException}
 * when connecting to Secrets Manager, this method will retry lookup {@code maxRetries} number
 * of times, each after a {@code retryDelaySeconds} period of time.
 *
 * @param name the name of the secret to fetch
 * @return the value of the secret if it exists, otherwise null
 */
@Override
public String lookup(String name) {
    if (secretsClient == null) {
        initializeClient();
    }
    GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(name).build();
    // Set up a retry policy to retry fetching secrets when unable to connect.
    RetryPolicy<Object> retryPolicy = new RetryPolicy<>().handle(SdkClientException.class).withDelay(Duration.ofSeconds(retryDelaySeconds)).withMaxRetries(maxRetries).onFailedAttempt(e -> LOG.error("Unable to connect to AWS Secrets Manager. Retrying after 30 seconds...", e.getLastFailure()));
    try {
        GetSecretValueResponse valueResponse = Failsafe.with(retryPolicy).get(() -> secretsClient.getSecretValue(valueRequest));
        return valueResponse.secretString();
    } catch (SecretsManagerException e) {
        LOG.error("Secret {} could not be read from AWS Secrets Manager", name, e);
        return null;
    }
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 12 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project radixdlt by radixdlt.

the class AWSSecretManager method updateAWSSecret.

public static void updateAWSSecret(Map<String, Object> awsSecret, String secretName, AWSSecretsOutputOptions awsSecretsOutputOptions, boolean compress, boolean binarySecret) {
    ObjectMapper objectMapper = new ObjectMapper();
    if (canBeUpdated(awsSecretsOutputOptions)) {
        System.out.format("Secret %s exists. And it's going to be replaced %n", secretName);
        try {
            String jsonSecret = objectMapper.writeValueAsString(awsSecret);
            if (compress) {
                byte[] compressedBytes = compressData(jsonSecret);
                updateBinarySecret(secretName, SdkBytes.fromByteArray(compressedBytes));
            } else {
                if (binarySecret) {
                    updateBinarySecret(secretName, SdkBytes.fromByteArray((byte[]) awsSecret.get("key")));
                } else {
                    updateSecret(secretName, jsonSecret);
                }
            }
        } catch (JsonProcessingException e) {
            System.out.println(e);
        } catch (SecretsManagerException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        } catch (IOException e) {
            System.out.println(e);
            System.exit(1);
        }
    } else {
        System.out.format("Secret %s exists. It will not be created again %n", secretName);
    }
}
Also used : SecretsManagerException(software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 with SecretsManagerException

use of software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException in project signers by ConsenSys.

the class AwsSecretsManager method fetchSecret.

public Optional<String> fetchSecret(final String secretName) {
    try {
        final GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
        final GetSecretValueResponse valueResponse = secretsManagerClient.getSecretValue(getSecretValueRequest);
        return Optional.of(valueResponse.secretString());
    } catch (final ResourceNotFoundException e) {
        return Optional.empty();
    } catch (final SecretsManagerException e) {
        throw new RuntimeException("Failed to fetch secret from AWS Secrets Manager.", e);
    }
}
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) ResourceNotFoundException(software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException)

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