Skip to content
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

Add support to customize httpClientBuilder for s3 client #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yidian1997
Copy link

@yidian1997 yidian1997 commented Nov 26, 2024

Pull Request

What

Add a new parameter S3_SDK_HTTP_CLIENT_BUILDER_CONFIG
This is a class to customize the httpClient used by the s3 client

Why

Our use case is:
We have a minio server with https endpoint. And the minio server uses self-signed certificate to do the TLS.
When our kafka client uses this lib to put object to that minio, we need to make sure the CA certificate of the minio server should in the truststore used by the s3 client. This is the reason we want to customize the httpClient.

Another limitation is that we need add the CA certificate to the truststore in the code instead of using the keytool command line. Since the command line is hard to automate in our use case. So finally I choose add this support in the this lib.

How

  1. Add the S3_SDK_HTTP_CLIENT_BUILDER_CONFIG class parameter
  2. Add method getAmazonSdkHttpClientBuilderInstance() to get the object of SdkHttpClient.Builder by reflection
  3. Add some logic in the createAmazonS3Client() method to config the SdkHttpClient.Builder

Test

I added this parameter with DefaultTlsTrustManagersProvider in the UT and it works fine.

@yidian1997 yidian1997 marked this pull request as ready for review December 5, 2024 03:46
@yidian1997 yidian1997 marked this pull request as draft December 5, 2024 03:47
@yidian1997 yidian1997 marked this pull request as ready for review December 5, 2024 06:20
@@ -301,12 +312,33 @@ private BlobStorageClient createAmazonS3Client() {
this.getAmazonEndpointOverride().ifPresent(clientBuilder::endpointOverride);
this.getAmazonRegion().ifPresent(clientBuilder::region);
this.getAmazonCredentialsProvider().ifPresent(clientBuilder::credentialsProvider);
if (this.getTlsTrustManagerProvider().isPresent()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ifPresent API as seen above

@@ -69,6 +69,7 @@ void shouldRoundtrip(final RoundtripArgument argument) {
final String bucket = "bucket";
final String basePath = "s3://" + bucket + "/base/";
final Map<String, Object> properties = ImmutableMap.<String, Object>builder()
.put(AbstractLargeMessageConfig.S3_TLS_TRUST_MANAGER_PROVIDER_CONFIG,DefaultTlsTrustManagersProvider.class.getName())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.put(AbstractLargeMessageConfig.S3_TLS_TRUST_MANAGER_PROVIDER_CONFIG,DefaultTlsTrustManagersProvider.class.getName())
.put(AbstractLargeMessageConfig.S3_TLS_TRUST_MANAGER_PROVIDER_CONFIG, DefaultTlsTrustManagersProvider.class.getName())

@@ -36,6 +36,7 @@
import io.confluent.common.config.ConfigDef;
import io.confluent.common.config.ConfigDef.Importance;
import io.confluent.common.config.ConfigDef.Type;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting does not follow our guildes in the README

if (this.enableAmazonS3PathStyleAccess()) {
clientBuilder.forcePathStyle(true);
}
return new AmazonS3Client(clientBuilder.build());
}

private Optional<TlsTrustManagersProvider> getTlsTrustManagerProvider() {
Class<?> c = this.getClass(S3_TLS_TRUST_MANAGER_PROVIDER_CONFIG);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Class<?> c = this.getClass(S3_TLS_TRUST_MANAGER_PROVIDER_CONFIG);
final Class<?> c = this.getClass(S3_TLS_TRUST_MANAGER_PROVIDER_CONFIG);

if (c == null) {
return Optional.empty();
} else {
Object o = Utils.newInstance(c);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object o = Utils.newInstance(c);
final Object o = Utils.newInstance(c);

@@ -301,12 +312,33 @@ private BlobStorageClient createAmazonS3Client() {
this.getAmazonEndpointOverride().ifPresent(clientBuilder::endpointOverride);
this.getAmazonRegion().ifPresent(clientBuilder::region);
this.getAmazonCredentialsProvider().ifPresent(clientBuilder::credentialsProvider);
if (this.getTlsTrustManagerProvider().isPresent()) {
clientBuilder.httpClientBuilder(
ApacheHttpClient.builder().tlsTrustManagersProvider(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which client is used by default? Would it be instead feasible to specify a HttpClientProvider and the user can implement it? Then we can get rid of the dependency on the apache module

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments, I will try to figure out if it is feasible

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is software.amazon.awssdk.core.internal.http.loader.DefaultSdkHttpClientBuilder
And I update the PR to let user specify the HttpClientProvider instead of TlsTrustManagerProvider

@yidian1997 yidian1997 changed the title Add support to customize tlsTrustManagersProvider for s3 client Add support to customize httpClientBuilder for s3 client Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants