Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoconfigure SsmClient even if config import is not used #1233

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.AwsAutoConfiguration;
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,
AwsAutoConfiguration.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<AwsClientCustomizer<SsmClientBuilder>> customizers,
ObjectProvider<AwsConnectionDetails> connectionDetails) {
return awsClientBuilderConfigurer.configure(SsmClient.builder(), properties, connectionDetails.getIfAvailable(),
customizers.getIfAvailable()).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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;
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;

/**
* 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);
}

}

}
Loading