Java Code Examples for com.amazonaws.ClientConfiguration#setProtocol()

The following examples show how to use com.amazonaws.ClientConfiguration#setProtocol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3Service.java    From crate with Apache License 2.0 6 votes vote down vote up
static ClientConfiguration buildConfiguration(S3ClientSettings clientSettings) {
    final ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(clientSettings.protocol);

    if (Strings.hasText(clientSettings.proxyHost)) {
        // TODO: remove this leniency, these settings should exist together and be validated
        clientConfiguration.setProxyHost(clientSettings.proxyHost);
        clientConfiguration.setProxyPort(clientSettings.proxyPort);
        clientConfiguration.setProxyUsername(clientSettings.proxyUsername);
        clientConfiguration.setProxyPassword(clientSettings.proxyPassword);
    }

    clientConfiguration.setMaxErrorRetry(clientSettings.maxRetries);
    clientConfiguration.setUseThrottleRetries(clientSettings.throttleRetries);
    clientConfiguration.setSocketTimeout(clientSettings.readTimeoutMillis);

    return clientConfiguration;
}
 
Example 2
Source File: KinesisPersistReader.java    From streams with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(Object configurationObject) {
  // Connect to Kinesis
  synchronized (this) {
    // Create the credentials Object
    AWSCredentials credentials = new BasicAWSCredentials(config.getKey(), config.getSecretKey());

    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.valueOf(config.getProtocol().toString()));

    this.client = new AmazonKinesisClient(credentials, clientConfig);
    if (StringUtils.isNotEmpty(config.getRegion()))
      this.client.setRegion(Region.getRegion(Regions.fromName(config.getRegion())));
  }
  streamNames = this.config.getStreams();
  executor = Executors.newFixedThreadPool(streamNames.size());
}
 
Example 3
Source File: WarehouseExport.java    From usergrid with Apache License 2.0 6 votes vote down vote up
private void copyToS3( String fileName ) {

        String bucketName = ( String ) properties.get( BUCKET_PROPNAME );
        String accessId = ( String ) properties.get( ACCESS_ID_PROPNAME );
        String secretKey = ( String ) properties.get( SECRET_KEY_PROPNAME );

        Properties overrides = new Properties();
        overrides.setProperty( "s3" + ".identity", accessId );
        overrides.setProperty( "s3" + ".credential", secretKey );

        final Iterable<? extends Module> MODULES = ImmutableSet
                .of( new JavaUrlHttpCommandExecutorServiceModule(), new Log4JLoggingModule(),
                        new NettyPayloadModule() );

        AWSCredentials credentials = new BasicAWSCredentials(accessId, secretKey);
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol( Protocol.HTTP);

        AmazonS3Client s3Client = new AmazonS3Client(credentials, clientConfig);

        s3Client.createBucket( bucketName );
        File uploadFile = new File( fileName );
        PutObjectResult putObjectResult = s3Client.putObject( bucketName, uploadFile.getName(), uploadFile );
        logger.info("Uploaded file etag={}", putObjectResult.getETag());
    }
 
Example 4
Source File: AWSBinaryStore.java    From usergrid with Apache License 2.0 6 votes vote down vote up
private AmazonS3 getS3Client() throws Exception{

        this.bucketName = properties.getProperty( "usergrid.binary.bucketname" );
        if(bucketName == null){
            logger.error( "usergrid.binary.bucketname not properly set so amazon bucket is null" );
            throw new AwsPropertiesNotFoundException( "usergrid.binary.bucketname" );

        }

        final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider();
        AWSCredentials credentials = ugProvider.getCredentials();
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTP);

        s3Client = new AmazonS3Client(credentials, clientConfig);
        if(regionName != null)
            s3Client.setRegion( Region.getRegion(Regions.fromName(regionName)) );

        return s3Client;
    }
 
