TExcelTAD
Home › Secrets & Policy as Code: Vault, SOPS, OPA & Conftest › Transit: Encryption as a Service

Transit: Encryption as a Service

Applications that store sensitive fields (emails, tokens, card fingerprints) need encryption keys, and keys in application config leak like any other secret. The transit engine keeps the keys inside Vault and offers encryption as an API: the application sends plaintext, gets ciphertext back, and stores that in its own database. Vault stores nothing but the keys. Applications never see a key, keys rotate without touching stored data, and every encrypt and decrypt is in Vault's audit log.

Endpoint Does
transit/keys/<name> create, read, configure a named key (types: aes256-gcm96 default, chacha20-poly1305, ed25519, ecdsa-p256, rsa-4096, …)
transit/encrypt/<name> plaintext (base64) → vault:v<N>:<ciphertext>
transit/decrypt/<name> ciphertext → plaintext (base64)
transit/keys/<name>/rotate adds a new key version; new encryptions use it
transit/rewrap/<name> re-encrypts ciphertext with the latest version, without exposing plaintext
`transit/datakey/plaintext wrapped/`
transit/sign, transit/verify, transit/hmac signatures and HMACs with keys that never leave Vault

Encrypt, rotate, rewrap

bash · try it yourself
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 secrets enable transit > /dev/null
vault write -f transit/keys/linkly-pii > /dev/null

enc() { vault write -field=ciphertext transit/encrypt/linkly-pii plaintext=$(printf '%s' "$1" | base64); }
dec() { vault write -field=plaintext transit/decrypt/linkly-pii ciphertext=$1 | base64 -d; echo; }

OLD=$(enc 'ada@example.com')
echo "stored in the database: ${OLD:0:9}… (${#OLD} chars)"
echo "decrypted: $(dec $OLD)"

vault write -f transit/keys/linkly-pii/rotate > /dev/null
NEW=$(enc 'grace@example.com')
echo "after rotation, new data: ${NEW:0:9}…   old data still decrypts: $(dec $OLD)"

# upgrade old ciphertext without the application ever seeing the plaintext
RE=$(vault write -field=ciphertext transit/rewrap/linkly-pii ciphertext=$OLD)
echo "rewrapped: ${RE:0:9}… -> $(dec $RE)"

# once everything is rewrapped, retire version 1
vault write transit/keys/linkly-pii/config min_decryption_version=2 > /dev/null
dec $OLD 2>&1 | grep -E '^\*'
vault read -format=json transit/keys/linkly-pii | jq -c '.data | {type, latest_version, min_decryption_version, exportable}'
stored in the database: vault:v1:… (69 chars)
decrypted: ada@example.com
after rotation, new data: vault:v2:…   old data still decrypts: ada@example.com
rewrapped: vault:v2:… -> ada@example.com
* ciphertext or signature version is disallowed by policy (too old)
{"type":"aes256-gcm96","latest_version":2,"min_decryption_version":2,"exportable":false}
  • The vault:v1: prefix records which key version encrypted a value, so rotation never breaks old data.
  • rewrap is a batch job you run after rotations; min_decryption_version then makes old ciphertext (for example in old backups) undecryptable, which is a kind of crypto-shredding.
  • exportable: false: the key can't be read out of Vault, ever. That is the point.

Envelope encryption for big data

Sending a 2 GB backup through Vault would be slow. Instead, ask for a data key: Vault returns it in plaintext (to encrypt locally) and wrapped with the transit key (to store next to the data). Later, unwrap it with transit/decrypt:

bash · try it yourself
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 secrets enable transit > /dev/null
vault write -f transit/keys/backups > /dev/null

seq 1 50000 > links-export.csv
vault write -f -format=json transit/datakey/plaintext/backups > dk.json
jq -r .data.plaintext dk.json > key.b64          # a fresh 256-bit key, base64
openssl enc -aes-256-cbc -pbkdf2 -pass file:key.b64 -in links-export.csv -out links-export.csv.enc
jq -r .data.ciphertext dk.json > links-export.csv.key                     # wrapped key, stored with the data
shred -u key.b64 dk.json links-export.csv                                 # plaintext key and data gone
ls

# restore: unwrap the key in Vault, decrypt locally
vault write -field=plaintext transit/decrypt/backups ciphertext=$(cat links-export.csv.key) > key.b64
openssl enc -d -aes-256-cbc -pbkdf2 -pass file:key.b64 -in links-export.csv.enc | tail -1
links-export.csv.enc
links-export.csv.key
vault.log
50000

For brevity the session feeds the data key to openssl as a passphrase file; real tools use the raw 256-bit key directly with an AEAD cipher such as AES-GCM. This is the same pattern cloud KMS services use (AWS KMS GenerateDataKey), and the one SOPS uses for every file (SOPS).

Signing

Asymmetric transit keys sign and verify without the private key leaving Vault, for example to sign release manifests or webhook payloads:

bash · try it yourself
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 secrets enable transit > /dev/null
vault write transit/keys/release-signing type=ed25519 > /dev/null

MSG=$(printf 'linkly 1.4.2 sha256:4f1c…' | base64)
SIG=$(vault write -field=signature transit/sign/release-signing input=$MSG)
echo "signature prefix: ${SIG:0:9}"
vault write -field=valid transit/verify/release-signing input=$MSG signature=$SIG; echo
vault write -field=valid transit/verify/release-signing input=$(printf 'linkly 9.9.9' | base64) signature=$SIG; echo
signature prefix: vault:v1:
true
false

Go further: Key Management, Rotation and Envelope Encryption covers the key lifecycle, envelope encryption and HSMs.

Test yourself

What does the application store when it uses transit?

  1. The plaintext and the key
  2. Only the ciphertext (vault:v1:…); keys stay in Vault
  3. Nothing
  4. The key only

After rotating a transit key, what happens to data encrypted with version 1?

  1. It can no longer be decrypted
  2. It still decrypts; rewrap can upgrade it to the latest version
  3. It is re-encrypted automatically
  4. It is deleted

Why use a data key (envelope encryption) for a large backup?

  1. Vault cannot encrypt anything
  2. Encrypt locally with a data key and store the key wrapped by Vault, instead of streaming gigabytes through Vault
  3. Data keys never expire
  4. It avoids authentication