-
Notifications
You must be signed in to change notification settings - Fork 3k
AWS: create HttpClientProperties, move s3 related methods into S3FileIOProperties #7562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amogh-jahagirdar
merged 8 commits into
apache:master
from
akshayakp97:add_http_client_props
May 10, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b74bca
AWS: create separate class for HttpClientProperties, move s3 helper m…
akshayakp97 5f50536
fix javadoc in S3FileIOProperties and HttpClientProperties
akshayakp97 65891ad
add AwsClientProperties class for aws creds provider
akshayakp97 7769028
fix javadoc in AwsProperties
akshayakp97 5601d12
add client region to AwsClientProperties, refactor credentialsProvide…
akshayakp97 74876a0
check secret key for null, update javadoc
akshayakp97 4c7494b
both access key and secret key should be non-null
akshayakp97 c59b956
remove javadoc from private method
akshayakp97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
183 changes: 183 additions & 0 deletions
183
aws/src/main/java/org/apache/iceberg/aws/AwsClientProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /* | ||
| * 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.iceberg.aws; | ||
|
|
||
| import java.util.Map; | ||
| import org.apache.iceberg.common.DynClasses; | ||
| import org.apache.iceberg.common.DynMethods; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Strings; | ||
| import org.apache.iceberg.util.PropertyUtil; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
| import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; | ||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; | ||
| import software.amazon.awssdk.regions.Region; | ||
|
|
||
| public class AwsClientProperties { | ||
| /** | ||
| * Configure the AWS credentials provider used to create AWS clients. A fully qualified concrete | ||
| * class with package that implements the {@link AwsCredentialsProvider} interface is required. | ||
| * | ||
| * <p>Additionally, the implementation class must also have a create() or create(Map) method | ||
| * implemented, which returns an instance of the class that provides aws credentials provider. | ||
| * | ||
| * <p>Example: | ||
| * client.credentials-provider=software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider | ||
| * | ||
| * <p>When set, the default client factory {@link | ||
| * org.apache.iceberg.aws.AwsClientFactories#defaultFactory()} and other AWS client factory | ||
| * classes will use this provider to get AWS credentials provided instead of reading the default | ||
| * credential chain to get AWS access credentials. | ||
| */ | ||
| public static final String CLIENT_CREDENTIALS_PROVIDER = "client.credentials-provider"; | ||
|
|
||
| /** | ||
| * Used by the client.credentials-provider configured value that will be used by {@link | ||
| * org.apache.iceberg.aws.AwsClientFactories#defaultFactory()} and other AWS client factory | ||
| * classes to pass provider-specific properties. Each property consists of a key name and an | ||
| * associated value. | ||
| */ | ||
| private static final String CLIENT_CREDENTIAL_PROVIDER_PREFIX = "client.credentials-provider."; | ||
|
|
||
| /** | ||
| * Used by {@link org.apache.iceberg.aws.AwsClientFactories.DefaultAwsClientFactory} and also | ||
| * other client factory classes. If set, all AWS clients except STS client will use the given | ||
| * region instead of the default region chain. | ||
| */ | ||
| public static final String CLIENT_REGION = "client.region"; | ||
|
|
||
| private String clientRegion; | ||
| private String clientCredentialsProvider; | ||
| private final Map<String, String> clientCredentialsProviderProperties; | ||
|
|
||
| public AwsClientProperties() { | ||
| this.clientRegion = null; | ||
| this.clientCredentialsProvider = null; | ||
| this.clientCredentialsProviderProperties = null; | ||
| } | ||
|
|
||
| public AwsClientProperties(Map<String, String> properties) { | ||
| this.clientRegion = properties.get(CLIENT_REGION); | ||
| this.clientCredentialsProvider = properties.get(CLIENT_CREDENTIALS_PROVIDER); | ||
| this.clientCredentialsProviderProperties = | ||
| PropertyUtil.propertiesWithPrefix(properties, CLIENT_CREDENTIAL_PROVIDER_PREFIX); | ||
| } | ||
|
|
||
| public String clientRegion() { | ||
| return clientRegion; | ||
| } | ||
|
|
||
| public void setClientRegion(String clientRegion) { | ||
| this.clientRegion = clientRegion; | ||
| } | ||
|
|
||
| /** | ||
| * Configure a client AWS region. | ||
| * | ||
| * <p>Sample usage: | ||
| * | ||
| * <pre> | ||
| * S3Client.builder().applyMutation(awsProperties::applyClientRegionConfiguration) | ||
| * </pre> | ||
| */ | ||
| public <T extends AwsClientBuilder> void applyClientRegionConfiguration(T builder) { | ||
| if (clientRegion != null) { | ||
| builder.region(Region.of(clientRegion)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns a credentials provider instance. If params were set, we return a new credentials | ||
| * instance. If none of the params are set, we try to dynamically load the provided credentials | ||
| * provider class. Upon loading the class, we try to invoke {@code create(Map<String, String>)} | ||
| * static method. If that fails, we fall back to {@code create()}. If credential provider class | ||
| * wasn't set, we fall back to default credentials provider. | ||
| * | ||
| * @param accessKeyId the AWS access key ID | ||
| * @param secretAccessKey the AWS secret access key | ||
| * @param sessionToken the AWS session token | ||
| * @return a credentials provider instance | ||
| */ | ||
| @SuppressWarnings("checkstyle:HiddenField") | ||
| public AwsCredentialsProvider credentialsProvider( | ||
jackye1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String accessKeyId, String secretAccessKey, String sessionToken) { | ||
| if (!Strings.isNullOrEmpty(accessKeyId) && !Strings.isNullOrEmpty(secretAccessKey)) { | ||
| if (Strings.isNullOrEmpty(sessionToken)) { | ||
| return StaticCredentialsProvider.create( | ||
| AwsBasicCredentials.create(accessKeyId, secretAccessKey)); | ||
| } else { | ||
| return StaticCredentialsProvider.create( | ||
| AwsSessionCredentials.create(accessKeyId, secretAccessKey, sessionToken)); | ||
| } | ||
| } | ||
|
|
||
| if (!Strings.isNullOrEmpty(this.clientCredentialsProvider)) { | ||
| return credentialsProvider(this.clientCredentialsProvider); | ||
| } | ||
|
|
||
| return DefaultCredentialsProvider.create(); | ||
| } | ||
|
|
||
| private AwsCredentialsProvider credentialsProvider(String credentialsProviderClass) { | ||
| Class<?> providerClass; | ||
| try { | ||
| providerClass = DynClasses.builder().impl(credentialsProviderClass).buildChecked(); | ||
| } catch (ClassNotFoundException e) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Cannot load class %s, it does not exist in the classpath", credentialsProviderClass), | ||
| e); | ||
| } | ||
|
|
||
| Preconditions.checkArgument( | ||
| AwsCredentialsProvider.class.isAssignableFrom(providerClass), | ||
| String.format( | ||
| "Cannot initialize %s, it does not implement %s.", | ||
| credentialsProviderClass, AwsCredentialsProvider.class.getName())); | ||
|
|
||
| try { | ||
| return createCredentialsProvider(providerClass); | ||
| } catch (NoSuchMethodException e) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Cannot create an instance of %s, it does not contain a static 'create' or 'create(Map<String, String>)' method", | ||
| credentialsProviderClass), | ||
| e); | ||
| } | ||
| } | ||
|
|
||
| private AwsCredentialsProvider createCredentialsProvider(Class<?> providerClass) | ||
| throws NoSuchMethodException { | ||
| AwsCredentialsProvider provider; | ||
| try { | ||
| provider = | ||
| DynMethods.builder("create") | ||
| .hiddenImpl(providerClass, Map.class) | ||
| .buildStaticChecked() | ||
| .invoke(clientCredentialsProviderProperties); | ||
| } catch (NoSuchMethodException e) { | ||
| provider = | ||
| DynMethods.builder("create").hiddenImpl(providerClass).buildStaticChecked().invoke(); | ||
| } | ||
| return provider; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.