Example 5
Source File: ProxyTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseProxyWithoutPort() throws Exception {
	EnvVars vars = new EnvVars();
	vars.put(ProxyConfiguration.HTTPS_PROXY, "http://127.0.0.1/");
	ClientConfiguration config = new ClientConfiguration();
	config.setProtocol(Protocol.HTTPS);
	ProxyConfiguration.configure(vars, config);

	Assert.assertNull(config.getProxyUsername());
	Assert.assertNull(config.getProxyPassword());
	Assert.assertEquals("127.0.0.1", config.getProxyHost());
	Assert.assertEquals(443, config.getProxyPort());
}
 
Example 6
Source File: AWSKinesisLocalContainerService.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Override
public AmazonKinesis getClient() {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setProtocol(Protocol.HTTP);

    return AmazonKinesisClientBuilder
            .standard()
            .withEndpointConfiguration(getContainer().getEndpointConfiguration(LocalStackContainer.Service.KINESIS))
            .withCredentials(getContainer().getDefaultCredentialsProvider())
            .withClientConfiguration(clientConfiguration)
            .build();
}
 
Example 7
Source File: AwsEc2ServiceImpl.java    From crate with Apache License 2.0 5 votes vote down vote up
static ClientConfiguration buildConfiguration(Logger logger, Ec2ClientSettings clientSettings) {
    final ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(clientSettings.protocol);
    if (Strings.hasText(clientSettings.proxyHost)) {
        // TODO: remove this leniency, these settings should exist together and be validated
        clientConfiguration.setProxyHost(clientSettings.proxyHost);
        clientConfiguration.setProxyPort(clientSettings.proxyPort);
        clientConfiguration.setProxyUsername(clientSettings.proxyUsername);
        clientConfiguration.setProxyPassword(clientSettings.proxyPassword);
    }
    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    final RetryPolicy retryPolicy = new RetryPolicy(
        RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
        (originalRequest, exception, retriesAttempted) -> {
            // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
            logger.warn("EC2 API request failed, retry again. Reason was:", exception);
            return 1000L * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
        },
        10,
        false);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout(clientSettings.readTimeoutMillis);
    return clientConfiguration;
}
 
Example 8
Source File: LambdaAggregatingForwarder.java    From kinesis-aggregation with Apache License 2.0 5 votes vote down vote up
/**
 * One-time initialization of resources for this Lambda function.
 */
public LambdaAggregatingForwarder()
{
    this.aggregator = new RecordAggregator();
    
    /*
     * If the Kinesis stream you're forwarding to is in the same account as this AWS Lambda function, you can just give the IAM Role executing
     * this function permissions to publish to the stream and DefaultAWSCredentialsProviderChain() will take care of it.  
     * 
     * If you're publishing to a Kinesis stream in another AWS account, it's trickier.  Kinesis doesn't currently provide cross-account publishing
     * permissions. You must create an IAM role in the AWS account with the DESTINATION stream that has permissions to publish to that stream
     * (fill that IAM role's ARN in for "<RoleToAssumeARN>" below) and then the IAM role executing this Lambda function must be given permission
     * to assume the role "<RoleToAssumeARN>" from the other AWS account.  
     */
    AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
    //AWSCredentialsProvider provider = new STSAssumeRoleSessionCredentialsProvider(new DefaultAWSCredentialsProviderChain(), "<RoleToAssumeARN>", "KinesisForwarder");
    
    //Set max conns to 1 since we use this client serially
    ClientConfiguration kinesisConfig = new ClientConfiguration();
    kinesisConfig.setMaxConnections(1);
    kinesisConfig.setProtocol(Protocol.HTTPS);
    kinesisConfig.setConnectionTimeout(DESTINATION_CONNECTION_TIMEOUT);
    kinesisConfig.setSocketTimeout(DESTINATION_SOCKET_TIMEOUT);
    
    this.kinesisForwarder = new AmazonKinesisClient(provider, kinesisConfig);
    this.kinesisForwarder.setRegion(Region.getRegion(DESTINATION_STREAM_REGION));
}
 
