Search in sources :

Example 31 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project spring-cloud-config by spring-cloud.

the class AwsSecretsManagerEnvironmentRepositoryTests method setupAwsSmClientMocks.

private void setupAwsSmClientMocks(Environment environment) {
    for (PropertySource ps : environment.getPropertySources()) {
        String path = StringUtils.delete(ps.getName(), environmentProperties.getOrigin());
        GetSecretValueRequest request = new GetSecretValueRequest().withSecretId(path);
        String secrets = getSecrets(ps);
        GetSecretValueResult response = new GetSecretValueResult().withSecretString(secrets);
        when(awsSmClientMock.getSecretValue(eq(request))).thenReturn(response);
    }
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 32 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project spring-cloud-config by spring-cloud.

the class AwsSecretsManagerEnvironmentRepository method findProperties.

private Map<Object, Object> findProperties(String path) {
    Map<Object, Object> properties = new HashMap<>();
    GetSecretValueRequest request = new GetSecretValueRequest().withSecretId(path);
    try {
        GetSecretValueResult response = awsSmClient.getSecretValue(request);
        if (response != null) {
            Map<String, Object> secretMap = objectMapper.readValue(response.getSecretString(), new TypeReference<Map<String, Object>>() {
            });
            for (Map.Entry<String, Object> secretEntry : secretMap.entrySet()) {
                properties.put(secretEntry.getKey(), secretEntry.getValue());
            }
        }
    } catch (ResourceNotFoundException | IOException e) {
        log.debug(String.format("Skip adding propertySource. Unable to load secrets from AWS Secrets Manager for secretId=%s", path), e);
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) ResourceNotFoundException(com.amazonaws.services.secretsmanager.model.ResourceNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project kork by spinnaker.

the class SecretsManagerSecretEngine method decrypt.

@Override
public byte[] decrypt(EncryptedSecret encryptedSecret) {
    String secretRegion = encryptedSecret.getParams().get(SECRET_REGION);
    String secretName = encryptedSecret.getParams().get(SECRET_NAME);
    String secretKey = encryptedSecret.getParams().get(SECRET_KEY);
    if (encryptedSecret.isEncryptedFile()) {
        GetSecretValueResult secretFileValue = getSecretValue(secretRegion, secretName);
        if (secretFileValue.getSecretBinary() != null) {
            return secretFileValue.getSecretBinary().array();
        } else {
            return secretFileValue.getSecretString().getBytes();
        }
    } else if (secretKey != null) {
        return getSecretString(secretRegion, secretName, secretKey);
    } else {
        return getSecretString(secretRegion, secretName);
    }
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult)

Example 34 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project kork by spinnaker.

the class SecretsManagerSecretEngine method decrypt.

@Override
@NonNull
public UserSecret decrypt(@NonNull UserSecretReference reference) {
    validate(reference);
    Map<String, String> parameters = reference.getParameters();
    String secretRegion = parameters.get(SECRET_REGION);
    String secretName = parameters.get(SECRET_NAME);
    String encoding = parameters.get(StandardSecretParameter.ENCODING.getParameterName());
    GetSecretValueResult secretValue = getSecretValue(secretRegion, secretName);
    ByteBuffer secretBinary = secretValue.getSecretBinary();
    if (secretBinary != null) {
        return userSecretMapper.deserialize(secretBinary.array(), encoding);
    }
    return userSecretMapper.deserialize(secretValue.getSecretString().getBytes(StandardCharsets.UTF_8), encoding);
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) ByteBuffer(java.nio.ByteBuffer) NonNull(org.springframework.lang.NonNull)

Example 35 with GetSecretValueResult

use of com.amazonaws.services.secretsmanager.model.GetSecretValueResult in project aws-appflow-custom-connector-java by awslabs.

the class CredentialsProvider method getCredentials.

private static Map<String, String> getCredentials(final AWSSecretsManager client, final String secretArn) {
    AWSSecretsManager secretsManager = null;
    GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest().withSecretId(secretArn);
    try {
        GetSecretValueResult secretValueResult;
        if (client != null) {
            secretValueResult = client.getSecretValue(getSecretValueRequest);
        } else {
            secretsManager = AWSSecretsManagerClientBuilder.defaultClient();
            secretValueResult = secretsManager.getSecretValue(getSecretValueRequest);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> credentials = objectMapper.readValue(secretValueResult.getSecretString(), HashMap.class);
        return credentials;
    } catch (Exception e) {
        throw new RuntimeException(String.format("Unable to get secrets from SecretsManager. SecretArn [%s]: %s", secretArn, e.getMessage()), e);
    } finally {
        if (secretsManager != null) {
            secretsManager.shutdown();
        }
    }
}
Also used : GetSecretValueResult(com.amazonaws.services.secretsmanager.model.GetSecretValueResult) AWSSecretsManager(com.amazonaws.services.secretsmanager.AWSSecretsManager) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

GetSecretValueResult (com.amazonaws.services.secretsmanager.model.GetSecretValueResult)60 GetSecretValueRequest (com.amazonaws.services.secretsmanager.model.GetSecretValueRequest)51 AWSSecretsManager (com.amazonaws.services.secretsmanager.AWSSecretsManager)25 Before (org.junit.Before)21 JdbcConnectionFactory (com.amazonaws.athena.connectors.jdbc.connection.JdbcConnectionFactory)18 JdbcCredentialProvider (com.amazonaws.athena.connectors.jdbc.connection.JdbcCredentialProvider)18 AmazonAthena (com.amazonaws.services.athena.AmazonAthena)17 FederatedIdentity (com.amazonaws.athena.connector.lambda.security.FederatedIdentity)16 Connection (java.sql.Connection)13 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)8 AmazonS3 (com.amazonaws.services.s3.AmazonS3)6 DatabaseConnectionConfig (com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionConfig)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Map (java.util.Map)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 QueryStatusChecker (com.amazonaws.athena.connector.lambda.QueryStatusChecker)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3