Securing GitOps with Mozilla SOPS and age: Cloud-Agnostic Secret Encryption Guide

DevOps tutorial - IT technology blog
DevOps tutorial - IT technology blog

Quick start (5 min)

Storing Kubernetes secrets in Git usually forces an ugly tradeoff. Base64 encoding offers zero real protection. On the other hand, fetching secrets at runtime from cloud vaults adds network latency, cloud lock-in, and monthly API bills. Mozilla SOPS paired with age fixes this. Your secrets stay encrypted in Git, right next to the workloads that consume them.

Here is how to build a working proof of concept in five minutes.

1. Install the binaries

Install both tools via Homebrew or grab the official GitHub release binaries:

# On macOS or Linux via Homebrew
brew install sops age

# Or download Linux binaries directly
curl -LO https://github.com/getsops/sops/releases/download/v3.9.4/sops-v3.9.4.linux.amd64
sudo mv sops-v3.9.4.linux.amd64 /usr/local/bin/sops
sudo chmod +x /usr/local/bin/sops

curl -LO https://github.com/FiloSottile/age/releases/download/v1.2.1/age-v1.2.1-linux-amd64.tar.gz
tar -xzf age-v1.2.1-linux-amd64.tar.gz
sudo mv age/age age/age-keygen /usr/local/bin/

2. Generate your age key pair

Create a dedicated directory and generate your private identity file:

mkdir -p ~/.config/sops/age
age-keygen -o ~/.config/sops/age/keys.txt

# Extract your public key recipient (starts with age1...)
age-keygen -y ~/.config/sops/age/keys.txt

Copy the public recipient address (for example, age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p). Then tell SOPS where to find your private key:

export SOPS_AGE_KEY_FILE="$HOME/.config/sops/age/keys.txt"

3. Encrypt and decrypt a manifest

Save a standard Kubernetes Secret manifest as secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: app-database-credentials
  namespace: default
type: Opaque
stringData:
  DB_USERNAME: app_user
  DB_PASSWORD: SuperSecretPassword123!

Run SOPS with your age recipient to encrypt the file in place:

sops --encrypt \
  --age age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p \
  --in-place secret.yaml

Open secret.yaml. The metadata, keys, and structure remain completely visible, while sensitive field values turn into ciphertext. Decrypting it back to standard output takes one quick command:

sops --decrypt secret.yaml

Deep dive

Unencrypted secrets committed to private Git repositories account for a huge share of leaked production credentials. Cloud vaults like AWS Secrets Manager solve this, but they get expensive quickly. AWS charges $0.40 per secret per month plus $0.05 per 10,000 API calls. If you run 20 microservices across staging and production clusters, secret retrieval calls can silently rack up hundreds of dollars each month.

We switched to SOPS and age across 40+ bare-metal and hybrid Kubernetes clusters. The setup has run without incident for two years, cut API sync latency to zero, and eliminated cloud vault charges entirely.

Why SOPS and age complement GitOps

Legacy tools like GPG or raw OpenSSL treat files as opaque blobs. If you run gpg -c secret.yaml, Git sees only scrambled binary data. Reviewing pull requests becomes impossible. Resolving merge conflicts turns into a nightmare. Worst of all, cluster operators cannot inspect metadata without decrypting the entire file first.

Mozilla SOPS takes a structured approach. It parses YAML, JSON, ENV, and INI files directly. SOPS leaves keys, comments, and top-level metadata untouched, encrypting only leaf values. Reviewers can easily verify whether an engineer changed a replica count or rotated an API key.

Meanwhile, age replaces clunky GPG setups. Filippo Valsorda designed age around modern cryptography like X25519 and ChaCha20-Poly1305. It needs no gnupg daemons, no key-signing parties, and no complex configuration files. Just clean, 62-character public keys.

The envelope encryption model

