HashiCorp Vault
The HashiCorp Vault integration reads secrets from a KV version 2 secrets engine and can write virtual keys to it. It supports Vault tokens, AppRole and TLS certificate authentication, renews its login on an interval, and works with Vault Enterprise namespaces.
The HashiCorp Vault integration needs an Enterprise license. Without one the gateway refuses to start when key_management_system names it. AWS Secrets Manager, AWS KMS, Google Cloud KMS and Azure Key Vault work on Starter, see secret managers.
A real-world scenario
A bank keeps every provider key in HashiCorp Vault and does not allow keys in environment variables or config files. The platform admin connects the gateway to Vault with an AppRole, stores the OpenAI key in Vault under the field key, and adds a model whose api_key is os.environ/OPENAI_API_KEY, which the gateway now reads from Vault.
Steps
-
Store the provider key in Vault with the field name
key.vault kv put secret/spanway/OPENAI_API_KEY key=sk-... -
In the Admin UI open Settings, then Admin Settings, and select the Hashicorp Vault tab. Click Configure Vault, enter the address, the AppRole role id and secret id, the mount and the path prefix, and save. The same with the API:
curl -X POST http://localhost:4000/config_overrides/hashicorp_vault \-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \-d '{"vault_addr": "https://vault.example.com:8200", "approle_role_id": "<role id>", "approle_secret_id": "<secret id>", "vault_mount_name": "secret", "vault_path_prefix": "spanway"}' -
Click Test Connection, or call the test route.
curl -X POST http://localhost:4000/config_overrides/hashicorp_vault/test_connection -H "Authorization: Bearer $SPANWAY_MASTER_KEY" -
Add
OPENAI_API_KEYtohosted_keysas in Configuration and send a request to a model that uses it.
Expected result
Test Connection reports success, and a chat completion to the model answers with the key read from Vault. The key never appears in the config file or the environment.
Configuration
general_settings:
key_management_system: hashicorp_vault
key_management_settings:
hosted_keys: ["OPENAI_API_KEY", "ANTHROPIC_API_KEY"]
| Variable | Default | Purpose |
|---|---|---|
HCP_VAULT_ADDR | http://127.0.0.1:8200 | Vault address, for example https://vault.example.com:8200. |
HCP_VAULT_MOUNT_NAME | secret | Mount path of the KV v2 engine. |
HCP_VAULT_PATH_PREFIX | unset | Optional path under the mount where secrets live. |
HCP_VAULT_NAMESPACE, HCP_VAULT_SECRET_NAMESPACE, HCP_VAULT_LOGIN_NAMESPACE | unset | Namespaces for secrets and for login on Vault Enterprise. |
HCP_VAULT_REFRESH_INTERVAL | 86400 (one day) | Seconds a secret read from Vault is kept in memory before it is read again. A shorter time picks up a changed secret sooner and costs more Vault reads. |
SECRET_MANAGER_REFRESH_INTERVAL | 86400 | The default for HCP_VAULT_REFRESH_INTERVAL when that is not set. |
Authentication
Choose one of three methods. When more than one is set, the gateway uses AppRole first, then the TLS certificate, then the token, so remove the old method's values when you switch.
| Method | Variables |
|---|---|
| Token | HCP_VAULT_TOKEN |
| AppRole | HCP_VAULT_APPROLE_ROLE_ID, HCP_VAULT_APPROLE_SECRET_ID, optional HCP_VAULT_APPROLE_MOUNT_PATH |
| TLS certificate | HCP_VAULT_CLIENT_CERT, HCP_VAULT_CLIENT_KEY, HCP_VAULT_CERT_ROLE |
AppRole and certificate logins are renewed automatically when their Vault lease ends. The token is never logged.
Secret layout
A reference os.environ/OPENAI_API_KEY reads the secret at <mount>/data/<prefix>/OPENAI_API_KEY and takes the value of its key field. Store each credential as its own secret with a key field.
vault kv put secret/spanway/OPENAI_API_KEY key="sk-provider-key"
export HCP_VAULT_ADDR="https://vault.example.com:8200"
export HCP_VAULT_MOUNT_NAME="secret"
export HCP_VAULT_PATH_PREFIX="spanway"
export HCP_VAULT_APPROLE_ROLE_ID="..."
export HCP_VAULT_APPROLE_SECRET_ID="..."
docker run --rm -p 4000:4000 -v "$PWD/config.yaml:/app/config.yaml" \
-e SPANWAY_MASTER_KEY -e HCP_VAULT_ADDR -e HCP_VAULT_MOUNT_NAME -e HCP_VAULT_PATH_PREFIX \
-e HCP_VAULT_APPROLE_ROLE_ID -e HCP_VAULT_APPROLE_SECRET_ID \
registry.spanway.ai/spanway/spanway-ai:2.0.0 --config /app/config.yaml
Writing virtual keys
With access_mode: read_and_write and store_virtual_keys: true in key_management_settings, new virtual keys are written to Vault under the configured prefix with the key alias as the secret name, and deleted when the key is deleted.
Troubleshooting
A 403 from Vault means the policy attached to the token or role does not allow read on the data path, or create and update for writes. A 404 means the mount or path prefix is wrong. A successful lookup writes no log line, so look for the Error reading secret from Hashicorp Vault and Defaulting to os.environ value for key= lines listed under Logs. See secret managers for the shared settings.
Errors
| What you see | What it means | Fix |
|---|---|---|
The gateway fails to start with Hashicorp secret manager is only available for premium users | No Enterprise license is set, and this store needs one. | Set SPANWAY_LICENSE to your license key and restart. |
The gateway fails to start with Missing Vault authentication credentials. Please set either: ... | No token, AppRole pair or certificate pair is set. | Set one of the three methods under Authentication. |
The gateway fails to start with Could not authenticate to Vault via AppRole: ... or Could not authenticate to Vault via TLS cert: ... | Vault refused the login. | Check the role id and secret id, or the certificate and role, and the login namespace. |
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| ERROR | Error reading secret from Hashicorp Vault: ... | A read failed, for example with 403 or 404. | Check the policy and the path. |
| ERROR | Defaulting to os.environ value for key=<NAME>. ... | After a failed read the gateway used the environment variable of the same name. | Fix the read, then restart. |
Related
- Secret managers for the shared settings.
- Admin Settings page for the Hashicorp Vault tab.
- Config overrides for how the stored connection is kept.