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