SOPS: encrypted secrets in a Git repository
By default, SOPS encrypts all the values of a YAML, JSON, ENV or INI file and leaves the keys in cleartext. The file can sit in Git beside the code, and the keys that decrypt it are what you manage.

What SOPS does
SOPS is an editor of encrypted files. Its formats are YAML, JSON, ENV, INI and BINARY. Its encryption keys come from AWS KMS, GCP KMS, Azure Key Vault, HuaweiCloud KMS, age or PGP. It was launched at Mozilla in 2015 and donated to the Cloud Native Computing Foundation as a Sandbox project in 2023. The code lives at github.com/getsops/sops and the documentation at getsops.io.
For a new file SOPS generates a random data key of 256 bits and has every master key encrypt that key. The encrypted copies are stored in the sops metadata of the file. SOPS then browses the file as a key/value tree and encrypts every leaf value with AES256_GCM using the data key. Later, one valid master key is enough to decrypt the data key and reach the data.
SOPS also computes a MAC over every value. The MAC shows whether a value was added or removed fraudulently. The project expects that keys do not carry sensitive information.
age and cloud KMS keys
- age. The SOPS documentation calls age a simple, modern and secure tool for encrypting files, and recommends it over PGP if possible.
age-keygen -o key.txtwrites a key file and prints its public key. You encrypt for one or more age recipients with the--ageoption. To decrypt, SOPS looks forkeys.txtin asopssubdirectory of your user configuration directory, or in the file named by theSOPS_AGE_KEY_FILEenvironment variable. - Cloud KMS. A creation rule takes
kmsfor AWS master keys,gcp_kmsfor GCP KMS resource IDs andazure_keyvaultfor Azure Key Vault key identifiers.
Creation rules in .sops.yaml
creation_rules:
- path_regex: secrets/prod/.*\.yaml$
encrypted_regex: ^(data|stringData)$
kms: arn:aws:kms:us-east-1:111122223333:key/<key-id>
age: <age-public-key>
- path_regex: secrets/dev/.*\.yaml$
encrypted_regex: ^(data|stringData)$
age: <age-public-key-1>,<age-public-key-2>The paths and keys in this example are yours to choose.
- The file must be named
.sops.yaml. SOPS searches the current working directory and then each parent directory, and takes the first file it finds. Another name, such as.sops.yml, works only with the--configoption. - Creation rules are evaluated sequentially, and the first match wins.
path_regexchecks the path of the file relative to the.sops.yamlfile. A rule with nopath_regexmatches all files.encrypted_regexencrypts a value only if its key matches. With^(data|stringData)$, SOPS encrypts the values under thedataandstringDatakeys of a Kubernetes Secret. It does not encrypt other values, such asmetadata.
The rules select keys for new files. For an existing file, the updatekeys command uses .sops.yaml to add or remove keys.
Encrypt, edit and decrypt
age-keygen -o key.txt
sops encrypt -i /path/to/existing/file.yaml
sops edit /path/to/existing/file.yaml
sops decrypt /path/to/existing/file.yaml
sops decrypt --extract '["app2"]["key"]' /path/to/existing/file.yaml
sops exec-env out.json './database-import'The file names and the extract path are the documentation's examples.
encrypt -ireplaces the original file with the encrypted one.editopens the file in a text editor. With the path of a new file, it creates that file.decryptwrites to standard output, and--extractreturns one part of the document tree.exec-envplaces the decrypted values in the environment of a child process. They are never written to disk.
Change encrypted files only through SOPS. The file's checksum covers unencrypted content too, and a change to that content made outside SOPS breaks the file integrity check.
Decryption in Flux
Once the Flux Kustomization sets decryption with provider: sops, Flux's kustomize-controller uses SOPS to decrypt Kubernetes secrets and then applies them on the cluster.
Key management cautions
- Use more than one master key. The SOPS documentation recommends three: two KMS master keys, each in a different region, and one PGP public key with its private key kept offline. If both KMS keys are lost, the data can still be recovered with the PGP private key.
- Keep the age key file out of the repository. It holds the identity that SOPS decrypts with. Add it to
.gitignore. - Know what each command changes.
updatekeysadds or removes keys and keeps the data key.rotategenerates a new data encryption key and encrypts all values again. - Follow the documented order after a key is compromised. Remove the key from
.sops.yaml. Then runsops updatekeysandsops rotate --in-placeon each encrypted file, and commit the result. Only after this is done, rotate the passwords, API keys and other secrets inside those files. - Rotate the data key when you remove a key. The documentation gives the reason: otherwise, owners of the removed key may have had access to the data key in the past.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

Secrets in GitOps: Sealed Secrets, SOPS or External Secrets Operator
GitOps · 4 min read
Sealed Secrets, SOPS and External Secrets Operator, with the documented facts for each side by side.

Kubernetes Secrets: encryption at rest and who can read them
Security · 4 min read
How Secrets are stored, how to encrypt them in etcd, and who can still read them.

Secret scanning: Gitleaks and GitHub push protection
Secrets · 4 min read
Gitleaks before the commit and in CI, GitHub push protection at the server, and what to do with a finding.