Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

sops

Encrypt only the values in YAML, JSON, ENV and INI files, so secrets can live safely in Git (GitOps).

Level
DevSecOps service
Group
Secrets
Needs root
No
Package
sops

What it does

SOPS (Secrets OPerationS, a CNCF project) encrypts the values of a file and keeps the keys readable, so you still see the structure and Git diffs make sense. It uses age keys, PGP, or cloud KMS (Azure Key Vault, AWS KMS, GCP KMS). A .sops.yaml file in the repo says which files use which keys.

Flux decrypts SOPS files by itself; Argo CD can with a plugin (KSOPS, helm-secrets).

How to write it

sops [encrypt|decrypt|edit|updatekeys|rotate] [OPTIONS] FILE

Main parts

Commands

edit FILE (or just `sops FILE`)
Open decrypted in your editor, encrypt on save.
encrypt FILE / -e
Encrypt a file.
decrypt FILE / -d
Decrypt to stdout.
updatekeys FILE
Re-encrypt for the keys now in .sops.yaml (add a person).
rotate FILE
Make a new data key.
exec-env FILE CMD
Run a command with the secrets as env vars.
exec-file FILE CMD
Run a command with a temporary decrypted file.

Options 8

Most used

Flags

--age RECIPIENT
Encrypt for an age public key.
--azure-kv URL
Encrypt with an Azure Key Vault key.
--kms ARN
Encrypt with AWS KMS.
--encrypted-regex REGEX
Only encrypt keys that match (e.g. ^(data|stringData)$).
-i, --in-place
Write the result into the same file.
--input-type / --output-type
yaml, json, dotenv, ini, binary.
--extract '["key"]'
Print one value.
SOPS_AGE_KEY_FILE
Where your age private key is (env var).

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Make an age key

    age-keygen -o ~/.config/sops/age/keys.txt
    Public key: age1qz8x0examplepublickeyonly0000000000000000000000000000q9lc5
  2. 2Set rules for the repo

    cat .sops.yaml
    creation_rules:
      - path_regex: k8s/.*\.secret\.yaml$
        encrypted_regex: ^(data|stringData)$
        age: age1qz8x0examplepublickeyonly0000000000000000000000000000q9lc5
  3. 3Encrypt a Kubernetes Secret

    sops encrypt -i k8s/db.secret.yaml
    head -12 k8s/db.secret.yaml
    apiVersion: v1
    kind: Secret
    metadata:
        name: db
    stringData:
        password: ENC[AES256_GCM,data:3kq9…,type:str]
  4. 4Edit it later

    sops k8s/db.secret.yaml
  5. 5Decrypt and apply

    sops decrypt k8s/db.secret.yaml | kubectl apply -f -
  6. 6Read one value in a script

    sops decrypt --extract '["stringData"]["password"]' k8s/db.secret.yaml
  7. 7Use an Azure Key Vault key

    sops encrypt --azure-kv https://kv-s0sta-lab.vault.azure.net/keys/sops/0123456789abcdef0123456789abcdef -i secrets.env
  8. 8Run a program with secrets as variables

    sops exec-env secrets.env 'terraform plan'
  9. 9Add a new team member

    vim .sops.yaml
    sops updatekeys k8s/db.secret.yaml
  10. 10Let Flux decrypt with the age key

    kubectl create secret generic sops-age -n flux-system --from-file=age.agekey=$HOME/.config/sops/age/keys.txt
  11. 11Stop unencrypted secrets in CI

    grep -L "ENC\[" k8s/*.secret.yaml && echo "Unencrypted secret found" && exit 1

Watch out

  • The age private key never goes into Git; back it up safely.
  • sops decrypt > file writes clear text to disk — do not commit it (add patterns to .gitignore).
  • Removing a person from .sops.yaml does not hide old secrets from them: rotate the secrets.

Files

.sops.yaml
Rules: which files, which keys.
~/.config/sops/age/keys.txt
Your age private key(s).

Tips to remember

  • Encrypt only data/stringData in Kubernetes Secrets so names and labels stay readable.

Exit codes

0Success.
1General error.
51MAC mismatch (the file was changed outside sops).
100No file given.
128Could not get the data key (no matching key — wrong age key, no Key Vault access).
200File not changed (edit).
203File is already encrypted.