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/09 09:58:00 UTC

[camel] 02/07: CAMEL-19159 - Camel-AWS: Support Profile Credential provider as configuration - AWS Lambda

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 80246aeb5a2a2af1eba933bb22a3903ab4a90a0d
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Jun 9 11:37:44 2023 +0200

    CAMEL-19159 - Camel-AWS: Support Profile Credential provider as configuration - AWS Lambda
    
    Signed-off-by: Andrea Cosentino <an...@gmail.com>
---
 .../impl/Lambda2ClientIAMProfileOptimizedImpl.java | 103 +++++++++++++++++++++
 1 file changed, 103 insertions(+)

diff --git a/components/camel-aws/camel-aws2-lambda/src/main/java/org/apache/camel/component/aws2/lambda/client/impl/Lambda2ClientIAMProfileOptimizedImpl.java b/components/camel-aws/camel-aws2-lambda/src/main/java/org/apache/camel/component/aws2/lambda/client/impl/Lambda2ClientIAMProfileOptimizedImpl.java
new file mode 100644
index 00000000000..958669daacc
--- /dev/null
+++ b/components/camel-aws/camel-aws2-lambda/src/main/java/org/apache/camel/component/aws2/lambda/client/impl/Lambda2ClientIAMProfileOptimizedImpl.java
@@ -0,0 +1,103 @@
+/*
+ * 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.lambda.client.impl;
+
+import org.apache.camel.component.aws2.lambda.Lambda2Configuration;
+import org.apache.camel.component.aws2.lambda.client.Lambda2InternalClient;
+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.lambda.LambdaClient;
+import software.amazon.awssdk.services.lambda.LambdaClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS Lambda client for all users to use. This implementation is for local instances to use a static and solid
+ * credential set.
+ */
+public class Lambda2ClientIAMProfileOptimizedImpl implements Lambda2InternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Lambda2ClientIAMProfileOptimizedImpl.class);
+    private Lambda2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Lambda2ClientIAMProfileOptimizedImpl(Lambda2Configuration configuration) {
+        LOG.trace("Creating an AWS Lambda manager using profile credentials.");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the Lambda aws client that is used.
+     *
+     * @return Amazon Lambda Client.
+     */
+    @Override
+    public LambdaClient getLambdaClient() {
+        LambdaClient client = null;
+        LambdaClientBuilder clientBuilder = LambdaClient.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 (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;
+    }
+}