diff --git a/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3FileSystemFactory.java b/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3FileSystemFactory.java index 3eef41c1f146..e2aecdb4b1be 100644 --- a/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3FileSystemFactory.java +++ b/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3FileSystemFactory.java @@ -66,30 +66,25 @@ public S3FileSystemFactory(OpenTelemetry openTelemetry, S3FileSystemConfig confi .retryPolicy(retryPolicy) .build()); - Optional staticCredentialsProvider = getStaticCredentialsProvider(config); - staticCredentialsProvider.ifPresent(s3::credentialsProvider); - Optional.ofNullable(config.getRegion()).map(Region::of).ifPresent(s3::region); Optional.ofNullable(config.getEndpoint()).map(URI::create).ifPresent(s3::endpointOverride); s3.forcePathStyle(config.isPathStyleAccess()); - if (config.getIamRole() != null) { - StsClientBuilder sts = StsClient.builder(); - Optional.ofNullable(config.getStsEndpoint()).map(URI::create).ifPresent(sts::endpointOverride); - Optional.ofNullable(config.getStsRegion()) - .or(() -> Optional.ofNullable(config.getRegion())) - .map(Region::of).ifPresent(sts::region); - staticCredentialsProvider.ifPresent(sts::credentialsProvider); + Optional staticCredentialsProvider = getStaticCredentialsProvider(config); + if (config.getIamRole() != null) { s3.credentialsProvider(StsAssumeRoleCredentialsProvider.builder() .refreshRequest(request -> request .roleArn(config.getIamRole()) .roleSessionName(config.getRoleSessionName()) .externalId(config.getExternalId())) - .stsClient(sts.build()) + .stsClient(getStsClient(config, staticCredentialsProvider)) .asyncCredentialUpdateEnabled(true) .build()); } + else { + staticCredentialsProvider.ifPresent(s3::credentialsProvider); + } ApacheHttpClient.Builder httpClient = ApacheHttpClient.builder() .maxConnections(config.getMaxConnections()) @@ -150,4 +145,15 @@ private static Optional getStaticCredentialsProvider( } return Optional.empty(); } + + private static StsClient getStsClient(S3FileSystemConfig config, Optional staticCredentialsProvider) + { + StsClientBuilder sts = StsClient.builder(); + Optional.ofNullable(config.getStsEndpoint()).map(URI::create).ifPresent(sts::endpointOverride); + Optional.ofNullable(config.getStsRegion()) + .or(() -> Optional.ofNullable(config.getRegion())) + .map(Region::of).ifPresent(sts::region); + staticCredentialsProvider.ifPresent(sts::credentialsProvider); + return sts.build(); + } }