How to read Hashicorp Vault Database engine from Java

25 views Asked by At

In my Spring application I have multiple postgresql databases, number of DBs is not static and can be different between different environments. In order to manage credentials for them I want to use Hashicorp Vault and its database engine to create users on the fly with a short TTL.

It is easy to read from KV using VaultTemplate (from spring-cloud-starter-vault-config), but it does not support databases.

I also found that it is supported as configuration on startup using https://cloud.spring.io/spring-cloud-vault/reference/html/#vault.config.backends.postgresql, but I need to have those credentials dynamic.

So - is it possible to read from database engine entries in a same way as for KV?

1

There are 1 answers

0
JoeBloggs On

Found a quick solution, but happy to see a better ones...

have a pojo class:

public class DbEntry {    
    public String password;
    public String username;
}

And then read it like so:

@Autowired
private VaultTemplate vaultTemplate;

public void vault_db_read() {
    var response = vaultTemplate
            .read("database/creds/readonly", DbEntry.class); //path to your db secret
    logger.info(response.getData().username);
    logger.info(response.getData().password);
}