This document describes how to use SecretVault for secure secret management in the Exhub project.
- Overview
- Installation
- Configuration
- Creating and Managing Secrets
- Using Secrets in Code
- Environment Variables
- Security Best Practices
- CI/CD Integration
- Troubleshooting
SecretVault is an Elixir library for securely storing and managing secrets. It encrypts secrets at rest using a master password and provides runtime access to these secrets without exposing them in the codebase or compiled artifacts.
- Encrypted at rest: Secrets are encrypted using AES-256-GCM
- No secrets in Git: Only encrypted
.vault_secretfiles are committed - No compile-time leaks: Secrets are loaded at runtime, not compiled into
app.src - Environment separation: Each
MIX_ENVhas its own isolated vault - Version control friendly: Individual secrets are separate files
SecretVault is already added to the dependencies in mix.exs:
defp deps do
[
# ... other deps
{:secret_vault, "~> 1.0"}
]
endInstall dependencies:
mix deps.getSet the master password for SecretVault:
export SECRET_VAULT_PASSWORD="your-secure-master-password"Important: Choose a strong password. This password encrypts all your secrets. If you lose it, your secrets cannot be recovered.
The configuration is already set up in config/config.exs:
config :secret_vault, :exhub,
default: [
password: System.get_env("SECRET_VAULT_PASSWORD", "")
],
secrets_dir: "priv/secrets"The config/runtime.exs file loads secrets from SecretVault at application startup. This ensures secrets are never compiled into the application.
Secrets are stored in the following structure:
priv/secrets/
├── dev/
│ └── default/
│ ├── gitee_api_key.vault_secret
│ ├── openai_api_key.vault_secret
│ └── ...
├── test/
│ └── default/
└── prod/
└── default/
Create a secret by providing the value directly:
# Set the password environment variable first!
export SECRET_VAULT_PASSWORD="your-master-password"
# Create a secret
mix scr.insert dev gitee_api_key "sk-abc123xyz789"Create a secret using your preferred editor:
export SECRET_VAULT_PASSWORD="your-master-password"
mix scr.create dev gitee_api_keyThis will open your $EDITOR to input the secret value.
Create the following secrets for full functionality:
# BurnCloud API (default OpenAI-compatible provider)
mix scr.insert dev burncloud_api_key "your-burncloud-api-key"
# BurnCloud Gemini API (dedicated key for Gemini models)
mix scr.insert dev burncloud_gemini_api_key "your-burncloud-gemini-api-key"
# BailianCloud API (Aliyun MaaS, OpenAI-compatible)
mix scr.insert dev bailiancloud_api_key "your-bailiancloud-api-key"
# Gitee AI API (alternative provider)
mix scr.insert dev gitee_api_key "your-gitee-ai-api-key"
# Gitee AI token pool — token-based billing mode key (small contexts)
mix scr.insert dev giteeai_token_api_key "your-giteeai-token-pool-key"
# Gitee AI token pool — request-based billing mode key (large contexts)
mix scr.insert dev giteeai_request_api_key "your-giteeai-request-pool-key"
# OpenAI API (for OpenAI-compatible endpoints)
mix scr.insert dev openai_api_key "your-openai-api-key"
# Gitee Cookie (for GiteeCat API access)
mix scr.insert dev gitee_cookie "your-gitee-cookie-string"
# Gitee Enterprise MCP access token (Gitee 企业版 MCP token, see
# https://gitee.com/profile/mcp_gitee_ent_access_tokens). Consumed by
# `Exhub.Gitee.Dashboard' — the enterprise dashboard endpoints
# (`GET /gitee/dashboard`, `GET /gitee/detail`) that serve
# exhub-gitee-dashboard.el. Requests must carry the `mcp-gitee-ent` user
# agent, which `GiteeCat.Client`'s `user_agent' option sets. After rotating
# it, run `M-x exhub-reload-keys` (or `exhub_reload_keys`) instead of
# restarting.
mix scr.insert dev gitee_ent_mcp_access_token "your-enterprise-mcp-access-token"
# Optional: Provider-specific keys
mix scr.insert dev siliconflow_api_key "your-siliconflow-key"
mix scr.insert dev mistral_api_key "your-mistral-key"
mix scr.insert dev codestral_api_key "your-codestral-key"
mix scr.insert dev anthropic_api_key "your-anthropic-key"
mix scr.insert dev groq_api_key "your-groq-key"
mix scr.insert dev gemini_api_key "your-gemini-key"
mix scr.insert dev cohere_api_key "your-cohere-key"
mix scr.insert dev samba_api_key "your-samba-key"
mix scr.insert dev mimo_api_key "your-mimo-key"
mix scr.insert dev runinfra_api_key "your-runinfra-key"Edit an existing secret:
export SECRET_VAULT_PASSWORD="your-master-password"
mix scr.edit dev gitee_api_keyTo view a secret:
export SECRET_VAULT_PASSWORD="your-master-password"
mix scr.show dev gitee_api_keyList all secrets in the current environment:
mix scr.list devDelete a secret using standard file operations:
rm priv/secrets/dev/default/gitee_api_key.vault_secretOr rename/move:
mv priv/secrets/dev/default/old_key.vault_secret \
priv/secrets/dev/default/new_key.vault_secretCheck the quality and security of your secrets:
mix scr.auditThis will report:
- Weak or common passwords
- Duplicate secrets
- Empty secrets
- Other security issues
Use runtime_secret!/2 in config/runtime.exs:
import Config
import SecretVault, only: [runtime_secret!: 2]
config :my_app, :api_key, runtime_secret!(:exhub, "api_key")Access secrets at runtime after they've been loaded:
# If using persistent_term storage (recommended)
# First fetch the config
{:ok, config} = SecretVault.Config.fetch_from_current_env(:exhub)
# Then retrieve a specific secret
secret = SecretVault.Storage.fetch_from_persistent_term!(config, "api_key")
# Or simply use from application environment (set by runtime.exs)
Application.get_env(:exhub, :giteeai_api_key)defmodule MyModule do
def do_something do
# Access from application env (set by runtime.exs)
api_key = Application.fetch_env!(:exhub, :giteeai_api_key)
# ... use api_key
end
end| Variable | Description | Example |
|---|---|---|
SECRET_VAULT_PASSWORD |
Master password for decrypting secrets | my-super-secret-password |
| Variable | Description | Default |
|---|---|---|
MIX_ENV |
Elixir environment | dev |
Create a .env file (not committed to Git):
# .env
export SECRET_VAULT_PASSWORD="your-dev-password"
export MIX_ENV=devSource it before working:
source .env
mix phx.server- Use a strong, unique password (20+ characters)
- Don't reuse passwords from other services
- Store the master password in a password manager
- Never commit the password to Git
Regularly rotate your API keys:
# Generate new key from provider dashboard
# Update the secret
mix scr.edit dev gitee_api_key
# Restart applicationCRITICAL: Never commit unencrypted secrets!
The following patterns should be in .gitignore:
# Environment files
.env
.env.local
.env.*.local
# Plain text secrets (just in case)
*.key
*.secret
secrets.txtDO commit the encrypted .vault_secret files:
# These SHOULD be committed
git add priv/secrets/dev/default/*.vault_secret
git commit -m "Add encrypted secrets"Each MIX_ENV has its own vault:
# Development secrets
export MIX_ENV=dev
mix scr.insert dev api_key "dev-key"
# Production secrets (different password recommended)
export MIX_ENV=prod
export SECRET_VAULT_PASSWORD="prod-master-password"
mix scr.insert prod api_key "prod-key"Limit who has access to:
- The master password
- The production secrets directory
- Environment variable configuration
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Elixir
uses: erlef/setup-beam@v1
with:
elixir-version: '1.16'
otp-version: '26'
- name: Install dependencies
run: mix deps.get
- name: Audit secrets
run: mix scr.audit
env:
SECRET_VAULT_PASSWORD: ${{ secrets.SECRET_VAULT_PASSWORD }}
- name: Build release
run: mix release
env:
SECRET_VAULT_PASSWORD: ${{ secrets.SECRET_VAULT_PASSWORD }}
- name: Deploy
run: |
# Deploy commands hereFROM elixir:1.16-alpine
WORKDIR /app
COPY mix.exs mix.lock ./
RUN mix deps.get
COPY . .
# Build the release
ARG SECRET_VAULT_PASSWORD
ENV SECRET_VAULT_PASSWORD=$SECRET_VAULT_PASSWORD
RUN mix release
# Runtime
ENV MIX_ENV=prod
CMD ["_build/prod/rel/exhub/bin/exhub", "start"]Build with:
docker build --build-arg SECRET_VAULT_PASSWORD="$SECRET_VAULT_PASSWORD" .Cause: Secret doesn't exist in the vault
Solution:
mix scr.list dev # Check available secrets
mix scr.insert dev missing_secret "value" # Create itCause: Wrong SECRET_VAULT_PASSWORD
Solution:
- Verify the environment variable is set
- Check if the secret was created with a different password
- Recreate the secret if needed
Cause: Application config not set up correctly
Solution:
- Check
config/config.exshas the secret_vault config - Verify the OTP app name matches (
:exhub)
Cause: Runtime.exs not being executed or errors in config
Solution:
- Check
config/runtime.exssyntax - Verify all referenced secrets exist
- Check application logs for errors
If you previously had secrets in config/config.exs:
- Extract all secret values
- Create them using
mix scr.insert - Replace with
runtime_secret!/2calls inruntime.exs - Remove hardcoded values from
config.exs - Add
.vault_secretfiles to Git - Update deployment documentation
WARNING: If you lose the master password, your secrets are unrecoverable.
Recovery:
- Regenerate all API keys from their respective providers
- Create new secrets with a new master password
- Update all deployments with new secrets
| Task | Command |
|---|---|
| Install deps | mix deps.get |
| Create secret | mix scr.create dev key_name |
| Insert secret | mix scr.insert dev key_name "value" |
| Edit secret | mix scr.edit dev key_name |
| Show secret | mix scr.show dev key_name |
| List secrets | mix scr.list dev |
| Audit secrets | mix scr.audit |
| Delete secret | rm priv/secrets/dev/default/key_name.vault_secret |