Skip to content

Create new DefaultCredentialsProvider instance every time instead of … #6080

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
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS CRT-based S3 Client",
"contributor": "",
"description": "Fixed \"Connection pool shut down\" error thrown when a default AWS CRT-based S3 client is created and closed per request. See [#5881](https://github.com/aws/aws-sdk-java-v2/issues/5881)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.concurrent.Executor;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.SdkTestInternalApi;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute;
import software.amazon.awssdk.awscore.AwsRequest;
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
Expand Down Expand Up @@ -126,6 +128,10 @@ private static S3AsyncClient initializeS3AsyncClient(DefaultS3CrtClientBuilder b
builder.executionInterceptors.forEach(overrideConfigurationBuilder::addExecutionInterceptor);
}

if (builder.credentialsProvider == null) {
builder = builder.credentialsProvider(DefaultCredentialsProvider.builder().build());
}

DefaultS3CrtClientBuilder finalBuilder = resolveChecksumConfiguration(builder);

S3AsyncClientBuilder s3AsyncClientBuilder =
Expand Down Expand Up @@ -228,6 +234,13 @@ public static final class DefaultS3CrtClientBuilder implements S3CrtAsyncClientB
private Executor futureCompletionExecutor;
private Boolean disableS3ExpressSessionAuth;


@Override
public DefaultS3CrtClientBuilder credentialsProvider(AwsCredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}

@Override
public DefaultS3CrtClientBuilder credentialsProvider(
IdentityProvider<? extends AwsCredentialsIdentity> credentialsProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.net.URI;
import java.time.Duration;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.crt.auth.credentials.CredentialsProvider;
import software.amazon.awssdk.crt.http.HttpMonitoringOptions;
import software.amazon.awssdk.crt.http.HttpProxyOptions;
Expand All @@ -36,6 +35,7 @@
import software.amazon.awssdk.services.s3.crt.S3CrtHttpConfiguration;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.SdkAutoCloseable;
import software.amazon.awssdk.utils.Validate;

/**
* Internal client configuration resolver
Expand Down Expand Up @@ -80,10 +80,8 @@ public S3NativeClientConfiguration(Builder builder) {
clientTlsContextOptions.withVerifyPeer(!builder.httpConfiguration.trustAllCertificatesEnabled());
}
this.tlsContext = new TlsContext(clientTlsContextOptions);
this.credentialProviderAdapter =
builder.credentialsProvider == null ?
new CrtCredentialsProviderAdapter(DefaultCredentialsProvider.create()) :
new CrtCredentialsProviderAdapter(builder.credentialsProvider);
this.credentialProviderAdapter = new CrtCredentialsProviderAdapter(
Validate.paramNotNull(builder.credentialsProvider, "credentialsProvider"));

this.credentialsProvider = credentialProviderAdapter.crtCredentials();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.signer.AwsS3V4Signer;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.identity.spi.IdentityProvider;
import software.amazon.awssdk.services.s3.DelegatingS3AsyncClient;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.endpoints.S3ClientContextParams;
Expand Down Expand Up @@ -116,4 +119,25 @@ void crtClient_with_crossRegionAccessEnabled_asFalse() {
}
}

@Test
void defaultClient_credentialsProvidersNotSingleton() {
try (S3AsyncClient client = S3AsyncClient.crtBuilder().build();
S3AsyncClient anotherClient = S3AsyncClient.crtBuilder().build()) {

IdentityProvider<? extends AwsCredentialsIdentity> identityProvider =
client.serviceClientConfiguration().credentialsProvider();

IdentityProvider<? extends AwsCredentialsIdentity> identityProviderFromAnotherClient =
anotherClient.serviceClientConfiguration().credentialsProvider();

assertThat(identityProvider)
.isNotEqualTo(identityProviderFromAnotherClient);
assertThat(identityProvider)
.isInstanceOf(DefaultCredentialsProvider.class);
assertThat(identityProvider)
.isNotEqualTo(DefaultCredentialsProvider.create());
assertThat(identityProviderFromAnotherClient)
.isNotEqualTo(DefaultCredentialsProvider.create());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ public void operationWithResponseAlgorithms_optOutValidationFromClient_shouldHon

s3NativeClientConfiguration = S3NativeClientConfiguration.builder()
.endpointOverride(DEFAULT_ENDPOINT)
.credentialsProvider(null)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test",
"test")))
.build();

asyncHttpClient = new S3CrtAsyncHttpClient(s3Client, S3CrtAsyncHttpClient.builder()
Expand All @@ -312,7 +313,8 @@ public void operationWithResponseAlgorithms_optInFromRequest_shouldHonor() {

s3NativeClientConfiguration = S3NativeClientConfiguration.builder()
.endpointOverride(DEFAULT_ENDPOINT)
.credentialsProvider(null)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test",
"test")))
.build();

asyncHttpClient = new S3CrtAsyncHttpClient(s3Client, S3CrtAsyncHttpClient.builder()
Expand Down Expand Up @@ -425,6 +427,8 @@ void build_shouldPassThroughParameters() {
.signingRegion(signingRegion)
.thresholdInBytes(1024L)
.targetThroughputInGbps(3.5)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test",
"test")))
.maxNativeMemoryLimitInBytes(5L * 1024 * 1024 * 1024)
.standardRetryOptions(
new StandardRetryOptions()
Expand Down Expand Up @@ -466,6 +470,8 @@ void build_partSizeConfigured_shouldApplyToThreshold() {
long partSizeInBytes = 1024 * 8L;
S3NativeClientConfiguration configuration =
S3NativeClientConfiguration.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test",
"test")))
.partSizeInBytes(partSizeInBytes)
.build();
try (S3CrtAsyncHttpClient client =
Expand All @@ -480,6 +486,8 @@ void build_partSizeConfigured_shouldApplyToThreshold() {
void build_nullHttpConfiguration() {
S3NativeClientConfiguration configuration =
S3NativeClientConfiguration.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test",
"test")))
.build();
try (S3CrtAsyncHttpClient client =
(S3CrtAsyncHttpClient) S3CrtAsyncHttpClient.builder().s3ClientConfiguration(configuration).build()) {
Expand Down Expand Up @@ -539,6 +547,8 @@ void build_ProxyConfigurationWithEnvironmentVariables(S3CrtHttpConfiguration s3C
S3NativeClientConfiguration configuration =
S3NativeClientConfiguration.builder()
.httpConfiguration(s3CrtHttpConfiguration)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test",
"test")))
.build();
try(S3CrtAsyncHttpClient client =
(S3CrtAsyncHttpClient) S3CrtAsyncHttpClient.builder().s3ClientConfiguration(configuration).build()) {
Expand Down
Loading