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 a02c1a842c8f9f9168ea833c38f82eb765c895eb Author: Andrea Cosentino <[email protected]> AuthorDate: Tue Dec 16 09:57:55 2025 +0100 CAMEL-22786 - Camel-AWS: Extract common logic for clients instantiation in a separated module - AWS DDB Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-ddb/pom.xml | 4 + .../component/aws2/ddb/Ddb2Configuration.java | 5 +- .../camel/component/aws2/ddb/Ddb2Endpoint.java | 2 +- .../aws2/ddb/client/Ddb2ClientFactory.java | 28 ++---- .../aws2/ddb/client/Ddb2InternalClient.java | 32 ------ .../client/impl/Ddb2ClientIAMOptimizedImpl.java | 93 ----------------- .../impl/Ddb2ClientIAMProfileOptimizedImpl.java | 98 ------------------ .../client/impl/Ddb2ClientSessionTokenImpl.java | 111 --------------------- .../ddb/client/impl/Ddb2ClientStandardImpl.java | 109 -------------------- .../aws2/ddbstream/Ddb2StreamConfiguration.java | 5 +- .../aws2/ddbstream/Ddb2StreamEndpoint.java | 2 +- .../ddbstream/client/Ddb2StreamClientFactory.java | 28 ++---- .../ddbstream/client/Ddb2StreamInternalClient.java | 32 ------ .../impl/Ddb2StreamClientIAMOptimizedImpl.java | 94 ----------------- .../Ddb2StreamClientIAMProfileOptimizedImpl.java | 99 ------------------ .../impl/Ddb2StreamClientSessionTokenImpl.java | 111 --------------------- .../client/impl/Ddb2StreamClientStandardImpl.java | 109 -------------------- .../component/aws2/ddb/Ddb2ClientFactoryTest.java | 45 ++++----- .../ddbstream/Ddb2StreamClientFactoryTest.java | 45 ++++----- 19 files changed, 76 insertions(+), 976 deletions(-) diff --git a/components/camel-aws/camel-aws2-ddb/pom.xml b/components/camel-aws/camel-aws2-ddb/pom.xml index 925560e3ac85..abfe8babe75c 100644 --- a/components/camel-aws/camel-aws2-ddb/pom.xml +++ b/components/camel-aws/camel-aws2-ddb/pom.xml @@ -36,6 +36,10 @@ </properties> <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-aws-common</artifactId> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Configuration.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Configuration.java index b52f15d06bee..e733f4eb8456 100644 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Configuration.java +++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Configuration.java @@ -17,6 +17,7 @@ package org.apache.camel.component.aws2.ddb; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.aws.common.AwsCommonConfiguration; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; @@ -25,7 +26,7 @@ import software.amazon.awssdk.core.Protocol; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; @UriParams -public class Ddb2Configuration implements Cloneable { +public class Ddb2Configuration implements Cloneable, AwsCommonConfiguration { @UriPath @Metadata(required = true) @@ -297,7 +298,7 @@ public class Ddb2Configuration implements Cloneable { this.useDefaultCredentialsProvider = useDefaultCredentialsProvider; } - public Boolean isUseDefaultCredentialsProvider() { + public boolean isUseDefaultCredentialsProvider() { return useDefaultCredentialsProvider; } diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java index d89f38d4ac92..e7babc7a82e7 100644 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java +++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/Ddb2Endpoint.java @@ -88,7 +88,7 @@ public class Ddb2Endpoint extends ScheduledPollEndpoint implements EndpointServi super.doStart(); ddbClient = configuration.getAmazonDDBClient() != null - ? configuration.getAmazonDDBClient() : Ddb2ClientFactory.getDynamoDBClient(configuration).getDynamoDBClient(); + ? configuration.getAmazonDDBClient() : Ddb2ClientFactory.getDynamoDBClient(configuration); String tableName = getConfiguration().getTableName(); LOG.trace("Querying whether table [{}] already exists...", tableName); diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2ClientFactory.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2ClientFactory.java index 571ba3d63a91..b4d3647d9ec5 100644 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2ClientFactory.java +++ b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2ClientFactory.java @@ -16,14 +16,12 @@ */ package org.apache.camel.component.aws2.ddb.client; +import org.apache.camel.component.aws.common.AwsClientBuilderUtil; import org.apache.camel.component.aws2.ddb.Ddb2Configuration; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientIAMOptimizedImpl; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientIAMProfileOptimizedImpl; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientStandardImpl; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; /** - * Factory class to return the correct type of AWS DynamoDB client. + * Factory class to create AWS DynamoDB clients using common configuration. */ public final class Ddb2ClientFactory { @@ -31,20 +29,14 @@ public final class Ddb2ClientFactory { } /** - * Return the correct AWS DynamoDB client (based on remote vs local). + * Create a DynamoDB client based on configuration. * - * @param configuration configuration - * @return DynamoDBClient + * @param configuration The DynamoDB configuration + * @return Configured DynamoDbClient */ - public static Ddb2InternalClient getDynamoDBClient(Ddb2Configuration configuration) { - if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) { - return new Ddb2ClientIAMOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) { - return new Ddb2ClientIAMProfileOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) { - return new Ddb2ClientSessionTokenImpl(configuration); - } else { - return new Ddb2ClientStandardImpl(configuration); - } + public static DynamoDbClient getDynamoDBClient(Ddb2Configuration configuration) { + return AwsClientBuilderUtil.buildClient( + configuration, + DynamoDbClient::builder); } } diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2InternalClient.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2InternalClient.java deleted file mode 100644 index 3adaf24ba893..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/Ddb2InternalClient.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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.ddb.client; - -import software.amazon.awssdk.services.dynamodb.DynamoDbClient; - -/** - * Manage the required actions of an DynamoDB client for either local or remote. - */ -public interface Ddb2InternalClient { - - /** - * Returns an DynamoDb client after a factory method determines which one to return. - * - * @return DynamoDbClient DynamoDbClient - */ - DynamoDbClient getDynamoDBClient(); -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientIAMOptimizedImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientIAMOptimizedImpl.java deleted file mode 100644 index bf3db027d99a..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientIAMOptimizedImpl.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.ddb.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.ddb.Ddb2Configuration; -import org.apache.camel.component.aws2.ddb.client.Ddb2InternalClient; -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.utils.AttributeMap; - -/** - * 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 Ddb2ClientIAMOptimizedImpl implements Ddb2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Ddb2ClientIAMOptimizedImpl.class); - private Ddb2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Ddb2ClientIAMOptimizedImpl(Ddb2Configuration configuration) { - LOG.trace("Creating an AWS DynamoDB client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the DynamoDB aws client that is used. - * - * @return DynamoDB Client. - */ - @Override - public DynamoDbClient getDynamoDBClient() { - DynamoDbClient client = null; - DynamoDbClientBuilder clientBuilder = DynamoDbClient.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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientIAMProfileOptimizedImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientIAMProfileOptimizedImpl.java deleted file mode 100644 index c1a75666f951..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientIAMProfileOptimizedImpl.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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.ddb.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.ddb.Ddb2Configuration; -import org.apache.camel.component.aws2.ddb.client.Ddb2InternalClient; -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.dynamodb.DynamoDbClient; -import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * 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 Ddb2ClientIAMProfileOptimizedImpl implements Ddb2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Ddb2ClientIAMProfileOptimizedImpl.class); - private Ddb2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Ddb2ClientIAMProfileOptimizedImpl(Ddb2Configuration configuration) { - LOG.trace("Creating an AWS DynamoDB client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the DynamoDB aws client that is used. - * - * @return DynamoDB Client. - */ - @Override - public DynamoDbClient getDynamoDBClient() { - DynamoDbClient client = null; - DynamoDbClientBuilder clientBuilder = DynamoDbClient.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.httpClientBuilder(httpClientBuilder) - .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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientSessionTokenImpl.java deleted file mode 100644 index be6fce1c6b39..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientSessionTokenImpl.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.ddb.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.ddb.Ddb2Configuration; -import org.apache.camel.component.aws2.ddb.client.Ddb2InternalClient; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; -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.dynamodb.DynamoDbClient; -import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS DynamoDB client for all users to use. This implementation is for local instances to use a static and - * solid credential set. - */ -public class Ddb2ClientSessionTokenImpl implements Ddb2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Ddb2ClientSessionTokenImpl.class); - private Ddb2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Ddb2ClientSessionTokenImpl(Ddb2Configuration configuration) { - LOG.trace("Creating an AWS DynamoDB manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the DynamoDB AWS client that is used. - * - * @return Amazon DynamoDB Client. - */ - @Override - public DynamoDbClient getDynamoDBClient() { - DynamoDbClient client = null; - DynamoDbClientBuilder clientBuilder = DynamoDbClient.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 - && configuration.getSessionToken() != null) { - AwsSessionCredentials cred = AwsSessionCredentials.create(configuration.getAccessKey(), - configuration.getSecretKey(), configuration.getSessionToken()); - 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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientStandardImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientStandardImpl.java deleted file mode 100644 index a4579fa41ff7..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddb/client/impl/Ddb2ClientStandardImpl.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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.ddb.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.ddb.Ddb2Configuration; -import org.apache.camel.component.aws2.ddb.client.Ddb2InternalClient; -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.dynamodb.DynamoDbClient; -import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS DynamoDB client for all users to use. This implementation is for local instances to use a static and - * solid credential set. - */ -public class Ddb2ClientStandardImpl implements Ddb2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Ddb2ClientStandardImpl.class); - private Ddb2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Ddb2ClientStandardImpl(Ddb2Configuration configuration) { - LOG.trace("Creating an AWS DynamoDB manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the DynamoDB AWS client that is used. - * - * @return Amazon DynamoDB Client. - */ - @Override - public DynamoDbClient getDynamoDBClient() { - DynamoDbClient client = null; - DynamoDbClientBuilder clientBuilder = DynamoDbClient.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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} 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 2c9b858b886e..c7769a7175a1 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 @@ -17,6 +17,7 @@ package org.apache.camel.component.aws2.ddbstream; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.aws.common.AwsCommonConfiguration; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; @@ -25,7 +26,7 @@ import software.amazon.awssdk.core.Protocol; import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient; @UriParams -public class Ddb2StreamConfiguration implements Cloneable { +public class Ddb2StreamConfiguration implements Cloneable, AwsCommonConfiguration { @UriPath(label = "consumer", description = "Name of the dynamodb table") @Metadata(required = true) @@ -196,7 +197,7 @@ public class Ddb2StreamConfiguration implements Cloneable { this.useDefaultCredentialsProvider = useDefaultCredentialsProvider; } - public Boolean isUseDefaultCredentialsProvider() { + 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 c9c72c939c51..c270c89f6f51 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 @@ -66,7 +66,7 @@ public class Ddb2StreamEndpoint extends ScheduledPollEndpoint implements Endpoin ddbStreamClient = configuration.getAmazonDynamoDbStreamsClient() != null ? configuration.getAmazonDynamoDbStreamsClient() - : Ddb2StreamClientFactory.getDynamoDBStreamClient(configuration).getDynamoDBStreamClient(); + : Ddb2StreamClientFactory.getDynamoDBStreamClient(configuration); } @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 index 1f8579d62d07..b895349f2975 100644 --- 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 @@ -16,14 +16,12 @@ */ package org.apache.camel.component.aws2.ddbstream.client; +import org.apache.camel.component.aws.common.AwsClientBuilderUtil; 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.Ddb2StreamClientIAMProfileOptimizedImpl; -import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientSessionTokenImpl; -import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientStandardImpl; +import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient; /** - * Factory class to return the correct type of AWS DynamoDB client. + * Factory class to create AWS DynamoDB Streams clients using common configuration. */ public final class Ddb2StreamClientFactory { @@ -31,20 +29,14 @@ public final class Ddb2StreamClientFactory { } /** - * Return the correct AWS DynamoDB client (based on remote vs local). + * Create a DynamoDB Streams client based on configuration. * - * @param configuration configuration - * @return DynamoDBClient + * @param configuration The DynamoDB Streams configuration + * @return Configured DynamoDbStreamsClient */ - public static Ddb2StreamInternalClient getDynamoDBStreamClient(Ddb2StreamConfiguration configuration) { - if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) { - return new Ddb2StreamClientIAMOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) { - return new Ddb2StreamClientIAMProfileOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) { - return new Ddb2StreamClientSessionTokenImpl(configuration); - } else { - return new Ddb2StreamClientStandardImpl(configuration); - } + public static DynamoDbStreamsClient getDynamoDBStreamClient(Ddb2StreamConfiguration configuration) { + return AwsClientBuilderUtil.buildClient( + configuration, + DynamoDbStreamsClient::builder); } } 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 deleted file mode 100644 index 31c0f625289e..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/Ddb2StreamInternalClient.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 deleted file mode 100644 index 66365ad5cf61..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMOptimizedImpl.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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 java.net.URI; - -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.streams.DynamoDbStreamsClient; -import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * 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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMProfileOptimizedImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMProfileOptimizedImpl.java deleted file mode 100644 index c3bcab74da5b..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientIAMProfileOptimizedImpl.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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 java.net.URI; - -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.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.dynamodb.streams.DynamoDbStreamsClient; -import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * 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 Ddb2StreamClientIAMProfileOptimizedImpl implements Ddb2StreamInternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Ddb2StreamClientIAMProfileOptimizedImpl.class); - private Ddb2StreamConfiguration configuration; - - /** - * Constructor that uses the config file. - */ - public Ddb2StreamClientIAMProfileOptimizedImpl(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 (configuration.getProfileCredentialsName() != null) { - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder) - .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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientSessionTokenImpl.java deleted file mode 100644 index e5142610b7a1..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientSessionTokenImpl.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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 java.net.URI; - -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.AwsSessionCredentials; -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.dynamodb.streams.DynamoDbStreamsClient; -import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * 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. - */ -public class Ddb2StreamClientSessionTokenImpl implements Ddb2StreamInternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Ddb2StreamClientSessionTokenImpl.class); - private Ddb2StreamConfiguration configuration; - - /** - * Constructor that uses the config file. - */ - public Ddb2StreamClientSessionTokenImpl(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 DynamoDbStreamsClient getDynamoDBStreamClient() { - DynamoDbStreamsClient client = null; - DynamoDbStreamsClientBuilder clientBuilder = DynamoDbStreamsClient.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 - && configuration.getSessionToken() != null) { - AwsSessionCredentials cred = AwsSessionCredentials.create(configuration.getAccessKey(), - configuration.getSecretKey(), configuration.getSessionToken()); - 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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientStandardImpl.java b/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientStandardImpl.java deleted file mode 100644 index 07c179dae700..000000000000 --- a/components/camel-aws/camel-aws2-ddb/src/main/java/org/apache/camel/component/aws2/ddbstream/client/impl/Ddb2StreamClientStandardImpl.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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 java.net.URI; - -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; -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.streams.DynamoDbStreamsClient; -import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * 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. - */ -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 DynamoDbStreamsClient getDynamoDBStreamClient() { - DynamoDbStreamsClient client = null; - DynamoDbStreamsClientBuilder clientBuilder = DynamoDbStreamsClient.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()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddb/Ddb2ClientFactoryTest.java b/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddb/Ddb2ClientFactoryTest.java index 9de292401463..7d563a14edc9 100644 --- a/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddb/Ddb2ClientFactoryTest.java +++ b/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddb/Ddb2ClientFactoryTest.java @@ -17,44 +17,43 @@ package org.apache.camel.component.aws2.ddb; import org.apache.camel.component.aws2.ddb.client.Ddb2ClientFactory; -import org.apache.camel.component.aws2.ddb.client.Ddb2InternalClient; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientIAMOptimizedImpl; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.ddb.client.impl.Ddb2ClientStandardImpl; import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class Ddb2ClientFactoryTest { @Test - public void getStandardDdb2ClientDefault() { + public void getDdb2ClientWithDefaultCredentials() { Ddb2Configuration ddb2Configuration = new Ddb2Configuration(); - Ddb2InternalClient ddb2Client = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); - assertTrue(ddb2Client instanceof Ddb2ClientStandardImpl); + ddb2Configuration.setUseDefaultCredentialsProvider(true); + ddb2Configuration.setRegion("eu-west-1"); + DynamoDbClient ddbClient = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); + assertNotNull(ddbClient); + ddbClient.close(); } @Test - public void getStandardDdb2Client() { + public void getDdb2ClientWithStaticCredentials() { Ddb2Configuration ddb2Configuration = new Ddb2Configuration(); - ddb2Configuration.setUseDefaultCredentialsProvider(false); - Ddb2InternalClient ddb2Client = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); - assertTrue(ddb2Client instanceof Ddb2ClientStandardImpl); + ddb2Configuration.setAccessKey("testAccessKey"); + ddb2Configuration.setSecretKey("testSecretKey"); + ddb2Configuration.setRegion("eu-west-1"); + DynamoDbClient ddbClient = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); + assertNotNull(ddbClient); + ddbClient.close(); } @Test - public void getIAMOptimizedDdb2Client() { + public void getDdb2ClientWithEndpointOverride() { Ddb2Configuration ddb2Configuration = new Ddb2Configuration(); ddb2Configuration.setUseDefaultCredentialsProvider(true); - Ddb2InternalClient ddb2Client = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); - assertTrue(ddb2Client instanceof Ddb2ClientIAMOptimizedImpl); - } - - @Test - public void getSessionTokenDdb2Client() { - Ddb2Configuration ddb2Configuration = new Ddb2Configuration(); - ddb2Configuration.setUseSessionCredentials(true); - Ddb2InternalClient ddb2Client = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); - assertTrue(ddb2Client instanceof Ddb2ClientSessionTokenImpl); + ddb2Configuration.setRegion("eu-west-1"); + ddb2Configuration.setOverrideEndpoint(true); + ddb2Configuration.setUriEndpointOverride("http://localhost:4566"); + DynamoDbClient ddbClient = Ddb2ClientFactory.getDynamoDBClient(ddb2Configuration); + assertNotNull(ddbClient); + ddbClient.close(); } } diff --git a/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamClientFactoryTest.java b/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamClientFactoryTest.java index 3f6539367610..f520eb5e4e54 100644 --- a/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamClientFactoryTest.java +++ b/components/camel-aws/camel-aws2-ddb/src/test/java/org/apache/camel/component/aws2/ddbstream/Ddb2StreamClientFactoryTest.java @@ -17,44 +17,43 @@ package org.apache.camel.component.aws2.ddbstream; import org.apache.camel.component.aws2.ddbstream.client.Ddb2StreamClientFactory; -import org.apache.camel.component.aws2.ddbstream.client.Ddb2StreamInternalClient; -import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientIAMOptimizedImpl; -import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientSessionTokenImpl; -import org.apache.camel.component.aws2.ddbstream.client.impl.Ddb2StreamClientStandardImpl; import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.dynamodb.streams.DynamoDbStreamsClient; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class Ddb2StreamClientFactoryTest { @Test - public void getStandardDdb2StreamClientDefault() { + public void getDdb2StreamClientWithDefaultCredentials() { Ddb2StreamConfiguration ddb2StreamConfiguration = new Ddb2StreamConfiguration(); - Ddb2StreamInternalClient ddb2StreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); - assertTrue(ddb2StreamClient instanceof Ddb2StreamClientStandardImpl); + ddb2StreamConfiguration.setUseDefaultCredentialsProvider(true); + ddb2StreamConfiguration.setRegion("eu-west-1"); + DynamoDbStreamsClient ddbStreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); + assertNotNull(ddbStreamClient); + ddbStreamClient.close(); } @Test - public void getStandardDdb2StreamClient() { + public void getDdb2StreamClientWithStaticCredentials() { Ddb2StreamConfiguration ddb2StreamConfiguration = new Ddb2StreamConfiguration(); - ddb2StreamConfiguration.setUseDefaultCredentialsProvider(false); - Ddb2StreamInternalClient ddb2StreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); - assertTrue(ddb2StreamClient instanceof Ddb2StreamClientStandardImpl); + ddb2StreamConfiguration.setAccessKey("testAccessKey"); + ddb2StreamConfiguration.setSecretKey("testSecretKey"); + ddb2StreamConfiguration.setRegion("eu-west-1"); + DynamoDbStreamsClient ddbStreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); + assertNotNull(ddbStreamClient); + ddbStreamClient.close(); } @Test - public void getIAMOptimizedDdb2StreamClient() { + public void getDdb2StreamClientWithEndpointOverride() { Ddb2StreamConfiguration ddb2StreamConfiguration = new Ddb2StreamConfiguration(); ddb2StreamConfiguration.setUseDefaultCredentialsProvider(true); - Ddb2StreamInternalClient ddb2StreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); - assertTrue(ddb2StreamClient instanceof Ddb2StreamClientIAMOptimizedImpl); - } - - @Test - public void getSessionTokenDdb2StreamClient() { - Ddb2StreamConfiguration ddb2StreamConfiguration = new Ddb2StreamConfiguration(); - ddb2StreamConfiguration.setUseSessionCredentials(true); - Ddb2StreamInternalClient ddb2StreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); - assertTrue(ddb2StreamClient instanceof Ddb2StreamClientSessionTokenImpl); + ddb2StreamConfiguration.setRegion("eu-west-1"); + ddb2StreamConfiguration.setOverrideEndpoint(true); + ddb2StreamConfiguration.setUriEndpointOverride("http://localhost:4566"); + DynamoDbStreamsClient ddbStreamClient = Ddb2StreamClientFactory.getDynamoDBStreamClient(ddb2StreamConfiguration); + assertNotNull(ddbStreamClient); + ddbStreamClient.close(); } }
