Skip to content

A Spring Boot app to read secrets from HashiCorp Vault using Spring Cloud Vault Config

Notifications You must be signed in to change notification settings

BuntyRaghani/spring-cloud-vault-read-secrets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Spring Cloud Read Secrets From Vault

A Spring Boot app to read secrets from HashiCorp Vault using Spring Cloud Vault Config.

Spring Cloud Vault Config

Spring Cloud Vault Config is used to read the secrets inside the Spring Boot app that are stored in HashiCorp Vault.

How to Use:

  1. Add spring-cloud-starter-vault-config dependency in pom.xml.
  2. Add application name and vault server configuration in application.properties.
  3. Use @value annotation to read the secret value and use it wherever required in your application.

NOTE: Do not hardcode the vault token inside the application.properties. You can pass it as an argument while building or running your application.

How to Run Application

Before starting the application, make sure:

  1. Vault is up and running on your localhost.
  2. You have stored the two secrets with key secret1 & secret2 in the path /secrets/spring-cloud-vault-read-secrets.

Start the application using any of the commands mentioned below:

Note: These commands need to run inside the root folder of this project i.e. inside the spring-cloud-vault-read-secrets folder.

  • Using maven
    mvn spring-boot:run -Dspring-boot.run.arguments=--vaultToken=vaultServerToken

  • From jar file
    Create a jar file using 'mvn clean install -DvaultToken=vaultServerToken' command and then execute
    java -jar target/vault-read-secrets-1.0.1-SNAPSHOT.jar --vaultToken=vaultServerToken

Note: By default spring boot application starts on port number 8080. If port 8080 is occupied in your system then you can change the port number by uncommenting and updating the server.port property inside the application.properties file that is available inside the src > main > resources folder.


Send an HTTP GET request to '/getSecretsFromVault' endpoint using any of the two methods:

  • Browser or REST client
    http://localhost:8080/getSecretsFromVault

  • cURL
    curl --request GET 'http://localhost:8080/getSecretsFromVault

How to Run Unit Test Cases

Run the test cases using any of the commands mentioned below:

Note: These commands need to run inside the root folder of this project i.e. inside the spring-cloud-vault-read-secrets folder.

  • To run all the test cases
    mvn test -DvaultToken=vaultServerToken

  • To run a particular test class
    mvn test -Dtest=VaultReadSecretsControllerTest -DvaultToken=vaultServerToken
    or
    mvn test -Dtest=VaultReadSecretsApplicationTest -DvaultToken=vaultServerToken


Note: While starting your application or while running the maven install command you need to provide the argument -DvaultToken=vaultServerToken or else your application will fail to start / maven install command will also fail due to test case failures.

About

A Spring Boot app to read secrets from HashiCorp Vault using Spring Cloud Vault Config

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages