This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch CAMEL-22786-complete in repository https://gitbox.apache.org/repos/asf/camel.git
commit 03d37c86272318da872bf565789674f2d4ed3148 Author: Andrea Cosentino <[email protected]> AuthorDate: Tue Dec 16 12:29:05 2025 +0100 CAMEL-22786 - Camel-AWS: Extract common logic for clients instantiation in a separated module - AWS IAM Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-iam/pom.xml | 4 + .../component/aws2/iam/IAM2Configuration.java | 9 +- .../camel/component/aws2/iam/IAM2Endpoint.java | 2 +- .../aws2/iam/client/IAM2ClientFactory.java | 28 ++---- .../aws2/iam/client/IAM2InternalClient.java | 32 ------ .../iam/client/impl/IAM2ClientOptimizedImpl.java | 93 ----------------- .../impl/IAM2ClientProfileOptimizedImpl.java | 98 ------------------ .../client/impl/IAM2ClientSessionTokenImpl.java | 111 --------------------- .../iam/client/impl/IAM2ClientStandardImpl.java | 109 -------------------- .../component/aws2/iam/IAMClientFactoryTest.java | 53 +++++----- 10 files changed, 47 insertions(+), 492 deletions(-) diff --git a/components/camel-aws/camel-aws2-iam/pom.xml b/components/camel-aws/camel-aws2-iam/pom.xml index 69f8731e5b5b..9dcdc0c25fc0 100644 --- a/components/camel-aws/camel-aws2-iam/pom.xml +++ b/components/camel-aws/camel-aws2-iam/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>iam</artifactId> diff --git a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Configuration.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Configuration.java index 386398198f4d..96bbe1d29955 100644 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Configuration.java +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Configuration.java @@ -17,6 +17,7 @@ package org.apache.camel.component.aws2.iam; 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; @@ -26,7 +27,7 @@ import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.iam.IamClient; @UriParams -public class IAM2Configuration implements Cloneable { +public class IAM2Configuration implements Cloneable, AwsCommonConfiguration { @UriPath(description = "Logical name") @Metadata(required = true) @@ -220,14 +221,16 @@ public class IAM2Configuration implements Cloneable { * Set whether the IAM 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-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Endpoint.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Endpoint.java index e26247fa21cb..9b7e51eff70b 100644 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Endpoint.java +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/IAM2Endpoint.java @@ -68,7 +68,7 @@ public class IAM2Endpoint extends ScheduledPollEndpoint implements EndpointServi iamClient = configuration.getIamClient() != null ? configuration.getIamClient() - : IAM2ClientFactory.getIamClient(configuration).getIamClient(); + : IAM2ClientFactory.getIamClient(configuration); } @Override diff --git a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2ClientFactory.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2ClientFactory.java index 2f7fbc21bb10..9381a1f48306 100644 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2ClientFactory.java +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2ClientFactory.java @@ -16,14 +16,12 @@ */ package org.apache.camel.component.aws2.iam.client; +import org.apache.camel.component.aws.common.AwsClientBuilderUtil; import org.apache.camel.component.aws2.iam.IAM2Configuration; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientOptimizedImpl; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientProfileOptimizedImpl; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientStandardImpl; +import software.amazon.awssdk.services.iam.IamClient; /** - * Factory class to return the correct type of AWS IAM client. + * Factory class to create AWS IAM clients using common configuration. */ public final class IAM2ClientFactory { @@ -31,20 +29,14 @@ public final class IAM2ClientFactory { } /** - * Return the correct AWS IAM client (based on remote vs local). + * Create an IAM client based on configuration. * - * @param configuration configuration - * @return IamClient + * @param configuration The IAM configuration + * @return Configured IamClient */ - public static IAM2InternalClient getIamClient(IAM2Configuration configuration) { - if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) { - return new IAM2ClientOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) { - return new IAM2ClientProfileOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) { - return new IAM2ClientSessionTokenImpl(configuration); - } else { - return new IAM2ClientStandardImpl(configuration); - } + public static IamClient getIamClient(IAM2Configuration configuration) { + return AwsClientBuilderUtil.buildClient( + configuration, + IamClient::builder); } } diff --git a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2InternalClient.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2InternalClient.java deleted file mode 100644 index 198448ef43ad..000000000000 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2InternalClient.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.iam.client; - -import software.amazon.awssdk.services.iam.IamClient; - -/** - * Manage the required actions of an IAM client for either local or remote. - */ -public interface IAM2InternalClient { - - /** - * Returns an IAM client after a factory method determines which one to return. - * - * @return IamClient IamClient - */ - IamClient getIamClient(); -} diff --git a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientOptimizedImpl.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientOptimizedImpl.java deleted file mode 100644 index e0f5db754c94..000000000000 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientOptimizedImpl.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.iam.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.iam.IAM2Configuration; -import org.apache.camel.component.aws2.iam.client.IAM2InternalClient; -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.iam.IamClient; -import software.amazon.awssdk.services.iam.IamClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS IAM 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 IAM2ClientOptimizedImpl implements IAM2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(IAM2ClientOptimizedImpl.class); - private IAM2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public IAM2ClientOptimizedImpl(IAM2Configuration configuration) { - LOG.trace("Creating an AWS IAM client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the IAM aws client that is used. - * - * @return IAM Client. - */ - @Override - public IamClient getIamClient() { - IamClient client = null; - IamClientBuilder clientBuilder = IamClient.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-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientProfileOptimizedImpl.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientProfileOptimizedImpl.java deleted file mode 100644 index de0c4498ec05..000000000000 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientProfileOptimizedImpl.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.iam.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.iam.IAM2Configuration; -import org.apache.camel.component.aws2.iam.client.IAM2InternalClient; -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.iam.IamClient; -import software.amazon.awssdk.services.iam.IamClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS IAM 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 IAM2ClientProfileOptimizedImpl implements IAM2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(IAM2ClientProfileOptimizedImpl.class); - private IAM2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public IAM2ClientProfileOptimizedImpl(IAM2Configuration configuration) { - LOG.trace("Creating an AWS IAM client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the IAM aws client that is used. - * - * @return IAM Client. - */ - @Override - public IamClient getIamClient() { - IamClient client = null; - IamClientBuilder clientBuilder = IamClient.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-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientSessionTokenImpl.java deleted file mode 100644 index ac1355b344c1..000000000000 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientSessionTokenImpl.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.iam.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.iam.IAM2Configuration; -import org.apache.camel.component.aws2.iam.client.IAM2InternalClient; -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.iam.IamClient; -import software.amazon.awssdk.services.iam.IamClientBuilder; -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 IAM2ClientSessionTokenImpl implements IAM2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(IAM2ClientStandardImpl.class); - private IAM2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public IAM2ClientSessionTokenImpl(IAM2Configuration configuration) { - LOG.trace("Creating an AWS IAM manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the IAM AWS client that is used. - * - * @return Amazon IAM Client. - */ - @Override - public IamClient getIamClient() { - IamClient client = null; - IamClientBuilder clientBuilder = IamClient.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-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientStandardImpl.java b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientStandardImpl.java deleted file mode 100644 index fbb6d9b94f5f..000000000000 --- a/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientStandardImpl.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.iam.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.iam.IAM2Configuration; -import org.apache.camel.component.aws2.iam.client.IAM2InternalClient; -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.iam.IamClient; -import software.amazon.awssdk.services.iam.IamClientBuilder; -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 IAM2ClientStandardImpl implements IAM2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(IAM2ClientStandardImpl.class); - private IAM2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public IAM2ClientStandardImpl(IAM2Configuration configuration) { - LOG.trace("Creating an AWS IAM manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the IAM AWS client that is used. - * - * @return Amazon IAM Client. - */ - @Override - public IamClient getIamClient() { - IamClient client = null; - IamClientBuilder clientBuilder = IamClient.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-iam/src/test/java/org/apache/camel/component/aws2/iam/IAMClientFactoryTest.java b/components/camel-aws/camel-aws2-iam/src/test/java/org/apache/camel/component/aws2/iam/IAMClientFactoryTest.java index ec283ebaf766..4bf369411c74 100644 --- a/components/camel-aws/camel-aws2-iam/src/test/java/org/apache/camel/component/aws2/iam/IAMClientFactoryTest.java +++ b/components/camel-aws/camel-aws2-iam/src/test/java/org/apache/camel/component/aws2/iam/IAMClientFactoryTest.java @@ -17,44 +17,43 @@ package org.apache.camel.component.aws2.iam; import org.apache.camel.component.aws2.iam.client.IAM2ClientFactory; -import org.apache.camel.component.aws2.iam.client.IAM2InternalClient; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientOptimizedImpl; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.iam.client.impl.IAM2ClientStandardImpl; import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.iam.IamClient; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class IAMClientFactoryTest { @Test - public void getStandardEIamClientDefault() { - IAM2Configuration iam2Configuration = new IAM2Configuration(); - IAM2InternalClient iamClient = IAM2ClientFactory.getIamClient(iam2Configuration); - assertTrue(iamClient instanceof IAM2ClientStandardImpl); + public void getIamClientWithDefaultCredentials() { + IAM2Configuration configuration = new IAM2Configuration(); + configuration.setUseDefaultCredentialsProvider(true); + configuration.setRegion("aws-global"); + IamClient iamClient = IAM2ClientFactory.getIamClient(configuration); + assertNotNull(iamClient); + iamClient.close(); } @Test - public void getStandardIamClient() { - IAM2Configuration iam2Configuration = new IAM2Configuration(); - iam2Configuration.setUseDefaultCredentialsProvider(false); - IAM2InternalClient iamClient = IAM2ClientFactory.getIamClient(iam2Configuration); - assertTrue(iamClient instanceof IAM2ClientStandardImpl); + public void getIamClientWithStaticCredentials() { + IAM2Configuration configuration = new IAM2Configuration(); + configuration.setAccessKey("testAccessKey"); + configuration.setSecretKey("testSecretKey"); + configuration.setRegion("aws-global"); + IamClient iamClient = IAM2ClientFactory.getIamClient(configuration); + assertNotNull(iamClient); + iamClient.close(); } @Test - public void getIAMOptimizedIamClient() { - IAM2Configuration iam2Configuration = new IAM2Configuration(); - iam2Configuration.setUseDefaultCredentialsProvider(true); - IAM2InternalClient iamClient = IAM2ClientFactory.getIamClient(iam2Configuration); - assertTrue(iamClient instanceof IAM2ClientOptimizedImpl); - } - - @Test - public void getSessionTokenIamClient() { - IAM2Configuration iam2Configuration = new IAM2Configuration(); - iam2Configuration.setUseSessionCredentials(true); - IAM2InternalClient iamClient = IAM2ClientFactory.getIamClient(iam2Configuration); - assertTrue(iamClient instanceof IAM2ClientSessionTokenImpl); + public void getIamClientWithEndpointOverride() { + IAM2Configuration configuration = new IAM2Configuration(); + configuration.setUseDefaultCredentialsProvider(true); + configuration.setRegion("aws-global"); + configuration.setOverrideEndpoint(true); + configuration.setUriEndpointOverride("http://localhost:4566"); + IamClient iamClient = IAM2ClientFactory.getIamClient(configuration); + assertNotNull(iamClient); + iamClient.close(); } }
