-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autoconfigure SecretsManagerClient event when config import is not used.
Fixes #1016
- Loading branch information
1 parent
02c0f97
commit 1da30c4
Showing
5 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...o/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.awspring.cloud.autoconfigure.config.secretsmanager; | ||
|
||
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.EnableAutoConfiguration; | ||
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.secretsmanager.SecretsManagerClient; | ||
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-Configuration} for Secrets Manager integration. | ||
* | ||
* @author Maciej Walkowiak | ||
* @since 3.2.0 | ||
*/ | ||
@AutoConfiguration | ||
@EnableConfigurationProperties(SecretsManagerProperties.class) | ||
@ConditionalOnClass({SecretsManagerClient.class}) | ||
@AutoConfigureAfter({CredentialsProviderAutoConfiguration.class, RegionProviderAutoConfiguration.class}) | ||
@ConditionalOnProperty(name = "spring.cloud.aws.secretsmanager.enabled", havingValue = "true", matchIfMissing = true) | ||
public class SecretsManagerAutoConfiguration { | ||
|
||
@ConditionalOnMissingBean | ||
@Bean | ||
public SecretsManagerClient secretsManagerClient(SecretsManagerProperties properties, | ||
AwsClientBuilderConfigurer awsClientBuilderConfigurer, | ||
ObjectProvider<AwsClientCustomizer<SecretsManagerClientBuilder>> customizer, | ||
ObjectProvider<AwsConnectionDetails> connectionDetails) { | ||
return awsClientBuilderConfigurer.configure(SecretsManagerClient.builder(), properties, connectionDetails.getIfAvailable(), | ||
customizer.getIfAvailable()).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...pring/cloud/autoconfigure/config/secretsmanager/SecretsManagerAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.awspring.cloud.autoconfigure.config.secretsmanager; | ||
|
||
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.secretsmanager.SecretsManagerClient; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class SecretsManagerAutoConfigurationTests { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withPropertyValues("spring.cloud.aws.region.static:eu-west-1") | ||
.withConfiguration(AutoConfigurations.of(RegionProviderAutoConfiguration.class, | ||
CredentialsProviderAutoConfiguration.class, SecretsManagerAutoConfiguration.class, | ||
AwsAutoConfiguration.class)); | ||
|
||
@Test | ||
void secretsManagerAutoConfigurationIsDisabled() { | ||
this.contextRunner.withPropertyValues("spring.cloud.aws.secretsmanager.enabled:false") | ||
.run(context -> assertThat(context).doesNotHaveBean(SecretsManagerClient.class)); | ||
} | ||
|
||
@Test | ||
void secretsManagerAutoConfigurationIsEnabled() { | ||
this.contextRunner.withPropertyValues("spring.cloud.aws.secretsmanager.enabled:true") | ||
.run(context -> assertThat(context).hasSingleBean(SecretsManagerClient.class)); | ||
} | ||
|
||
@Test | ||
void createsClientBeanByDefault() { | ||
this.contextRunner | ||
.run(context -> assertThat(context).hasSingleBean(SecretsManagerClient.class)); | ||
} | ||
|
||
@Test | ||
void usesCustomBeanWhenProvided() { | ||
this.contextRunner.withUserConfiguration(CustomConfiguration.class) | ||
.run(context -> { | ||
assertThat(context).hasSingleBean(SecretsManagerClient.class); | ||
assertThat(MockUtil.isMock(context.getBean(SecretsManagerClient.class))).isTrue(); | ||
}); | ||
} | ||
|
||
@TestConfiguration | ||
static class CustomConfiguration { | ||
|
||
@Bean | ||
SecretsManagerClient secretsManagerClient() { | ||
return mock(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...ud-aws-samples/spring-cloud-aws-secrets-manager-sample/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters