You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2023/04/24 09:47:22 UTC

[camel] 01/06: CAMEL-19159 - Camel-AWS: Support Profile Credential provider as configuration - AWS SNS

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 95e8bf8b9c3d7cd9e9f272f569245d4c8ec70745
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Mon Apr 24 10:16:33 2023 +0200

    CAMEL-19159 - Camel-AWS: Support Profile Credential provider as configuration - AWS SNS
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 .../component/aws2/sns/Sns2Configuration.java      | 26 ++++++
 .../client/impl/Sns2ClientIAMProfileOptimized.java | 94 ++++++++++++++++++++++
 2 files changed, 120 insertions(+)

diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
index a549d3d115d..ed5ffe7c34f 100644
--- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
+++ b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java
@@ -72,6 +72,10 @@ public class Sns2Configuration implements Cloneable {
     private boolean trustAllCertificates;
     @UriParam(defaultValue = "false")
     private boolean useDefaultCredentialsProvider;
+    @UriParam(defaultValue = "false")
+    private boolean useProfileCredentialsProvider;
+    @UriParam
+    private String profileCredentialsName;
     @UriParam(label = "producer", javaType = "java.lang.String", enums = "useConstant,useExchangeId,usePropertyValue")
     private MessageGroupIdStrategy messageGroupIdStrategy;
     @UriParam(label = "producer", javaType = "java.lang.String", defaultValue = "useExchangeId",
@@ -290,6 +294,28 @@ public class Sns2Configuration implements Cloneable {
         this.useDefaultCredentialsProvider = useDefaultCredentialsProvider;
     }
 
+    /**
+     * Set whether the SNS 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 getProfileCredentialsName() {
+        return profileCredentialsName;
+    }
+
+    /**
+     * If using a profile credentials provider this parameter will set the profile name
+     */
+    public void setProfileCredentialsName(String profileCredentialsName) {
+        this.profileCredentialsName = profileCredentialsName;
+    }
+
     /**
      * Only for FIFO Topic. Strategy for setting the messageGroupId on the message. Can be one of the following options:
      * *useConstant*, *useExchangeId*, *usePropertyValue*. For the *usePropertyValue* option, the value of property
diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMProfileOptimized.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMProfileOptimized.java
new file mode 100644
index 00000000000..d4917344d69
--- /dev/null
+++ b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMProfileOptimized.java
@@ -0,0 +1,94 @@
+/*
+ * 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 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.ProfileCredentialsProvider;
+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;
+
+import java.net.URI;
+
+/**
+ * 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 Sns2ClientIAMProfileOptimized implements Sns2InternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Sns2ClientIAMProfileOptimized.class);
+    private Sns2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Sns2ClientIAMProfileOptimized(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;
+
+        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());
+            clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder);
+        }
+        if (configuration.getProfileCredentialsName() != null) {
+                clientBuilder = clientBuilder
+                        .credentialsProvider(ProfileCredentialsProvider.create(configuration.getProfileCredentialsName()));
+        }
+        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;
+    }
+}