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 a97017bc4e03b07c77fb6070db8cb216c86c49b8 Author: Andrea Cosentino <[email protected]> AuthorDate: Thu May 27 09:17:05 2021 +0200 CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the components - STS Component --- .../aws2/sts/client/STS2ClientFactory.java | 41 ++++++++ .../aws2/sts/client/STS2InternalClient.java | 30 ++++++ .../sts/client/impl/STS2ClientIAMOptimized.java | 89 +++++++++++++++++ .../sts/client/impl/STS2ClientStandardImpl.java | 105 +++++++++++++++++++++ 4 files changed, 265 insertions(+) diff --git a/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/STS2ClientFactory.java b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/STS2ClientFactory.java new file mode 100644 index 0000000..8b2faab --- /dev/null +++ b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/STS2ClientFactory.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.sts.client; + +import org.apache.camel.component.aws2.sts.STS2Configuration; +import org.apache.camel.component.aws2.sts.client.impl.STS2ClientIAMOptimized; +import org.apache.camel.component.aws2.sts.client.impl.STS2ClientStandardImpl; + +/** + * Factory class to return the correct type of AWS STS aws. + */ +public final class STS2ClientFactory { + + private STS2ClientFactory() { + } + + /** + * Return the correct aws STS client (based on remote vs local). + * + * @param configuration configuration + * @return StsClient + */ + public static STS2InternalClient getStsClient(STS2Configuration configuration) { + return configuration.isUseDefaultCredentialsProvider() + ? new STS2ClientIAMOptimized(configuration) : new STS2ClientStandardImpl(configuration); + } +} diff --git a/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/STS2InternalClient.java b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/STS2InternalClient.java new file mode 100644 index 0000000..1b80b0a --- /dev/null +++ b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/STS2InternalClient.java @@ -0,0 +1,30 @@ +/* + * 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.sts.client; + +import software.amazon.awssdk.services.sts.StsClient; + +public interface STS2InternalClient { + + /** + * Returns an sts client after a factory method determines which one to return. + * + * @return StsClient stsClient + */ + StsClient getStsClient(); + +} diff --git a/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/impl/STS2ClientIAMOptimized.java b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/impl/STS2ClientIAMOptimized.java new file mode 100644 index 0000000..e52ec22 --- /dev/null +++ b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/impl/STS2ClientIAMOptimized.java @@ -0,0 +1,89 @@ +/* + * 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.sts.client.impl; + +import org.apache.camel.component.aws2.sts.STS2Configuration; +import org.apache.camel.component.aws2.sts.client.STS2InternalClient; +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.sts.StsClient; +import software.amazon.awssdk.services.sts.StsClientBuilder; +import software.amazon.awssdk.utils.AttributeMap; + +import java.net.URI; + +/** + * Manage an AWS STS client for all users to use. This implementation is for remote instances to manage the credentials + * on their own (eliminating credential rotations) + */ +public class STS2ClientIAMOptimized implements STS2InternalClient { + private static final Logger LOG = LoggerFactory.getLogger(STS2ClientIAMOptimized.class); + private STS2Configuration configuration; + + /** + * Constructor that uses the config file. + */ + public STS2ClientIAMOptimized(STS2Configuration configuration) { + LOG.trace("Creating an AWS STS client for working on AWS Services"); + this.configuration = configuration; + } + + /** + * Getting the STS aws client that is used. + * + * @return Amazon STS Client. + */ + @Override + public StsClient getStsClient() { + StsClient client = null; + StsClientBuilder clientBuilder = StsClient.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()); + 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-sts/src/main/java/org/apache/camel/component/aws2/sts/client/impl/STS2ClientStandardImpl.java b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/impl/STS2ClientStandardImpl.java new file mode 100644 index 0000000..b9c4a5d --- /dev/null +++ b/components/camel-aws/camel-aws2-sts/src/main/java/org/apache/camel/component/aws2/sts/client/impl/STS2ClientStandardImpl.java @@ -0,0 +1,105 @@ +/* + * 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.sts.client.impl; + +import org.apache.camel.component.aws2.sts.STS2Configuration; +import org.apache.camel.component.aws2.sts.client.STS2InternalClient; +import org.apache.camel.util.FileUtil; +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.sts.StsClient; +import software.amazon.awssdk.services.sts.StsClientBuilder; +import software.amazon.awssdk.utils.AttributeMap; + +import java.net.URI; + +/** + * Manage an AWS STS client for all users to use. This implementation is for local instances to use a static and solid + * credential set. + */ +public class STS2ClientStandardImpl implements STS2InternalClient { + private static final Logger LOG = LoggerFactory.getLogger(STS2ClientStandardImpl.class); + private STS2Configuration configuration; + + /** + * Constructor that uses the config file. + */ + public STS2ClientStandardImpl(STS2Configuration configuration) { + LOG.trace("Creating an AWS STS manager using static credentials."); + this.configuration = configuration; + } + + /** + * Getting the STS aws client that is used. + * + * @return Amazon STS Client. + */ + @Override + public StsClient getStsClient() { + StsClient client = null; + StsClientBuilder clientBuilder = StsClient.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; + } +}
