2

I'm trying to setup a simple springboot app, with spring vault to store DB and API credentials.

But I'm struggling with accessing my key value pair. When I try to access it in my controller (just for testing purpose), it throws a IllegalArgumenException

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'username' in value "${username}"

Does anyone have insight on why this is not working?

Im following the code from this repo: https://github.com/rwinch/spring-vault-talk/tree/master/hello-spring-vault

What I have done so far:

  • Running vault locally with the command

vault server -dev -dev-root-token-id=12345

  • Added a key value with the command

vault kv put secret/spring-vault-demo username=foo password=bar

  • Setup a simple spring boot app

Controller

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VaultController {


    @Value("${username}")
    String username;


    @GetMapping("/vault")
    public String vault(){
        return "This value is from vault: " + username;
    }
}

VaultConfig

import org.springframework.context.annotation.Configuration;
import org.springframework.vault.annotation.VaultPropertySource;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.authentication.TokenAuthentication;
import org.springframework.vault.client.VaultEndpoint;
import org.springframework.vault.config.AbstractVaultConfiguration;

import java.net.URI;

@Configuration
@VaultPropertySource("secrets/spring-vault-demo")
public class VaultConfig extends AbstractVaultConfiguration {

    @Override
    public VaultEndpoint vaultEndpoint() {
        String uri = getEnvironment().getProperty("vault.uri");
        if (uri != null){
            return VaultEndpoint.from(URI.create(uri));
        }else throw new IllegalStateException();
    }

    @Override
    public ClientAuthentication clientAuthentication() {
        String token = getEnvironment().getProperty("vault.token");
        if (token != null){
            return new TokenAuthentication(token);
        }else throw new IllegalStateException();
    }
}

Application.properties

vault.uri=http://localhost:8200
vault.token=12345
dev.tom
  • 489
  • 2
  • 5
  • 16

1 Answers1

0

If anyone is looking for a solution:

after reading through the documentation i found that you have to add a Bootstrap.properties file with more configuration

this is how my bootstrap.yml looks like

spring:
  application:
    name: spring-vault-demo
  cloud:
    vault:
      token: 12345
      scheme: http

dev.tom
  • 489
  • 2
  • 5
  • 16