Search in sources :

Example 91 with ClientConfiguration

use of com.amazonaws.ClientConfiguration in project herd by FINRAOS.

the class S3DaoTest method testGetAmazonS3AssertProxyIsNotSetWhenProxyHostIsBlank.

@Test
public void testGetAmazonS3AssertProxyIsNotSetWhenProxyHostIsBlank() {
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    try {
        String s3BucketName = "s3BucketName";
        String s3KeyPrefix = "s3KeyPrefix";
        String httpProxyHost = "";
        Integer httpProxyPort = 1234;
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
        s3FileTransferRequestParamsDto.setS3BucketName(s3BucketName);
        s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefix);
        s3FileTransferRequestParamsDto.setHttpProxyHost(httpProxyHost);
        s3FileTransferRequestParamsDto.setHttpProxyPort(httpProxyPort);
        when(mockS3Operations.putObject(any(), any())).then(new Answer<PutObjectResult>() {

            @Override
            public PutObjectResult answer(InvocationOnMock invocation) throws Throwable {
                AmazonS3Client amazonS3Client = invocation.getArgument(1);
                ClientConfiguration clientConfiguration = (ClientConfiguration) ReflectionTestUtils.getField(amazonS3Client, "clientConfiguration");
                assertNull(clientConfiguration.getProxyHost());
                return new PutObjectResult();
            }
        });
        s3Dao.createDirectory(s3FileTransferRequestParamsDto);
    } finally {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConfiguration(com.amazonaws.ClientConfiguration) Test(org.junit.Test)

Example 92 with ClientConfiguration

use of com.amazonaws.ClientConfiguration in project herd by FINRAOS.

the class CredStashHelper method getCredentialFromCredStash.

/**
 * Gets a password from the credstash.
 *
 * @param credStashEncryptionContext the encryption context
 * @param credentialName the credential name
 *
 * @return the password
 * @throws CredStashGetCredentialFailedException if CredStash fails to get a credential
 */
@Retryable(maxAttempts = 3, value = CredStashGetCredentialFailedException.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public String getCredentialFromCredStash(String credStashEncryptionContext, String credentialName) throws CredStashGetCredentialFailedException {
    // Get the credstash table name and credential names for the keystore and truststore.
    String credStashAwsRegion = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_AWS_REGION_NAME);
    String credStashTableName = configurationHelper.getProperty(ConfigurationValue.CREDSTASH_TABLE_NAME);
    // Log configuration values and input parameters.
    LOGGER.info("credStashAwsRegion={} credStashTableName={} credStashEncryptionContext={} credentialName={}", credStashAwsRegion, credStashTableName, credStashEncryptionContext, credentialName);
    // Get the AWS client configuration.
    ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsHelper.getAwsParamsDto());
    // Get the keystore and truststore passwords from Credstash.
    CredStash credstash = credStashFactory.getCredStash(credStashAwsRegion, credStashTableName, clientConfiguration);
    // Try to obtain the credentials from cred stash.
    String password = null;
    String errorMessage = null;
    try {
        // Convert the JSON config file version of the encryption context to a Java Map class.
        @SuppressWarnings("unchecked") Map<String, String> credstashEncryptionContextMap = jsonHelper.unmarshallJsonToObject(Map.class, credStashEncryptionContext);
        // Get the keystore and truststore passwords from credstash.
        password = credstash.getCredential(credentialName, credstashEncryptionContextMap);
    } catch (Exception exception) {
        LOGGER.error("Caught exception when attempting to get a credential value from CredStash.", exception);
        errorMessage = exception.getMessage();
    }
    // as credentials from cred stash, then throw a CredStashGetCredentialFailedException.
    if (StringUtils.isEmpty(password)) {
        throw new CredStashGetCredentialFailedException(String.format("Failed to obtain the keystore or truststore credential from credstash.%s " + "credStashAwsRegion=%s credStashTableName=%s credStashEncryptionContext=%s credentialName=%s", StringUtils.isNotBlank(errorMessage) ? " Reason: " + errorMessage : "", credStashAwsRegion, credStashTableName, credStashEncryptionContext, credentialName));
    }
    // Return the keystore and truststore passwords in a map.
    return password;
}
Also used : CredStash(org.finra.herd.dao.credstash.CredStash) ClientConfiguration(com.amazonaws.ClientConfiguration) CredStashGetCredentialFailedException(org.finra.herd.dao.exception.CredStashGetCredentialFailedException) CredStashGetCredentialFailedException(org.finra.herd.dao.exception.CredStashGetCredentialFailedException) Retryable(org.springframework.retry.annotation.Retryable)

Example 93 with ClientConfiguration

use of com.amazonaws.ClientConfiguration in project photon-model by vmware.

the class AWSUtils method getStatsAsyncClient.

/**
 * Method to get a CloudWatch Async Client.
 *
 * Note: ARN-based credentials will not work unless they have already been exchanged to
 * AWS for session credentials. If unset, this method will fail. To enable ARN-based
 * credentials, migrate to {@link #getCloudWatchStatsAsyncClient(AuthCredentialsServiceState,
 * String, ExecutorService, boolean)}.
 *
 * @param credentials An {@link AuthCredentialsServiceState} object.
 * @param region The region to get the AWS client in.
 * @param executorService The executor service to run async services in.
 */
