Only in Mage Pro.Try our fully managed solution to access this advanced feature.
Overview
HashiCorp Vault is a secrets management tool that provides secure storage and access to sensitive data. Mage integrates with Vault to retrieve secrets at runtime, ensuring that sensitive credentials are never stored in your codebase or configuration files.Prerequisites
Before using Vault with Mage, ensure you have:- A running HashiCorp Vault instance
- Appropriate permissions to read secrets from Vault
- A valid Vault token or authentication method
Configuration
Required Environment Variables
Configure your Mage environment with the following environment variables:VAULT_URL
: The URL of your Vault server (default:http://127.0.0.1:8200
)VAULT_TOKEN
: Your Vault authentication token
Optional Environment Variables
VAULT_MOUNT_POINT
: The KV v2 mount point for secrets (default:secret
)
Example Environment Setup
Using Secrets in Mage
Python Code
Use the Vault integration directly in your Python blocks. Theget_secret()
function accepts two parameters:
path
: The secret path in Vault (e.g.,'mage/database'
)key
: Optional specific key within the secret (e.g.,'password'
)
YAML Configuration
Reference Vault secrets in your YAML configuration files using thevault_secret_var()
function. This function accepts two parameters:
path
: The secret path in Vault (e.g.,'mage/database'
)key
: The specific key within the secret (e.g.,'password'
)
"{{ vault_secret_var('secret/path', 'key_name') }}"
syntax in any YAML field that supports variable interpolation:
Creating Secrets in Vault
Using Vault CLI
-
Authenticate with Vault:
-
Create a secret:
-
Verify the secret:
Using Vault UI
- Navigate to your Vault UI:
- Local development:
http://127.0.0.1:8200/ui
- Production:
https://vault.company.com/ui
- Local development:
- Go to Secrets → KV v2
- Click Create secret
- Enter the secret path (e.g.,
mage/database
) - Add key-value pairs for your credentials
- Click Save
Error Handling
The Vault integration provides specific error handling for common scenarios:Security Best Practices
1. Token Management
- Use short-lived tokens when possible
- Implement token rotation
- Store tokens securely (environment variables, not in code)
2. Secret Organization
- Use hierarchical paths (e.g.,
mage/prod/database
,mage/dev/database
) - Follow consistent naming conventions
- Separate secrets by environment
3. Access Control
- Implement least-privilege access
- Use Vault policies to restrict access
- Regularly audit secret access
4. Monitoring
- Enable Vault audit logs
- Monitor secret access patterns
- Set up alerts for unusual activity
Troubleshooting
Common Issues
1. Authentication Failed- Verify your
VAULT_TOKEN
is valid - Check if the token has expired
- Ensure the Vault URL is correct
- Verify the secret path exists
- Check the mount point configuration
- Ensure you have read permissions
- Check your Vault token permissions
- Verify the secret path is accessible
- Contact your Vault administrator