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 2024/01/22 11:11:49 UTC

(camel) 01/02: CAMEL-20281 - Camel-AWS Components: Make it possible to use AwsSessionCredentials to support temporary credentials - AWS SES

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

acosentino pushed a commit to branch CAMEL-20281-SES
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 27ca6f469195fa846c4bfbdb561b57472614fdc8
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Mon Jan 22 11:57:52 2024 +0100

    CAMEL-20281 - Camel-AWS Components: Make it possible to use AwsSessionCredentials to support temporary credentials - AWS SES
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 .../camel/component/aws2/ses/Ses2Component.java    |   3 +-
 .../component/aws2/ses/Ses2Configuration.java      |  28 ++++++
 .../aws2/ses/client/Ses2ClientFactory.java         |   5 +-
 .../client/impl/Ses2ClientSessionTokenImpl.java    | 110 +++++++++++++++++++++
 .../component/aws2/ses/Ses2ClientFactoryTest.java  |   9 ++
 5 files changed, 153 insertions(+), 2 deletions(-)

diff --git a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Component.java b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Component.java
index f82895d0e03..5e3796f2c61 100644
--- a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Component.java
+++ b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Component.java
@@ -53,10 +53,11 @@ public class Ses2Component extends HealthCheckComponent {
         setProperties(endpoint, parameters);
         if (Boolean.FALSE.equals(configuration.isUseDefaultCredentialsProvider())
                 && Boolean.FALSE.equals(configuration.isUseProfileCredentialsProvider())
+                && Boolean.FALSE.equals(configuration.isUseSessionCredentials())
                 && configuration.getAmazonSESClient() == null
                 && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) {
             throw new IllegalArgumentException(
-                    "useDefaultCredentialsProvider is set to false, useProfileCredentialsProvider is set to false, AmazonSESClient or accessKey and secretKey must be specified");
+                    "useDefaultCredentialsProvider is set to false, useProfileCredentialsProvider is set to false, useSessionCredentials is set to false, AmazonSESClient or accessKey and secretKey must be specified");
         }
 
         return endpoint;