Example 9
Source File: ProxyTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSetNonProxyHostsLowerCase() throws Exception {
	EnvVars vars = new EnvVars();
	vars.put(ProxyConfiguration.NO_PROXY_LC, "127.0.0.1,localhost");
	vars.put(ProxyConfiguration.HTTPS_PROXY, "http://127.0.0.1:8888/");
	ClientConfiguration config = new ClientConfiguration();
	config.setProtocol(Protocol.HTTPS);
	ProxyConfiguration.configure(vars, config);

	Assert.assertEquals("127.0.0.1|localhost", config.getNonProxyHosts());
}
 
Example 10
Source File: ProxyTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSetNonProxyHosts() throws Exception {
	EnvVars vars = new EnvVars();
	vars.put(ProxyConfiguration.NO_PROXY, "127.0.0.1,localhost");
	vars.put(ProxyConfiguration.HTTPS_PROXY, "http://127.0.0.1:8888/");
	ClientConfiguration config = new ClientConfiguration();
	config.setProtocol(Protocol.HTTPS);
	ProxyConfiguration.configure(vars, config);

	Assert.assertEquals("127.0.0.1|localhost", config.getNonProxyHosts());
}
 
Example 11
Source File: ProxyTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseProxyWithAuth() throws Exception {
	EnvVars vars = new EnvVars();
	vars.put(ProxyConfiguration.HTTPS_PROXY, "http://foo:[email protected]:8888/");
	ClientConfiguration config = new ClientConfiguration();
	config.setProtocol(Protocol.HTTPS);
	ProxyConfiguration.configure(vars, config);

	Assert.assertEquals("foo", config.getProxyUsername());
	Assert.assertEquals("bar", config.getProxyPassword());
	Assert.assertEquals("127.0.0.1", config.getProxyHost());
	Assert.assertEquals(8888, config.getProxyPort());
}
 
Example 12
Source File: ProxyTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldParseProxyLowerCase() throws Exception {
	EnvVars vars = new EnvVars();
	vars.put(ProxyConfiguration.HTTPS_PROXY_LC, "http://127.0.0.1:8888/");
	ClientConfiguration config = new ClientConfiguration();
	config.setProtocol(Protocol.HTTPS);
	ProxyConfiguration.configure(vars, config);

	Assert.assertNull(config.getProxyUsername());
	Assert.assertNull(config.getProxyPassword());
	Assert.assertEquals("127.0.0.1", config.getProxyHost());
	Assert.assertEquals(8888, config.getProxyPort());
}
 
Example 13
Source File: AWSClientUtils.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
public static AmazonSQS newSQSClient() {
    LOG.debug("Creating a custom SQS client for running a AWS SNS test");
    AmazonSQSClientBuilder clientBuilder = AmazonSQSClientBuilder
            .standard();

    String awsInstanceType = System.getProperty("aws-service.instance.type");
    String region = getRegion();

    if (awsInstanceType == null || awsInstanceType.equals("local-aws-container")) {
        String amazonHost = System.getProperty(AWSConfigs.AMAZON_AWS_HOST);

        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProtocol(Protocol.HTTP);

        clientBuilder
                .withClientConfiguration(clientConfiguration)
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(amazonHost, region))
                .withCredentials(new TestAWSCredentialsProvider("accesskey", "secretkey"));
    } else {
        clientBuilder
                .withRegion(region)
                .withCredentials(new TestAWSCredentialsProvider());
    }



    return clientBuilder.build();
}
 
Example 14
Source File: ProxyTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotChangeIfNotPresent() throws Exception {
	ClientConfiguration config = new ClientConfiguration();
	config.setProtocol(Protocol.HTTPS);
	ProxyConfiguration.configure(new EnvVars(), config);

	Assert.assertNull(config.getProxyUsername());
	Assert.assertNull(config.getProxyPassword());
	Assert.assertNull(config.getProxyHost());
	Assert.assertEquals(-1, config.getProxyPort());
}
 
