TExcelTAD
Home › Secrets & Policy as Code: Vault, SOPS, OPA & Conftest › SOPS for Teams: Creation Rules & Key Rotation

SOPS for Teams: Creation Rules & Key Rotation

One developer and one key is easy. A team needs answers to: who can decrypt dev vs prod? What happens when someone joins or leaves? How is the data key rotated? SOPS handles all three through .sops.yaml and two commands.

Per-environment creation rules

Rules are matched top to bottom; the first rule whose path_regex matches the file decides the keys. A typical layout gives developers dev secrets, and only the production CI identity (and a break-glass key kept offline) prod secrets:

bash · try it yourself
umask 077
for who in ada grace ci-dev ci-prod breakglass; do age-keygen -o $who.key 2> /dev/null; done
pub() { age-keygen -y $1.key; }
cat > .sops.yaml <<EOF
creation_rules:
  - path_regex: envs/prod/.*\.yaml$
    encrypted_regex: ^(data|stringData)$
    age: $(pub ci-prod),$(pub breakglass)
  - path_regex: envs/dev/.*\.yaml$
    encrypted_regex: ^(data|stringData)$
    age: $(pub ada),$(pub grace),$(pub ci-dev)
EOF
mkdir -p envs/dev envs/prod
for env in dev prod; do
  printf 'apiVersion: v1\nkind: Secret\nmetadata:\n  name: linkly-db\nstringData:\n  password: %s-Pa55\n' $env > envs/$env/db.yaml
  sops encrypt -i envs/$env/db.yaml
done

can() {   # can <person> <file>
  SOPS_AGE_KEY_FILE=$1.key sops decrypt --extract '["stringData"]["password"]' $2 > /dev/null 2>&1 && echo yes || echo no
}
printf '%-11s %-5s %-5s\n' who dev prod
for who in ada grace ci-dev ci-prod breakglass; do
  printf '%-11s %-5s %-5s\n' $who $(can $who envs/dev/db.yaml) $(can $who envs/prod/db.yaml)
done
who         dev   prod
ada         yes   no
grace       yes   no
ci-dev      yes   no
ci-prod     no    yes
breakglass  no    yes

The access matrix is visible in one file that is reviewed like code: adding someone to prod is a pull request to .sops.yaml.

Someone joins, someone leaves: updatekeys

Changing .sops.yaml doesn't change already-encrypted files. sops updatekeys re-encrypts each file's data key for the current recipients (the values themselves are untouched):

bash · try it yourself
umask 077
for who in ada grace linus; do age-keygen -o $who.key 2> /dev/null; done
pub() { age-keygen -y $1.key; }
printf 'creation_rules:\n  - age: %s,%s\n' "$(pub ada)" "$(pub grace)" > .sops.yaml
printf 'password: dev-Pa55\n' > db.yaml
sops encrypt -i db.yaml
recipients() { yq '.sops.age | length' db.yaml; }
echo "recipients: $(recipients)"

# grace leaves the team, linus joins
printf 'creation_rules:\n  - age: %s,%s\n' "$(pub ada)" "$(pub linus)" > .sops.yaml
SOPS_AGE_KEY_FILE=ada.key sops updatekeys -y db.yaml 2>&1 | grep -E '^(\+|-|Syncing|File)' | sed 's/age1.*/age1…/'
echo "recipients: $(recipients)"
for who in ada grace linus; do
  printf '%-6s %s\n' $who "$(SOPS_AGE_KEY_FILE=$who.key sops decrypt --extract '["password"]' db.yaml 2> /dev/null || echo 'cannot decrypt')"
done
recipients: 2
+++ age1…
--- age1…
recipients: 2
ada    dev-Pa55
grace  cannot decrypt
linus  dev-Pa55

Updating keys is not enough when someone leaves. Grace can no longer decrypt the new file, but she could decrypt the old one, which is still in Git history, and she may have the password in memory. Rotate the secret values too, and rotate the data key:

bash
sops rotate -i envs/prod/db.yaml     # new data key, all values re-encrypted

Key groups: two parties must agree

For the most sensitive files, key_groups with shamir_threshold splits the data key so that decryption needs keys from several groups, e.g. the production KMS key and a security-team key:

yaml
creation_rules:
  - path_regex: envs/prod/root-ca\.yaml$
    shamir_threshold: 2
    key_groups:
      - kms:
          - arn: arn:aws:kms:eu-west-1:111122223333:alias/sops-prod
      - age:
          - age1securityteam…

Cloud KMS instead of key files

In CI, an age identity is itself a secret to store. A KMS master key avoids it: kms: (AWS), gcp_kms:, azure_keyvault: or hc_vault_transit_uri: in a creation rule, and the job gets permission to call Decrypt through its OIDC identity (JWT/OIDC Auth for CI). Keep one age recipient as an offline break-glass key in case the KMS is unavailable.

yaml
creation_rules:
  - path_regex: envs/prod/.*
    kms: arn:aws:kms:eu-west-1:111122223333:alias/sops-prod
    age: age1breakglass…

Test yourself

Two creation rules match a file. Which one applies?

  1. Both, merged
  2. The first one in .sops.yaml
  3. The last one
  4. The most specific regex

A developer leaves. You remove their recipient and run sops updatekeys. What else should you do?

  1. Nothing
  2. Rotate the secret values (and the data key with sops rotate), since they could decrypt older versions in Git history
  3. Delete the Git history
  4. Change the file names