'Spring boot cannot read Vault secret: IllegalArgumentException Could not resolve placeholder
Below is my bootstrap.yml file:
spring.application.name: backend
spring.cloud.vault:
host: localhost
port: 8200
scheme: http
authentication: token
token: root
My secrets are located at secret/backend
I am trying to read these secrets in a controller class like so:
@Value("${masterpassword}")
private String masterPassword;
but this leads to IllegalArgumentException as spring complains it cannot find any masterpassword:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'secretsController': Injection of autowired dependencies failed;
nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'masterpassword' in value "${masterpassword}"
Can someone please tell me what I am doing wrong? Thanks for your help :)
Solution 1:[1]
For me downgrading the spring boot version works. I changed the parent version to: <version>2.3.0.RELEASE</version> and the spring cloud version to: <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
Solution 2:[2]
Depending on the spring boot version you are using, you might have to use spring.config.import: vault:// in your configuration file (which by the way can be deprecated):
With Spring Cloud Vault 3.0 and Spring Boot 2.4, the bootstrap context initialization (bootstrap.yml, bootstrap.properties) of property sources was deprecated. Instead, Spring Cloud Vault favors Spring Boot’s Config Data API which allows importing configuration from Vault.
https://docs.spring.io/spring-cloud-vault/docs/current/reference/html/#client-side-usage
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | f.trajkovski |
| Solution 2 | Ninroot |