SOPS uses envelope encryption rather than locking the entire document with your age key:

  1. SOPS generates a unique 256-bit Data Encryption Key (DEK).
  2. The payload values in the YAML file are encrypted using this DEK with AES-256-GCM.
  3. The DEK itself is encrypted with each recipient’s age public key and stored in the sops.age metadata stanza.
  4. During decryption, the client uses their private age key to unlock the DEK, which then decrypts the field values.

Advanced usage

Typing CLI recipient flags manually breaks down fast once you manage dozens of microservices. Instead, add a .sops.yaml file at the root of your repository to automatically route encryption rules by path.

Multi-environment routing via .sops.yaml

Drop a .sops.yaml configuration file in your repo root. You can bind specific environments to developers, platform leads, and automated CI deployment bots:

creation_rules:
  # Staging environment: accessible by dev team and staging CI runner
  - path_regex: environments/staging/.*\.enc\.ya?ml$
    age: >-
      age1stagingdevteamkey99283742837492837492837492837492837492837,
      age1stagingcibotkey10293840192830192830192830192830192830192

  # Production environment: strictly guarded production keys
  - path_regex: environments/production/.*\.enc\.ya?ml$
    age: >-
      age1prodleadkey8839201928301928301928301928301928301928301928,
      age1prodcibotkey772910293840192830192830192830192830192830192

  # Fallback rule for shared cluster utilities
  - path_regex: infrastructure/.*\.enc\.ya?ml$
    age: age1infraadminskey33829102938401928301928301928301928301928

Once saved, you can drop the recipient flags completely. SOPS matches the file path automatically:

sops --encrypt --in-place environments/staging/secrets.enc.yaml

Direct integration with FluxCD

GitOps engines like FluxCD decrypt manifests directly inside your cluster. Store the private age identity key in a Kubernetes Secret, and Flux handles decryption automatically during reconciliation:

# Inject the private key into the flux-system namespace
kubectl create secret generic sops-age \
  --namespace=flux-system \
  --from-file=identity.agekey=$HOME/.config/sops/age/keys.txt

Tell your Flux Kustomization to decrypt files with that key before applying manifests:

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: production-workloads
  namespace: flux-system
spec:
  interval: 10m
  path: ./environments/production
  prune: true
  sourceRef:
    kind: GitRepository
    name: fleet-infra
  decryption:
    provider: sops
    secretRef:
      name: sops-age

Practical tips

Tip 1: Edit files safely with in-place sessions

Never write decrypted secrets to disk when editing passwords. Plaintext temp files often get committed by mistake. Use SOPS in edit mode instead:

sops environments/staging/secrets.enc.yaml

SOPS spawns your default $EDITOR, holds the decrypted text purely in RAM, and re-encrypts the manifest the moment you write and exit.

Tip 2: Enforce pre-commit safety guards

Accidents happen. Prevent developers from committing unencrypted secret manifests by adding a pre-commit hook to .pre-commit-config.yaml:

repos:
  - repo: https://github.com/getsops/sops
    rev: v3.9.4
    hooks:
      - id: sops-encryption
        files: .*\.enc\.ya?ml$

The hook halts git commits if any target YAML file lacks a valid SOPS encryption stanza.

Tip 3: Rotate recipient keys without retyping secrets

When an engineer leaves your team, revoke their access immediately. You do not need to re-enter sensitive passwords. Update the recipient list in .sops.yaml and run:

sops updatekeys environments/production/database-secrets.enc.yaml

SOPS reads the file, unlocks the DEK with your remaining private key, re-encrypts that DEK for the updated recipients, and saves the new header. The underlying secret values remain untouched.

Tip 4: Keep raw age identities out of environment variables

While SOPS_AGE_KEY accepts a raw private key string, do not hardcode it into CI pipeline files. Inject the key into runner containers using masked secrets, write it to a tmpfs RAM disk, point SOPS_AGE_KEY_FILE to that mount, and scrub the file as soon as reconciliation finishes.

Share: