This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0eb391575ce41e20a8d50273c341c2de29cec54f
Author: Andrea Cosentino <[email protected]>
AuthorDate: Tue Mar 21 10:20:40 2023 +0100

    CAMEL-19159 - Camel-AWS: Support Profile Credential provider as 
configuration - AWS SQS
    
    Signed-off-by: Andrea Cosentino <[email protected]>
---
 .../component/aws2/sqs/Sqs2Configuration.java      |  27 ++++
 .../aws2/sqs/client/Sqs2ClientFactory.java         |  10 +-
 .../impl/Sqs2ClientIAMProfileOptimizedImpl.java    | 142 +++++++++++++++++++++
 3 files changed, 177 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
 
b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
index 908a7d25f4f..4cbac3f2b88 100644
--- 
a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
+++ 
b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
@@ -122,6 +122,11 @@ public class Sqs2Configuration implements Cloneable {
     @UriParam(defaultValue = "false")
     private boolean useDefaultCredentialsProvider;
 
+    @UriParam(defaultValue = "false")
+    private boolean useProfileCredentialsProvider;
+    @UriParam
+    private String profileCredentialsName;
+
     /**
      * Whether or not the queue is a FIFO queue
      */
@@ -578,6 +583,17 @@ public class Sqs2Configuration implements Cloneable {
         this.useDefaultCredentialsProvider = useDefaultCredentialsProvider;
     }
 
+    /**
+     * Set whether the SQS client should expect to load credentials through a 
profile credentials provider.
+     */
+    public void setUseProfileCredentialsProvider(boolean 
useProfileCredentialsProvider) {
+        this.useProfileCredentialsProvider = useProfileCredentialsProvider;
+    }
+
+    public boolean isUseProfileCredentialsProvider() {
+        return useProfileCredentialsProvider;
+    }
+
     public String getBatchSeparator() {
         return batchSeparator;
     }
@@ -629,6 +645,17 @@ public class Sqs2Configuration implements Cloneable {
         this.uriEndpointOverride = uriEndpointOverride;
     }
 
+    public String getProfileCredentialsName() {
+        return profileCredentialsName;
+    }
+
+    /**
+     * If using a profile credentials provider this parameter will set the 
profile name
+     */
+    public void setProfileCredentialsName(String profileCredentialsName) {
+        this.profileCredentialsName = profileCredentialsName;
+    }
+
     // *************************************************
     //
     // *************************************************
diff --git 
a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
 
b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
index 9b87a1c261b..0ae106f0d90 100644
--- 
a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
+++ 
b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.aws2.sqs.client;
 
 import org.apache.camel.component.aws2.sqs.Sqs2Configuration;
 import org.apache.camel.component.aws2.sqs.client.impl.Sqs2ClientIAMOptimized;
+import 
org.apache.camel.component.aws2.sqs.client.impl.Sqs2ClientIAMProfileOptimizedImpl;
 import org.apache.camel.component.aws2.sqs.client.impl.Sqs2ClientStandardImpl;
 
 /**
@@ -35,7 +36,12 @@ public final class Sqs2ClientFactory {
      * @return               SqsClient
      */
     public static Sqs2InternalClient getSqsClient(Sqs2Configuration 
configuration) {
-        return configuration.isUseDefaultCredentialsProvider()
-                ? new Sqs2ClientIAMOptimized(configuration) : new 
Sqs2ClientStandardImpl(configuration);
+        if 
(Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) {
+            return new Sqs2ClientIAMOptimized(configuration);
+        } else if 
(Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) {
+            return new Sqs2ClientIAMProfileOptimizedImpl(configuration);
+        } else {
+            return new Sqs2ClientStandardImpl(configuration);
+        }
     }
 }
diff --git 
a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientIAMProfileOptimizedImpl.java
 
b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientIAMProfileOptimizedImpl.java
new file mode 100644
index 00000000000..1ccbc587487
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientIAMProfileOptimizedImpl.java
@@ -0,0 +1,142 @@
+/*
+ * 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.sqs.client.impl;
+
+import org.apache.camel.component.aws2.sqs.Sqs2Configuration;
+import org.apache.camel.component.aws2.sqs.client.Sqs2InternalClient;
+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.ProfileCredentialsProvider;
+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.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.SqsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS SQS client for all users to use. This implementation is for 
local instances to use a static and solid
+ * credential set.
+ */
+public class Sqs2ClientIAMProfileOptimizedImpl implements Sqs2InternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(Sqs2ClientStandardImpl.class);
+    private Sqs2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Sqs2ClientIAMProfileOptimizedImpl(Sqs2Configuration configuration) {
+        LOG.trace("Creating an AWS SQS manager using static credentials.");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the s3 aws client that is used.
+     *
+     * @return Amazon S3 Client.
+     */
+    @Override
+    public SqsClient getSQSClient() {
+        SqsClient client = null;
+        SqsClientBuilder clientBuilder = SqsClient.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.getProfileCredentialsName() != null) {
+            if (isClientConfigFound) {
+                clientBuilder = 
clientBuilder.httpClientBuilder(httpClientBuilder)
+                        
.credentialsProvider(ProfileCredentialsProvider.create(configuration.getProfileCredentialsName()));
+            } else {
+                clientBuilder = clientBuilder
+                        
.credentialsProvider(ProfileCredentialsProvider.create(configuration.getProfileCredentialsName()));
+            }
+        } else {
+            if (!isClientConfigFound) {
+                clientBuilder = 
clientBuilder.httpClientBuilder(httpClientBuilder);
+            }
+        }
+
+        if (!isDefaultAwsHost()) {
+            String endpointOverrideUri = getAwsEndpointUri();
+            clientBuilder.endpointOverride(URI.create(endpointOverrideUri));
+        }
+
+        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;
+    }
+
+    private boolean isDefaultAwsHost() {
+        return configuration.getAmazonAWSHost().equals("amazonaws.com");
+    }
+
+    /*
+     * Gets the base endpoint for AWS (ie.: http(s)://host:port.
+     *
+     * Do not confuse with other Camel endpoint methods: this one is named 
after AWS'
+     * own endpoint terminology and can also be used for the endpoint override 
in the
+     * client builder.
+     */
+    private String getAwsEndpointUri() {
+        return configuration.getProtocol() + "://" + 
getFullyQualifiedAWSHost();
+    }
+
+    /*
+     * If using a different AWS host, do not assume specific parts of the AWS
+     * host and, instead, just return whatever is provided as the host.
+     */
+    private String getFullyQualifiedAWSHost() {
+        String host = configuration.getAmazonAWSHost();
+        host = FileUtil.stripTrailingSeparator(host);
+
+        if (isDefaultAwsHost()) {
+            return "sqs." + Region.of(configuration.getRegion()).id() + "." + 
host;
+        }
+
+        return host;
+    }
+}

Reply via email to