Search in sources :

Example 1 with GetSecretValueRequest

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

Example 2 with GetSecretValueRequest

use of software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest in project cu-kfs by CU-CommunityApps.

the class AwsSecretServiceImpl method retrieveSecretFromAws.

protected String retrieveSecretFromAws(String fullAwsKey) {
    SecretsManagerClient client = buildSecretsManagerClient();
    try {
        GetSecretValueRequest valueRequest = GetSecretValueRequest.builder().secretId(fullAwsKey).build();
        GetSecretValueResponse valueResponse = client.getSecretValue(valueRequest);
        return valueResponse.secretString();
    } catch (SecretsManagerException e) {
        LOG.error("retrieveSecretFromAws, had an error getting value for secret " + fullAwsKey, e);
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}
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) SecretsManagerClient(software.amazon.awssdk.services.secretsmanager.SecretsManagerClient)

Example 3 with GetSecretValueRequest

use of software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class SecretsClient method getSecretValue.

/**
 * Wrapper for getSecretValue function in SecretsManagerClient.
 * @param secretId - secretArn for the secret
 * @return secret value as String
 * @throws EdgeConnectorForKVSException - Wrapper to all exception thrown by SecretsManagerClient
 */
public String getSecretValue(@NonNull String secretId) throws EdgeConnectorForKVSException {
    try {
        log.info("Retrieving Secret Value for " + secretId);
        GetSecretValueRequest secretValueRequest = GetSecretValueRequest.builder().secretId(secretId).versionStage(VERSION_STAGE).build();
        GetSecretValueResponse secretValueResponse = this.secretsManagerClient.getSecretValue(secretValueRequest);
        return secretValueResponse.secretString();
    } catch (Exception e) {
        final String errorMessage = String.format("Could not getSecretValue for secretId: %s", secretId);
        log.error(errorMessage);
        throw new EdgeConnectorForKVSException(errorMessage, e);
    }
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) GetSecretValueRequest(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest) GetSecretValueResponse(software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)

Example 4 with GetSecretValueRequest

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

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

GetSecretValueRequest (software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest)6 GetSecretValueResponse (software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse)6 SecretsManagerException (software.amazon.awssdk.services.secretsmanager.model.SecretsManagerException)3 EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)1 RetryPolicy (net.jodah.failsafe.RetryPolicy)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1 SecretsManagerClient (software.amazon.awssdk.services.secretsmanager.SecretsManagerClient)1