Search in sources :

Example 11 with GetSecretValueResponse

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

use of software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse in project spring-cloud-aws by awspring.

the class SecretsManagerPropertySourceTest method shouldParsePlainTextSecretValue.

@Test
void shouldParsePlainTextSecretValue() {
    GetSecretValueResponse secretValueResult = GetSecretValueResponse.builder().secretString("my secret").name("secret name").build();
    when(client.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);
    propertySource.init();
    assertThat(propertySource.getPropertyNames()).containsExactly("secret name");
    assertThat(propertySource.getProperty("secret name")).isEqualTo("my secret");
}
Also used : GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse) Test(org.junit.jupiter.api.Test)

Example 13 with GetSecretValueResponse

use of software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse in project spring-cloud-aws by awspring.

the class SecretsManagerPropertySourceTest method shouldParseSecretValue.

@Test
void shouldParseSecretValue() {
    GetSecretValueResponse secretValueResult = GetSecretValueResponse.builder().secretString("{\"key1\": \"value1\", \"key2\": \"value2\"}").build();
    when(client.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);
    propertySource.init();
    assertThat(propertySource.getPropertyNames()).containsExactly("key1", "key2");
    assertThat(propertySource.getProperty("key1")).isEqualTo("value1");
    assertThat(propertySource.getProperty("key2")).isEqualTo("value2");
}
Also used : GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse) Test(org.junit.jupiter.api.Test)

Example 14 with GetSecretValueResponse

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

the class AWSSecretManager method getValue.

private static String getValue(SecretsManagerClient secretsClient, String secretName) {
    GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
    GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
    return valueResponse.secretString();
}
Also used : GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse)

Example 15 with GetSecretValueResponse

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

the class AWSSecretManager method getBinaryValue.

private static SdkBytes getBinaryValue(SecretsManagerClient secretsClient, String secretName) {
    GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
    GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
    return valueResponse.secretBinary();
}
Also used : GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse)

Aggregations

GetSecretValueResponse (software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse)21 GetSecretValueRequest (software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest)15 Test (org.junit.jupiter.api.Test)6 SecretsManagerException (software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException)4 VaultSecretNotFoundException (com.quorum.tessera.key.vault.VaultSecretNotFoundException)2 Test (org.junit.Test)2 SecretsManagerClient (software.amazon.awssdk.services.secretsmanager.SecretsManagerClient)2 ResourceNotFoundException (software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException)2 SecretException (co.com.bancolombia.secretsmanager.api.exceptions.SecretException)1 EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 RetryPolicy (net.jodah.failsafe.RetryPolicy)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1 SecretsManagerClientBuilder (software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder)1 InvalidParameterException (software.amazon.awssdk.services.secretsmanager.model.InvalidParameterException)1 InvalidRequestException (software.amazon.awssdk.services.secretsmanager.model.InvalidRequestException)1