Spring boot cannot read Vault secret: IllegalArgumentException Could not resolve placeholder

3.4k views Asked by At

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

enter image description here

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 :)

2

There are 2 answers

2
f.trajkovski On

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>

0
Ninroot On

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