Secretes management using open source tool Hashicorp Vault
Exploring Hashicorp Vault's Key Features
Table of contents
- Step 1: Install and Set Up HashiCorp Vault
- Step 2: Initialize and Unseal Vault
- Step 3: Authenticate with Vault
- Step 4: Enable a Secrets Engine
- Step 5: Store a Secret
- Step 6: Retrieve a Secret
- Step 7: Configure Policies for Access Control
- Step 8: Integrate Vault with Applications
- Step 9: Enable Secret Rotation
- Step 10: Audit and Monitor Secrets
Step 1: Install and Set Up HashiCorp Vault
Download Vault:
- Use the official HashiCorp website or package manager.
bashCopy codecurl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install vault
Start Vault:
- Start Vault in development mode (not recommended for production).
bashCopy codevault server -dev
Step 2: Initialize and Unseal Vault
Initialize Vault:
bashCopy codevault operator init
This generates unseal keys and a root token.
Save these securely as they are needed to operate the Vault.
Unseal Vault:
- Use unseal keys (provided during initialization) to unseal Vault:
bashCopy codevault operator unseal <unseal_key>
Step 3: Authenticate with Vault
Login using the Root Token:
- Use the root token generated during initialization.
bashCopy codevault login <root_token>
Step 4: Enable a Secrets Engine
- Vault uses secrets engines to manage secrets. Enable the KV (Key-Value) secrets engine for storing secrets.
bashCopy codevault secrets enable -path=kv kv
Step 5: Store a Secret
Store a Secret:
bashCopy codevault kv put kv/my-app username=admin password=securepassword123
Verify Stored Secrets:
bashCopy codevault kv get kv/my-app
Step 6: Retrieve a Secret
- Applications can use the CLI or APIs to retrieve secrets.
bashCopy codevault kv get -field=password kv/my-app
Step 7: Configure Policies for Access Control
Define a Policy:
- Create a policy to restrict access to certain secrets.
hclCopy codepath "kv/my-app" {
capabilities = ["read", "list"]
}
- Save it as
policy.hcl
.
Apply the Policy:
bashCopy codevault policy write my-app-policy policy.hcl
Step 8: Integrate Vault with Applications
Use Vault's APIs or libraries to fetch secrets dynamically at runtime without hardcoding them.
Example API Call:
bashCopy codecurl --header "X-Vault-Token: <token>" \ --request GET \ http://127.0.0.1:8200/v1/kv/my-app
Step 9: Enable Secret Rotation
Set Up Dynamic Secrets:
- Use Vault to generate dynamic credentials for databases.
bashCopy codevault secrets enable database
Configure Database Secrets:
bashCopy codevault write database/config/my-database \ plugin_name=mysql-database-plugin \ connection_url="{{username}}:{{password}}@tcp(127.0.0.1:3306)/" \ allowed_roles="my-role" \ username="root" \ password="rootpassword"
Create a Role for Dynamic Secrets:
bashCopy codevault write database/roles/my-role \ db_name=my-database \ creation_statements="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';" \ default_ttl="1h" \ max_ttl="24h"
Step 10: Audit and Monitor Secrets
- Enable auditing to log all access to secrets:
bashCopy codevault audit enable file file_path=/var/log/vault_audit.log