Example 15
Source File: S3ClientFactory.java    From front50 with Apache License 2.0 5 votes vote down vote up
public static AmazonS3 create(
    AWSCredentialsProvider awsCredentialsProvider, S3Properties s3Properties) {
  ClientConfiguration clientConfiguration = new ClientConfiguration();
  if (s3Properties.getProxyProtocol() != null) {
    if (s3Properties.getProxyProtocol().equalsIgnoreCase("HTTPS")) {
      clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
      clientConfiguration.setProtocol(Protocol.HTTP);
    }
    Optional.ofNullable(s3Properties.getProxyHost()).ifPresent(clientConfiguration::setProxyHost);
    Optional.ofNullable(s3Properties.getProxyPort())
        .map(Integer::parseInt)
        .ifPresent(clientConfiguration::setProxyPort);
  }

  AmazonS3Client client = new AmazonS3Client(awsCredentialsProvider, clientConfiguration);

  if (!StringUtils.isEmpty(s3Properties.getEndpoint())) {
    client.setEndpoint(s3Properties.getEndpoint());

    if (!StringUtils.isEmpty(s3Properties.getRegionOverride())) {
      client.setSignerRegionOverride(s3Properties.getRegionOverride());
    }

    client.setS3ClientOptions(
        S3ClientOptions.builder().setPathStyleAccess(s3Properties.getPathStyleAccess()).build());
  } else {
    Optional.ofNullable(s3Properties.getRegion())
        .map(Regions::fromName)
        .map(Region::getRegion)
        .ifPresent(client::setRegion);
  }

  return client;
}
 
Example 16
Source File: S3PersistReader.java    From streams with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare(Object configurationObject) {

  streamsConfiguration = StreamsConfigurator.detectConfiguration();

  lineReaderUtil = LineReadWriteUtil.getInstance(s3ReaderConfiguration);

  // Connect to S3
  synchronized (this) {
    // Create the credentials Object
    AWSCredentials credentials = new BasicAWSCredentials(s3ReaderConfiguration.getKey(), s3ReaderConfiguration.getSecretKey());

    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.valueOf(s3ReaderConfiguration.getProtocol().toString()));

    // We do not want path style access
    S3ClientOptions clientOptions = new S3ClientOptions();
    clientOptions.setPathStyleAccess(false);

    this.amazonS3Client = new AmazonS3Client(credentials, clientConfig);
    if (StringUtils.isNotEmpty(s3ReaderConfiguration.getRegion())) {
      this.amazonS3Client.setRegion(Region.getRegion(Regions.fromName(s3ReaderConfiguration.getRegion())));
    }
    this.amazonS3Client.setS3ClientOptions(clientOptions);
  }

  final ListObjectsRequest request = new ListObjectsRequest()
      .withBucketName(this.s3ReaderConfiguration.getBucket())
      .withPrefix(s3ReaderConfiguration.getReaderPath())
      .withMaxKeys(500);


  ObjectListing listing = this.amazonS3Client.listObjects(request);

  this.files = new ArrayList<>();

  /*
   * If you can list files that are in this path, then you must be dealing with a directory
   * if you cannot list files that are in this path, then you are most likely dealing with
   * a simple file.
   */
  boolean hasCommonPrefixes = listing.getCommonPrefixes().size() > 0;
  boolean hasObjectSummaries = listing.getObjectSummaries().size() > 0;

  if (hasCommonPrefixes || hasObjectSummaries) {
    // Handle the 'directory' use case
    do {
      if (hasCommonPrefixes) {
        for (String file : listing.getCommonPrefixes()) {
          this.files.add(file);
        }
      } else {
        for (final S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
          this.files.add(objectSummary.getKey());
        }
      }

      // get the next batch.
      listing = this.amazonS3Client.listNextBatchOfObjects(listing);
    }
    while (listing.isTruncated());
  } else {
    // handle the single file use-case
    this.files.add(s3ReaderConfiguration.getReaderPath());
  }

  if (this.files.size() <= 0) {
    LOGGER.error("There are no files to read");
  }

  this.persistQueue = Queues.synchronizedQueue(new LinkedBlockingQueue<StreamsDatum>(streamsConfiguration.getQueueSize().intValue()));
  this.executor = Executors.newSingleThreadExecutor();
}
 
