Search code examples
spring-boothashicorp-vault

How to read Vault kv with java/spring boot


I'm trying to figure out how to use Hashicorp's Vault with spring boot.

Initially, I have tried to follow the guide:

https://spring.io/guides/gs/vault-config/#scratch

But due to api changes I used following command in the vault CLI:

vault kv put secret/gs-vault-config example.username=demouser example.password=demopassword

which saved both and I'm able to retrieve it with the following command

vault kv get secret/gs-vault-config

Then I created the Application.java and MyConfiguration.java as described in the guide. At first, I ran the program without having the vault server running which resulted in a connection refused. Then I started the vault server and entered the username and password from the CLI. From the log I can see it actually enters the Application and writes out Here we goooo

@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private VaultTemplate vaultTemplate;

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
public void run(String... strings) throws Exception {

    // You usually would not print a secret to stdout
    System.out.println("Here we gooooo");
    VaultResponse response = vaultTemplate.read("secret/gs-vault-config");
    System.out.println("Value of username");
    System.out.println("-------------------------------");
    System.out.println(response.getData().get("example.username"));
    System.out.println("-------------------------------");
    System.out.println();

But im unable to retrieve any data from Vault - probably due to the V1 vs V2 issues

2018-08-30 17:10:07.375 ERROR 21582 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at hello.Application.main(Application.java:23) [classes!/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [gs-vault-config-0.1.0.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [gs-vault-config-0.1.0.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [gs-vault-config-0.1.0.jar:na]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [gs-vault-config-0.1.0.jar:na]
Caused by: java.lang.NullPointerException: null
    at hello.Application.run(Application.java:34) [classes!/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    ... 13 common frames omitted

Does anyone know if there is a similar guide to a spring-boot code snippet where data is retrieved from vault which has been entered with the kv engine?


Solution

  • I stumbled a single note in this page : https://cloud.spring.io/spring-cloud-vault/multi/multi_vault.config.backends.html

    In which i says : Spring Cloud Vault adds the data/ context between the mount path and the actual context path

    So i tried to change the code to :

    VaultResponse response = vaultTemplate.read("/secret/data/gs-vault-config");
    

    And then it worked.