KV v2: Versions, Metadata & Check-and-Set
The KV (key/value) engine stores static secrets. Version 2, the one dev mode mounts at secret/, keeps a history of every write, which is what makes static secrets rotatable and recoverable. (KV v1 stores only the latest value; you will still meet it in older setups, mounted with vault secrets enable -version=1 kv.)
| Command | Does |
|---|---|
vault kv put |
writes a new version with exactly these keys |
vault kv patch |
writes a new version, merging these keys into the latest data |
vault kv get [-version=N] |
reads the latest (or a given) version |
vault kv delete |
soft-deletes the latest version: hidden, recoverable |
vault kv undelete -versions=N |
brings soft-deleted versions back |
vault kv destroy -versions=N |
permanently removes the data of those versions |
| `vault kv metadata get | put |
Versions in action
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
vault kv put -mount=secret linkly/db username=linkly password='second-Pa55' > /dev/null
vault kv patch -mount=secret linkly/db port=5432 > /dev/null
echo "latest: $(vault kv get -mount=secret -format=json linkly/db | jq -c '.data | {version: .metadata.version, data}')"
echo "version 1: $(vault kv get -mount=secret -version=1 -field=password linkly/db)"
vault kv delete -mount=secret linkly/db
vault kv get -mount=secret -format=json linkly/db | jq -c '{data: .data.data, deleted: (.data.metadata.deletion_time != "")}'
vault kv undelete -mount=secret -versions=3 linkly/db > /dev/null
vault kv destroy -mount=secret -versions=1 linkly/db > /dev/null
vault kv metadata get -mount=secret -format=json linkly/db \
| jq -r '.data.versions | to_entries[] | "v\(.key) destroyed=\(.value.destroyed) deleted=\(.value.deletion_time != "")"'latest: {"version":3,"data":{"password":"second-Pa55","port":"5432","username":"linkly"}}
version 1: first-Pa55
Success! Data deleted (if it existed) at: secret/data/linkly/db
{"data":null,"deleted":true}
v1 destroyed=true deleted=false
v2 destroyed=false deleted=false
v3 destroyed=false deleted=false
putreplaced the whole secret;patchaddedportand kept the rest.- After
delete, reads return no data but the version still exists, soundeletebrought it back.destroyis final: version 1's data is gone, though its metadata entry remains.
Check-and-set: no lost updates
Two pipelines writing the same secret can overwrite each other. With check-and-set (-cas=N), a write succeeds only if the current version is N; -cas=0 means "only if the secret doesn't exist yet". Setting cas_required=true on the secret (or the whole mount) makes it mandatory:
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 -cas=0 linkly/api-key value=k1 > /dev/null && echo "created v1"
vault kv put -mount=secret -cas=0 linkly/api-key value=other 2>&1 | grep -o 'check-and-set.*'
vault kv metadata put -mount=secret -cas-required=true -max-versions=5 \
-custom-metadata=owner=team-links -custom-metadata=rotation=90d linkly/api-key
vault kv put -mount=secret linkly/api-key value=k2 2>&1 | grep -o 'check-and-set.*'
vault kv put -mount=secret -cas=1 linkly/api-key value=k2 > /dev/null && echo "updated to v2"
vault kv metadata get -mount=secret -format=json linkly/api-key \
| jq -c '.data | {cas_required, max_versions, current_version, custom_metadata}'created v1
check-and-set parameter did not match the current version
Success! Data written to: secret/metadata/linkly/api-key
check-and-set parameter required for this call
updated to v2
{"cas_required":true,"max_versions":5,"current_version":2,"custom_metadata":{"owner":"team-links","rotation":"90d"}}
max_versions bounds the history (older versions are removed as new ones arrive), and custom metadata records ownership and rotation policy right next to the secret, where audit scripts can find it.
Reading only what you need
Applications usually need one field. Two habits keep secrets out of logs and shells:
vault kv get -field=password …returns the raw value, ready for a file:vault kv get -field=password -mount=secret linkly/db > /run/secrets/db.vault kv get -format=json … | jq '.data.data | keys'lists the keys without printing values, which is what a debugging step should do.
There is also subkeys, which returns the structure of a secret with values replaced by null: vault read secret/subkeys/linkly/db. Policies can grant subkeys without granting data, so a tool can check that a secret has the right shape without seeing it.
Path layout
Paths are the unit of access control, so design them like a directory tree of ownership:
secret/
linkly/ ← team-links owns this subtree
db ← {username, password}
api-key
billing/
stripe
shared/
smtp ← read by several teamsA policy can then say "linkly's deploy role reads secret/data/linkly/*" and nothing else (Policies). Per-environment separation usually means separate mounts (kv-prod/, kv-dev/) or separate Vault namespaces/clusters, not a suffix in the key name.
Test yourself
What is the difference between vault kv delete and vault kv destroy on KV v2?
- None
- delete is a recoverable soft delete; destroy permanently removes the version's data
- destroy is recoverable
- delete removes all versions
A write with -cas=3 fails. Why?
- The token expired
- The secret's current version is not 3, so another writer changed it in between
- CAS is not supported on KV v2
- The value is too long
How does kv patch differ from kv put?
- patch merges the given keys into the latest data; put replaces the whole secret
- patch skips versioning
- put merges, patch replaces
- They are identical