Skip to content

Deploying SOPS + age + Flux for GitOps secrets with KMS-backed encryption

Install Flux on a Kubernetes cluster with SOPS decryption, use age keypairs for developer workflow and AWS KMS for production, and commit encrypted Secret manifests to git that Flux decrypts on reconcile.

You moved a customer to GitOps last quarter — Flux reconciles every manifest in the repo and the cluster state matches the branch. Three weeks later the lead developer messages you: “where do I put the database password?” The honest answer for most teams is “in a Secret you applied by hand outside the pipeline” — which is exactly the configuration drift GitOps was supposed to eliminate. The fix is to encrypt the secret file with SOPS before committing, give Flux a decryption key, and let the reconciler do exactly what it does with every other manifest.

What’s happening

SOPS (Secrets OPerationS) is a file-level encryption tool from Mozilla that encrypts values inside structured files (YAML, JSON, env, ini) while leaving keys in cleartext. The resulting file is committable to git — a reviewer can see that wordpress.replicas changed without ever decrypting wordpress.db_password. SOPS supports multiple key backends: age (a modern asymmetric cipher with one-line public/private keys, ideal for developer workstations), PGP (legacy, painful), AWS KMS, GCP KMS, Azure Key Vault, and HashiCorp Vault. A .sops.yaml file at the repo root maps file patterns to recipients — typically “everything in clusters/production/secrets/ is encrypted with both the production age key and the production KMS key.”

Flux’s kustomize-controller reads a decryption.provider: sops block on the Kustomization resource and a decryption.secretRef pointing at a Secret in the cluster that holds the age private key or KMS access. On reconcile, kustomize-controller calls sops --decrypt on each manifest, applies the result, and never persists the decrypted form. The developer flow: sops -e secret.yaml > secret.sops.yaml, commit, push. The cluster flow: Flux pulls the repo, decrypts, applies. The hybrid setup below uses age keys for the developer’s local encrypt + decrypt cycle and AWS KMS as the second recipient on production secrets so the in-cluster Secret never needs an age private key — kustomize-controller authenticates to KMS via IRSA when on EKS or via a node IAM role on EC2 instances.

How to verify

After install, Flux controllers should be Running and the SOPS-enabled Kustomization should reconcile encrypted files:

flux check
# ► checking prerequisites
# ✔ Kubernetes 1.30.4 >=1.28.0-0
# ► checking version in cluster
# ✔ distribution: flux-v2.3.0
# ✔ bootstrapped: true
# ► checking controllers
# ✔ source-controller: deployment ready
# ✔ kustomize-controller: deployment ready
# ✔ helm-controller: deployment ready
# ✔ notification-controller: deployment ready

flux get kustomizations
# NAME            REVISION                SUSPENDED       READY   MESSAGE
# flux-system     main@sha1:abc123        False           True    Applied revision
# apps            main@sha1:abc123        False           True    Applied revision

Confirm a SOPS-encrypted Secret committed in git was decrypted into a live cluster Secret:

cat clusters/production/secrets/wordpress-db.sops.yaml
# apiVersion: v1
# kind: Secret
# metadata:
#   name: wordpress-db
# stringData:
#   password: ENC[AES256_GCM,data:...,iv:...,tag:...,type:str]

kubectl -n wordpress get secret wordpress-db \
  -o jsonpath='{.data.password}' | base64 -d
# (the plaintext password — decryption succeeded)

If the decryption fails, kustomize-controller logs unable to read OpenPGP keys from ... or unable to decrypt, and flux get kustomizations shows ReconciliationFailed.

The fix

Save the script as /root/install-sops-flux.sh and run on a node with kubectl, helm, and flux CLI. It performs the full installer: SOPS 3.9 + age 1.2 CLI install, generation of an age keypair, generation of an AWS KMS key via Terraform-equivalent CLI calls (you supply the AWS profile), flux bootstrap github against a private repo, kustomize-controller patched with --decryption-provider=sops and access to both age and KMS, an in-cluster Secret holding the age private key for the developer-workflow path, a sample .sops.yaml repo policy file, and a sample encrypted Secret that round-trips through SOPS.

#!/usr/bin/env bash
set -euo pipefail

### Kubernetes 1.30 — install Flux 2.3 with SOPS (age + AWS KMS) decryption
### Assumes: kubectl works, helm available, GH_TOKEN exported, AWS profile configured

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