diff --git a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Configuration.java b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Configuration.java
index 6039573b509..8d3e01ced58 100644
--- a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Configuration.java
+++ b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/Ses2Configuration.java
@@ -37,6 +37,9 @@ public class Ses2Configuration implements Cloneable {
     private String accessKey;
     @UriParam(label = "security", secret = true)
     private String secretKey;
+
+    @UriParam(label = "security", secret = true)
+    private String sessionToken;
     @UriParam
     private String subject;
     @UriParam
@@ -70,6 +73,8 @@ public class Ses2Configuration implements Cloneable {
     @UriParam(label = "security")
     private boolean useProfileCredentialsProvider;
     @UriParam(label = "security")
+    private boolean useSessionCredentials;
+    @UriParam(label = "security")
     private String profileCredentialsName;
 
     public String getAccessKey() {
@@ -151,6 +156,17 @@ public class Ses2Configuration implements Cloneable {
         this.secretKey = secretKey;
     }
 
+    public String getSessionToken() {
+        return sessionToken;
+    }
+
+    /**
+     * Amazon AWS Session Token used when the user needs to assume a IAM role
+     */
+    public void setSessionToken(String sessionToken) {
+        this.sessionToken = sessionToken;
+    }
+
     public String getSubject() {
         return subject;
     }
@@ -302,6 +318,18 @@ public class Ses2Configuration implements Cloneable {
         this.useProfileCredentialsProvider = useProfileCredentialsProvider;
     }
 
+    public boolean isUseSessionCredentials() {
+        return useSessionCredentials;
+    }
+
+    /**
+     * Set whether the SES client should expect to use Session Credentials. This is useful in situation in which the user
+     * needs to assume a IAM role for doing operations in SES.
+     */
+    public void setUseSessionCredentials(boolean useSessionCredentials) {
+        this.useSessionCredentials = useSessionCredentials;
+    }
+
     public String getProfileCredentialsName() {
         return profileCredentialsName;
     }
diff --git a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/Ses2ClientFactory.java b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/Ses2ClientFactory.java
index 890c7964ce4..648baefe6d0 100644
--- a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/Ses2ClientFactory.java
+++ b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/Ses2ClientFactory.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.aws2.ses.client;
 import org.apache.camel.component.aws2.ses.Ses2Configuration;
 import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientOptimizedImpl;
 import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientProfileOptimizedImpl;
+import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientSessionTokenImpl;
 import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientStandardImpl;
 
 /**
@@ -40,7 +41,9 @@ public final class Ses2ClientFactory {
             return new Ses2ClientOptimizedImpl(configuration);
         } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) {
             return new Ses2ClientProfileOptimizedImpl(configuration);
-        } else {
+        } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) {
+            return new Ses2ClientSessionTokenImpl(configuration);
+        }else {
             return new Ses2ClientStandardImpl(configuration);
         }
     }
diff --git a/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/impl/Ses2ClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/impl/Ses2ClientSessionTokenImpl.java
new file mode 100644
index 00000000000..0180baee217
--- /dev/null
+++ b/components/camel-aws/camel-aws2-ses/src/main/java/org/apache/camel/component/aws2/ses/client/impl/Ses2ClientSessionTokenImpl.java
@@ -0,0 +1,110 @@
+/*
+ * 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.ses.client.impl;
+
+import org.apache.camel.component.aws2.ses.Ses2Configuration;
+import org.apache.camel.component.aws2.ses.client.Ses2InternalClient;
+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.AwsSessionCredentials;
+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.ses.SesClient;
+import software.amazon.awssdk.services.ses.SesClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS SES client for all users to use. This implementation is for local instances to use a static and solid
+ * credential set.
+ */
+public class Ses2ClientSessionTokenImpl implements Ses2InternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Ses2ClientSessionTokenImpl.class);
+    private Ses2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Ses2ClientSessionTokenImpl(Ses2Configuration configuration) {
+        LOG.trace("Creating an AWS SES manager using static credentials.");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the SES AWS client that is used.
+     *
+     * @return Amazon SES Client.
+     */
+    @Override
+    public SesClient getSesClient() {
+        SesClient client = null;
+        SesClientBuilder clientBuilder = SesClient.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) {
+            AwsSessionCredentials cred = AwsSessionCredentials.create(configuration.getAccessKey(), configuration.getSecretKey(), configuration.getSessionToken());
+            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()) {
+            if (httpClientBuilder == null) {
+                httpClientBuilder = ApacheHttpClient.builder();
+            }
+            SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap
+                    .builder()
+                    .put(
+                            SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES,
+                            Boolean.TRUE)
+                    .build());
+            // set created http client to use instead of builder
+            clientBuilder.httpClient(ahc);
+            clientBuilder.httpClientBuilder(null);
+        }
+        client = clientBuilder.build();
+        return client;
+    }
+}
diff --git a/components/camel-aws/camel-aws2-ses/src/test/java/org/apache/camel/component/aws2/ses/Ses2ClientFactoryTest.java b/components/camel-aws/camel-aws2-ses/src/test/java/org/apache/camel/component/aws2/ses/Ses2ClientFactoryTest.java
index 55266ab18ee..b194e8787e4 100644
--- a/components/camel-aws/camel-aws2-ses/src/test/java/org/apache/camel/component/aws2/ses/Ses2ClientFactoryTest.java
+++ b/components/camel-aws/camel-aws2-ses/src/test/java/org/apache/camel/component/aws2/ses/Ses2ClientFactoryTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.aws2.ses;
 import org.apache.camel.component.aws2.ses.client.Ses2ClientFactory;
 import org.apache.camel.component.aws2.ses.client.Ses2InternalClient;
 import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientOptimizedImpl;
+import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientSessionTokenImpl;
 import org.apache.camel.component.aws2.ses.client.impl.Ses2ClientStandardImpl;
 import org.junit.jupiter.api.Test;
 
@@ -48,4 +49,12 @@ public class Ses2ClientFactoryTest {
         Ses2InternalClient sesClient = Ses2ClientFactory.getSesClient(ses2Configuration);
         assertTrue(sesClient instanceof Ses2ClientOptimizedImpl);
     }
+
+    @Test
+    public void getSESSessionTokenImplClient() {
+        Ses2Configuration ses2Configuration = new Ses2Configuration();
+        ses2Configuration.setUseSessionCredentials(true);
+        Ses2InternalClient sesClient = Ses2ClientFactory.getSesClient(ses2Configuration);
+        assertTrue(sesClient instanceof Ses2ClientSessionTokenImpl);
+    }
 }