When using TestContainers
to start a Vault
container, the port that is exposed by the container is randomly selected during startup.
@Container
static VaultContainer vaultContainer = new VaultContainer<>("vault:1.7.2")
.withVaultToken(TOKEN)
.withInitCommand("secrets enable --path foo kv-v2")
.withInitCommand("kv put foo/app bar=foo");
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
.
A small project showing the issue can be found on GitHub.
Am I doing something wrong or is there an alternative way to override the configuration?
When using Spring-Vault
with a @VaultPropertySource
instead of Spring-Cloud-Vault
things work as expected.