Example 17
Source File: S3Operations.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Gets configured AmazonS3 client instance. Does not perform actual request until first remote data is needed
 */
private AmazonS3 getClient() {

    if (s3Client != null) {
        return s3Client; // already cached
    }

    ClientConfiguration config = new ClientConfiguration();
    if (endpoint != null && endpoint.startsWith("https://")) {
        config.setProtocol(Protocol.HTTPS);
    } else {
        config.setProtocol(Protocol.HTTP);
    }

    BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating S3 client to " + ( (endpoint == null)
                                                                  ? "default Amazon"
                                                                  : endpoint)
                  + " endpoint with access key " + accessKey);
    }

    if (this.endpoint != null) {
        if (region == null || region.trim().length() == 0) {
            region = Regions.DEFAULT_REGION.name();
        }
        s3Client = AmazonS3ClientBuilder.standard()
                                        .withCredentials(new AWSStaticCredentialsProvider(creds))
                                        .withEndpointConfiguration(new EndpointConfiguration(endpoint, region))
                                        .withClientConfiguration(config)
                                        .withPathStyleAccessEnabled(true)
                                        .build();
    } else {
        s3Client = AmazonS3ClientBuilder.standard()
                                        .withCredentials(new AWSStaticCredentialsProvider(creds))
                                        .withClientConfiguration(config)
                                        .withPathStyleAccessEnabled(true)
                                        .build();
    }
    return s3Client;
}
 
