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 90b1a2af4597eed7e26d4c35efd3bca3259a7121
Author: Andrea Cosentino <anco...@gmail.com>
AuthorDate: Mon Nov 9 08:11:20 2020 +0100

    CAMEL-15785 - Camel-AWS2: make possibile to use a client based on 
DefaultCredentialProvider in all the interested components - SNS
---
 .../component/aws2/sns/Sns2Configuration.java      |  14 +++
 .../camel/component/aws2/sns/Sns2Endpoint.java     |  61 +-----------
 .../aws2/sns/client/Sns2ClientFactory.java         |  41 +++++++++
 .../aws2/sns/client/Sns2InternalClient.java        |  30 ++++++
 .../sns/client/impl/Sns2ClientIAMOptimized.java    | 101 ++++++++++++++++++++
 .../sns/client/impl/Sns2ClientStandardImpl.java    | 102 +++++++++++++++++++++
 6 files changed, 291 insertions(+), 58 deletions(-)

diff --git 
a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
index 7ef6f92..716fe14 100644
--- 
a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
+++ 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
@@ -65,6 +65,8 @@ public class Sns2Configuration implements Cloneable {
     private boolean trustAllCertificates;
     @UriParam(label = "common", defaultValue = "true")
     private boolean autoDiscoverClient = true;
+    @UriParam(defaultValue = "false")
+    private boolean useIAMCredentials;    
 
     public String getSubject() {
         return subject;
@@ -276,6 +278,18 @@ public class Sns2Configuration implements Cloneable {
     public void setAutoDiscoverClient(boolean autoDiscoverClient) {
         this.autoDiscoverClient = autoDiscoverClient;
     }
+    
+    public boolean isUseIAMCredentials() {
+        return useIAMCredentials;
+    }
+
+    /**
+     * Set whether the SQS client should expect to load credentials on an AWS 
infra instance or to expect static
+     * credentials to be passed in.
+     */
+    public void setUseIAMCredentials(boolean useIAMCredentials) {
+        this.useIAMCredentials = useIAMCredentials;
+    }
 
     // *************************************************
     //
diff --git 
a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java
 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java
index 2b1ecca..aeb11b0 100644
--- 
a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java
+++ 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java
@@ -25,6 +25,7 @@ import org.apache.camel.Component;
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.component.aws2.sns.client.Sns2ClientFactory;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategyAware;
 import org.apache.camel.spi.Metadata;
@@ -35,16 +36,8 @@ import org.apache.camel.support.DefaultEndpoint;
 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.awscore.exception.AwsServiceException;
-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.sns.SnsClient;
-import software.amazon.awssdk.services.sns.SnsClientBuilder;
 import software.amazon.awssdk.services.sns.model.CreateTopicRequest;
 import software.amazon.awssdk.services.sns.model.CreateTopicResponse;
 import software.amazon.awssdk.services.sns.model.ListTopicsRequest;
@@ -53,7 +46,6 @@ import 
software.amazon.awssdk.services.sns.model.SetTopicAttributesRequest;
 import software.amazon.awssdk.services.sns.model.SubscribeRequest;
 import software.amazon.awssdk.services.sns.model.SubscribeResponse;
 import software.amazon.awssdk.services.sns.model.Topic;
-import software.amazon.awssdk.utils.AttributeMap;
 
 /**
  * Send messages to an AWS Simple Notification Topic using AWS SDK version 2.x.
@@ -106,7 +98,8 @@ public class Sns2Endpoint extends DefaultEndpoint implements 
HeaderFilterStrateg
     @Override
     public void doInit() throws Exception {
         super.doInit();
-        snsClient = configuration.getAmazonSNSClient() != null ? 
configuration.getAmazonSNSClient() : createSNSClient();
+        snsClient = configuration.getAmazonSNSClient() != null
+                ? configuration.getAmazonSNSClient() : 
Sns2ClientFactory.getSnsClient(configuration).getSNSClient();
 
         // check the setting the headerFilterStrategy
         if (headerFilterStrategy == null) {
@@ -204,52 +197,4 @@ public class Sns2Endpoint extends DefaultEndpoint 
implements HeaderFilterStrateg
     public SnsClient getSNSClient() {
         return snsClient;
     }
-
-    /**
-     * Provide the possibility to override this method for an mock 
implementation
-     *
-     * @return AmazonSNSClient
-     */
-    SnsClient createSNSClient() {
-        SnsClient client = null;
-        SnsClientBuilder clientBuilder = SnsClient.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.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-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.java
 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.java
new file mode 100644
index 0000000..3e1fd60
--- /dev/null
+++ 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.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.sns.client;
+
+import org.apache.camel.component.aws2.sns.Sns2Configuration;
+import org.apache.camel.component.aws2.sns.client.impl.Sns2ClientIAMOptimized;
+import org.apache.camel.component.aws2.sns.client.impl.Sns2ClientStandardImpl;
+
+/**
+ * Factory class to return the correct type of AWS SNS aws.
+ */
+public final class Sns2ClientFactory {
+
+    private Sns2ClientFactory() {
+    }
+
+    /**
+     * Return the correct aws SNS client (based on remote vs local).
+     * 
+     * @param  configuration configuration
+     * @return               SNSClient
+     */
+    public static Sns2InternalClient getSnsClient(Sns2Configuration 
configuration) {
+        return configuration.isUseIAMCredentials()
+                ? new Sns2ClientIAMOptimized(configuration) : new 
Sns2ClientStandardImpl(configuration);
+    }
+}
diff --git 
a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2InternalClient.java
 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2InternalClient.java
new file mode 100644
index 0000000..015692f
--- /dev/null
+++ 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2InternalClient.java
@@ -0,0 +1,30 @@
+/*
+ * 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.sns.client;
+
+import software.amazon.awssdk.services.sns.SnsClient;
+
+public interface Sns2InternalClient {
+
+    /**
+     * Returns an sns client after a factory method determines which one to 
return.
+     * 
+     * @return SnsClient snsClient
+     */
+    SnsClient getSNSClient();
+
+}
diff --git 
a/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMOptimized.java
 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMOptimized.java
new file mode 100644
index 0000000..d97e70b
--- /dev/null
+++ 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMOptimized.java
@@ -0,0 +1,101 @@
+/*
+ * 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.sns.client.impl;
+
+import java.net.URI;
+
+import org.apache.camel.component.aws2.sns.Sns2Configuration;
+import org.apache.camel.component.aws2.sns.client.Sns2InternalClient;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+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.sns.SnsClient;
+import software.amazon.awssdk.services.sns.SnsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+/**
+ * Manage an AWS SNS client for all users to use. This implementation is for 
remote instances to manage the credentials
+ * on their own (eliminating credential rotations)
+ */
+public class Sns2ClientIAMOptimized implements Sns2InternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(Sns2ClientIAMOptimized.class);
+    private Sns2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Sns2ClientIAMOptimized(Sns2Configuration configuration) {
+        LOG.trace("Creating an AWS SNS client for working on AWS Services");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the SNS AWS client that is used.
+     * 
+     * @return Amazon SNS Client.
+     */
+    @Override
+    public SnsClient getSNSClient() {
+        SnsClient client = null;
+        SnsClientBuilder clientBuilder = SnsClient.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.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-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientStandardImpl.java
 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientStandardImpl.java
new file mode 100644
index 0000000..7b17b9f
--- /dev/null
+++ 
b/components/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientStandardImpl.java
@@ -0,0 +1,102 @@
+/*
+ * 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.sns.client.impl;
+
+import java.net.URI;
+
+import org.apache.camel.component.aws2.sns.Sns2Configuration;
+import org.apache.camel.component.aws2.sns.client.Sns2InternalClient;
+import org.apache.camel.util.FileUtil;
+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.sns.SnsClient;
+import software.amazon.awssdk.services.sns.SnsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+/**
+ * Manage an AWS SNS client for all users to use. This implementation is for 
local instances to use a static and solid
+ * credential set.
+ */
+public class Sns2ClientStandardImpl implements Sns2InternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(Sns2ClientStandardImpl.class);
+    private Sns2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Sns2ClientStandardImpl(Sns2Configuration configuration) {
+        LOG.trace("Creating an AWS SNS manager using static credentials.");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the SNS aws client that is used.
+     * 
+     * @return Amazon SNS Client.
+     */
+       @Override
+       public SnsClient getSNSClient() {
+        SnsClient client = null;
+        SnsClientBuilder clientBuilder = SnsClient.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.isTrustAllCertificates()) {
+            SdkHttpClient ahc = 
ApacheHttpClient.builder().buildWithDefaults(AttributeMap
+                    .builder()
+                    .put(
+                            SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES,
+                            Boolean.TRUE)
+                    .build());
+            clientBuilder.httpClient(ahc);
+        }
+        client = clientBuilder.build();
+        return client;
+       }
+}

Reply via email to