Do not require SessionManager when configuring SecretLeaseContainer using bootstrap config.

Closes gh-722
This commit is contained in:
Mark Paluch
2024-03-08 11:38:27 +01:00
parent d21b776f56
commit d0ca0aa00d
3 changed files with 43 additions and 16 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -132,7 +133,7 @@ public class VaultBootstrapPropertySourceConfiguration implements InitializingBe
* @param vaultOperations the {@link VaultOperations}.
* @param taskSchedulerWrapper the {@link TaskSchedulerWrapper}.
* @param sessionManager the {@link SessionManager} to listen for authentication
* events.
* events. Bean can be absent.
* @return the {@link SecretLeaseContainer} for Vault secret lease management.
* @see SessionManager
* @see LifecycleAwareSessionManager
@@ -141,9 +142,9 @@ public class VaultBootstrapPropertySourceConfiguration implements InitializingBe
@Lazy
@ConditionalOnMissingBean
public SecretLeaseContainer secretLeaseContainer(VaultOperations vaultOperations,
TaskSchedulerWrapper taskSchedulerWrapper, SessionManager sessionManager) {
TaskSchedulerWrapper taskSchedulerWrapper, ObjectProvider<SessionManager> sessionManager) {
return this.configuration.createSecretLeaseContainer(vaultOperations, taskSchedulerWrapper::getTaskScheduler,
sessionManager);
sessionManager.getIfAvailable());
}
}

View File

@@ -45,7 +45,7 @@ public class VaultBootstrapConfigurationTests {
@Test
public void shouldConfigureWithoutAuthentication() {
this.contextRunner.withPropertyValues("spring.cloud.vault.kv.enabled=false",
this.contextRunner.withPropertyValues("spring.cloud.vault.kv.enabled=true",
"spring.cloud.vault.authentication=NONE", "spring.cloud.bootstrap.enabled=true").run(context -> {
assertThat(context).doesNotHaveBean(SessionManager.class);

View File

@@ -26,11 +26,13 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.vault.authentication.SessionManager;
import org.springframework.vault.core.VaultOperations;
import org.springframework.vault.core.lease.LeaseEndpoints;
import org.springframework.vault.core.lease.SecretLeaseContainer;
import org.springframework.vault.support.LeaseStrategy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -48,7 +50,10 @@ public class VaultBootstrapPropertySourceConfigurationTests {
@Test
public void shouldConfigureExpiryTimeoutsAndStrategy() {
this.contextRunner.withUserConfiguration(MockConfiguration.class).withAllowBeanDefinitionOverriding(true)
this.contextRunner
.withUserConfiguration(MockSecretLeaseContainerConfiguration.class,
MockVaultOperationsConfiguration.class)
.withAllowBeanDefinitionOverriding(true)
.withPropertyValues("spring.cloud.vault.kv.enabled=false",
"spring.cloud.vault.config.lifecycle.expiry-threshold=5m",
"spring.cloud.vault.config.lifecycle.min-renewal=6m",
@@ -65,19 +70,24 @@ public class VaultBootstrapPropertySourceConfigurationTests {
});
}
@Test
public void shouldConfigureWithoutAuthentication() {
this.contextRunner.withUserConfiguration(MockVaultOperationsConfiguration.class)
.withAllowBeanDefinitionOverriding(true)
.withPropertyValues("spring.cloud.vault.kv.enabled=true",
"spring.cloud.vault.config.lifecycle.enabled=true", "spring.cloud.vault.authentication=NONE",
"spring.cloud.bootstrap.enabled=true")
.run(context -> {
assertThat(context).doesNotHaveBean(SessionManager.class);
assertThat(context).hasSingleBean(SecretLeaseContainer.class);
});
}
@EnableConfigurationProperties(VaultProperties.class)
@Configuration(proxyBeanMethods = false)
private static class MockConfiguration {
@Bean
VaultOperations vaultOperations() {
return mock(VaultOperations.class);
}
@Bean
VaultBootstrapConfiguration.TaskSchedulerWrapper taskSchedulerWrapper() {
return new VaultBootstrapConfiguration.TaskSchedulerWrapper(mock(ThreadPoolTaskScheduler.class));
}
private static class MockSecretLeaseContainerConfiguration {
@Bean
SecretLeaseContainer secretLeaseContainer(VaultProperties properties) {
@@ -90,4 +100,20 @@ public class VaultBootstrapPropertySourceConfigurationTests {
}
@EnableConfigurationProperties(VaultProperties.class)
@Configuration(proxyBeanMethods = false)
private static class MockVaultOperationsConfiguration {
@Bean
VaultOperations vaultOperations() {
return mock(VaultOperations.class);
}
@Bean
VaultBootstrapConfiguration.TaskSchedulerWrapper taskSchedulerWrapper() {
return new VaultBootstrapConfiguration.TaskSchedulerWrapper(mock(ThreadPoolTaskScheduler.class));
}
}
}