Example 18
Source File: AwsConfiguration.java    From kayenta with Apache License 2.0 4 votes vote down vote up
@Bean
boolean registerAwsCredentials(
    AwsConfigurationProperties awsConfigurationProperties,
    AccountCredentialsRepository accountCredentialsRepository)
    throws IOException {
  for (AwsManagedAccount awsManagedAccount : awsConfigurationProperties.getAccounts()) {
    String name = awsManagedAccount.getName();
    List<AccountCredentials.Type> supportedTypes = awsManagedAccount.getSupportedTypes();

    log.info("Registering AWS account {} with supported types {}.", name, supportedTypes);

    ClientConfiguration clientConfiguration = new ClientConfiguration();

    if (awsManagedAccount.getProxyProtocol() != null) {
      if (awsManagedAccount.getProxyProtocol().equalsIgnoreCase("HTTPS")) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
      } else {
        clientConfiguration.setProtocol(Protocol.HTTP);
      }
      Optional.ofNullable(awsManagedAccount.getProxyHost())
          .ifPresent(clientConfiguration::setProxyHost);
      Optional.ofNullable(awsManagedAccount.getProxyPort())
          .map(Integer::parseInt)
          .ifPresent(clientConfiguration::setProxyPort);
    }

    AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard();
    String profileName = awsManagedAccount.getProfileName();

    if (!StringUtils.isEmpty(profileName)) {
      amazonS3ClientBuilder.withCredentials(new ProfileCredentialsProvider(profileName));
    }

    AwsManagedAccount.ExplicitAwsCredentials explicitCredentials =
        awsManagedAccount.getExplicitCredentials();
    if (explicitCredentials != null) {
      String sessionToken = explicitCredentials.getSessionToken();
      AWSCredentials awsCreds =
          (sessionToken == null)
              ? new BasicAWSCredentials(
                  explicitCredentials.getAccessKey(), explicitCredentials.getSecretKey())
              : new BasicSessionCredentials(
                  explicitCredentials.getAccessKey(),
                  explicitCredentials.getSecretKey(),
                  sessionToken);
      amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(awsCreds));
    }

    String endpoint = awsManagedAccount.getEndpoint();

    if (!StringUtils.isEmpty(endpoint)) {
      amazonS3ClientBuilder.setEndpointConfiguration(
          new AwsClientBuilder.EndpointConfiguration(endpoint, null));
      amazonS3ClientBuilder.setPathStyleAccessEnabled(true);
    } else {
      Optional.ofNullable(awsManagedAccount.getRegion())
          .ifPresent(amazonS3ClientBuilder::setRegion);
    }

    AmazonS3 amazonS3 = amazonS3ClientBuilder.build();

    try {
      AwsCredentials awsCredentials = new AwsCredentials();
      AwsNamedAccountCredentials.AwsNamedAccountCredentialsBuilder
          awsNamedAccountCredentialsBuilder =
              AwsNamedAccountCredentials.builder().name(name).credentials(awsCredentials);

      if (!CollectionUtils.isEmpty(supportedTypes)) {
        if (supportedTypes.contains(AccountCredentials.Type.OBJECT_STORE)) {
          String bucket = awsManagedAccount.getBucket();
          String rootFolder = awsManagedAccount.getRootFolder();

          if (StringUtils.isEmpty(bucket)) {
            throw new IllegalArgumentException(
                "AWS/S3 account " + name + " is required to specify a bucket.");
          }

          if (StringUtils.isEmpty(rootFolder)) {
            throw new IllegalArgumentException(
                "AWS/S3 account " + name + " is required to specify a rootFolder.");
          }

          awsNamedAccountCredentialsBuilder.bucket(bucket);
          awsNamedAccountCredentialsBuilder.region(awsManagedAccount.getRegion());
          awsNamedAccountCredentialsBuilder.rootFolder(rootFolder);
          awsNamedAccountCredentialsBuilder.amazonS3(amazonS3);
        }

        awsNamedAccountCredentialsBuilder.supportedTypes(supportedTypes);
      }

      AwsNamedAccountCredentials awsNamedAccountCredentials =
          awsNamedAccountCredentialsBuilder.build();
      accountCredentialsRepository.save(name, awsNamedAccountCredentials);
    } catch (Throwable t) {
      log.error("Could not load AWS account " + name + ".", t);
    }
  }

  return true;
}
 
Example 19
Source File: S3Utils.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public static TransferManager getTransferManager(final ClientOptions clientOptions) {

        if(TRANSFERMANAGER_ACCESSKEY_MAP.containsKey(clientOptions.getAccessKey())) {
            return TRANSFERMANAGER_ACCESSKEY_MAP.get(clientOptions.getAccessKey());
        }

        final AWSCredentials basicAWSCredentials = new BasicAWSCredentials(clientOptions.getAccessKey(), clientOptions.getSecretKey());

        final ClientConfiguration configuration = new ClientConfiguration();

        if (clientOptions.isHttps() != null) {
            configuration.setProtocol(clientOptions.isHttps() ? HTTPS : HTTP);
        }

        if (clientOptions.getConnectionTimeout() != null) {
            configuration.setConnectionTimeout(clientOptions.getConnectionTimeout());
        }

        if (clientOptions.getMaxErrorRetry() != null) {
            configuration.setMaxErrorRetry(clientOptions.getMaxErrorRetry());
        }

        if (clientOptions.getSocketTimeout() != null) {
            configuration.setSocketTimeout(clientOptions.getSocketTimeout());
        }

        if (clientOptions.getUseTCPKeepAlive() != null) {
            configuration.setUseTcpKeepAlive(clientOptions.getUseTCPKeepAlive());
        }

        if (clientOptions.getConnectionTtl() != null) {
            configuration.setConnectionTTL(clientOptions.getConnectionTtl());
        }

        if (clientOptions.getSigner() != null) {

            configuration.setSignerOverride(clientOptions.getSigner());
        }

        LOGGER.debug(format("Creating S3 client with configuration: [protocol: %1$s, signer: %2$s, connectionTimeOut: %3$s, maxErrorRetry: %4$s, socketTimeout: %5$s, useTCPKeepAlive: %6$s, connectionTtl: %7$s]",
                configuration.getProtocol(), configuration.getSignerOverride(), configuration.getConnectionTimeout(), configuration.getMaxErrorRetry(), configuration.getSocketTimeout(),
                clientOptions.getUseTCPKeepAlive(), clientOptions.getConnectionTtl()));

        final AmazonS3Client client = new AmazonS3Client(basicAWSCredentials, configuration);

        if (isNotBlank(clientOptions.getEndPoint())) {
            LOGGER.debug(format("Setting the end point for S3 client with access key %1$s to %2$s.", clientOptions.getAccessKey(), clientOptions.getEndPoint()));

            client.setEndpoint(clientOptions.getEndPoint());
        }

        TRANSFERMANAGER_ACCESSKEY_MAP.put(clientOptions.getAccessKey(), new TransferManager(client));

        return TRANSFERMANAGER_ACCESSKEY_MAP.get(clientOptions.getAccessKey());
    }
 