# === EDIT ===
FLUX_VERSION="2.3.0"
SOPS_VERSION="3.9.0"
AGE_VERSION="1.2.0"

GITHUB_OWNER="acme-co"
GITHUB_REPO="acme-platform"
GITHUB_BRANCH="main"
CLUSTER_PATH="clusters/production"

AWS_REGION="us-east-1"
AWS_KMS_KEY_ALIAS="alias/flux-sops-prod"
# ============

if [[ -z "${GH_TOKEN:-}" ]]; then
  echo "GH_TOKEN env not set"; exit 1
fi

echo
echo "========================================"
echo "Flux 2.3 + SOPS (age + KMS) installer"
echo "========================================"
echo

echo "==> Install age ${AGE_VERSION}"
ARCH="$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')"
curl -fsSL "https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-linux-${ARCH}.tar.gz" \
  -o /tmp/age.tar.gz
tar -C /tmp -xzf /tmp/age.tar.gz
install -m 0755 /tmp/age/age /usr/local/bin/age
install -m 0755 /tmp/age/age-keygen /usr/local/bin/age-keygen

echo "==> Install SOPS ${SOPS_VERSION}"
curl -fsSL "https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.linux.${ARCH}" \
  -o /usr/local/bin/sops
chmod 0755 /usr/local/bin/sops
sops --version

echo "==> Install flux CLI"
if ! command -v flux >/dev/null; then
  curl -s https://fluxcd.io/install.sh | bash
fi
flux --version

echo "==> Generate age keypair for prod-developer workflow"
mkdir -p /etc/sops-age
chmod 700 /etc/sops-age
if [[ ! -f /etc/sops-age/age.key ]]; then
  age-keygen -o /etc/sops-age/age.key 2>/dev/null
  chmod 600 /etc/sops-age/age.key
fi

AGE_PUBLIC="$(grep '^# public key:' /etc/sops-age/age.key | awk '{print $4}')"
echo "Age public key: ${AGE_PUBLIC}"

echo "==> Ensure AWS KMS key exists"
if ! aws --region "${AWS_REGION}" kms describe-key \
    --key-id "${AWS_KMS_KEY_ALIAS}" >/dev/null 2>&1; then
  KEY_ID="$(aws --region "${AWS_REGION}" kms create-key \
    --description "Flux SOPS production secrets" \
    --key-usage ENCRYPT_DECRYPT \
    --key-spec SYMMETRIC_DEFAULT \
    --query 'KeyMetadata.KeyId' --output text)"
  aws --region "${AWS_REGION}" kms create-alias \
    --alias-name "${AWS_KMS_KEY_ALIAS}" \
    --target-key-id "${KEY_ID}"
  aws --region "${AWS_REGION}" kms enable-key-rotation --key-id "${KEY_ID}"
fi
KMS_ARN="$(aws --region "${AWS_REGION}" kms describe-key \
  --key-id "${AWS_KMS_KEY_ALIAS}" \
  --query 'KeyMetadata.Arn' --output text)"
echo "KMS key ARN: ${KMS_ARN}"

echo "==> flux bootstrap github"
export GITHUB_TOKEN="${GH_TOKEN}"
flux bootstrap github \
  --owner="${GITHUB_OWNER}" \
  --repository="${GITHUB_REPO}" \
  --branch="${GITHUB_BRANCH}" \
  --path="${CLUSTER_PATH}" \
  --personal=false \
  --private=true \
  --components-extra=image-reflector-controller,image-automation-controller

echo "==> Wait for Flux controllers"
kubectl -n flux-system rollout status deploy/kustomize-controller --timeout=5m
kubectl -n flux-system rollout status deploy/source-controller --timeout=5m

echo "==> Create in-cluster Secret with age private key"
kubectl -n flux-system create secret generic sops-age \
  --from-file=age.agekey=/etc/sops-age/age.key \
  --dry-run=client -o yaml | kubectl apply -f -

echo "==> Patch kustomize-controller for KMS via IRSA / node role"
# Service account annotation for IRSA (no-op if not on EKS)
kubectl -n flux-system annotate serviceaccount kustomize-controller \
  eks.amazonaws.com/role-arn=arn:aws:iam::000000000000:role/flux-sops \
  --overwrite || true

