spring-cloud / spring-cloud-vault

Configuration Integration with HashiCorp Vault
http://cloud.spring.io/spring-cloud-vault/
Apache License 2.0
272 stars 151 forks source link

Problem using Spring Cloud Vault with @DynamicPropertySource #602

Closed tzie0062 closed 3 years ago

tzie0062 commented 3 years ago

Description When using TestContainers to start a Vault container, the port that is exposed by the container is randomly selected during startup. Using a @DynamicPropertySource to override properties

@DynamicPropertySource
static void addProperties(DynamicPropertyRegistry registry) {
   registry.add("spring.cloud.vault.host",()->vaultContainer.getHost());
   registry.add("spring.cloud.vault.port",()->vaultContainer.getFirstMappedPort());
   registry.add("spring.cloud.vault.uri",()->"http://"+vaultContainer.getHost()+":"+vaultContainer.getFirstMappedPort());
   registry.add("spring.cloud.vault.token",()->TOKEN);
}

does not work since Spring Cloud Vault does not seem to "see" the added properties.

The issue is present in Spring-Boot 2.5.1 and Spring-Cloud-Vault-Config 3.0.3.

Sample This sample shows the issue.

When using Spring-Vault with a @VaultPropertySource instead of Spring-Cloud-Vault things work as expected.

mp911de commented 3 years ago

Thanks for report. @DynamicPropertySource is evaluated after processing config data imports. At the time Vault is imported as config data source, the @DynamicPropertySource method was not yet invoked.

This question was already raised in the Spring Boot issue tracker, see https://github.com/spring-projects/spring-boot/issues/26148