Example 20
Source File: AbstractAWSProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected ClientConfiguration createConfiguration(final ProcessContext context) {
    final ClientConfiguration config = new ClientConfiguration();
    config.setMaxConnections(context.getMaxConcurrentTasks());
    config.setMaxErrorRetry(0);
    config.setUserAgent(DEFAULT_USER_AGENT);
    // If this is changed to be a property, ensure other uses are also changed
    config.setProtocol(DEFAULT_PROTOCOL);
    final int commsTimeout = context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue();
    config.setConnectionTimeout(commsTimeout);
    config.setSocketTimeout(commsTimeout);

    if(this.getSupportedPropertyDescriptors().contains(SSL_CONTEXT_SERVICE)) {
        final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
        if (sslContextService != null) {
            final SSLContext sslContext = sslContextService.createSSLContext(SslContextFactory.ClientAuth.NONE);
            // NIFI-3788: Changed hostnameVerifier from null to DHV (BrowserCompatibleHostnameVerifier is deprecated)
            SdkTLSSocketFactory sdkTLSSocketFactory = new SdkTLSSocketFactory(sslContext, new DefaultHostnameVerifier());
            config.getApacheHttpClientConfig().setSslSocketFactory(sdkTLSSocketFactory);
        }
    }

    final ProxyConfiguration proxyConfig = ProxyConfiguration.getConfiguration(context, () -> {
        if (context.getProperty(PROXY_HOST).isSet()) {
            final ProxyConfiguration componentProxyConfig = new ProxyConfiguration();
            String proxyHost = context.getProperty(PROXY_HOST).evaluateAttributeExpressions().getValue();
            Integer proxyPort = context.getProperty(PROXY_HOST_PORT).evaluateAttributeExpressions().asInteger();
            String proxyUsername = context.getProperty(PROXY_USERNAME).evaluateAttributeExpressions().getValue();
            String proxyPassword = context.getProperty(PROXY_PASSWORD).evaluateAttributeExpressions().getValue();
            componentProxyConfig.setProxyType(Proxy.Type.HTTP);
            componentProxyConfig.setProxyServerHost(proxyHost);
            componentProxyConfig.setProxyServerPort(proxyPort);
            componentProxyConfig.setProxyUserName(proxyUsername);
            componentProxyConfig.setProxyUserPassword(proxyPassword);
            return componentProxyConfig;
        }
        return ProxyConfiguration.DIRECT_CONFIGURATION;
    });

    if (Proxy.Type.HTTP.equals(proxyConfig.getProxyType())) {
        config.setProxyHost(proxyConfig.getProxyServerHost());
        config.setProxyPort(proxyConfig.getProxyServerPort());

        if (proxyConfig.hasCredential()) {
            config.setProxyUsername(proxyConfig.getProxyUserName());
            config.setProxyPassword(proxyConfig.getProxyUserPassword());
        }
    }

    return config;
}