Vault Agent: Auto-Auth & Templates
Teaching every application to log in to Vault, renew tokens, renew leases and re-read secrets is a lot of code to get right in every language. Vault Agent is the same vault binary running as a helper next to the application:
- auto-auth: logs in with a configured method (AppRole, Kubernetes, JWT, cloud IAM…), keeps the token renewed and logs in again when it expires;
- templates: renders secrets into files (or environment variables) using Consul Template syntax, re-rendering when they change;
- process supervisor (
exec): starts the application with secrets in its environment and restarts it when they change; - caching / API proxy: a local listener that forwards requests with the agent's token (also available as the separate
vault proxycommand).
The application then reads a file and knows nothing about Vault. This is what the Agent Injector runs as a sidecar in Kubernetes.
Render a config file once
exit_after_auth = true makes the agent authenticate, render and exit: an init-container or a deploy step.
vault server -dev -dev-root-token-id=root -dev-listen-address=127.0.0.1:8200 > vault.log 2>&1 &
for i in $(seq 50); do vault status > /dev/null 2>&1 && break; sleep 0.2; done
export VAULT_TOKEN=root
vault kv put -mount=secret linkly/db username=linkly password='c0rrect-h0rse' > /dev/null
printf 'path "secret/data/linkly/*" {\n capabilities = ["read"]\n}\n' | vault policy write linkly-read - > /dev/null
vault auth enable approle > /dev/null
vault write auth/approle/role/linkly token_policies=linkly-read token_ttl=20m > /dev/null
vault read -field=role_id auth/approle/role/linkly/role-id > role_id
vault write -f -field=secret_id auth/approle/role/linkly/secret-id > secret_id
chmod 600 role_id secret_id
cat > agent.hcl <<'HCL'
vault {
address = "http://127.0.0.1:8200"
}
auto_auth {
method "approle" {
config = {
role_id_file_path = "role_id"
secret_id_file_path = "secret_id"
remove_secret_id_file_after_reading = true
}
}
}
template {
destination = "linkly.env"
perms = "0400"
contents = <<EOT
{{ with secret "secret/data/linkly/db" -}}
DATABASE_URL=postgres://{{ .Data.data.username }}:{{ .Data.data.password }}@postgres:5432/linkly
{{ end -}}
EOT
}
exit_after_auth = true
HCL
unset VAULT_TOKEN # the agent must authenticate on its own
vault agent -config=agent.hcl > agent.log 2>&1; echo "agent exit $?"
ls -l linkly.env | cut -d' ' -f1
cat linkly.env
ls # the secret_id file is gone: it was used onceagent exit 0
-r--------
DATABASE_URL=postgres://linkly:c0rrect-h0rse@postgres:5432/linkly
agent.hcl
agent.log
linkly.env
role_id
vault.log
remove_secret_id_file_after_reading = true (the default) deletes the secret id after login, so it can't be read again from disk. With perms = "0400" only the owner can read the rendered file.
Keep secrets current and restart the app
Run without exit_after_auth and the agent stays up. In exec mode it starts the application with secrets in environment variables (env_template) and restarts it when a secret changes. The session rotates linkly's password in Vault while the "app" runs:
vault server -dev -dev-root-token-id=root -dev-listen-address=127.0.0.1:8200 > vault.log 2>&1 &
for i in $(seq 50); do vault status > /dev/null 2>&1 && break; sleep 0.2; done
export VAULT_TOKEN=root
vault kv put -mount=secret linkly/db username=linkly password='first-Pa55' > /dev/null
printf 'path "secret/data/linkly/*" {\n capabilities = ["read"]\n}\n' | vault policy write linkly-read - > /dev/null
vault auth enable approle > /dev/null
vault write auth/approle/role/linkly token_policies=linkly-read token_ttl=20m > /dev/null
vault read -field=role_id auth/approle/role/linkly/role-id > role_id
vault write -f -field=secret_id auth/approle/role/linkly/secret-id > secret_id
cat > agent.hcl <<'HCL'
vault {
address = "http://127.0.0.1:8200"
}
auto_auth {
method "approle" {
config = {
role_id_file_path = "role_id"
secret_id_file_path = "secret_id"
}
}
}
template_config {
static_secret_render_interval = "1s" # default 5m: how often KV secrets are re-read
}
env_template "DATABASE_URL" {
contents = "{{ with secret \"secret/data/linkly/db\" }}postgres://{{ .Data.data.username }}:{{ .Data.data.password }}@postgres:5432/linkly{{ end }}"
error_on_missing_key = true
}
exec {
# stands in for linkly: logs which password it started with (never do that for real)
command = ["sh", "-c", "echo \"started with ${DATABASE_URL#*://}\" >> app.log; sleep 600"]
restart_on_secret_changes = "always"
restart_stop_signal = "SIGTERM"
}
HCL
(unset VAULT_TOKEN; vault agent -config=agent.hcl > agent.log 2>&1) &
for i in $(seq 50); do grep -q first app.log 2> /dev/null && break; sleep 0.2; done
vault kv put -mount=secret linkly/db username=linkly password='second-Pa55' > /dev/null # rotation
for i in $(seq 50); do grep -q second app.log && break; sleep 0.2; done
awk '!seen[$0]++' app.logstarted with linkly:first-Pa55@postgres:5432/linkly
started with linkly:second-Pa55@postgres:5432/linkly
For applications that read files and reload on change, use template blocks instead of exec, optionally with an exec inside the template block (for example command = ["pkill", "-HUP", "linkly"]) to signal a reload.
Templates for every engine
The secret function works with every engine, and pkiCert renders and renews certificates:
template {
destination = "/run/secrets/db.env"
contents = <<EOT
{{ with secret "database/creds/linkly-app" -}}
PGUSER={{ .Data.username }}
PGPASSWORD={{ .Data.password }}
{{ end -}}
EOT
}
template {
destination = "/run/tls/api.pem"
contents = <<EOT
{{ with pkiCert "pki_int/issue/linkly-internal" "common_name=api.linkly.internal" "ttl=24h" -}}
{{ .Cert }}{{ .CA }}{{ .Key }}
{{ end -}}
EOT
}For dynamic secrets the agent renews the lease and fetches new credentials before the max TTL; for certificates it re-issues before expiry. Either way the file changes and the application must notice (Rotation Without Downtime).
Test yourself
What does auto-auth in Vault Agent do?
- Creates Vault policies
- Logs in with a configured auth method, keeps the token renewed and re-authenticates when needed
- Unseals Vault
- Encrypts files with age
In exec mode with restart_on_secret_changes = "always", what happens when the secret changes in Vault?
- Nothing until the agent restarts
- The agent re-renders the environment and restarts the child process with the new values
- The application is killed and not restarted
- Vault pushes the value to the app