Skip to main content

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:
  1. A running HashiCorp Vault instance
  2. Appropriate permissions to read secrets from Vault
  3. 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. The get_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')
Syntax:
Example:

YAML Configuration

Reference Vault secrets in your YAML configuration files using the vault_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')
Syntax: Use the "{{ vault_secret_var('secret/path', 'key_name') }}" syntax in any YAML field that supports variable interpolation:
Example 1 - IO Config:
Example 2 - Data Integration Pipeline:

Creating Secrets in Vault

Using Vault CLI

  1. Authenticate with Vault:
  2. Create a secret:
  3. Verify the secret:

Using Vault UI

  1. Navigate to your Vault UI:
    • Local development: http://127.0.0.1:8200/ui
    • Production: https://vault.company.com/ui
  2. Go to SecretsKV v2
  3. Click Create secret
  4. Enter the secret path (e.g., mage/database)
  5. Add key-value pairs for your credentials
  6. 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
2. Secret Not Found
  • Verify the secret path exists
  • Check the mount point configuration
  • Ensure you have read permissions
3. Access Denied
  • Check your Vault token permissions
  • Verify the secret path is accessible
  • Contact your Vault administrator

Advanced Usage

Custom Mount Points

If your Vault instance uses a different mount point:

Multiple Vault Instances

For different environments, you can create multiple Vault clients:

Integration Examples

Database Connection

API Authentication

Cloud Provider Credentials

Migration from Other Secret Managers

If you’re migrating from other secret management solutions:

From Mage Built-in Secrets

From Environment Variables

Support

For additional help with Vault integration: