From fe4db76f47a0e4d3bbd3ff31a46c8189201cae96 Mon Sep 17 00:00:00 2001 From: Rastislav Zlacky Date: Sat, 4 Mar 2023 23:55:04 +0100 Subject: [PATCH] Adjust auto-configuration to register at most one Vault health indicator. Fixes gh-676 Original pull request: gh-677 --- .../VaultHealthIndicatorAutoConfiguration.java | 4 ++-- ...VaultReactiveHealthIndicatorConfiguration.java | 8 ++++---- ...aultHealthIndicatorAutoConfigurationTests.java | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfiguration.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfiguration.java index 263ce45af..de5bdb718 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfiguration.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfiguration.java @@ -30,7 +30,7 @@ * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration * Auto-configuration} for Vault providing beans for the application context. * - * @author Mark Paluch + * @author Mark Paluch, Rastislav Zlacky * @since 2.0 */ @Configuration(proxyBeanMethods = false) @@ -39,7 +39,7 @@ @ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true) @AutoConfigureBefore(HealthContributorAutoConfiguration.class) @AutoConfigureAfter({ VaultAutoConfiguration.class, VaultReactiveAutoConfiguration.class }) -@Import({ VaultHealthIndicatorConfiguration.class, VaultReactiveHealthIndicatorConfiguration.class }) +@Import({ VaultReactiveHealthIndicatorConfiguration.class, VaultHealthIndicatorConfiguration.class }) public class VaultHealthIndicatorAutoConfiguration { } diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultReactiveHealthIndicatorConfiguration.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultReactiveHealthIndicatorConfiguration.java index e0effa0be..996c8c68e 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultReactiveHealthIndicatorConfiguration.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultReactiveHealthIndicatorConfiguration.java @@ -32,7 +32,7 @@ /** * Configuration for {@link VaultReactiveHealthIndicator}. * - * @author Mark Paluch + * @author Mark Paluch, Rastislav Zlacky * @since 2.0 */ @Configuration(proxyBeanMethods = false) @@ -47,9 +47,9 @@ class VaultReactiveHealthIndicatorConfiguration this.reactiveVaultTemplates = reactiveVaultTemplates; } - @Bean - @ConditionalOnMissingBean(name = { "vaultReactiveHealthIndicator" }) - ReactiveHealthContributor vaultReactiveHealthIndicator() { + @Bean(name = { "vaultHealthIndicator", "vaultReactiveHealthIndicator" }) + @ConditionalOnMissingBean(name = { "vaultHealthIndicator" }) + ReactiveHealthContributor vaultHealthIndicator() { return createContributor(this.reactiveVaultTemplates); } diff --git a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfigurationTests.java b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfigurationTests.java index ff7aba323..c9e63e56f 100644 --- a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfigurationTests.java +++ b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultHealthIndicatorAutoConfigurationTests.java @@ -30,7 +30,7 @@ /** * Unit tests for {@link VaultHealthIndicatorAutoConfiguration}. * - * @author Mark Paluch + * @author Mark Paluch, Rastislav Zlacky */ class VaultHealthIndicatorAutoConfigurationTests { @@ -62,6 +62,17 @@ void shouldConfigureReactiveHealthIndicator() { } + @Test + void shouldConfigureSingleHealthIndicator() { + + this.contextRunner.withUserConfiguration(ImperativeConfiguration.class, ReactiveConfiguration.class) + .run(context -> { + assertThat(context).hasBean("vaultHealthIndicator") + .hasSingleBean(VaultReactiveHealthIndicator.class) + .doesNotHaveBean(VaultHealthIndicator.class); + }); + } + static class ImperativeConfiguration { @Bean @@ -74,7 +85,7 @@ VaultOperations vaultOperations() { static class ReactiveConfiguration { @Bean - ReactiveVaultOperations vaultOperations() { + ReactiveVaultOperations reactiveVaultOperations() { return mock(ReactiveVaultOperations.class); }