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 2ede2b763fe0ba65db315235646e1e5d011a54f7
Author: Andrea Cosentino <[email protected]>
AuthorDate: Fri May 14 07:17:40 2021 +0200

    CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the 
components - KMS Component
---
 .../aws2/kms/client/KMS2ClientFactory.java         |  41 ++++++++
 .../aws2/kms/client/KMS2InternalClient.java        |  32 +++++++
 .../kms/client/impl/KMS2ClientOptimizedImpl.java   |  88 +++++++++++++++++
 .../kms/client/impl/KMS2ClientStandardImpl.java    | 104 +++++++++++++++++++++
 4 files changed, 265 insertions(+)

diff --git 
a/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/KMS2ClientFactory.java
 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/KMS2ClientFactory.java
new file mode 100644
index 0000000..f56dd7f
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/KMS2ClientFactory.java
@@ -0,0 +1,41 @@
+/*
+ * 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.kms.client;
+
+import org.apache.camel.component.aws2.kms.KMS2Configuration;
+import org.apache.camel.component.aws2.kms.client.impl.KMS2ClientOptimizedImpl;
+import org.apache.camel.component.aws2.kms.client.impl.KMS2ClientStandardImpl;
+
+/**
+ * Factory class to return the correct type of AWS Eventbridge client.
+ */
+public final class KMS2ClientFactory {
+
+    private KMS2ClientFactory() {
+    }
+
+    /**
+     * Return the correct AWS KMS client (based on remote vs local).
+     * 
+     * @param  configuration configuration
+     * @return               KMSClient
+     */
+    public static KMS2InternalClient getKmsClient(KMS2Configuration 
configuration) {
+        return configuration.isUseDefaultCredentialsProvider()
+                ? new KMS2ClientOptimizedImpl(configuration) : new 
KMS2ClientStandardImpl(configuration);
+    }
+}
diff --git 
a/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/KMS2InternalClient.java
 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/KMS2InternalClient.java
new file mode 100644
index 0000000..eeb7bf3
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/KMS2InternalClient.java
@@ -0,0 +1,32 @@
+/*
+ * 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.kms.client;
+
+import software.amazon.awssdk.services.kms.KmsClient;
+
+/**
+ * Manage the required actions of an KMS client for either local or remote.
+ */
+public interface KMS2InternalClient {
+
+    /**
+     * Returns an KMS client after a factory method determines which one to 
return.
+     * 
+     * @return KmsClient KmsClient
+     */
+    KmsClient getKmsClient();
+}
diff --git 
a/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/impl/KMS2ClientOptimizedImpl.java
 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/impl/KMS2ClientOptimizedImpl.java
new file mode 100644
index 0000000..36648cb
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/impl/KMS2ClientOptimizedImpl.java
@@ -0,0 +1,88 @@
+/*
+ * 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.kms.client.impl;
+
+import org.apache.camel.component.aws2.kms.KMS2Configuration;
+import org.apache.camel.component.aws2.kms.client.KMS2InternalClient;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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.kms.KmsClient;
+import software.amazon.awssdk.services.kms.KmsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS KMS 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 KMS2ClientOptimizedImpl implements KMS2InternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(KMS2ClientOptimizedImpl.class);
+    private KMS2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public KMS2ClientOptimizedImpl(KMS2Configuration configuration) {
+        LOG.trace("Creating an AWS KMS client for an ec2 instance with IAM 
temporary credentials (normal for ec2s).");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the KMS aws client that is used.
+     * 
+     * @return KMS Client.
+     */
+    @Override
+    public KmsClient getKmsClient() {
+        KmsClient client = null;
+        KmsClientBuilder clientBuilder = KmsClient.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 (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;
+    }
+}
diff --git 
a/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/impl/KMS2ClientStandardImpl.java
 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/impl/KMS2ClientStandardImpl.java
new file mode 100644
index 0000000..ced9cbb
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-kms/src/main/java/org/apache/camel/component/aws2/kms/client/impl/KMS2ClientStandardImpl.java
@@ -0,0 +1,104 @@
+/*
+ * 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.kms.client.impl;
+
+import org.apache.camel.component.aws2.kms.KMS2Configuration;
+import org.apache.camel.component.aws2.kms.client.KMS2InternalClient;
+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.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.kms.KmsClient;
+import software.amazon.awssdk.services.kms.KmsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS KMS client for all users to use. This implementation is for 
local instances to use a static and solid
+ * credential set.
+ */
+public class KMS2ClientStandardImpl implements KMS2InternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(KMS2ClientStandardImpl.class);
+    private KMS2Configuration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public KMS2ClientStandardImpl(KMS2Configuration configuration) {
+        LOG.trace("Creating an AWS KMS manager using static credentials.");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the KMS AWS client that is used.
+     * 
+     * @return Amazon KMS Client.
+     */
+    @Override
+    public KmsClient getKmsClient() {
+        KmsClient client = null;
+        KmsClientBuilder clientBuilder = KmsClient.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) {
+            AwsBasicCredentials cred = 
AwsBasicCredentials.create(configuration.getAccessKey(), 
configuration.getSecretKey());
+            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()) {
+            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