Lockwell

Docs · Deliver

Environment variables

This shell function reads one or more folders through Vault's merged view (references filled in) and exports every field as a variable. It needs curl, jq, and VAULT_ADDR and VAULT_TOKEN set (see Machine identities).

# lw_env <tenant> <folder/>...: export every secret in the folders as variables, later folders winning.
# A secret's "value" field is named after the secret (DATABASE_URL); another field is NAME_FIELD (DB_PASSWORD).
lw_env() {
  t=$1; shift
  for f in "$@"; do
    j=$(curl -sf -H "X-Vault-Token: $VAULT_TOKEN" "$VAULT_ADDR/v1/$t/resolved/$f?values=true&resolve=true") \
      || { echo "lw_env: can't read $t/$f" >&2; return 1; }
    eval "$(printf '%s' "$j" | jq -r '.data.secrets[] | .name as $n | (.data // {}) | to_entries[]
      | (if .key == "value" then $n else "\($n)_\(.key)" end | ascii_upcase | gsub("[^A-Z0-9_]"; "_")) as $k
      | "export \($k)=\(.value | if type == "string" then . else tojson end | @sh)"')"
  done
}
lw_env lw-<team> shared/ prod/ && exec ./server --port 8080

Naming

  • A secret's value field becomes a variable named after the secret: prod/DATABASE_URL becomes DATABASE_URL.
  • Any other field becomes SECRET_FIELD: field password of prod/db becomes DB_PASSWORD.
  • Names are upper-cased, with anything but letters, digits and _ made _. That's the rule Vault's own syncs use.
  • Later folders override earlier ones. Within one folder, two secrets that give the same name: the later one wins, so keep names unique.

In a container

# entrypoint.sh, with curl and jq in the image and the IDs from your platform's secret settings
#!/bin/sh
set -e
. /app/lw_env.sh   # the function above
export VAULT_ADDR=https://vault.lunaor.com
export VAULT_TOKEN=$(curl -sf "$VAULT_ADDR/v1/auth/approle/login" \
  -d "{\"role_id\":\"$LOCKWELL_ROLE_ID\",\"secret_id\":\"$LOCKWELL_SECRET_ID\"}" | jq -r .auth.client_token)
lw_env lw-<team> shared/ prod/
unset VAULT_TOKEN LOCKWELL_SECRET_ID
exec "$@"

Vault's own vault-run binary does the same, but it isn't published as a download for Lockwell yet.