From 5ab6b1730b5a2524ef97395e76680d9c4a6518fb Mon Sep 17 00:00:00 2001 From: Hussein Ahmed Date: Wed, 18 Sep 2024 12:33:53 +0200 Subject: [PATCH] remove the use of try for StsClient to avoid closing the client prematurely --- .../mongo/auth/AwsAuthenticationHelper.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/internal/utils/persistence/src/main/java/org/eclipse/ditto/internal/utils/persistence/mongo/auth/AwsAuthenticationHelper.java b/internal/utils/persistence/src/main/java/org/eclipse/ditto/internal/utils/persistence/mongo/auth/AwsAuthenticationHelper.java index 442507f4f8..b6650d8dbb 100644 --- a/internal/utils/persistence/src/main/java/org/eclipse/ditto/internal/utils/persistence/mongo/auth/AwsAuthenticationHelper.java +++ b/internal/utils/persistence/src/main/java/org/eclipse/ditto/internal/utils/persistence/mongo/auth/AwsAuthenticationHelper.java @@ -61,25 +61,24 @@ public static MongoCredential provideAwsIamBasedMongoCredential( } final Supplier awsFreshCredentialSupplier; - try (final StsClient stsClient = stsClientBuilder.build()) { - awsFreshCredentialSupplier = () -> { - LOGGER.info("Supplying AWS IAM credentials, assuming role <{}> in session name <{}>", - awsRoleArn, awsSessionName); + final StsClient stsClient = stsClientBuilder.build(); + awsFreshCredentialSupplier = () -> { + LOGGER.info("Supplying AWS IAM credentials, assuming role <{}> in session name <{}>", + awsRoleArn, awsSessionName); - // assume role using the AWS SDK - final AssumeRoleRequest roleRequest = AssumeRoleRequest.builder() - .roleArn(awsRoleArn) - .roleSessionName(awsSessionName) - .build(); - final AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest); - final Credentials awsCredentials = roleResponse.credentials(); + // assume role using the AWS SDK + final AssumeRoleRequest roleRequest = AssumeRoleRequest.builder() + .roleArn(awsRoleArn) + .roleSessionName(awsSessionName) + .build(); + final AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest); + final Credentials awsCredentials = roleResponse.credentials(); - return new AwsCredential(awsCredentials.accessKeyId(), awsCredentials.secretAccessKey(), - awsCredentials.sessionToken()); - }; + return new AwsCredential(awsCredentials.accessKeyId(), awsCredentials.secretAccessKey(), + awsCredentials.sessionToken()); + }; - return MongoCredential.createAwsCredential(null, null) - .withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier); - } + return MongoCredential.createAwsCredential(null, null) + .withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier); } }