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 667d01735e9a317587551e69c1457cbb27870d75 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Thu May 13 15:23:05 2021 +0200 CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the components - IAM Component --- .../component/aws2/iam/IAM2Configuration.java | 13 +++ .../aws2/iam/client/IAM2ClientFactory.java | 41 ++++++++ .../aws2/iam/client/IAM2InternalClient.java | 32 +++++++ .../iam/client/impl/IAM2ClientOptimizedImpl.java | 88 +++++++++++++++++ .../iam/client/impl/IAM2ClientStandardImpl.java | 104 +++++++++++++++++++++ 5 files changed, 278 insertions(+) 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 f0981c3..09bc4dc 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 @@ -56,6 +56,8 @@ public class IAM2Configuration implements Cloneable { private boolean overrideEndpoint; @UriParam private String uriEndpointOverride; + @UriParam(defaultValue = "false") + private boolean useDefaultCredentialsProvider; public IamClient getIamClient() { return iamClient; @@ -194,6 +196,17 @@ public class IAM2Configuration implements Cloneable { this.uriEndpointOverride = uriEndpointOverride; } + /** + * 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) { + this.useDefaultCredentialsProvider = useDefaultCredentialsProvider; + } + + public Boolean isUseDefaultCredentialsProvider() { + return useDefaultCredentialsProvider; + } // ************************************************* // // ************************************************* 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 new file mode 100644 index 0000000..3fb113f --- /dev/null +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2ClientFactory.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.iam.client; + +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.IAM2ClientStandardImpl; + +/** + * Factory class to return the correct type of AWS Eventbridge client. + */ +public final class IAM2ClientFactory { + + private IAM2ClientFactory() { + } + + /** + * Return the correct AWS IAM client (based on remote vs local). + * + * @param configuration configuration + * @return IamClient + */ + public static IAM2InternalClient getEventbridgeClient(IAM2Configuration configuration) { + return configuration.isUseDefaultCredentialsProvider() + ? new IAM2ClientOptimizedImpl(configuration) : new IAM2ClientStandardImpl(configuration); + } +} 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 new file mode 100644 index 0000000..82725f1 --- /dev/null +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/IAM2InternalClient.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.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 new file mode 100644 index 0000000..03cd74c --- /dev/null +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientOptimizedImpl.java @@ -0,0 +1,88 @@ +/* + * 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 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; + +import java.net.URI; + +/** + * 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()) { + SdkHttpClient ahc = ApacheHttpClient.builder().buildWithDefaults(AttributeMap + .builder() + .put( + SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, + Boolean.TRUE) + .build()); + clientBuilder.httpClient(ahc); + } + client = clientBuilder.build(); + return client; + } +} diff --git a/components/camel-aws/camel-aws2-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 new file mode 100644 index 0000000..bcec215 --- /dev/null +++ b/components/camel-aws/camel-aws2-iam/src/main/java/org/apache/camel/component/aws2/iam/client/impl/IAM2ClientStandardImpl.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.iam.client.impl; + +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; + +import java.net.URI; + +/** + * 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()) { + SdkHttpClient ahc = ApacheHttpClient.builder().buildWithDefaults(AttributeMap + .builder() + .put( + SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, + Boolean.TRUE) + .build()); + clientBuilder.httpClient(ahc); + } + client = clientBuilder.build(); + return client; + } +}