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 06174bc675f93679d75affd7e61d6ea4d3811c97 Author: Andrea Cosentino <[email protected]> AuthorDate: Fri Apr 30 15:27:21 2021 +0200 CAMEL-16465 - Camel-AWS: Add useDefaultCredentialProvider option to all the components - ECS Component --- .../aws2/ecs/client/ECS2ClientFactory.java | 41 ++++++++ .../aws2/ecs/client/ECS2InternalClient.java | 32 +++++++ .../client/impl/ECS2ClientIAMOptimizedImpl.java | 88 +++++++++++++++++ .../ecs/client/impl/ECS2ClientStandardImpl.java | 104 +++++++++++++++++++++ 4 files changed, 265 insertions(+) diff --git a/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/ECS2ClientFactory.java b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/ECS2ClientFactory.java new file mode 100644 index 0000000..5c1b776 --- /dev/null +++ b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/ECS2ClientFactory.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.ecs.client; + +import org.apache.camel.component.aws2.ecs.ECS2Configuration; +import org.apache.camel.component.aws2.ecs.client.impl.ECS2ClientIAMOptimizedImpl; +import org.apache.camel.component.aws2.ecs.client.impl.ECS2ClientStandardImpl; + +/** + * Factory class to return the correct type of AWS Athena client. + */ +public final class ECS2ClientFactory { + + private ECS2ClientFactory() { + } + + /** + * Return the correct AWS ECS client (based on remote vs local). + * + * @param configuration configuration + * @return EcsClient + */ + public static ECS2InternalClient getEcsClient(ECS2Configuration configuration) { + return configuration.isUseDefaultCredentialsProvider() + ? new ECS2ClientIAMOptimizedImpl(configuration) : new ECS2ClientStandardImpl(configuration); + } +} diff --git a/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/ECS2InternalClient.java b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/ECS2InternalClient.java new file mode 100644 index 0000000..f62ef52 --- /dev/null +++ b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/ECS2InternalClient.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.ecs.client; + +import software.amazon.awssdk.services.ecs.EcsClient; + +/** + * Manage the required actions of an ECS client for either local or remote. + */ +public interface ECS2InternalClient { + + /** + * Returns an ECS client after a factory method determines which one to return. + * + * @return EcSClient EcSClient + */ + EcsClient getEcsClient(); +} diff --git a/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/impl/ECS2ClientIAMOptimizedImpl.java b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/impl/ECS2ClientIAMOptimizedImpl.java new file mode 100644 index 0000000..856563e --- /dev/null +++ b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/impl/ECS2ClientIAMOptimizedImpl.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.ecs.client.impl; + +import org.apache.camel.component.aws2.ecs.ECS2Configuration; +import org.apache.camel.component.aws2.ecs.client.ECS2InternalClient; +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.ecs.EcsClient; +import software.amazon.awssdk.services.ecs.EcsClientBuilder; +import software.amazon.awssdk.utils.AttributeMap; + +import java.net.URI; + +/** + * Manage an AWS ECS 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 ECS2ClientIAMOptimizedImpl implements ECS2InternalClient { + private static final Logger LOG = LoggerFactory.getLogger(ECS2ClientIAMOptimizedImpl.class); + private ECS2Configuration configuration; + + /** + * Constructor that uses the config file. + */ + public ECS2ClientIAMOptimizedImpl(ECS2Configuration configuration) { + LOG.trace("Creating an AWS ECS client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); + this.configuration = configuration; + } + + /** + * Getting the ECS aws client that is used. + * + * @return EcsClient Client. + */ + @Override + public EcsClient getEcsClient() { + EcsClient client = null; + EcsClientBuilder clientBuilder = EcsClient.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-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/impl/ECS2ClientStandardImpl.java b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/impl/ECS2ClientStandardImpl.java new file mode 100644 index 0000000..6c392d7 --- /dev/null +++ b/components/camel-aws/camel-aws2-ecs/src/main/java/org/apache/camel/component/aws2/ecs/client/impl/ECS2ClientStandardImpl.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.ecs.client.impl; + +import org.apache.camel.component.aws2.ecs.ECS2Configuration; +import org.apache.camel.component.aws2.ecs.client.ECS2InternalClient; +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.ecs.EcsClient; +import software.amazon.awssdk.services.ecs.EcsClientBuilder; +import software.amazon.awssdk.utils.AttributeMap; + +import java.net.URI; + +/** + * Manage an AWS ECS client for all users to use. This implementation is for local instances to use a static and solid + * credential set. + */ +public class ECS2ClientStandardImpl implements ECS2InternalClient { + private static final Logger LOG = LoggerFactory.getLogger(ECS2ClientStandardImpl.class); + private ECS2Configuration configuration; + + /** + * Constructor that uses the config file. + */ + public ECS2ClientStandardImpl(ECS2Configuration configuration) { + LOG.trace("Creating an AWS ECS manager using static credentials."); + this.configuration = configuration; + } + + /** + * Getting the ECS AWS client that is used. + * + * @return Amazon ECS Client. + */ + @Override + public EcsClient getEcsClient() { + EcsClient client = null; + EcsClientBuilder clientBuilder = EcsClient.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; + } +}