# Add AWS region env var; kustomize-controller uses default credential chain
kubectl -n flux-system patch deploy kustomize-controller --type='json' -p='[
  {"op":"add","path":"/spec/template/spec/containers/0/env/-","value":{"name":"AWS_REGION","value":"'"${AWS_REGION}"'"}},
  {"op":"add","path":"/spec/template/spec/containers/0/env/-","value":{"name":"AWS_SDK_LOAD_CONFIG","value":"true"}}
]'

kubectl -n flux-system rollout status deploy/kustomize-controller --timeout=3m

echo "==> Write .sops.yaml policy into repo (instructions, no auto-push)"
mkdir -p /tmp/sops-repo-template
cat > /tmp/sops-repo-template/.sops.yaml <<EOF
creation_rules:
  # All Kubernetes secret manifests under clusters/production/secrets/
  - path_regex: clusters/production/secrets/.*\.ya?ml$
    encrypted_regex: '^(data|stringData)$'
    age: ${AGE_PUBLIC}
    kms: ${KMS_ARN}
  # Developer-only secrets in clusters/staging — age recipient only
  - path_regex: clusters/staging/secrets/.*\.ya?ml$
    encrypted_regex: '^(data|stringData)$'
    age: ${AGE_PUBLIC}
EOF

echo "==> Sample Kustomization with sops decryption"
cat > /tmp/sops-repo-template/secrets-kustomization.yaml <<EOF
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: secrets
  namespace: flux-system
spec:
  interval: 5m
  path: ./${CLUSTER_PATH}/secrets
  prune: true
  sourceRef:
    kind: GitRepository
    name: flux-system
  decryption:
    provider: sops
    secretRef:
      name: sops-age
EOF

echo "==> Sample encrypted Secret round-trip"
cat > /tmp/wp-db.plain.yaml <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: wordpress-db
  namespace: wordpress
type: Opaque
stringData:
  password: "hunter2-replace-me"
  host: "10.10.0.42"
EOF

SOPS_AGE_RECIPIENTS="${AGE_PUBLIC}" \
  sops --encrypt \
       --encrypted-regex '^(data|stringData)$' \
       --age "${AGE_PUBLIC}" \
       --kms "${KMS_ARN}" \
       --output /tmp/wp-db.sops.yaml \
       /tmp/wp-db.plain.yaml

echo "==> Encrypted Secret preview:"
head -30 /tmp/wp-db.sops.yaml

echo "==> Local decrypt round-trip"
SOPS_AGE_KEY_FILE=/etc/sops-age/age.key \
  sops --decrypt /tmp/wp-db.sops.yaml | head -20

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Flux:"
flux get all -A | head -40
echo
echo "What to commit to ${GITHUB_OWNER}/${GITHUB_REPO}:"
echo "  - /tmp/sops-repo-template/.sops.yaml"
echo "  - /tmp/sops-repo-template/secrets-kustomization.yaml under ${CLUSTER_PATH}/"
echo "  - /tmp/wp-db.sops.yaml under ${CLUSTER_PATH}/secrets/"
echo
echo "Developer workflow:"
echo "  export SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt"
echo "  sops -e secret.yaml > secret.sops.yaml && git add . && git commit"
echo
echo "Age public recipient: ${AGE_PUBLIC}"
echo "KMS key ARN:          ${KMS_ARN}"
echo "Age private key (in-cluster): kube-system/sops-age   (keep off-cluster backup encrypted)"

Make it executable and run with the right env:

chmod 700 /root/install-sops-flux.sh
export GH_TOKEN=ghp_...
export AWS_PROFILE=acme-platform
/root/install-sops-flux.sh

The developer workflow has three rules. First, never put a plaintext Secret in the path matched by .sops.yaml — pre-commit hooks should fail the commit. Use the sops-pre-commit hook or the simpler git secrets --register-aws. Second, always specify both recipients in creation_rules — age for the developer’s local edit, KMS for the cluster’s decrypt. If you encrypt for one recipient only, the other path can’t decrypt and you’ll find out at the next reconcile, not at commit time. Third, when an age key is leaked or a developer leaves, rotate by adding the new recipient, decrypting and re-encrypting every file matched by that pattern (sops updatekeys does this in place), and rotating the in-cluster Secret. KMS rotation is the AWS side — aws kms enable-key-rotation once, then it rotates the underlying key material yearly without changing the alias.

For Kubernetes clusters under Managed Cloud Ops, we run Flux with SOPS, KMS-backed encryption, and the developer onboarding workflow as part of the standard GitOps build. If you’re trying to retrofit GitOps onto a cluster that’s already in production, scope the migration with us.