Search in sources :

Example 1 with SecretsManagerCredentials

use of com.amazonaws.athena.connector.integ.data.SecretsManagerCredentials in project aws-athena-query-federation by awslabs.

the class SecretsManagerCredentialsProvider method getCredentials.

/**
 * Gets the SecretManager credentials obtained using a secret name stored in the test-config.json file.
 * @param testConfig Contains the test configurations from the test-config.json file.
 * @return Optional credentials object, or empty Optional if the secrets_manager_secret attribute is not in the
 * configuration file or is empty.
 * @throws RuntimeException Error encountered attempting to parse the json string returned from SecretsManager.
 */
public static Optional<SecretsManagerCredentials> getCredentials(TestConfig testConfig) throws RuntimeException {
    Optional<String> secretsManagerSecret = testConfig.getStringItem(TEST_CONFIG_SECRETS_MANAGER_SECRET);
    if (secretsManagerSecret.isPresent()) {
        String secret = secretsManagerSecret.get();
        AWSSecretsManager secretsManager = AWSSecretsManagerClientBuilder.defaultClient();
        try {
            GetSecretValueResult secretValueResult = secretsManager.getSecretValue(new GetSecretValueRequest().withSecretId(secret));
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, String> credentials = objectMapper.readValue(secretValueResult.getSecretString(), HashMap.class);
            return Optional.of(new SecretsManagerCredentials(secret, credentials.get("username"), credentials.get("password"), secretValueResult.getARN()));
        } catch (IOException e) {
            throw new RuntimeException(String.format("Unable to parse SecretsManager secret (%s): %s", secret, e.getMessage()), e);
        } finally {
            secretsManager.shutdown();
        }
    }
    return Optional.empty();
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) SecretsManagerCredentials(com.amazonaws.athena.connector.integ.data.SecretsManagerCredentials) AWSSecretsManager(com.amazonaws.services.secretsmanager.AWSSecretsManager) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

SecretsManagerCredentials (com.amazonaws.athena.connector.integ.data.SecretsManagerCredentials)1 AWSSecretsManager (com.amazonaws.services.secretsmanager.AWSSecretsManager)1 GetSecretValueRequest (com.amazonaws.services.secretsmanager.model.GetSecretValueRequest)1 GetSecretValueResult (com.amazonaws.services.secretsmanager.model.GetSecretValueResult)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1