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 e3899d28babcff0657df35ff612ddbb2aa2d2737
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Jun 15 12:19:08 2023 +0200

    CAMEL-19159 - Camel-AWS: Support Profile Credential provider as 
configuration - AWS Eventbridge
    
    Signed-off-by: Andrea Cosentino <[email protected]>
---
 .../client/EventbridgeClientFactory.java           | 10 ++-
 .../EventbridgeClientIAMProfileOptimizedImpl.java  | 93 ++++++++++++++++++++++
 2 files changed, 101 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/EventbridgeClientFactory.java
 
b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/EventbridgeClientFactory.java
index 2ef3c71df10..e1588a05195 100644
--- 
a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/EventbridgeClientFactory.java
+++ 
b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/EventbridgeClientFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.aws2.eventbridge.client;
 
 import org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration;
 import 
org.apache.camel.component.aws2.eventbridge.client.impl.EventbridgeClientIAMOptimizedImpl;
+import 
org.apache.camel.component.aws2.eventbridge.client.impl.EventbridgeClientIAMProfileOptimizedImpl;
 import 
org.apache.camel.component.aws2.eventbridge.client.impl.EventbridgeClientStandardImpl;
 
 /**
@@ -35,7 +36,12 @@ public final class EventbridgeClientFactory {
      * @return               EventBridgeClient
      */
     public static EventbridgeInternalClient 
getEventbridgeClient(EventbridgeConfiguration configuration) {
-        return 
Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())
-                ? new EventbridgeClientIAMOptimizedImpl(configuration) : new 
EventbridgeClientStandardImpl(configuration);
+        if 
(Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) {
+            return new EventbridgeClientIAMOptimizedImpl(configuration);
+        } else if 
(Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) {
+            return new EventbridgeClientIAMProfileOptimizedImpl(configuration);
+        } else {
+            return new EventbridgeClientStandardImpl(configuration);
+        }
     }
 }
diff --git 
a/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/impl/EventbridgeClientIAMProfileOptimizedImpl.java
 
b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/impl/EventbridgeClientIAMProfileOptimizedImpl.java
new file mode 100644
index 00000000000..21e5893d261
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-eventbridge/src/main/java/org/apache/camel/component/aws2/eventbridge/client/impl/EventbridgeClientIAMProfileOptimizedImpl.java
@@ -0,0 +1,93 @@
+/*
+ * 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.eventbridge.client.impl;
+
+import org.apache.camel.component.aws2.eventbridge.EventbridgeConfiguration;
+import 
org.apache.camel.component.aws2.eventbridge.client.EventbridgeInternalClient;
+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.eventbridge.EventBridgeClient;
+import software.amazon.awssdk.services.eventbridge.EventBridgeClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS Eventbridge client for all users to use (enabling temporary 
creds). This implementation is for remote
+ * instances to manage the credentials on their own (eliminating credential 
rotations)
+ */
+public class EventbridgeClientIAMProfileOptimizedImpl implements 
EventbridgeInternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(EventbridgeClientIAMProfileOptimizedImpl.class);
+    private EventbridgeConfiguration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public EventbridgeClientIAMProfileOptimizedImpl(EventbridgeConfiguration 
configuration) {
+        LOG.trace("Creating an AWS Eventbridge client for an ec2 instance with 
IAM temporary credentials (normal for ec2s).");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the Eventbridge aws client that is used.
+     *
+     * @return Eventbridge Client.
+     */
+    @Override
+    public EventBridgeClient getEventbridgeClient() {
+        EventBridgeClient client = null;
+        EventBridgeClientBuilder clientBuilder = EventBridgeClient.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;
+    }
+}

Reply via email to