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 8a58b42dd9e818a90d677aec69bc1ae5554225a3 Author: Andrea Cosentino <[email protected]> AuthorDate: Tue Dec 16 11:35:35 2025 +0100 CAMEL-22786 - Camel-AWS: Extract common logic for clients instantiation in a separated module - AWS EKS Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-eks/pom.xml | 4 + .../component/aws2/eks/EKS2Configuration.java | 9 +- .../camel/component/aws2/eks/EKS2Endpoint.java | 2 +- .../aws2/eks/client/EKS2ClientFactory.java | 28 ++---- .../aws2/eks/client/EKS2InternalClient.java | 32 ------ .../client/impl/EKS2ClientIAMOptimizedImpl.java | 93 ----------------- .../impl/EKS2ClientIAMProfileOptimizedImpl.java | 98 ------------------ .../client/impl/EKS2ClientSessionTokenImpl.java | 111 --------------------- .../eks/client/impl/EKS2ClientStandardImpl.java | 109 -------------------- .../component/aws2/eks/EKS2ClientFactoryTest.java | 53 +++++----- 10 files changed, 47 insertions(+), 492 deletions(-) diff --git a/components/camel-aws/camel-aws2-eks/pom.xml b/components/camel-aws/camel-aws2-eks/pom.xml index 88641f4e6909..5b6a132898c6 100644 --- a/components/camel-aws/camel-aws2-eks/pom.xml +++ b/components/camel-aws/camel-aws2-eks/pom.xml @@ -40,6 +40,10 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-aws-common</artifactId> + </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>eks</artifactId> diff --git a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Configuration.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Configuration.java index e05b4c594278..44322f905945 100644 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Configuration.java +++ b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Configuration.java @@ -17,6 +17,7 @@ package org.apache.camel.component.aws2.eks; 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.eks.EksClient; @UriParams -public class EKS2Configuration implements Cloneable { +public class EKS2Configuration implements Cloneable, AwsCommonConfiguration { @UriPath(description = "Logical name") @Metadata(required = true) @@ -216,14 +217,16 @@ public class EKS2Configuration implements Cloneable { * Set whether the EKS client should expect to load credentials through a default credentials provider or to expect * static credentials to be passed in. */ - public void setUseDefaultCredentialsProvider(Boolean useDefaultCredentialsProvider) { + public void setUseDefaultCredentialsProvider(boolean useDefaultCredentialsProvider) { this.useDefaultCredentialsProvider = useDefaultCredentialsProvider; } - public Boolean isUseDefaultCredentialsProvider() { + @Override + public boolean isUseDefaultCredentialsProvider() { return useDefaultCredentialsProvider; } + @Override public boolean isUseProfileCredentialsProvider() { return useProfileCredentialsProvider; } diff --git a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java index ea54c814f8be..5ca1727beb9d 100644 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java +++ b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/EKS2Endpoint.java @@ -67,7 +67,7 @@ public class EKS2Endpoint extends ScheduledPollEndpoint implements EndpointServi super.doStart(); eksClient = configuration.getEksClient() != null - ? configuration.getEksClient() : EKS2ClientFactory.getEksClient(configuration).getEksClient(); + ? configuration.getEksClient() : EKS2ClientFactory.getEksClient(configuration); } @Override diff --git a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2ClientFactory.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2ClientFactory.java index 58e1f0d697f4..ded08760b7a8 100644 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2ClientFactory.java +++ b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2ClientFactory.java @@ -16,14 +16,12 @@ */ package org.apache.camel.component.aws2.eks.client; +import org.apache.camel.component.aws.common.AwsClientBuilderUtil; import org.apache.camel.component.aws2.eks.EKS2Configuration; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientIAMOptimizedImpl; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientIAMProfileOptimizedImpl; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientStandardImpl; +import software.amazon.awssdk.services.eks.EksClient; /** - * Factory class to return the correct type of AWS EKS client. + * Factory class to create AWS EKS clients using common configuration. */ public final class EKS2ClientFactory { @@ -31,20 +29,14 @@ public final class EKS2ClientFactory { } /** - * Return the correct AWS EKS client (based on remote vs local). + * Create an EKS client based on configuration. * - * @param configuration configuration - * @return EKSClient + * @param configuration The EKS configuration + * @return Configured EksClient */ - public static EKS2InternalClient getEksClient(EKS2Configuration configuration) { - if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) { - return new EKS2ClientIAMOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) { - return new EKS2ClientIAMProfileOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) { - return new EKS2ClientSessionTokenImpl(configuration); - } else { - return new EKS2ClientStandardImpl(configuration); - } + public static EksClient getEksClient(EKS2Configuration configuration) { + return AwsClientBuilderUtil.buildClient( + configuration, + EksClient::builder); } } diff --git a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2InternalClient.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2InternalClient.java deleted file mode 100644 index 2929d7f3c519..000000000000 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/EKS2InternalClient.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.eks.client; - -import software.amazon.awssdk.services.eks.EksClient; - -/** - * Manage the required actions of an EKS client for either local or remote. - */ -public interface EKS2InternalClient { - - /** - * Returns an EKS client after a factory method determines which one to return. - * - * @return EksClient EksClient - */ - EksClient getEksClient(); -} diff --git a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientIAMOptimizedImpl.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientIAMOptimizedImpl.java deleted file mode 100644 index 83e1cce10976..000000000000 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientIAMOptimizedImpl.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.eks.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.eks.EKS2Configuration; -import org.apache.camel.component.aws2.eks.client.EKS2InternalClient; -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.eks.EksClient; -import software.amazon.awssdk.services.eks.EksClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS EKS 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 EKS2ClientIAMOptimizedImpl implements EKS2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(EKS2ClientIAMOptimizedImpl.class); - private EKS2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public EKS2ClientIAMOptimizedImpl(EKS2Configuration configuration) { - LOG.trace("Creating an AWS EKS client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the EKS aws client that is used. - * - * @return EcsClient Client. - */ - @Override - public EksClient getEksClient() { - EksClient client = null; - EksClientBuilder clientBuilder = EksClient.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-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientIAMProfileOptimizedImpl.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientIAMProfileOptimizedImpl.java deleted file mode 100644 index 1b441e95697a..000000000000 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientIAMProfileOptimizedImpl.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.eks.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.eks.EKS2Configuration; -import org.apache.camel.component.aws2.eks.client.EKS2InternalClient; -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.eks.EksClient; -import software.amazon.awssdk.services.eks.EksClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS EKS 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 EKS2ClientIAMProfileOptimizedImpl implements EKS2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(EKS2ClientIAMProfileOptimizedImpl.class); - private EKS2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public EKS2ClientIAMProfileOptimizedImpl(EKS2Configuration configuration) { - LOG.trace("Creating an AWS EKS client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the EKS aws client that is used. - * - * @return EcsClient Client. - */ - @Override - public EksClient getEksClient() { - EksClient client = null; - EksClientBuilder clientBuilder = EksClient.builder(); - ProxyConfiguration.Builder proxyConfig = null; - ApacheHttpClient.Builder httpClientBuilder = null; - if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { - proxyConfig = ProxyConfiguration.builder(); - URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" - + configuration.getProxyPort()); - proxyConfig.endpoint(proxyEndpoint); - httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); - } - if (configuration.getProfileCredentialsName() != null) { - clientBuilder = clientBuilder - .credentialsProvider(ProfileCredentialsProvider.create(configuration.getProfileCredentialsName())); - } - if (ObjectHelper.isNotEmpty(configuration.getRegion())) { - clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); - } - if (configuration.isOverrideEndpoint()) { - clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); - } - if (configuration.isTrustAllCertificates()) { - 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-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientSessionTokenImpl.java deleted file mode 100644 index 8645f3ab5055..000000000000 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientSessionTokenImpl.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.eks.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.eks.EKS2Configuration; -import org.apache.camel.component.aws2.eks.client.EKS2InternalClient; -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.eks.EksClient; -import software.amazon.awssdk.services.eks.EksClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS EKS client for all users to use. This implementation is for local instances to use a static and solid - * credential set. - */ -public class EKS2ClientSessionTokenImpl implements EKS2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(EKS2ClientSessionTokenImpl.class); - private EKS2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public EKS2ClientSessionTokenImpl(EKS2Configuration configuration) { - LOG.trace("Creating an AWS EKS manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the EKS AWS client that is used. - * - * @return Amazon EKS Client. - */ - @Override - public EksClient getEksClient() { - EksClient client = null; - EksClientBuilder clientBuilder = EksClient.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-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientStandardImpl.java b/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientStandardImpl.java deleted file mode 100644 index 22d4011d129c..000000000000 --- a/components/camel-aws/camel-aws2-eks/src/main/java/org/apache/camel/component/aws2/eks/client/impl/EKS2ClientStandardImpl.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.eks.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.eks.EKS2Configuration; -import org.apache.camel.component.aws2.eks.client.EKS2InternalClient; -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.eks.EksClient; -import software.amazon.awssdk.services.eks.EksClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS EKS client for all users to use. This implementation is for local instances to use a static and solid - * credential set. - */ -public class EKS2ClientStandardImpl implements EKS2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(EKS2ClientStandardImpl.class); - private EKS2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public EKS2ClientStandardImpl(EKS2Configuration configuration) { - LOG.trace("Creating an AWS EKS manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the EKS AWS client that is used. - * - * @return Amazon EKS Client. - */ - @Override - public EksClient getEksClient() { - EksClient client = null; - EksClientBuilder clientBuilder = EksClient.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-eks/src/test/java/org/apache/camel/component/aws2/eks/EKS2ClientFactoryTest.java b/components/camel-aws/camel-aws2-eks/src/test/java/org/apache/camel/component/aws2/eks/EKS2ClientFactoryTest.java index f2222e79c060..238ecc63ded6 100644 --- a/components/camel-aws/camel-aws2-eks/src/test/java/org/apache/camel/component/aws2/eks/EKS2ClientFactoryTest.java +++ b/components/camel-aws/camel-aws2-eks/src/test/java/org/apache/camel/component/aws2/eks/EKS2ClientFactoryTest.java @@ -17,44 +17,43 @@ package org.apache.camel.component.aws2.eks; import org.apache.camel.component.aws2.eks.client.EKS2ClientFactory; -import org.apache.camel.component.aws2.eks.client.EKS2InternalClient; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientIAMOptimizedImpl; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.eks.client.impl.EKS2ClientStandardImpl; import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.eks.EksClient; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class EKS2ClientFactoryTest { @Test - public void getStandardEKS2ClientDefault() { - EKS2Configuration eks2Configuration = new EKS2Configuration(); - EKS2InternalClient eks2Client = EKS2ClientFactory.getEksClient(eks2Configuration); - assertTrue(eks2Client instanceof EKS2ClientStandardImpl); + public void getEksClientWithDefaultCredentials() { + EKS2Configuration configuration = new EKS2Configuration(); + configuration.setUseDefaultCredentialsProvider(true); + configuration.setRegion("eu-west-1"); + EksClient eksClient = EKS2ClientFactory.getEksClient(configuration); + assertNotNull(eksClient); + eksClient.close(); } @Test - public void getStandardEKS2Client() { - EKS2Configuration eks2Configuration = new EKS2Configuration(); - eks2Configuration.setUseDefaultCredentialsProvider(false); - EKS2InternalClient eks2Client = EKS2ClientFactory.getEksClient(eks2Configuration); - assertTrue(eks2Client instanceof EKS2ClientStandardImpl); + public void getEksClientWithStaticCredentials() { + EKS2Configuration configuration = new EKS2Configuration(); + configuration.setAccessKey("testAccessKey"); + configuration.setSecretKey("testSecretKey"); + configuration.setRegion("eu-west-1"); + EksClient eksClient = EKS2ClientFactory.getEksClient(configuration); + assertNotNull(eksClient); + eksClient.close(); } @Test - public void getIAMOptimizedEKS2Client() { - EKS2Configuration eks2Configuration = new EKS2Configuration(); - eks2Configuration.setUseDefaultCredentialsProvider(true); - EKS2InternalClient eks2Client = EKS2ClientFactory.getEksClient(eks2Configuration); - assertTrue(eks2Client instanceof EKS2ClientIAMOptimizedImpl); - } - - @Test - public void getSessionTokenEKS2Client() { - EKS2Configuration eks2Configuration = new EKS2Configuration(); - eks2Configuration.setUseSessionCredentials(true); - EKS2InternalClient eks2Client = EKS2ClientFactory.getEksClient(eks2Configuration); - assertTrue(eks2Client instanceof EKS2ClientSessionTokenImpl); + public void getEksClientWithEndpointOverride() { + EKS2Configuration configuration = new EKS2Configuration(); + configuration.setUseDefaultCredentialsProvider(true); + configuration.setRegion("eu-west-1"); + configuration.setOverrideEndpoint(true); + configuration.setUriEndpointOverride("http://localhost:4566"); + EksClient eksClient = EKS2ClientFactory.getEksClient(configuration); + assertNotNull(eksClient); + eksClient.close(); } }
