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/06/23 10:47:27 UTC

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

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 296b07bd665e344ba4154f2f04076dda924461c1
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Jun 23 11:09:43 2023 +0200

    CAMEL-19159 - Camel-AWS: Support Profile Credential provider as configuration - AWS Translate
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 .../aws2/translate/Translate2Component.java        |  4 +-
 .../aws2/translate/Translate2Configuration.java    | 27 +++++++
 .../translate/client/Translate2ClientFactory.java  | 10 ++-
 .../impl/Translate2ClientIAMProfileOptimized.java  | 94 ++++++++++++++++++++++
 4 files changed, 131 insertions(+), 4 deletions(-)

diff --git a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Component.java b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Component.java
index 1eb86343d81..c021af3b832 100644
--- a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Component.java
+++ b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Component.java
@@ -47,10 +47,10 @@ public class Translate2Component extends DefaultComponent {
 
         Translate2Endpoint endpoint = new Translate2Endpoint(uri, this, configuration);
         setProperties(endpoint, parameters);
-        if (Boolean.FALSE.equals(configuration.isUseDefaultCredentialsProvider()) && configuration.getTranslateClient() == null
+        if (Boolean.FALSE.equals(configuration.isUseDefaultCredentialsProvider()) && Boolean.FALSE.equals(configuration.isUseProfileCredentialsProvider()) && configuration.getTranslateClient() == null
                 && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) {
             throw new IllegalArgumentException(
-                    "useDefaultCredentialsProvider is set to false, Amazon translate client or accessKey and secretKey must be specified");
+                    "useDefaultCredentialsProvider is set to false, useProfileCredentialsProvider is set to false, Amazon translate client or accessKey and secretKey must be specified");
         }
         return endpoint;
     }
diff --git a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Configuration.java b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Configuration.java
index c8c2e114483..355f4effd44 100644
--- a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Configuration.java
+++ b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/Translate2Configuration.java
@@ -64,6 +64,11 @@ public class Translate2Configuration implements Cloneable {
     private String uriEndpointOverride;
     @UriParam(defaultValue = "false")
     private boolean useDefaultCredentialsProvider;
+    @UriParam(defaultValue = "false")
+    private boolean useProfileCredentialsProvider;
+    @UriParam
+    private String profileCredentialsName;
+
 
     public TranslateClient getTranslateClient() {
         return translateClient;
@@ -243,6 +248,28 @@ public class Translate2Configuration implements Cloneable {
     public Boolean isUseDefaultCredentialsProvider() {
         return useDefaultCredentialsProvider;
     }
+
+    public boolean isUseProfileCredentialsProvider() {
+        return useProfileCredentialsProvider;
+    }
+
+    /**
+     * Set whether the Translate client should expect to load credentials through a profile credentials provider.
+     */
+    public void setUseProfileCredentialsProvider(boolean useProfileCredentialsProvider) {
+        this.useProfileCredentialsProvider = 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;
+    }
     // *************************************************
     //
     // *************************************************
diff --git a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/Translate2ClientFactory.java b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/Translate2ClientFactory.java
index b34bf738a64..5407b83a36f 100644
--- a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/Translate2ClientFactory.java
+++ b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/Translate2ClientFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.aws2.translate.client;
 
 import org.apache.camel.component.aws2.translate.Translate2Configuration;
 import org.apache.camel.component.aws2.translate.client.impl.Translate2ClientIAMOptimized;
+import org.apache.camel.component.aws2.translate.client.impl.Translate2ClientIAMProfileOptimized;
 import org.apache.camel.component.aws2.translate.client.impl.Translate2ClientStandardImpl;
 
 /**
@@ -35,7 +36,12 @@ public final class Translate2ClientFactory {
      * @return               TranslateClient
      */
     public static Translate2InternalClient getTranslateClient(Translate2Configuration configuration) {
-        return Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())
-                ? new Translate2ClientIAMOptimized(configuration) : new Translate2ClientStandardImpl(configuration);
+        if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) {
+            return new Translate2ClientIAMOptimized(configuration);
+        } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) {
+            return new Translate2ClientIAMProfileOptimized(configuration);
+        } else {
+            return new Translate2ClientStandardImpl(configuration);
+        }
     }
 }
diff --git a/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/impl/Translate2ClientIAMProfileOptimized.java b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/impl/Translate2ClientIAMProfileOptimized.java
new file mode 100644
index 00000000000..04fcc69f04e
--- /dev/null
+++ b/components/camel-aws/camel-aws2-translate/src/main/java/org/apache/camel/component/aws2/translate/client/impl/Translate2ClientIAMProfileOptimized.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.translate.client.impl;
+
+import org.apache.camel.component.aws2.translate.Translate2Configuration;
+import org.apache.camel.component.aws2.translate.client.Translate2InternalClient;
+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.translate.TranslateClient;
+import software.amazon.awssdk.services.translate.TranslateClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS Translate client for all users to use. This implementation is for remote instances to manage the
+ * credentials on their own (eliminating credential rotations)
+ */
+public class Translate2ClientIAMProfileOptimized implements Translate2InternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Translate2ClientIAMProfileOptimized.class);
+    private Translate2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Translate2ClientIAMProfileOptimized(Translate2Configuration configuration) {
+        LOG.trace("Creating an AWS Translate client for working on AWS Services");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the Translate aws client that is used.
+     *
+     * @return Amazon Translate Client.
+     */
+    @Override
+    public TranslateClient getTranslateClient() {
+        TranslateClient client = null;
+        TranslateClientBuilder clientBuilder = TranslateClient.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.httpClientBuilder(httpClientBuilder)
+                    .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;
+    }
+}