TExcelTAD
Home › Secrets & Policy as Code: Vault, SOPS, OPA & Conftest › SOPS: Encrypted Values in Git

SOPS: Encrypted Values in Git

Vault keeps secrets out of Git. SOPS (Secrets OPerationS, a CNCF project) takes the other approach: secrets live in Git, encrypted. It encrypts the values of a YAML, JSON, ENV or INI file and leaves the keys readable, so a pull request still shows which setting changed, and Git history, review and GitOps work as usual.

yaml
# what a reviewer sees in Git (abridged)
stringData:
  password: ENC[AES256_GCM,data:…,iv:…,tag:…,type:str]
  username: ENC[AES256_GCM,data:…,iv:…,tag:…,type:str]
sops:
  age:
    - recipient: age1…
      enc: |
        -----BEGIN AGE ENCRYPTED FILE-----
        …
  lastmodified: "2026-10-28T09:00:00Z"
  mac: ENC[AES256_GCM,data:…]
  encrypted_regex: ^(data|stringData)$
  version: 3.13.3

How it works: SOPS generates a random data key, encrypts each value with AES-256-GCM under it, and stores the data key encrypted for each master key: age recipients, PGP keys, AWS/GCP/Azure KMS keys, HashiCorp Vault transit or OpenBao. A MAC over all values detects tampering, including values moved between keys.

Encrypt a Kubernetes Secret

A .sops.yaml at the repository root holds creation rules: which files to encrypt, which fields, and for which keys. Then sops encrypt needs no flags:

bash · try it yourself
umask 077
age-keygen -o key.txt 2> /dev/null
export SOPS_AGE_KEY_FILE=$PWD/key.txt         # where sops finds the age identity
cat > .sops.yaml <<EOF
creation_rules:
  - path_regex: secrets/.*\.yaml$
    encrypted_regex: ^(data|stringData)$
    age: $(age-keygen -y key.txt)
EOF

mkdir secrets
cat > secrets/linkly-db.yaml <<'YAML'
apiVersion: v1
kind: Secret
metadata:
  name: linkly-db
  namespace: linkly
type: Opaque
stringData:
  username: linkly
  password: c0rrect-h0rse
YAML
sops encrypt -i secrets/linkly-db.yaml

echo "--- in Git: structure readable, values encrypted"
yq '.metadata.name, .type' secrets/linkly-db.yaml
yq '.stringData | to_entries | .[] | .key + ": " + (.value | sub(",data:.*"; ",…]"))' secrets/linkly-db.yaml
yq '.sops | keys | join(", ")' secrets/linkly-db.yaml
grep -c c0rrect-h0rse secrets/linkly-db.yaml

echo "--- decrypt"
sops decrypt secrets/linkly-db.yaml | yq '.stringData'
sops decrypt --extract '["stringData"]["password"]' secrets/linkly-db.yaml; echo
--- in Git: structure readable, values encrypted
linkly-db
Opaque
username: ENC[AES256_GCM,…]
password: ENC[AES256_GCM,…]
age, encrypted_regex, lastmodified, mac, version
0
--- decrypt
username: linkly
password: c0rrect-h0rse
c0rrect-h0rse
  • encrypted_regex limited encryption to data/stringData, so metadata.name and kind stay readable and tools like Kustomize or Flux can still find the object. Without it SOPS encrypts every value. (Alternatives: unencrypted_suffix: _unencrypted, encrypted_suffix, unencrypted_regex.)
  • --extract reads one value, for scripts.

Editing and tamper detection

sops edit file.yaml decrypts into $EDITOR and re-encrypts on save; sops set changes one value non-interactively. Any change to the ciphertext outside SOPS breaks the MAC:

bash · try it yourself
umask 077
age-keygen -o key.txt 2> /dev/null
export SOPS_AGE_KEY_FILE=$PWD/key.txt
printf 'creation_rules:\n  - age: %s\n' "$(age-keygen -y key.txt)" > .sops.yaml
printf 'db_password: c0rrect-h0rse\napi_key: k1\n' > app.yaml
sops encrypt -i app.yaml

sops set app.yaml '["api_key"]' '"k2"'
sops decrypt app.yaml

echo "--- someone swaps two encrypted values by hand"
a=$(yq '.db_password' app.yaml); b=$(yq '.api_key' app.yaml)
A="$a" B="$b" yq -i '.db_password = strenv(B) | .api_key = strenv(A)' app.yaml
sops decrypt app.yaml 2>&1 | head -2; echo "exit ${PIPESTATUS[0]}"
db_password: c0rrect-h0rse
api_key: k2
--- someone swaps two encrypted values by hand
Error decrypting tree: Error walking tree: Could not decrypt value: Could not decrypt with AES_GCM: cipher: message authentication failed
exit 25

The swapped file is refused: values are bound to their position (the key path is authenticated data), and the MAC covers the whole file.

Other formats and running commands

bash · try it yourself
umask 077
age-keygen -o key.txt 2> /dev/null
export SOPS_AGE_KEY_FILE=$PWD/key.txt
printf 'creation_rules:\n  - age: %s\n' "$(age-keygen -y key.txt)" > .sops.yaml

printf 'DB_PASSWORD=c0rrect-h0rse\nLOG_LEVEL=info\n' > linkly.env
sops encrypt --input-type dotenv --output-type dotenv linkly.env > linkly.enc.env
cut -d= -f1 linkly.enc.env
rm linkly.env

# run a command with the decrypted values as environment variables, without writing them to disk
sops exec-env linkly.enc.env 'echo "LOG_LEVEL=$LOG_LEVEL, password length ${#DB_PASSWORD}"'
# or as a temporary file (a named pipe by default) passed to the command
sops exec-file linkly.enc.env 'grep -c = {}'
DB_PASSWORD
LOG_LEVEL
sops_age__list_0__map_enc
sops_age__list_0__map_recipient
sops_lastmodified
sops_mac
sops_unencrypted_suffix
sops_version
LOG_LEVEL=info, password length 13
2

exec-env and exec-file avoid the classic sops decrypt > .env that leaves a plaintext file behind (and in backups, and sometimes in Git).

Where decryption happens

Consumer How it gets the key
Developer personal age identity in ~/.config/sops/age/keys.txt (the default location)
CI job SOPS_AGE_KEY / SOPS_AGE_KEY_FILE from a CI secret, or better a cloud KMS key the job reaches through OIDC
Flux kustomize-controller decrypts natively with a sops-age Secret or KMS workload identity (Secrets in GitOps)
Argo CD a config management plugin, or helm-secrets/KSOPS
Terraform the carlpett/sops provider's sops_file data source (the values then end up in state; see How Secrets Leak)

Test yourself

What does SOPS leave readable in an encrypted YAML file?

  1. Nothing
  2. The keys and structure (and any values excluded by encrypted_regex or similar rules)
  3. Only comments
  4. Everything; it just signs the file

Why did the file with two swapped ciphertexts fail to decrypt?

  1. age keys expired
  2. The MAC and the per-value authenticated data detect any modification outside SOPS
  3. SOPS only supports one value
  4. yq corrupted the YAML

Why prefer sops exec-env over sops decrypt > .env?

  1. It is faster
  2. Values reach the process without a plaintext file on disk
  3. It re-encrypts the file
  4. It works without keys