0

I am very new to Spring Vault, and so I followed a lot of guides and YouTube videos. The problem I am facing is that I think, I am able to access the vault but when I try to retrieve the values from the vault it returns null for me.

So in my Spring Boot app, my spring main method (SpringApp.java) is this:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import com.SpringApp.config.VaultConfig;

@SpringBootApplication
@EnableConfigurationProperties(VaultConfig.class)
public class SpringApp implements CommandLineRunner{
    
    private static Logger log = LoggerFactory.getLogger(SpringApp.class);
    private VaultConfig vaultConfig;
    
    public SpringApp (VaultConfig vaultConfig) {
        super();
        this.vaultConfig = vaultConfig;
    }

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

    @Override
    public void run(String... args) throws Exception {
        log.info("----------------------------------------");
        log.info("Configuration properties");
        log.info("   the secret key for jwt is {}", vaultConfig.getJwt());
        log.info("----------------------------------------");
        
    }
}

My VaultConfig.java is this:

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

@Data
@ConfigurationProperties("secret")
public class VaultConfig {
    private String jwt;
}

When I run the command

vault server --dev --dev-root-token-id="00000000-0000-0000-0000-000000000000"

I was able to start up my vault server and when I run the command

vault kv put secret/spring-vault-config secret.jwt=123456789

I was able to register the secret into the vault and I checked both in the UI and the command line and the value is there

localhost:8200
and
vault kv get secret/spring-vault-config

However, when I run the spring boot app I get

the secret key for jwt is null

At first, I have all properties in the "bootstrap.properties" files but I keep getting the error

"spring.cloud.vault.token" (Must not be empty) 

So then I change all those properties into the "application.properties" and it worked

But now for some reason I can’t retrieve it, any help of suggestion will be greatly appreciate it. Thank you in advanced.

Oh this is what in my application.properties file

spring.application.name=spring-vault-config
spring.cloud.vault.token=00000000-0000-0000-0000-000000000000
spring.cloud.vault.scheme=http
spring.cloud.vault.kv.enabled=true
Thinh Pham
  • 21
  • 4
  • Can you try adding `spring.config.import=vault://` ? I had the same problem and now it is working for me. `spring.cloud.vault.kv.enabled` is not necessary (true by default) – tweetysat Jan 07 '22 at 12:09
  • @tweetysat can you add it as an answer? That is what was happening wrong with my code, thanks! – Norberto Ritzmann Aug 20 '22 at 22:30

1 Answers1

2

You should add spring.config.import=vault:// I had the same problem and now it is working for me. spring.cloud.vault.kv.enabled is not necessary (true by default)

tweetysat
  • 2,187
  • 14
  • 38
  • 75