From 299049201b7a97cc88cfb547b1d99cfad7a97636 Mon Sep 17 00:00:00 2001 From: Kunal Varpe Date: Sun, 22 Dec 2024 01:04:44 +0530 Subject: [PATCH 1/2] Support `@ServiceConnection` endpoint attribute for `S3Presigner` Fixes: gh-1289 --- .../cloud/autoconfigure/s3/S3AutoConfiguration.java | 9 +++------ .../AwsContainerConnectionDetailsFactoryTest.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java index b84345952..e0a48c0ad 100644 --- a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java @@ -110,12 +110,9 @@ S3Presigner s3Presigner(S3Properties properties, AwsProperties awsProperties, .credentialsProvider(credentialsProvider).region(AwsClientBuilderConfigurer.resolveRegion(properties, connectionDetails.getIfAvailable(), regionProvider)); - if (properties.getEndpoint() != null) { - builder.endpointOverride(properties.getEndpoint()); - } - else if (awsProperties.getEndpoint() != null) { - builder.endpointOverride(awsProperties.getEndpoint()); - } + Optional.ofNullable(properties.getEndpoint()).ifPresent(builder::endpointOverride); + Optional.ofNullable(awsProperties.getEndpoint()).ifPresent(builder::endpointOverride); + Optional.ofNullable(connectionDetails.getIfAvailable()).map(AwsConnectionDetails::getEndpoint).ifPresent(builder::endpointOverride); Optional.ofNullable(awsProperties.getFipsEnabled()).ifPresent(builder::fipsEnabled); Optional.ofNullable(awsProperties.getDualstackEnabled()).ifPresent(builder::dualstackEnabled); return builder.build(); diff --git a/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java b/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java index d3cdf2f90..271f70121 100644 --- a/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java +++ b/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java @@ -28,6 +28,7 @@ import io.awspring.cloud.autoconfigure.ses.SesAutoConfiguration; import io.awspring.cloud.autoconfigure.sns.SnsAutoConfiguration; import io.awspring.cloud.autoconfigure.sqs.SqsAutoConfiguration; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -40,10 +41,14 @@ import org.testcontainers.utility.DockerImageName; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.presigner.S3Presigner; +import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; import software.amazon.awssdk.services.ses.SesClient; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sqs.SqsAsyncClient; +import java.time.Duration; + @SpringJUnitConfig @Testcontainers(disabledWithoutDocker = true) class AwsContainerConnectionDetailsFactoryTest { @@ -88,6 +93,14 @@ void configuresS3ClientWithServiceConnection(@Autowired S3Client client) { assertThatCode(client::listBuckets).doesNotThrowAnyException(); } + @Test + @Disabled + void configuresS3PresignerWithServiceConnection(@Autowired S3Presigner s3Presigner) { + assertThatCode(() -> s3Presigner.presignGetObject(req1 -> req1.signatureDuration(Duration.ofSeconds(2)) + .getObjectRequest(req2 -> req2.bucket("my-bucket").build()))) + .doesNotThrowAnyException(); + } + @Configuration(proxyBeanMethods = false) @ImportAutoConfiguration({ AwsAutoConfiguration.class, CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class, DynamoDbAutoConfiguration.class, SesAutoConfiguration.class, From b68f9a98e6f48ad59eea313cf5adae30c1cf92b5 Mon Sep 17 00:00:00 2001 From: Kunal Varpe Date: Mon, 23 Dec 2024 18:57:24 +0530 Subject: [PATCH 2/2] Fix failing test Reason: - Endpoint override conditions are added as else if blocks - Removed the Disable test annotation as added correct test for validating aws connection details with s3 presigner. --- .../autoconfigure/s3/S3AutoConfiguration.java | 14 +++++++++++--- .../AwsContainerConnectionDetailsFactoryTest.java | 9 ++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java index e0a48c0ad..133a29885 100644 --- a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/s3/S3AutoConfiguration.java @@ -56,6 +56,7 @@ * * @author Maciej Walkowiak * @author Matej Nedic + * @author Kunal Varpe */ @AutoConfiguration @ConditionalOnClass({ S3Client.class, S3OutputStreamProvider.class }) @@ -110,9 +111,16 @@ S3Presigner s3Presigner(S3Properties properties, AwsProperties awsProperties, .credentialsProvider(credentialsProvider).region(AwsClientBuilderConfigurer.resolveRegion(properties, connectionDetails.getIfAvailable(), regionProvider)); - Optional.ofNullable(properties.getEndpoint()).ifPresent(builder::endpointOverride); - Optional.ofNullable(awsProperties.getEndpoint()).ifPresent(builder::endpointOverride); - Optional.ofNullable(connectionDetails.getIfAvailable()).map(AwsConnectionDetails::getEndpoint).ifPresent(builder::endpointOverride); + if (properties.getEndpoint() != null) { + builder.endpointOverride(properties.getEndpoint()); + } + else if (awsProperties.getEndpoint() != null) { + builder.endpointOverride(awsProperties.getEndpoint()); + } + else if (connectionDetails.getIfAvailable() != null + && connectionDetails.getIfAvailable().getEndpoint() != null) { + builder.endpointOverride(connectionDetails.getIfAvailable().getEndpoint()); + } Optional.ofNullable(awsProperties.getFipsEnabled()).ifPresent(builder::fipsEnabled); Optional.ofNullable(awsProperties.getDualstackEnabled()).ifPresent(builder::dualstackEnabled); return builder.build(); diff --git a/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java b/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java index 271f70121..47ae03aae 100644 --- a/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java +++ b/spring-cloud-aws-testcontainers/src/test/java/io/awspring/cloud/testcontainers/AwsContainerConnectionDetailsFactoryTest.java @@ -28,7 +28,7 @@ import io.awspring.cloud.autoconfigure.ses.SesAutoConfiguration; import io.awspring.cloud.autoconfigure.sns.SnsAutoConfiguration; import io.awspring.cloud.autoconfigure.sqs.SqsAutoConfiguration; -import org.junit.jupiter.api.Disabled; +import java.time.Duration; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -42,13 +42,10 @@ import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.presigner.S3Presigner; -import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; import software.amazon.awssdk.services.ses.SesClient; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sqs.SqsAsyncClient; -import java.time.Duration; - @SpringJUnitConfig @Testcontainers(disabledWithoutDocker = true) class AwsContainerConnectionDetailsFactoryTest { @@ -94,11 +91,9 @@ void configuresS3ClientWithServiceConnection(@Autowired S3Client client) { } @Test - @Disabled void configuresS3PresignerWithServiceConnection(@Autowired S3Presigner s3Presigner) { assertThatCode(() -> s3Presigner.presignGetObject(req1 -> req1.signatureDuration(Duration.ofSeconds(2)) - .getObjectRequest(req2 -> req2.bucket("my-bucket").build()))) - .doesNotThrowAnyException(); + .getObjectRequest(req2 -> req2.bucket("my-bucket").key("test").build()))).doesNotThrowAnyException(); } @Configuration(proxyBeanMethods = false)