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 e9282c6490d184290e81919b73095334ea7a3099
Author: Andrea Cosentino <[email protected]>
AuthorDate: Tue Jun 1 07:50:52 2021 +0200

    CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the 
components - Secrets Manager Component
---
 .../client/SecretsManagerClientFactory.java        |  41 ++++++++
 .../client/SecretsManagerInternalClient.java       |  30 ++++++
 .../impl/SecretsManagerClientIAMOptimized.java     |  89 ++++++++++++++++++
 .../impl/SecretsManagerClientStandardImpl.java     | 104 +++++++++++++++++++++
 4 files changed, 264 insertions(+)

diff --git 
a/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/SecretsManagerClientFactory.java
 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/SecretsManagerClientFactory.java
new file mode 100644
index 0000000..efb6a17
--- /dev/null
+++ 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/SecretsManagerClientFactory.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.aws.secretsmanager.client;
+
+import 
org.apache.camel.component.aws.secretsmanager.SecretsManagerConfiguration;
+import 
org.apache.camel.component.aws.secretsmanager.client.impl.SecretsManagerClientIAMOptimized;
+import 
org.apache.camel.component.aws.secretsmanager.client.impl.SecretsManagerClientStandardImpl;
+
+/**
+ * Factory class to return the correct type of AWS Secrets Manager aws.
+ */
+public final class SecretsManagerClientFactory {
+
+    private SecretsManagerClientFactory() {
+    }
+
+    /**
+     * Return the correct aws Secrets Manager client (based on remote vs 
local).
+     * 
+     * @param  configuration configuration
+     * @return               SecretsManagerClient
+     */
+    public static SecretsManagerInternalClient 
getTranslateClient(SecretsManagerConfiguration configuration) {
+        return configuration.isUseDefaultCredentialsProvider()
+                ? new SecretsManagerClientIAMOptimized(configuration) : new 
SecretsManagerClientStandardImpl(configuration);
+    }
+}
diff --git 
a/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/SecretsManagerInternalClient.java
 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/SecretsManagerInternalClient.java
new file mode 100644
index 0000000..c199917
--- /dev/null
+++ 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/SecretsManagerInternalClient.java
@@ -0,0 +1,30 @@
+/*
+ * 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.aws.secretsmanager.client;
+
+import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
+
+public interface SecretsManagerInternalClient {
+
+    /**
+     * Returns an secrets manager client after a factory method determines 
which one to return.
+     * 
+     * @return SecretsManagerClient secretsManagerClient
+     */
+    SecretsManagerClient getSecretsManagerClient();
+
+}
diff --git 
a/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/impl/SecretsManagerClientIAMOptimized.java
 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/impl/SecretsManagerClientIAMOptimized.java
new file mode 100644
index 0000000..b24a7e8
--- /dev/null
+++ 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/impl/SecretsManagerClientIAMOptimized.java
@@ -0,0 +1,89 @@
+/*
+ * 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.aws.secretsmanager.client.impl;
+
+import 
org.apache.camel.component.aws.secretsmanager.SecretsManagerConfiguration;
+import 
org.apache.camel.component.aws.secretsmanager.client.SecretsManagerInternalClient;
+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.secretsmanager.SecretsManagerClient;
+import 
software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS Secrets Manager client for all users to use. This 
implementation is for remote instances to manage the
+ * credentials on their own (eliminating credential rotations)
+ */
+public class SecretsManagerClientIAMOptimized implements 
SecretsManagerInternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SecretsManagerClientIAMOptimized.class);
+    private SecretsManagerConfiguration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public SecretsManagerClientIAMOptimized(SecretsManagerConfiguration 
configuration) {
+        LOG.trace("Creating an AWS Secrets Manager client for working on AWS 
Services");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the Secrets Manager aws client that is used.
+     * 
+     * @return Amazon Secrets Manager Client.
+     */
+    @Override
+    public SecretsManagerClient getSecretsManagerClient() {
+        SecretsManagerClient client = null;
+        SecretsManagerClientBuilder clientBuilder = 
SecretsManagerClient.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());
+            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-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/impl/SecretsManagerClientStandardImpl.java
 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/impl/SecretsManagerClientStandardImpl.java
new file mode 100644
index 0000000..7c6c094
--- /dev/null
+++ 
b/components/camel-aws/camel-aws-secrets-manager/src/main/java/org/apache/camel/component/aws/secretsmanager/client/impl/SecretsManagerClientStandardImpl.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.aws.secretsmanager.client.impl;
+
+import 
org.apache.camel.component.aws.secretsmanager.SecretsManagerConfiguration;
+import 
org.apache.camel.component.aws.secretsmanager.client.SecretsManagerInternalClient;
+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.secretsmanager.SecretsManagerClient;
+import 
software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS Secrets Manager client for all users to use. This 
implementation is for local instances to use a static and
+ * solid credential set.
+ */
+public class SecretsManagerClientStandardImpl implements 
SecretsManagerInternalClient {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SecretsManagerClientStandardImpl.class);
+    private SecretsManagerConfiguration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public SecretsManagerClientStandardImpl(SecretsManagerConfiguration 
configuration) {
+        LOG.trace("Creating an AWS Secrets Manager manager using static 
credentials.");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the Secrets Manager aws client that is used.
+     * 
+     * @return Amazon Secrets Manager Client.
+     */
+    @Override
+    public SecretsManagerClient getSecretsManagerClient() {
+        SecretsManagerClient client = null;
+        SecretsManagerClientBuilder clientBuilder = 
SecretsManagerClient.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