All guides

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.

Lines of code on a monitor, seen at an angle

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.txt writes a key file and prints its public key. You encrypt for one or more age recipients with the --age option. To decrypt, SOPS looks for keys.txt in a sops subdirectory of your user configuration directory, or in the file named by the SOPS_AGE_KEY_FILE environment variable.
  • Cloud KMS. A creation rule takes kms for AWS master keys, gcp_kms for GCP KMS resource IDs and azure_keyvault for Azure Key Vault key identifiers.

Creation rules in .sops.yaml

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 --config option.
  • Creation rules are evaluated sequentially, and the first match wins.
  • path_regex checks the path of the file relative to the .sops.yaml file. A rule with no path_regex matches all files.
  • encrypted_regex encrypts a value only if its key matches. With ^(data|stringData)$, SOPS encrypts the values under the data and stringData keys of a Kubernetes Secret. It does not encrypt other values, such as metadata.

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

Shell

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 -i replaces the original file with the encrypted one.
  • edit opens the file in a text editor. With the path of a new file, it creates that file.
  • decrypt writes to standard output, and --extract returns one part of the document tree.
  • exec-env places 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

  1. 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.
  2. Keep the age key file out of the repository. It holds the identity that SOPS decrypts with. Add it to .gitignore.
  3. Know what each command changes. updatekeys adds or removes keys and keeps the data key. rotate generates a new data encryption key and encrypts all values again.
  4. Follow the documented order after a key is compromised. Remove the key from .sops.yaml. Then run sops updatekeys and sops rotate --in-place on each encrypted file, and commit the result. Only after this is done, rotate the passwords, API keys and other secrets inside those files.
  5. 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.

Book a call

More guides