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); + } + + } + +}