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;
}
}
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);
}
}
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);
}
}
Aggregations