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 2021/04/29 06:22:18 UTC

[camel] 01/06: CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the components - DDB Streams component

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 24fc22e575386ba866252a7b848f20c1630987ec
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Apr 29 07:58:22 2021 +0200

    CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the components - DDB Streams component
---
 .../aws2/ddbstream/Ddb2StreamConfiguration.java    |  12 +++
 .../aws2/ddbstream/Ddb2StreamEndpoint.java         |   4 +-
 .../ddbstream/client/Ddb2StreamClientFactory.java  |  42 ++++++++
 .../ddbstream/client/Ddb2StreamInternalClient.java |  32 ++++++
 .../impl/Ddb2StreamClientIAMOptimizedImpl.java     |  91 +++++++++++++++++
 .../impl/Ddb2StreamClientStandardImpl.java}        | 110 +++++----------------
 6 files changed, 204 insertions(+), 87 deletions(-)

diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamConfiguration.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamConfiguration.java
index 8dffc9e..f295ba1 100644
--- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamConfiguration.java
+++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamConfiguration.java
@@ -73,6 +73,10 @@ public class Ddb2StreamConfiguration implements Cloneable {
     private boolean overrideEndpoint;
     @UriParam(description = " Set the overriding uri endpoint. This option needs to be used in combination with overrideEndpoint option")
     private String uriEndpointOverride;
+    @UriParam(defaultValue = "false", description = "Set whether the DynamoDB Streams client should expect to load credentials through a default credentials provider or to expect " +
+            " static credentials to be passed in.")
+    private boolean useDefaultCredentialsProvider;
+
 
     public DynamoDbStreamsClient getAmazonDynamoDbStreamsClient() {
         return amazonDynamoDbStreamsClient;
@@ -186,6 +190,14 @@ public class Ddb2StreamConfiguration implements Cloneable {
         this.uriEndpointOverride = uriEndpointOverride;
     }
 
+    public void setUseDefaultCredentialsProvider(Boolean useDefaultCredentialsProvider) {
+        this.useDefaultCredentialsProvider = useDefaultCredentialsProvider;
+    }
+
+    public Boolean isUseDefaultCredentialsProvider() {
+        return useDefaultCredentialsProvider;
+    }
+
     // *************************************************
     //
     // *************************************************
diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java
index 677db78..9d6db93 100644
--- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java
+++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java
@@ -22,6 +22,8 @@ import org.apache.camel.Category;
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.component.aws2.ddb.client.Ddb2ClientFactory;
+import org.apache.camel.component.aws2.ddbstream.client.Ddb2StreamClientFactory;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.support.ScheduledPollEndpoint;
@@ -72,7 +74,7 @@ public class Ddb2StreamEndpoint extends ScheduledPollEndpoint {
         super.doStart();
 
         ddbStreamClient = configuration.getAmazonDynamoDbStreamsClient() != null
-                ? configuration.getAmazonDynamoDbStreamsClient() : createDdbStreamClient();
+                ? configuration.getAmazonDynamoDbStreamsClient() : Ddb2StreamClientFactory.getDynamoDBStreamClient(configuration).getDynamoDBStreamClient();
     }
 
     @Override
diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamClientFactory.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamClientFactory.java
new file mode 100644
index 0000000..feb56e8
--- /dev/null
+++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamClientFactory.java
@@ -0,0 +1,42 @@
+/*
+ * 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.ddbstream.client;
+
+import org.apache.camel.component.aws2.ddb.Ddb2Configuration;
+import org.apache.camel.component.aws2.ddbstream.Ddb2StreamConfiguration;
+import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientIAMOptimizedImpl;
+import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientStandardImpl;
+
+/**
+ * Factory class to return the correct type of AWS DynamoDB client.
+ */
+public final class Ddb2StreamClientFactory {
+
+    private Ddb2StreamClientFactory() {
+    }
+
+    /**
+     * Return the correct AWS DynamoDB client (based on remote vs local).
+     * 
+     * @param  configuration configuration
+     * @return               DynamoDBClient
+     */
+    public static Ddb2StreamInternalClient getDynamoDBStreamClient(Ddb2StreamConfiguration configuration) {
+        return configuration.isUseDefaultCredentialsProvider()
+                ? new Ddb2StreamClientIAMOptimizedImpl(configuration) : new Ddb2StreamClientStandardImpl(configuration);
+    }
+}
diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamInternalClient.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamInternalClient.java
new file mode 100644
index 0000000..13d945e
--- /dev/null
+++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamInternalClient.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.ddbstream.client;
+
+import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient;
+
+/**
+ * Manage the required actions of an DynamoDB Stream client for either local or remote.
+ */
+public interface Ddb2StreamInternalClient {
+
+    /**
+     * Returns an DynamoDB Stream client after a factory method determines which one to return.
+     * 
+     * @return DynamoDbClient DynamoDbClient
+     */
+    DynamoDbStreamsClient getDynamoDBStreamClient();
+}
diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMOptimizedImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMOptimizedImpl.java
new file mode 100644
index 0000000..7e662b4
--- /dev/null
+++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMOptimizedImpl.java
@@ -0,0 +1,91 @@
+/*
+ * 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.ddbstream.client.impl;
+
+import org.apache.camel.component.aws2.ddb.Ddb2Configuration;
+import org.apache.camel.component.aws2.ddbstream.Ddb2StreamConfiguration;
+import org.apache.camel.component.aws2.ddbstream.client.Ddb2StreamInternalClient;
+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.dynamodb.DynamoDbClient;
+import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder;
+import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient;
+import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClientBuilder;
+import software.amazon.awssdk.utils.AttributeMap;
+
+import java.net.URI;
+
+/**
+ * Manage an AWS DynamoDB 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 Ddb2StreamClientIAMOptimizedImpl implements Ddb2StreamInternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Ddb2StreamClientIAMOptimizedImpl.class);
+    private Ddb2StreamConfiguration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Ddb2StreamClientIAMOptimizedImpl(Ddb2StreamConfiguration configuration) {
+        LOG.trace("Creating an AWS DynamoDB Streams client for an ec2 instance with IAM temporary credentials (normal for ec2s).");
+        this.configuration = configuration;
+    }
+
+    /**
+     * Getting the DynamoDB Streams aws client that is used.
+     * 
+     * @return DynamoDB Streams Client.
+     */
+    @Override
+    public DynamoDbStreamsClient getDynamoDBStreamClient() {
+        DynamoDbStreamsClient client = null;
+        DynamoDbStreamsClientBuilder clientBuilder = DynamoDbStreamsClient.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-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientStandardImpl.java
similarity index 54%
copy from components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java
copy to components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientStandardImpl.java
index 677db78..c1179da 100644
--- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamEndpoint.java
+++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientStandardImpl.java
@@ -14,18 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.aws2.ddbstream;
+package org.apache.camel.component.aws2.ddbstream.client.impl;
 
-import java.net.URI;
-
-import org.apache.camel.Category;
-import org.apache.camel.Consumer;
-import org.apache.camel.Processor;
-import org.apache.camel.Producer;
-import org.apache.camel.spi.UriEndpoint;
-import org.apache.camel.spi.UriParam;
-import org.apache.camel.support.ScheduledPollEndpoint;
+import org.apache.camel.component.aws2.ddbstream.Ddb2StreamConfiguration;
+import org.apache.camel.component.aws2.ddbstream.client.Ddb2StreamInternalClient;
 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;
@@ -37,79 +32,31 @@ import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient;
 import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClientBuilder;
 import software.amazon.awssdk.utils.AttributeMap;
 
+import java.net.URI;
+
 /**
- * Receive messages from AWS DynamoDB Stream service using AWS SDK version 2.x.
+ * Manage an AWS DynamoDB Streams client for all users to use. This implementation is for local instances to use a static and
+ * solid credential set.
  */
-@UriEndpoint(firstVersion = "3.1.0", scheme = "aws2-ddbstream", title = "AWS 2 DynamoDB Streams", consumerOnly = true,
-             syntax = "aws2-ddbstream:tableName", category = { Category.CLOUD, Category.MESSAGING, Category.STREAMS })
-public class Ddb2StreamEndpoint extends ScheduledPollEndpoint {
-
-    @UriParam
-    Ddb2StreamConfiguration configuration;
-
-    private DynamoDbStreamsClient ddbStreamClient;
-
-    public Ddb2StreamEndpoint(String uri, Ddb2StreamConfiguration configuration, Ddb2StreamComponent component) {
-        super(uri, component);
+public class Ddb2StreamClientStandardImpl implements Ddb2StreamInternalClient {
+    private static final Logger LOG = LoggerFactory.getLogger(Ddb2StreamClientStandardImpl.class);
+    private Ddb2StreamConfiguration configuration;
+
+    /**
+     * Constructor that uses the config file.
+     */
+    public Ddb2StreamClientStandardImpl(Ddb2StreamConfiguration configuration) {
+        LOG.trace("Creating an AWS DynamoDB Streams manager using static credentials.");
         this.configuration = configuration;
     }
 
+    /**
+     * Getting the DynamoDB Streams AWS client that is used.
+     * 
+     * @return Amazon DynamoDB Streams Client.
+     */
     @Override
-    public Producer createProducer() throws Exception {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
-
-    @Override
-    public Consumer createConsumer(Processor processor) throws Exception {
-        Ddb2StreamConsumer consumer = new Ddb2StreamConsumer(this, processor);
-        consumer.setSchedulerProperties(consumer.getEndpoint().getSchedulerProperties());
-        configureConsumer(consumer);
-        return consumer;
-    }
-
-    @Override
-    public void doStart() throws Exception {
-        super.doStart();
-
-        ddbStreamClient = configuration.getAmazonDynamoDbStreamsClient() != null
-                ? configuration.getAmazonDynamoDbStreamsClient() : createDdbStreamClient();
-    }
-
-    @Override
-    public void doStop() throws Exception {
-        if (ObjectHelper.isEmpty(configuration.getAmazonDynamoDbStreamsClient())) {
-            if (ddbStreamClient != null) {
-                ddbStreamClient.close();
-            }
-        }
-        super.doStop();
-    }
-
-    public Ddb2StreamConfiguration getConfiguration() {
-        return configuration;
-    }
-
-    public DynamoDbStreamsClient getClient() {
-        return ddbStreamClient;
-    }
-
-    public String getSequenceNumber() {
-        switch (configuration.getIteratorType()) {
-            case AFTER_SEQUENCE_NUMBER:
-            case AT_SEQUENCE_NUMBER:
-                if (null == configuration.getSequenceNumberProvider()) {
-                    throw new IllegalStateException(
-                            "sequenceNumberProvider must be" + " provided, either as an implementation of"
-                                                    + " SequenceNumberProvider or a literal String.");
-                } else {
-                    return configuration.getSequenceNumberProvider().getSequenceNumber();
-                }
-            default:
-                return "";
-        }
-    }
-
-    DynamoDbStreamsClient createDdbStreamClient() {
+    public DynamoDbStreamsClient getDynamoDBStreamClient() {
         DynamoDbStreamsClient client = null;
         DynamoDbStreamsClientBuilder clientBuilder = DynamoDbStreamsClient.builder();
         ProxyConfiguration.Builder proxyConfig = null;
@@ -154,13 +101,4 @@ public class Ddb2StreamEndpoint extends ScheduledPollEndpoint {
         client = clientBuilder.build();
         return client;
     }
-
-    @Override
-    public String toString() {
-        return "DdbStreamEndpoint{" + "tableName=" + configuration.getTableName()
-               + ", amazonDynamoDbStreamsClient=[redacted], maxResultsPerRequest="
-               + configuration.getMaxResultsPerRequest() + ", iteratorType=" + configuration.getIteratorType()
-               + ", sequenceNumberProvider="
-               + configuration.getSequenceNumberProvider() + ", uri=" + getEndpointUri() + '}';
-    }
 }