public static AmazonCloudWatchAsyncClient getStatsAsyncClient(AuthCredentialsServiceState credentials, String region, ExecutorService executorService, boolean isMockRequest) {
    ClientConfiguration configuration = createClientConfiguration();
    AmazonCloudWatchAsyncClientBuilder amazonCloudWatchAsyncClientBuilder = AmazonCloudWatchAsyncClientBuilder.standard().withClientConfiguration(configuration).withCredentials(getAwsStaticCredentialsProvider(credentials)).withExecutorFactory(() -> executorService);
    if (region == null) {
        region = Regions.DEFAULT_REGION.getName();
    }
    if (isAwsClientMock()) {
        configuration.addHeader(AWS_REGION_HEADER, region);
        amazonCloudWatchAsyncClientBuilder.setClientConfiguration(configuration);
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(getAWSMockHost() + AWS_MOCK_CLOUDWATCH_ENDPOINT, region);
        amazonCloudWatchAsyncClientBuilder.setEndpointConfiguration(endpointConfiguration);
    } else {
        amazonCloudWatchAsyncClientBuilder.setRegion(region);
    }
    return (AmazonCloudWatchAsyncClient) amazonCloudWatchAsyncClientBuilder.build();
}
Also used : AmazonCloudWatchAsyncClientBuilder(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClientBuilder) AmazonCloudWatchAsyncClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 94 with ClientConfiguration

use of com.amazonaws.ClientConfiguration in project photon-model by vmware.

the class AWSUtils method getAsyncClient.

/**
 * Method to get an EC2 Async Client.
 *
 * Note: ARN-based credentials will not work unless they have already been exchanged to
 * AWS for session credentials. If unset, this method will fail. To enable ARN-based
 * credentials, migrate to {@link #getEc2AsyncClient(AuthCredentialsServiceState, String,
 * ExecutorService)}.
 *
 * @param credentials An {@link AuthCredentialsServiceState} object.
 * @param region The region to get the AWS client in.
 * @param executorService The executor service to run async services in.
 */
public static AmazonEC2AsyncClient getAsyncClient(AuthCredentialsServiceState credentials, String region, ExecutorService executorService) {
    ClientConfiguration configuration = createClientConfiguration().withMaxConnections(100);
    AmazonEC2AsyncClientBuilder ec2AsyncClientBuilder = AmazonEC2AsyncClientBuilder.standard().withClientConfiguration(configuration).withCredentials(getAwsStaticCredentialsProvider(credentials)).withExecutorFactory(() -> executorService);
    if (region == null) {
        region = Regions.DEFAULT_REGION.getName();
    }
    if (isAwsClientMock()) {
        configuration.addHeader(AWS_REGION_HEADER, region);
        ec2AsyncClientBuilder.setClientConfiguration(configuration);
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(getAWSMockHost() + AWS_MOCK_EC2_ENDPOINT, region);
        ec2AsyncClientBuilder.setEndpointConfiguration(endpointConfiguration);
    } else {
        ec2AsyncClientBuilder.setRegion(region);
    }
    return (AmazonEC2AsyncClient) ec2AsyncClientBuilder.build();
}
Also used : AmazonEC2AsyncClientBuilder(com.amazonaws.services.ec2.AmazonEC2AsyncClientBuilder) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 95 with ClientConfiguration

use of com.amazonaws.ClientConfiguration in project neo4j-apoc-procedures by neo4j-contrib.

the class S3URLConnection method buildClientConfig.

public static ClientConfiguration buildClientConfig() {
    final String userAgent = System.getProperty(PROP_S3_HANDLER_USER_AGENT, null);
    final String protocol = System.getProperty(PROP_S3_HANDLER_PROTOCOL, "https").toLowerCase();
    final String signerOverride = System.getProperty(PROP_S3_HANDLER_SIGNER_OVERRIDE, null);
    final ClientConfiguration clientConfig = new ClientConfiguration().withProtocol("https".equals(protocol) ? Protocol.HTTPS : Protocol.HTTP);
    if (userAgent != null) {
        clientConfig.setUserAgentPrefix(userAgent);
    }
    if (signerOverride != null) {
        clientConfig.setSignerOverride(signerOverride);
    }
    return clientConfig;
}
Also used : ClientConfiguration(com.amazonaws.ClientConfiguration)

Aggregations

ClientConfiguration (com.amazonaws.ClientConfiguration)134 Test (org.junit.Test)35 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)29 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)17 AWSCredentials (com.amazonaws.auth.AWSCredentials)14 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)13 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)13 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)10 AwsParamsDto (org.finra.herd.model.dto.AwsParamsDto)8 ClientConfigurationFactory (com.amazonaws.ClientConfigurationFactory)7 EnvVars (hudson.EnvVars)7 File (java.io.File)7 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)6 Configuration (org.apache.hadoop.conf.Configuration)6 AmazonClientException (com.amazonaws.AmazonClientException)5 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)5 EndpointConfiguration (com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration)5 URI (java.net.URI)5 Properties (java.util.Properties)5 Test (org.testng.annotations.Test)5