This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 475ef60d475982196b95d623e4cba7bacbe9d9e7 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Wed Apr 7 13:59:27 2021 +0200 CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the components - Kinesis Firehose component --- .../client/KinesisFirehoseClientFactory.java | 41 ++++++++ .../client/KinesisFirehoseInternalClient.java | 33 +++++++ .../KinesisFirehoseClientIAMOptimizedImpl.java | 108 +++++++++++++++++++++ .../impl/KinesisFirehoseClientStandardImpl.java | 107 ++++++++++++++++++++ 4 files changed, 289 insertions(+) diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/KinesisFirehoseClientFactory.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/KinesisFirehoseClientFactory.java new file mode 100644 index 0000000..848343d --- /dev/null +++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/KinesisFirehoseClientFactory.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.firehose.client; + +import org.apache.camel.component.aws2.firehose.KinesisFirehose2Configuration; +import org.apache.camel.component.aws2.firehose.client.impl.KinesisFirehoseClientIAMOptimizedImpl; +import org.apache.camel.component.aws2.firehose.client.impl.KinesisFirehoseClientStandardImpl; + +/** + * Factory class to return the correct type of AWS Kinesis client. + */ +public final class KinesisFirehoseClientFactory { + + private KinesisFirehoseClientFactory() { + } + + /** + * Return the correct aws Kinesis Firehose client (based on remote vs local). + * + * @param configuration configuration + * @return FirehoseClient + */ + public static KinesisFirehoseInternalClient getKinesisFirehoseClient(KinesisFirehose2Configuration configuration) { + return configuration.isUseDefaultCredentialsProvider() + ? new KinesisFirehoseClientIAMOptimizedImpl(configuration) : new KinesisFirehoseClientStandardImpl(configuration); + } +} diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/KinesisFirehoseInternalClient.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/KinesisFirehoseInternalClient.java new file mode 100644 index 0000000..df28929 --- /dev/null +++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/KinesisFirehoseInternalClient.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.firehose.client; + +import software.amazon.awssdk.services.firehose.FirehoseClient; +import software.amazon.awssdk.services.kinesis.KinesisClient; + +/** + * Manage the required actions of a Kinesis Firehose client for either local or remote. + */ +public interface KinesisFirehoseInternalClient { + + /** + * Returns a Kinesis Firehose client after a factory method determines which one to return. + * + * @return FirehoseClient client + */ + FirehoseClient getKinesisFirehoseClient(); +} diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/impl/KinesisFirehoseClientIAMOptimizedImpl.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/impl/KinesisFirehoseClientIAMOptimizedImpl.java new file mode 100644 index 0000000..5cb6286 --- /dev/null +++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/impl/KinesisFirehoseClientIAMOptimizedImpl.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.firehose.client.impl; + +import org.apache.camel.component.aws2.firehose.KinesisFirehose2Configuration; +import org.apache.camel.component.aws2.firehose.client.KinesisFirehoseInternalClient; +import org.apache.camel.component.aws2.kinesis.Kinesis2Configuration; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.http.SdkHttpClient; +import software.amazon.awssdk.http.SdkHttpConfigurationOption; +import software.amazon.awssdk.http.apache.ApacheHttpClient; +import software.amazon.awssdk.http.apache.ProxyConfiguration; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.firehose.FirehoseClient; +import software.amazon.awssdk.services.firehose.FirehoseClientBuilder; +import software.amazon.awssdk.services.kinesis.KinesisClient; +import software.amazon.awssdk.services.kinesis.KinesisClientBuilder; +import software.amazon.awssdk.utils.AttributeMap; + +import java.net.URI; + +/** + * Manage an AWS Kinesis Firehose client for all users to use (enabling temporary creds). This implementation is for remote + * instances to manage the credentials on their own (eliminating credential rotations) + */ +public class KinesisFirehoseClientIAMOptimizedImpl implements KinesisFirehoseInternalClient { + private static final Logger LOG = LoggerFactory.getLogger(KinesisFirehoseClientIAMOptimizedImpl.class); + private KinesisFirehose2Configuration configuration; + + /** + * Constructor that uses the config file. + */ + public KinesisFirehoseClientIAMOptimizedImpl(KinesisFirehose2Configuration configuration) { + LOG.trace("Creating an AWS Kinesis Firehose client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); + this.configuration = configuration; + } + + /** + * Getting the Kinesis client that is used. + * + * @return Amazon Kinesis Client. + */ + @Override + public FirehoseClient getKinesisFirehoseClient() { + FirehoseClient client = null; + FirehoseClientBuilder clientBuilder = FirehoseClient.builder(); + ProxyConfiguration.Builder proxyConfig = null; + ApacheHttpClient.Builder httpClientBuilder = null; + boolean isClientConfigFound = false; + if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { + proxyConfig = ProxyConfiguration.builder(); + URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" + + configuration.getProxyPort()); + proxyConfig.endpoint(proxyEndpoint); + httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); + isClientConfigFound = true; + } + if (configuration.getAccessKey() != null && configuration.getSecretKey() != null) { + DefaultCredentialsProvider cred = DefaultCredentialsProvider.create(); + if (isClientConfigFound) { + clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder) + .credentialsProvider(cred); + } else { + clientBuilder = clientBuilder.credentialsProvider(cred); + } + } else { + if (!isClientConfigFound) { + clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); + } + } + if (ObjectHelper.isNotEmpty(configuration.getRegion())) { + clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); + } + if (configuration.isOverrideEndpoint()) { + clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); + } + if (configuration.isTrustAllCertificates()) { + SdkHttpClient ahc = ApacheHttpClient.builder().buildWithDefaults(AttributeMap + .builder() + .put( + SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, + Boolean.TRUE) + .build()); + clientBuilder.httpClient(ahc); + } + client = clientBuilder.build(); + return client; + } +} diff --git a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/impl/KinesisFirehoseClientStandardImpl.java b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/impl/KinesisFirehoseClientStandardImpl.java new file mode 100644 index 0000000..9f20d0e --- /dev/null +++ b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/firehose/client/impl/KinesisFirehoseClientStandardImpl.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.firehose.client.impl; + +import org.apache.camel.component.aws2.firehose.KinesisFirehose2Configuration; +import org.apache.camel.component.aws2.firehose.client.KinesisFirehoseInternalClient; +import org.apache.camel.component.aws2.kinesis.Kinesis2Configuration; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.http.SdkHttpClient; +import software.amazon.awssdk.http.SdkHttpConfigurationOption; +import software.amazon.awssdk.http.apache.ApacheHttpClient; +import software.amazon.awssdk.http.apache.ProxyConfiguration; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.firehose.FirehoseClient; +import software.amazon.awssdk.services.firehose.FirehoseClientBuilder; +import software.amazon.awssdk.services.kinesis.KinesisClient; +import software.amazon.awssdk.services.kinesis.KinesisClientBuilder; +import software.amazon.awssdk.utils.AttributeMap; + +import java.net.URI; + +/** + * Manage an AWS Kinesis Firehose client for all users to use. This implementation is for local instances to use a static and + * solid credential set. + */ +public class KinesisFirehoseClientStandardImpl implements KinesisFirehoseInternalClient { + private static final Logger LOG = LoggerFactory.getLogger(KinesisFirehoseClientStandardImpl.class); + private KinesisFirehose2Configuration configuration; + + /** + * Constructor that uses the config file. + */ + public KinesisFirehoseClientStandardImpl(KinesisFirehose2Configuration configuration) { + LOG.trace("Creating an AWS Kinesis Firehose manager using static credentials."); + this.configuration = configuration; + } + + /** + * Getting the Kinesis Firehose client that is used. + * + * @return Amazon Kinesis Firehose Client. + */ + @Override + public FirehoseClient getKinesisFirehoseClient() { + FirehoseClient client = null; + FirehoseClientBuilder clientBuilder = FirehoseClient.builder(); + ProxyConfiguration.Builder proxyConfig = null; + ApacheHttpClient.Builder httpClientBuilder = null; + boolean isClientConfigFound = false; + if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { + proxyConfig = ProxyConfiguration.builder(); + URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" + + configuration.getProxyPort()); + proxyConfig.endpoint(proxyEndpoint); + httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); + isClientConfigFound = true; + } + if (configuration.getAccessKey() != null && configuration.getSecretKey() != null) { + AwsBasicCredentials cred = AwsBasicCredentials.create(configuration.getAccessKey(), configuration.getSecretKey()); + if (isClientConfigFound) { + clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder) + .credentialsProvider(StaticCredentialsProvider.create(cred)); + } else { + clientBuilder = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)); + } + } else { + if (!isClientConfigFound) { + clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); + } + } + if (ObjectHelper.isNotEmpty(configuration.getRegion())) { + clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); + } + if (configuration.isOverrideEndpoint()) { + clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); + } + if (configuration.isTrustAllCertificates()) { + SdkHttpClient ahc = ApacheHttpClient.builder().buildWithDefaults(AttributeMap + .builder() + .put( + SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, + Boolean.TRUE) + .build()); + clientBuilder.httpClient(ahc); + } + client = clientBuilder.build(); + return client; + } +}