I have a Spring Boot project that gets properties and passwords from vault. In Spring Boot 2.4 and later, the team has decided to change way to load config files. Now we need to use Spring Config Data to load this.
After reading through docs and examples I setup sample project. Here I am defining vault details on application.yml
by following official example. And defined environment specific files in dev.yml
and prod.yml
, which contain environment specific properties and files.
application.yml
server:
port: 8081
spring:
application:
name: pres
cloud:
vault:
authentication: TOKEN
uri: ${VAULT_URL}
connection-timeout: 5000
read-timeout: 15000
kv:
enabled: true
backend: secret
application-name: app/pres
token: ${TOKEN}
config:
import: vault://secret/app/pres
---
spring:
config:
activate:
on-profile: "dev"
import: dev.yml
---
spring:
config:
activate:
on-profile: "prod"
import: prod.yml
dev.yml
spring:
datasource:
url: "jdbc:mysql://localhost/dev"
username: "dev"
password: "dev"
#### ELK Logging
elk:
logging:
rabbitmq:
hostname: ${pres.elk.logging.rabbitmq.hostname}
port: 5672
username: ${pres.elk.logging.rabbitmq.username}
password: ${pres.elk.logging.rabbitmq.password}
projectVersion: '@project.version@'
prod.yml
spring:
datasource:
url: "jdbc:mysql://localhost:3306/prod"
username: "prod"
password: "prod"
#### ELK Logging
elk:
logging:
rabbitmq:
hostname: ${pres.elk.logging.rabbitmq.hostname}
port: 5672
username: ${pres.elk.logging.rabbitmq.username}
password: ${pres.elk.logging.rabbitmq.password}
projectVersion: '@project.version@'
So when I start the application, Spring Boot supposed to replace placeholders with actual values from vault. However I just see place holders as below
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)
2021-06-26 12:14:03.546 INFO 69016 --- [ restartedMain] c.e.profiletest.ProfileTestApplication : Starting ProfileTestApplication using Java 16.0.1 on pjmacbookpro with PID 69016 (/Users/pjadda/IdeaProjects/ProfileTest/target/classes started by pjadda in /Users/pjadda/IdeaProjects/ProfileTest)
2021-06-26 12:14:03.548 INFO 69016 --- [ restartedMain] c.e.profiletest.ProfileTestApplication : The following profiles are active: dev
2021-06-26 12:14:03.593 INFO 69016 --- [ restartedMain] o.s.v.c.e.LeaseAwareVaultPropertySource : Vault location [secret/app/pres] not resolvable: Not found
2021-06-26 12:14:03.594 INFO 69016 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-06-26 12:14:03.594 INFO 69016 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-06-26 12:14:04.176 INFO 69016 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=6bdb5f01-aa31-3158-8281-edfa1a02ac83
2021-06-26 12:14:04.603 INFO 69016 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2021-06-26 12:14:04.615 INFO 69016 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-26 12:14:04.615 INFO 69016 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-06-26 12:14:04.680 INFO 69016 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-26 12:14:04.680 INFO 69016 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1085 ms
2021-06-26 12:14:05.032 INFO 69016 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-06-26 12:14:05.121 INFO 69016 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2021-06-26 12:14:05.135 INFO 69016 --- [ restartedMain] c.e.profiletest.ProfileTestApplication : Started ProfileTestApplication in 2.79 seconds (JVM running for 3.622)
2021-06-26 12:14:05.465 INFO 69016 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-26 12:14:05.465 INFO 69016 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-06-26 12:14:05.466 INFO 69016 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
username:${pres.elk.logging.rabbitmq.username}
password:${pres.elk.logging.rabbitmq.password}