basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Leaked sops key: rotate and replace the secrets themselves

    If a private age key may have leaked, removing it from .sops.yaml and running sops updatekeys isn’t enough: updatekeys leaves the data key unchanged, so the compromised key can decrypt future edits too. Reissue the data key in every file one by one with sops rotate -i and replace the actual secrets in each service.

    Inside a sops file, every value is encrypted with one symmetric data key (AES-256-GCM), and copies of that data key sit in the file header — one per age recipient (Server secrets in git).

    sops updatekeys:   old data key  ── re-encrypt header ──→ old data key (unchanged)
    sops rotate -i:    new data key  ── re-encrypt file   ──→ new data key

    When retiring an old server normally, sops updatekeys is enough: its key drops out of the header and you’re done. When a private age key leaks (lost laptop, compromised server, keys.txt copied to the wrong place), updatekeys doesn’t even protect future edits: an attacker can pull the old encrypted data key from git history, decrypt it with the leaked age key, and use that same data key to open new values added after updatekeys.

    Old commits in git history and old backups also stay decryptable forever. Replacing the age key and running rotate protects only future secrets; you must still reissue every current secret in the services themselves (A secret in a commit).

    What to do after a key leak

    1. Generate a new age key wherever the leak happened — on your workstation (age-keygen) or on the server (/root/.config/sops/age/keys.txt).
    2. Replace the public key in .sops.yaml — remove the compromised key and add the new one.
    3. Update the recipient list in every file under secrets/:
    sops updatekeys -y $(git ls-files secrets)
    1. Rotate the data key one file at a time. Unlike updatekeys, sops rotate -i takes only one file: pass a list and it re-encrypts the first file, exits 0, and leaves the rest on the old data key. Loop over them instead:
    git ls-files secrets | while IFS= read -r f; do
      sops rotate -i "$f"
    done
    1. Replace the secrets in the services themselves — a new ntfy topic, new Healthchecks.io ping URLs, new passwords and API tokens. Write the new values with sops edit, commit, push, and deploy to the server (secrets.sh plus recreating containers that read .env or Docker secrets).

    How to check

    Confirm the old public key no longer appears in .sops.yaml or any file header under secrets/:

    git grep "age1<old-key-prefix>" .sops.yaml secrets/

    The output must be empty.

    Confirm sops rotate -i updated every file, not only the first one:

    git diff --stat secrets/

    Every file under secrets/ must appear in the list. If you test with the old key before deleting it, trying to decrypt any updated file must fail:

    SOPS_AGE_KEY_FILE=/tmp/old-keys.txt sops decrypt secrets/gatus/.env
    # Failed to get the data key required to decrypt the SOPS file

    If it doesn’t work

    Ran sops rotate -i $(git ls-files secrets) with no errors, yet git status shows only one modified file

    Cause
    sops rotate handles a single file per invocation: it silently ignores extra arguments and exits with status 0.
    Fix
    Run sops rotate -i in a loop, one file at a time, and check the full list with git diff --stat secrets/.

    Replaced the key in .sops.yaml, ran sops rotate -i, and the old key is still in the file’s recipients

    Cause
    Without --rm-age and --add-age, sops rotate -i generates a new data key while keeping the existing recipient list from the file itself; it doesn’t re-read .sops.yaml.
    Fix
    Sync recipients with .sops.yaml first using sops updatekeys -y $(git ls-files secrets), then replace the data key in a sops rotate -i loop.

    After replacing the server key, deploy.sh rolls back the commit with identity did not match any of the recipients

    Cause
    The server’s new public key wasn’t added to .sops.yaml, or sops updatekeys wasn’t run after editing .sops.yaml.
    Fix
    Match the Public key: line in /root/.config/sops/age/keys.txt against .sops.yaml, run sops updatekeys -y $(git ls-files secrets) on your workstation, commit, and push.