Disabling Vault in Spring Boot tests

Disabling Vault in Spring Boot tests

Vault is a secret management developed by HashiCorp. It is a very useful tool, especially in big projects. So that we don’t need to configure much to provide credentials or sensitive information to your application.

Hence, we don’t need to pass let say database credentials via environment variables. In fact, the application communicates with the Vault server and requests for the credentials. You can read more about Vault here.

Spring has integration with Vault, called Spring Vault. So all we need to do is to add the dependency to the project and configure the Vault path in bootstrap.yml. And we don’t need to worry about anything else.

However, the problem starts to show up when running integration tests. Spring tests try to connect to the Vault server which most probably is not available. A naive approach is to have the Vault server running in the environment which is quite the pain. A better approach though is disabled the Vault for the test altogether and put dummy credentials in the bootstrap.yml

To that, all we need is to create another let say bootstrap.ymlfile under test/.../resource path and disable the Vault like this:

spring:
  cloud.vault.enabled: false

Then we define dummy credentials in the application.yml. For example,

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db
    username: username
    password: password

Inline/featured images credits