HashiCorp Vault - Technological watch
What is Vault ?
HashiCorp Vault is a popular open-source tool for securely managing secrets, such as passwords, tokens, and certificates, in modern cloud and container environments.
Vault provides a centralized way to store and manage secrets, which can be accessed programmatically through a variety of APIs or via a web interface. It also provides access control mechanisms to ensure that only authorized users and applications can access specific secrets.
Vault uses a variety of encryption techniques to ensure that secrets are stored securely, including encryption at rest and in transit. It also supports various authentication methods, such as LDAP, Kerberos, and OAuth, to authenticate users and applications.
Some common use cases for Vault include managing database credentials, API keys, SSH keys, and TLS certificates. By using Vault, organizations can improve their security posture by ensuring that secrets are stored securely and accessed only by authorized users and applications.
- Centralized Management: Store all secrets securely in one place.
- Encryption: Protect secrets with encryption.
- Dynamic Secrets: Generate credentials on-demand.
- Access Control: Define detailed access policies.
- Revocation and Expiration: Revoke and expire secrets as needed.
- Encryption Service: Encrypt/decrypt data with Vault keys.
- Audit and Compliance: Monitor secret usage.
- High Availability: Ensure access during failures.
- Integration: Seamlessly integrate with various tools.
- Community Support: Benefit from active community and updates.
Installation with Docker
Dev mode
Take care, data are stored only in :memory: when vault is run in dev mode !
Production mode
Here, the data are stored in a volume !
Functionnalities of Hashicorps Vault
Secrets engines
Vault provides various secrets engines, which are plugins that enable Vault to interact with different types of secrets and securely store them. Some of the most commonly used secrets engines include:
-
KV (Key-Value) Engine : The KV engine is a fundamental secrets engine that allows you to store arbitrary key-value secrets.
-
PKI (Public Key Infrastructure) Engine : The PKI engine is used to manage X.509 certificates and private keys.
-
SSH Engine : With the SSH engine, Vault can dynamically generate SSH key pairs and sign SSH keys for secure access to servers.
-
TOTP (Time-Based One-Time Password) Engine : The TOTP engine generates time-based one-time passwords, often used for two-factor authentication (2FA) in applications.
-
Kubernetes Engine : The Kubernetes engine allows Vault to issue short-lived client certificates to Kubernetes pods.
And many more! The availability of multiple secrets engines gives you the flexibility to securely store different types of secrets according to your specific requirements.
Access
Managing authentication methods and controlling access to Vault is essential for maintaining a secure environment. Vault offers several authentication methods to verify users’ identities:
-
AppRole : AppRole is designed for applications and services to authenticate with Vault to access secrets. It provides a way for applications to authenticate without exposing sensitive credentials.
-
Tokens : Tokens are a core authentication method in Vault. They are issued to users upon successful login and act as temporary access credentials to interact with the Vault.
-
Username & Password : Vault supports traditional username and password-based authentication for users who need direct access to Vault.
-
JWT (JSON Web Tokens) : With JWT authentication, users can authenticate using JSON Web Tokens, which are commonly used in web applications and APIs.
By configuring the appropriate authentication methods, you can ensure that only authorized users and applications can access sensitive data stored in Vault.
Policies
Vault policies define the access control rules and permissions for different paths within Vault. These policies are written in HashiCorp Configuration Language (HCL) and allow fine-grained control over what actions users and applications can perform. Here’s an example of a Vault policy:
In the above example, the policy grants full CRUD (Create, Read, Update, Delete) access to the path secret/app-1, while allowing only read and list access to secret/app-2. By crafting well-defined policies, you can ensure that users and applications have the appropriate level of access to secrets within Vault.
Usage
You can connect on port 8200 with your browser.
- If you are in dev mode, use the
VAULT_DEV_ROOT_TOKEN_ID
to sign in. - If you are in production mode, look at the logs in the vault container.
You have multiple ways to interact with Vault, Vault UI, Vault CLI and Vault REST API.
Vault CLI
Let’s use Vault CLI:
You can define new authentication methods:
You can define policies. It is specific permissions on secrets (["create", "read", "update", "patch", "delete", "list"]
)
Take care, don’t forget to add /data
.
You can find information about policy here : https://developer.hashicorp.com/vault/tutorials/policies/policies.
You can create an admin
policy to avoid using root
token.
REST API
You can use REST API to manipulate secrets. This would be a request:
Example : Manage an AppRole with REST API
Full vault example with docker, setup and usage
In this example, we want to use Vault with this project : https://github.com/spring-projects/spring-petclinic
Setup all the services with docker-compose
To begin, let’s create our services:
- petclinic is our application where we will pass database credential secrets
- mysql is our database where petclinic will store its data
- vault will be the chest where the database secrets will be stored
Note: The petclinic service will need some environment variables. We will see how to set them up shortly!
Setup vault
Token approach
The petclinic tokens can be obtained with these commands, for example:
Approle approach
Instead of getting directly tokens, we can get them from approle:
Now, we can generate a new vault token with correct policies.
Setup the petclinic application
The Dockerfile for the PetClinic application looks like this:
We will have to pass the vault secret to the petclinic application. To achieve this, we will call a script (entrypoint.bash
) when the container starts up. This script will fetch the secrets and write them in a file named application-mysql.properties
. Here is an example: application-mysql.properties.
We can rename this file to application-template.properties
and adjust it like this to make it generic:
Note : the bash script bellow will replace the variables who are inside {{ }}
Here, all the VAULT secret variables are replaced when the container starts up. We can verify it with the command docker-compose up
.
🎉 TADA, the application is working ! 🎉
Practice code with the "Quick Sort" algorithm
Enhance your coding skills by learning how the Quick Sort algorithm works!
Atomic design - Technological watch
Learn what is the atomic design in less than 5 minutes !
Redis - Technological watch
Learn what is Redis in less than 5 minutes !
The SOLID/STUPID principles
Learn what are the SOLID and STUPID principles with examples
Create a Docker Swarm playground
Let's create Docker Swarm playground on your local machine
Svelte - Technological watch
Learn what is Svelte in less than 5 minutes !
Create an Ansible playground with Docker
Let's create an Ansible playground with Docker
Jenkins - Technological watch
Learn what is Jenkins in less than 5 minutes !