spring-cloud / spring-cloud-vault

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

Overriding configuration in profile specific files does not seem to work #697

Open rohithkk opened 11 months ago

rohithkk commented 11 months ago

I'm trying to integrate my Spring Boot app with Vault for loading secrets. The app runs on PCF environment so the main configuration file (application.properties) is configured appropriately. For local development, the vault authentication is done via a regular token. So I have created application-local.properties file with appropriate configuration for local development. Then I set the "Active Profiles" value to "local" under the run/debug configuration.

Problem is that when I try to start the app in IDEA, I get the following error. So it's clear that the vault related properties mentioned in application-local.properties are not being picked up during startup.


23:32:40.289 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: Illegal character in path at index 1: ${VAULT_ADDR}
    at java.base/java.net.URI.create(URI.java:906)
    at org.springframework.cloud.vault.config.VaultConfiguration.createVaultEndpoint(VaultConfiguration.java:121)
    at org.springframework.cloud.vault.config.VaultConfigDataLoader$ImperativeInfrastructure.<init>(VaultConfigDataLoader.java:445)
    at org.springframework.cloud.vault.config.VaultConfigDataLoader.registerImperativeInfrastructure(VaultConfigDataLoader.java:177)

Here is the complete pom and main application.properties and application-local.properties files.

I have generated the deployment artifact using command mvn clean install and then tried to run the app using java -jar demo-0.0.0.1-snapshot.jar -Dspring.profiles.active=local.

Since I'm passing the profile, I was expecting that configuration from the application-local.properties file will be picked up but from the stacktrace it seems like the main configuration file is being read.

pom.xml ``` 4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.14 com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 17 2021.0.8 org.springframework.boot spring-boot-starter-webflux org.springframework.cloud spring-cloud-starter-vault-config org.springframework.boot spring-boot-starter-test test io.projectreactor reactor-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin ```
application-local.properties ``` spring.application.name=sbvaultapp spring.main.allow-bean-definition-overriding=true spring.main.web-application-type= reactive spring.main.lazy-initialization= false spring.profiles.group.dev=cloud spring.profiles.group.qa=cloud spring.profiles.group.uat=cloud spring.profiles.group.prod=cloud spring.config.import=optional:vault:// spring.cloud.vault.enabled=true spring.cloud.vault.reactive.enabled=true spring.cloud.vault.authentication= PCF spring.cloud.vault.uri= ${VAULT_ADDR} spring.cloud.vault.namespace= ${VAULT_NAMESPACE} spring.cloud.vault.pcf.role= ${VAULT_CF_ROLE} spring.cloud.vault.pcf.pcf-path= ${VAULT_CF_PATH} spring.cloud.vault.kv.enabled= true spring.cloud.vault.kv.backend= kv spring.cloud.vault.kv.default-context=${spring.application.name} ```
application-local.properties ``` spring.application.name=sbvaultapp spring.main.allow-bean-definition-overriding=true spring.main.web-application-type= reactive spring.main.lazy-initialization= false spring.profiles.group.dev=cloud spring.profiles.group.qa=cloud spring.profiles.group.uat=cloud spring.profiles.group.prod=cloud spring.config.import=optional:vault:// spring.cloud.vault.enabled=true spring.cloud.vault.reactive.enabled=true spring.cloud.vault.authentication= token spring.cloud.vault.token='Xxxxxxxxx' spring.cloud.vault.uri= 'https://vault.myvault.org:8200' spring.cloud.vault.namespace= 'apps' spring.cloud.vault.scheme=https spring.cloud.vault.kv.enabled= true spring.cloud.vault.kv.backend= kv spring.cloud.vault.kv.default-context=${spring.application.name} spring.cloud.vault.kv.profiles=dev ```
mp911de commented 11 months ago

Profile selection is subject to Spring Boot. Spring Cloud Vault uses Boot's ConfigData API and the failures in bootstrapping the Vault Client are a consequence of the provided configuration.

You're mentioning application-local.properties twice. With a application.properties and a application-local.properties along with -Dspring.profiles.active=local, I can successfully select the desired profile.

If you would like us to spend some more time helping you to diagnose the problem, please provide a minimal yet complete sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

rmvc-mirza commented 4 months ago

Hi there, I've noticed that I'm having the same issue, ie. i run my app with the flag -Dspring.profiles.active=staging but the app still reads the config from the default application.properties. I'm attaching a demo project on which the issue is present demo.zip

thuhlig commented 5 days ago

I had the same issue. Setting the spring.config.activate.on-profile property solved the problem.

So try to add spring.config.activate.on-profile=default to your application.properties and spring.config.activate.on-profile=local to your application-local.properties.