HashiCorp Vault
Overview
HashiCorp Vault is a powerful tool designed for securely managing application secrets. It offers a range of features including persistent storage configurations, cloud integration, and dynamic secret generation. By integrating with Spring Boot, Vault allows developers to externalize secrets from application properties files, enhancing security by preventing sensitive information such as passwords and API keys from being hardcoded into the application.
Interaction with Spring Boot
The integration of HashiCorp Vault with a Spring Boot application facilitates the secure management of secrets. This integration is depicted in Figure 6.1, which illustrates the interaction between a user, a Spring Boot application, and HashiCorp Vault.
Figure 6.1 Interaction between user, Spring Boot application, and Hashicorp Vault
Configuring HashiCorp Vault in a Spring Boot Application
To integrate HashiCorp Vault with a Spring Boot application, certain configurations need to be made in the pom.xml and application.properties files.
Updating pom.xml
The pom.xml file must be updated to include the necessary dependencies for Spring Cloud Vault. Below is the updated pom.xml configuration:
Listing 6.4 Updated pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:/ /maven.apache.org/POM/4.0.0"
xmlns:xsi="http:/ /www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:/ /maven.apache.org/POM/4.0.0
➥ https:/ /maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.manning.sbip.ch06</groupId>
<artifactId>course-tracker-implementing-vault-final</artifactId>
<version>1.0.0</version>
<name>course-tracker-implementing-vault-final</name>
<description>Spring Boot application for Chapter 06</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2021.0.0</spring-cloud.version>
</properties>
<dependencies>
/ / additional configurations
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
/ / additional configurations
</project>
Configuring application.properties
The application.properties file must be configured to connect to the Vault server. Below is an example configuration:
Listing 6.5 Application.properties changes
spring.cloud.vault.token=s.YGgzy5qOtEf4d6Xo0i6qqQGL
spring.cloud.vault.authentication=token
spring.cloud.vault.host=localhost
spring.cloud.vault.port=8200
spring.cloud.vault.scheme=http
spring.config.import=vault:/ /secret/coursetracker #1
spring.application.name=coursetracker
server.ssl.key-store-password=${keystore} #2
#1 Spring configuration to refer to the secret/coursetracker in the vault
#2 Replace the server.ssl.key-store-password=p@ssw0rd property with this configuration.
Security Considerations
In the example above, the initial root token is placed in the application.properties file. This token is generated when the vault is initialized using the vault operator init command. For production applications, it is advisable to reference this token from an environment variable or use another secure method to supply it to the application. Additionally, while the example uses HTTP for communication with the vault, it is recommended to configure HTTPS to ensure the security of secrets in a production environment.