https://github.com/ivangfr/springboot-vault-examples
The goal of this project is to explore the capabilities of Vault. To achieve this, we will develop applications that utilize Vault for storing and retrieving secrets. Vault dynamically generates credentials for accessing databases and relies on Consul as the backend. The authentication method employed in Vault is AppRole.
https://github.com/ivangfr/springboot-vault-examples
cassandra consul docker java jib mysql spring-boot spring-cloud-vault spring-data-jpa spring-vault spring-web-mvc springdoc-openapi vault
Last synced: 3 months ago
JSON representation
The goal of this project is to explore the capabilities of Vault. To achieve this, we will develop applications that utilize Vault for storing and retrieving secrets. Vault dynamically generates credentials for accessing databases and relies on Consul as the backend. The authentication method employed in Vault is AppRole.
- Host: GitHub
- URL: https://github.com/ivangfr/springboot-vault-examples
- Owner: ivangfr
- Created: 2019-07-16T21:49:46.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-20T17:49:00.000Z (7 months ago)
- Last Synced: 2025-03-24T08:42:24.726Z (4 months ago)
- Topics: cassandra, consul, docker, java, jib, mysql, spring-boot, spring-cloud-vault, spring-data-jpa, spring-vault, spring-web-mvc, springdoc-openapi, vault
- Language: Java
- Homepage:
- Size: 1.51 MB
- Stars: 24
- Watchers: 2
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# springboot-vault-examples
The goal of this project is to explore the capabilities of [`Vault`](https://www.vaultproject.io). To achieve this, we will develop applications that utilize `Vault` for storing and retrieving secrets. `Vault` dynamically generates credentials for accessing databases and relies on [`Consul`](https://www.consul.io) as the backend. The authentication method employed in `Vault` is `AppRole`.
## Proof-of-Concepts & Articles
On [ivangfr.github.io](https://ivangfr.github.io), I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.
## Additional Readings
- \[**Medium**\] [**Using HashiCorp Vault & Spring Cloud Vault to handle Spring Boot App Key/Value Secrets**](https://medium.com/@ivangfr/using-hashicorp-vault-spring-cloud-vault-to-handle-spring-boot-app-key-value-secrets-926b81d0173b)
- \[**Medium**\] [**Using HashiCorp Vault & Spring Cloud Vault to obtain Dynamic MySQL Credentials**](https://medium.com/@ivangfr/using-hashicorp-vault-spring-cloud-vault-to-obtain-dynamic-mysql-credentials-5726f4fa53c2)
- \[**Medium**\] [**How to Rotate Expired Spring Cloud Vault Relational DB Credentials Without Restarting the App**](https://medium.com/@ivangfr/how-to-rotate-expired-spring-cloud-vault-relational-db-credentials-without-restarting-the-app-66976fbb4bbe)## Lease Rotation
Many people encounter issues when using `Vault`, particularly with rotating the lease for backend databases. When a [`Spring Boot`](https://docs.spring.io/spring-boot/index.html) application requests a lease from `Vault` through the [`Spring Cloud Vault`](https://cloud.spring.io/spring-cloud-vault/reference/html/) library, the library **can automatically renew** the lease periodically (based on `default_lease_ttl`).
However, once the maximum lease expiration time (`max_lease_ttl`) is reached, the lease cannot be renewed, and a new lease is needed. In this case, the `Spring Cloud Vault` library **cannot rotate** the lease, which may leave the application unable to connect to the database.
To address this issue, we have developed solutions for applications using `Spring Cloud Vault` or [`Spring Vault`](https://docs.spring.io/spring-vault/reference/). Please see the examples below.
## Examples
| Example | Diagram |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|
| [spring-cloud-vault-approle-mysql](https://github.com/ivangfr/springboot-vault-examples/tree/master/spring-cloud-vault-approle-mysql) **(with lease rotation)** |  |
| [spring-cloud-vault-approle-cassandra](https://github.com/ivangfr/springboot-vault-examples/tree/master/spring-cloud-vault-approle-cassandra) |  |
| [spring-vault-approle-mysql](https://github.com/ivangfr/springboot-vault-examples/tree/master/spring-vault-approle-mysql) **(with lease rotation)** |  |
| [spring-vault-approle-multi-datasources-mysql](https://github.com/ivangfr/springboot-vault-examples/tree/master/spring-vault-approle-multi-datasources-mysql) **(with lease rotation)** |  |## Prerequisites
- [`Java 21+`](https://www.oracle.com/java/technologies/downloads/#java21)
- Some containerization tool [`Docker`](https://www.docker.com), [`Podman`](https://podman.io), etc.## Initialize Environment
Open a terminal and, inside the `springboot-vault-examples` root folder, run the following script:
```
./init-environment.sh
```This script will:
- start `Consul`, `Vault`, `MySQL`, and `Cassandra` Docker containers;
- unseal `Vault` and enable `AppRole` in it;
- setup Database `roles` and `policies` in `Vault` for the application so that they can access their databases using dynamically generated credentials;
- setup `KV Secrets` in `Vault` for the application;## Shutdown Environment
To shut down the environment, go to a terminal and, inside the `springboot-vault-examples` root folder, run the script below:
```
./shutdown-environment.sh
```## Cleanup
To remove all Docker images created by this project, go to a terminal and, inside the `springboot-vault-examples` root folder, run the following script:
```
./remove-docker-images.sh all
```