From f5532638b3000f2c59e45925612be865c1ee3750 Mon Sep 17 00:00:00 2001 From: Oleh Onufryk Date: Wed, 25 Sep 2024 15:38:21 +0300 Subject: [PATCH 1/3] Autoconfigure SsmClient even if config import is not used --- .../ParameterStoreAutoConfiguration.java | 57 +++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../ParameterStoreAutoConfigurationTest.java | 63 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java create mode 100644 spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java new file mode 100644 index 000000000..4cdb9b27f --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.awspring.cloud.autoconfigure.config.parameterstore; + +import io.awspring.cloud.autoconfigure.core.AwsClientBuilderConfigurer; +import io.awspring.cloud.autoconfigure.core.AwsClientCustomizer; +import io.awspring.cloud.autoconfigure.core.AwsConnectionDetails; +import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; +import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import software.amazon.awssdk.services.ssm.SsmClient; +import software.amazon.awssdk.services.ssm.SsmClientBuilder; + +/** + * {@link AutoConfiguration Auto-Configuration} for AWS Parameter Store integration. + * + * @author Oleh Onufryk + * @since 3.3.0 + */ +@AutoConfiguration +@EnableConfigurationProperties(ParameterStoreProperties.class) +@ConditionalOnClass({ SsmClient.class }) +@AutoConfigureAfter({ CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class }) +@ConditionalOnProperty(name = "spring.cloud.aws.parameterstore.enabled", havingValue = "true", matchIfMissing = true) +public class ParameterStoreAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public SsmClient ssmClient(ParameterStoreProperties properties, + AwsClientBuilderConfigurer awsClientBuilderConfigurer, + ObjectProvider> customizers, + ObjectProvider connectionDetails) { + return awsClientBuilderConfigurer.configure(SsmClient.builder(), properties, connectionDetails.getIfAvailable(), + customizers.getIfAvailable()).build(); + } + +} diff --git a/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 45f2b8888..a52f7b419 100644 --- a/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -13,3 +13,4 @@ io.awspring.cloud.autoconfigure.dynamodb.DynamoDbAutoConfiguration io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerReloadAutoConfiguration io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerAutoConfiguration io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreReloadAutoConfiguration +io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreAutoConfiguration diff --git a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java new file mode 100644 index 000000000..d15df319c --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java @@ -0,0 +1,63 @@ +package io.awspring.cloud.autoconfigure.config.parameterstore; + +import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration; +import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; +import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; +import org.junit.jupiter.api.Test; +import org.mockito.internal.util.MockUtil; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import software.amazon.awssdk.services.ssm.SsmClient; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link ParameterStoreAutoConfiguration} + */ +class ParameterStoreAutoConfigurationTest { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.cloud.aws.region.static:eu-west-1") + .withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class, + CredentialsProviderAutoConfiguration.class, ParameterStoreAutoConfiguration.class, + AwsAutoConfiguration.class)); + + @Test + void parameterStoreAutoConfigurationIsDisabled() { + this.contextRunner.withPropertyValues("spring.cloud.aws.parameterstore.enabled:false") + .run(context -> assertThat(context).doesNotHaveBean(SsmClient.class)); + } + + @Test + void parameterStoreAutoConfigurationIsEnabled() { + this.contextRunner.withPropertyValues("spring.cloud.aws.parameterstore.enabled:true") + .run(context -> assertThat(context).hasSingleBean(SsmClient.class)); + } + + @Test + void createsClientBeanByDefault() { + this.contextRunner.run(context -> assertThat(context).hasSingleBean(SsmClient.class)); + } + + @Test + void usesCustomBeanWhenProvided() { + this.contextRunner.withUserConfiguration(CustomParameterStoreConfiguration.class).run(context -> { + assertThat(context).hasSingleBean(SsmClient.class); + assertThat(MockUtil.isMock(context.getBean(SsmClient.class))).isTrue(); + }); + } + + @TestConfiguration + static class CustomParameterStoreConfiguration { + + @Bean + SsmClient ssmClient() { + return mock(SsmClient.class); + } + + } + +} From 49bd3d01bb33752055cde6c7c1b1f176056685ac Mon Sep 17 00:00:00 2001 From: Oleh Onufryk Date: Wed, 25 Sep 2024 15:46:01 +0300 Subject: [PATCH 2/3] formatting --- .../ParameterStoreAutoConfigurationTest.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java index d15df319c..36e6d5694 100644 --- a/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java +++ b/spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfigurationTest.java @@ -1,5 +1,23 @@ +/* + * Copyright 2013-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.awspring.cloud.autoconfigure.config.parameterstore; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration; import io.awspring.cloud.autoconfigure.core.CredentialsProviderAutoConfiguration; import io.awspring.cloud.autoconfigure.core.RegionProviderAutoConfiguration; @@ -11,30 +29,27 @@ import org.springframework.context.annotation.Bean; import software.amazon.awssdk.services.ssm.SsmClient; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - /** * Tests for {@link ParameterStoreAutoConfiguration} */ class ParameterStoreAutoConfigurationTest { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .withPropertyValues("spring.cloud.aws.region.static:eu-west-1") - .withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class, - CredentialsProviderAutoConfiguration.class, ParameterStoreAutoConfiguration.class, - AwsAutoConfiguration.class)); + .withPropertyValues("spring.cloud.aws.region.static:eu-west-1") + .withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class, + CredentialsProviderAutoConfiguration.class, ParameterStoreAutoConfiguration.class, + AwsAutoConfiguration.class)); @Test void parameterStoreAutoConfigurationIsDisabled() { this.contextRunner.withPropertyValues("spring.cloud.aws.parameterstore.enabled:false") - .run(context -> assertThat(context).doesNotHaveBean(SsmClient.class)); + .run(context -> assertThat(context).doesNotHaveBean(SsmClient.class)); } @Test void parameterStoreAutoConfigurationIsEnabled() { this.contextRunner.withPropertyValues("spring.cloud.aws.parameterstore.enabled:true") - .run(context -> assertThat(context).hasSingleBean(SsmClient.class)); + .run(context -> assertThat(context).hasSingleBean(SsmClient.class)); } @Test From f57b99c7fa3797dcd10e2f625b9158f9d6651e64 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Wed, 25 Sep 2024 15:15:56 +0200 Subject: [PATCH 3/3] autoconfigure after AwsAutoConfiguration. --- .../parameterstore/ParameterStoreAutoConfiguration.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java index 4cdb9b27f..f397a7387 100644 --- a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/config/parameterstore/ParameterStoreAutoConfiguration.java @@ -15,6 +15,7 @@ */ package io.awspring.cloud.autoconfigure.config.parameterstore; +import io.awspring.cloud.autoconfigure.core.AwsAutoConfiguration; import io.awspring.cloud.autoconfigure.core.AwsClientBuilderConfigurer; import io.awspring.cloud.autoconfigure.core.AwsClientCustomizer; import io.awspring.cloud.autoconfigure.core.AwsConnectionDetails; @@ -40,7 +41,8 @@ @AutoConfiguration @EnableConfigurationProperties(ParameterStoreProperties.class) @ConditionalOnClass({ SsmClient.class }) -@AutoConfigureAfter({ CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class }) +@AutoConfigureAfter({ CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class, + AwsAutoConfiguration.class }) @ConditionalOnProperty(name = "spring.cloud.aws.parameterstore.enabled", havingValue = "true", matchIfMissing = true) public class ParameterStoreAutoConfiguration {