Skip to content

Lesson 4 of 12 · 16m

SOPS: Ciphertext in Git

sops encrypts each value under a per-file AES-256-GCM data key wrapped to your recipients, so secrets can live in the repo with full diff and review history.

sops -e -i secrets.yaml reads the file, generates one random data key, encrypts each value with AES-256-GCM, then encrypts the data key itself once per recipient — an age X25519 key, a PGP fingerprint, an AWS KMS ARN, a GCP KMS resource, an Azure Key Vault key, or a Vault transit key — and stores all of that in a sops: metadata block at the bottom of the file. The YAML keys stay cleartext. Only the values are ciphertext. git diff still shows you that db.host changed; db.password renders as ENC[AES256_GCM,data:…,iv:…,tag:…,type:str].

Why the wrong model loses

The reflex: "secrets must never touch git." That reflex is correct about plaintext and wrong about ciphertext. Committing ciphertext is not a compromise — it is the whole design. You get versioning, PR review on secret changes (you can see which key rotated without seeing its value), blame, and an audit trail, with decryption gated by whoever holds a recipient key. The threat model shrinks to one question: who can unwrap the data key. FluxCD supports sops decryption natively in its controllers precisely because this is the standard GitOps secrets pattern.

.sops.yaml at the repo root is where the recipients live:

creation_rules:
  - path_regex: secrets/prod/.*\.yaml$
    key_groups:
      - age:
          - age1ql3z7hjy54pw3hyww5ayyjj7ahg5fjn9xne5ltu0fwwcmdsuu2fysl2m3q
        kms:
          - arn: arn:aws:kms:us-east-1:123456789012:key/9f3c…
            aws_profile: ops
    shamir_threshold: 2

Key groups with a Shamir threshold mean no single recipient can decrypt alone — two of the group must contribute. sops updatekeys re-wraps data keys when the recipient list changes without touching the values. Two details worth knowing in the metadata: the sops.mac field is a MAC over the plaintext tree, so tampered ciphertext gets flagged rather than silently decrypted, and lastmodified/version lines are there for merging — no merge driver ships with sops itself, but a widely-used community gist script (and third-party tools like Clef) decrypts both sides of a conflict so two branches editing different keys in the same file merge cleanly.

Two knobs in creation_rules matter more than most setups realize: encrypted_regex limits encryption to keys matching a pattern (encrypt .*pass.*|.*secret.* and let the rest stay readable), and unencrypted_suffix (_unencrypted) exempts specific keys — useful for keeping replicas or log_level diffable in the same file.

What the file does not protect

sops decrypts to memory or to a pipe; it does not make the plaintext safe once decoded. sops exec-env secrets.env -- ./app injects decrypted values into the child's environment without writing plaintext to disk — better, but now the env-var exposure rules from the last two lessons apply. sops exec-file renders to a temp file under the OS temp dir and deletes it after. Both beat sops -d secrets.yaml > .env, which recreates the .env trap with extra steps.

The age private key is the crown jewel: age-keygen emits AGE-SECRET-KEY-… — keep it off the repo, passphrase-protected or on hardware, one per operator laptop. CI decrypts through the KMS recipient, not a shared age key checked into a CI secret (which just moves the plaintext to a different store).

Concrete practice

  • One .sops.yaml, recipient rules per path; prod paths get tighter key groups than dev.
  • Encrypt before first commit: sops refuses nothing — nothing stops you from committing plaintext password: hunter2. A pre-commit hook that rejects password: lines not matching ENC[ earns its keep.
  • Never sops -d to disk as part of app boot; use exec-env/exec-file, or have the app read a mounted secret directly.
  • Rotate the file's data key when a recipient leaves (updatekeys then re-encrypt), and rotate the underlying secret when the value may have been read.

Ciphertext in git is a feature — the diff history alone pays for the setup. Plaintext anywhere is the bug, and sops's whole job is making sure the only plaintext is the one your app is actively using.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.