camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [camel] 01/03: CAMEL-15785 - Camel-AWS2: make possibile to use a client based on DefaultCredentialProvider in all the interested components - AWS2-SQS
Date Wed, 04 Nov 2020 10:33:22 GMT
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 baed3560c6e7bb263500f2190c7a52cbd02d08f0
Author: Andrea Cosentino <ancosen@gmail.com>
AuthorDate: Wed Nov 4 09:03:02 2020 +0100

    CAMEL-15785 - Camel-AWS2: make possibile to use a client based on DefaultCredentialProvider
in all the interested components - AWS2-SQS
---
 .../component/aws2/sqs/Sqs2Configuration.java      |  18 ++-
 .../camel/component/aws2/sqs/Sqs2Endpoint.java     |   5 +-
 .../aws2/sqs/client/Sqs2ClientFactory.java         |  38 ++++++
 .../aws2/sqs/client/Sqs2InternalClient.java        |  30 +++++
 .../sqs/client/impl/Sqs2ClientIAMOptimized.java    | 102 +++++++++++++++
 .../sqs/client/impl/Sqs2ClientStandardImpl.java    | 138 +++++++++++++++++++++
 6 files changed, 329 insertions(+), 2 deletions(-)

diff --git a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
index 37d69a0..3d56e14 100644
--- a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
+++ b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Configuration.java
@@ -111,6 +111,9 @@ public class Sqs2Configuration implements Cloneable {
     // Likely used only for testing
     @UriParam(defaultValue = "https")
     private String protocol = "https";
+    
+    @UriParam(defaultValue = "false")
+    private boolean useIAMCredentials;
 
     /**
      * Whether or not the queue is a FIFO queue
@@ -567,12 +570,25 @@ public class Sqs2Configuration 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;
+	}    
 
+    
     // *************************************************
     //
     // *************************************************
 
-    public Sqs2Configuration copy() {
+	public Sqs2Configuration copy() {
         try {
             return (Sqs2Configuration) super.clone();
         } catch (CloneNotSupportedException e) {
diff --git a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Endpoint.java
b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Endpoint.java
index a8106c5..1ec1b67 100644
--- a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Endpoint.java
+++ b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Endpoint.java
@@ -28,6 +28,8 @@ import org.apache.camel.ExchangePattern;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.component.aws2.s3.client.AWS2S3ClientFactory;
+import org.apache.camel.component.aws2.sqs.client.Sqs2ClientFactory;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategyAware;
 import org.apache.camel.spi.Metadata;
@@ -152,7 +154,8 @@ public class Sqs2Endpoint extends ScheduledPollEndpoint implements HeaderFilterS
     @Override
     protected void doInit() throws Exception {
         super.doInit();
-        client = getConfiguration().getAmazonSQSClient() != null ? getConfiguration().getAmazonSQSClient()
: getClient();
+        client = configuration.getAmazonSQSClient() != null
+                ? configuration.getAmazonSQSClient() : Sqs2ClientFactory.getSqsClient(configuration).getSQSClient();
 
         // check the setting the headerFilterStrategy
         if (headerFilterStrategy == null) {
diff --git a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
new file mode 100644
index 0000000..3f96011
--- /dev/null
+++ b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2ClientFactory.java
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+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.Sqs2ClientStandardImpl;
+
+/**
+ * Factory class to return the correct type of AWS SQS aws.
+ */
+public final class Sqs2ClientFactory {
+
+    /**
+     * Return the correct aws SQS client (based on remote vs local).
+     * 
+     * @param  configuration configuration
+     * @return               AWSS3Client
+     */
+    public static Sqs2InternalClient getSqsClient(Sqs2Configuration configuration) {
+        return configuration.isUseIAMCredentials()
+                ? new Sqs2ClientIAMOptimized(configuration) : new Sqs2ClientStandardImpl(configuration);
+    }
+}
diff --git a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2InternalClient.java
b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2InternalClient.java
new file mode 100644
index 0000000..d76ea53
--- /dev/null
+++ b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/Sqs2InternalClient.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.sqs.client;
+
+import software.amazon.awssdk.services.sqs.SqsClient;
+
+public interface Sqs2InternalClient {
+	
+    /**
+     * Returns an sqs client after a factory method determines which one to return.
+     * 
+     * @return SqsClient sqsClient
+     */
+	SqsClient getSQSClient();
+
+}
diff --git a/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientIAMOptimized.java
b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientIAMOptimized.java
new file mode 100644
index 0000000..1170f18
--- /dev/null
+++ b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientIAMOptimized.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.sqs.client.impl;
+
+import java.net.URI;
+
+import org.apache.camel.component.aws2.sqs.Sqs2Configuration;
+import org.apache.camel.component.aws2.sqs.client.Sqs2InternalClient;
+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.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.SqsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+/**
+ * Manage an AWS SQS client for all users to use. This implementation is for remote instances
+ * to manage the credentials on their own (eliminating credential rotations)
+ */
+public class Sqs2ClientIAMOptimized implements Sqs2InternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Sqs2ClientIAMOptimized.class);
+    private Sqs2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Sqs2ClientIAMOptimized(Sqs2Configuration configuration) {
+        LOG.trace("Creating an AWS SQS client for working on AWS Services");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the sqs aws client that is used.
+     * 
+     * @return Amazon SQS 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.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-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientStandardImpl.java
b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientStandardImpl.java
new file mode 100644
index 0000000..73bfaa8
--- /dev/null
+++ b/components/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/client/impl/Sqs2ClientStandardImpl.java
@@ -0,0 +1,138 @@
+/*
+ * 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 java.net.URI;
+
+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.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;
+
+/**
+ * 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 Sqs2ClientStandardImpl implements Sqs2InternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Sqs2ClientStandardImpl.class);
+    private Sqs2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Sqs2ClientStandardImpl(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.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 (!isDefaultAwsHost()) {
+            String endpointOverrideUri = getAwsEndpointUri();
+            clientBuilder.endpointOverride(URI.create(endpointOverrideUri));
+        }
+
+        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;
+    }
+    
+    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;
+    }
+}


Mime
View raw message