Giter VIP home page Giter VIP logo

demo-spring-cloud-vault's Introduction

Vaulted

This is a demo application that shows how to use Spring Cloud Vault Config to retrieve some secret values from HashiCorp Vault and inject them into the application context using @Value annotation in DemoApplication.java or placeholders in application.yml.

In particular, it retrieves some standard Spring properties spring.datasource.username=demo-user and spring.datasource.password to configure an H2 datasource. These properties are defined differently according to active Spring profile(s).

Starting Vault server

To simplfy things, we use Vault in dev mode but this demo can adapted to use a real configuration with authentication and TLS connections, spring-cloud-vault as some doc and bash scripts to make it easy.

Command below starts a Vault server in dev mode with a known initial root token that we can use for dev and tests and listening on http://localhost:8200

vault server -dev -log-level=INFO -dev-root-token-id=00000000-0000-0000-0000-000000000000

Before using the CLI to configure the vault, you must set this environment variable:

export VAULT_ADDR=http://localhost:8200

Add some secret application properties.

# default application, default profile
vault write secret/application spring.datasource.username=default-user spring.datasource.password=default-pass

# demo application, default profile
vault write secret/demo spring.datasource.username=demo-user spring.datasource.password=demo-pass

# demo application, dev profile
vault write secret/demo/dev spring.datasource.username=demo-user-dev spring.datasource.password=demo-pass-dev

# demo application, swagger profile
vault write secret/demo/swagger spring.datasource.username=demo-user-swagger spring.datasource.password=demo-pass-swagger

Gotcha

โš ๏ธ When writing to a path in Vault, you must write all key/value pairs at once.

vault write <existing-path> key1=value1 will blow away any keys other than key1.

# Wrong, only spring.datasource.password is stored
vault write secret/application spring.datasource.username=default-user
vault write secret/application spring.datasource.password=default-pass

# Good, the 2 properties are stored
vault write secret/application spring.datasource.username=default-user spring.datasource.password=default-pass

Running the application

mvnw clean package

Without profile, the application prints "default" as spring.application.name property is not defined in bootstrap.yml

java -jar target/demo-0.0.1-SNAPSHOT.jar

##########################
profile(s): null
username: default-user
password: default-pass
other: default-user
Successfully connected to database
##########################

With dev profile, the application prints "demo-dev" because spring.application.name property is defined in bootstrap-dev.yml

java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

##########################
profile(s): dev
username: demo-user-dev
password: demo-pass-dev
other: demo-user-dev
Successfully connected to database
##########################

With "dev,swagger" profiles, the application prints "demo-swagger"

java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev,swagger

##########################
profile(s): dev,swagger
username: demo-user-swagger
password: demo-pass-swagger
other: demo-user-swagger
Successfully connected to database
##########################

demo-spring-cloud-vault's People

Contributors

gmarziou avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.