TExcelTAD
Home › Secrets & Policy as Code: Vault, SOPS, OPA & Conftest › External Secrets & Sealed Secrets

External Secrets & Sealed Secrets

A Kubernetes Secret is base64, not encryption (Secrets), so it can't go into a Git repository as is. Besides SOPS, two operators solve "how does the secret get into the cluster?" for GitOps setups. Secrets in GitOps compares the three options; this lesson goes deeper on the two operators, wires External Secrets to Vault, and adds a CI check that keeps plain Secrets out of the repository.

Note: both operators run in a cluster, so their manifests and commands are non-run samples. The policy check at the end is a real session.

External Secrets Operator with Vault

ESO's ClusterSecretStore describes how to reach a store, here Vault with Kubernetes auth (Kubernetes Auth); each ExternalSecret maps keys in the store to a Kubernetes Secret and refreshes it:

yaml
apiVersion: external-secrets.io/v1
kind: ClusterSecretStore
metadata:
  name: vault
spec:
  provider:
    vault:
      server: https://vault.acme.internal:8200
      path: secret
      version: v2
      auth:
        kubernetes:
          mountPath: kubernetes
          role: external-secrets
          serviceAccountRef:
            name: external-secrets
            namespace: external-secrets
---
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
  name: linkly-db
  namespace: linkly
spec:
  refreshInterval: 15m
  secretStoreRef:
    kind: ClusterSecretStore
    name: vault
  target:
    name: linkly-db
    template:
      data:
        DATABASE_URL: "postgres://{{ .username }}:{{ .password }}@postgres:5432/linkly"
  dataFrom:
    - extract:
        key: linkly/db          # all keys of secret/data/linkly/db become template variables

The Deployment references secretKeyRef: {name: linkly-db, key: DATABASE_URL} and never knows Vault exists. Rotating the password in Vault reaches the Secret within refreshInterval; restarting pods to pick it up is a separate step (Reloader, or reading a mounted file instead of an environment variable).

Useful extras:

  • PushSecret writes a Kubernetes Secret to a store (e.g. a cert-manager certificate into Vault).
  • Generators (Password, ECRAuthorizationToken, VaultDynamicSecret, …) create values instead of reading them.
  • Status shows sync errors: kubectl get externalsecret -n linkly lists SecretSynced or the reason it isn't.
text
NAME        STORETYPE            STORE   REFRESH INTERVAL   STATUS         READY
linkly-db   ClusterSecretStore   vault   15m                SecretSynced   True

Sealed Secrets

The Sealed Secrets controller holds a key pair in the cluster. kubeseal encrypts a Secret with the public key into a SealedSecret, which is safe to commit; only the controller can decrypt it and create the real Secret.

bash
kubectl create secret generic linkly-db -n linkly \
  --from-literal=password='c0rrect-h0rse' --dry-run=client -o yaml \
  | kubeseal --format yaml > linkly-db.sealed.yaml
yaml
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
  name: linkly-db
  namespace: linkly
spec:
  encryptedData:
    password: AgB4k2…   # RSA-OAEP + AES-GCM, bound to name and namespace
  template:
    metadata:
      name: linkly-db
      namespace: linkly
  • Scopes: by default (strict) the ciphertext is bound to the Secret's name and namespace, so it can't be copied into another namespace to be decrypted there. namespace-wide and cluster-wide loosen that.
  • Key renewal: the controller adds a new sealing key every 30 days by default and keeps the old ones, so existing SealedSecrets still decrypt. Renewal isn't rotation: re-seal with kubeseal --re-encrypt and change the real secret values when needed.
  • Disaster recovery: back up the controller's private keys (kubectl get secret -n kube-system -l sealedsecrets.bitnami.com/sealed-secrets-key -o yaml), encrypted, outside the cluster. Lose them and every SealedSecret in Git is unreadable.
  • Each cluster has its own keys: multi-cluster setups re-seal per cluster, or share keys deliberately.

Choosing

SOPS Sealed Secrets External Secrets / VSO
Git contains encrypted values encrypted values references only
Source of truth Git Git the external store
Rotation edit + commit re-seal + commit in the store; synced automatically
Extra infrastructure keys or KMS a controller a store (Vault, cloud) + an operator
Works outside Kubernetes yes no the store does

A gate against plain Secrets in Git

Whatever you choose, a CI check should reject plain kind: Secret manifests with data, while allowing SOPS-encrypted ones. That is a policy, so it is written in Rego and run with Conftest (explained from Policy as Code onwards):

bash · try it yourself
mkdir -p policy deploy
cat > policy/secrets.rego <<'REGO'
package main

sops_encrypted if input.sops.mac

deny contains msg if {
	input.kind == "Secret"
	not sops_encrypted
	count(object.get(input, "data", {})) + count(object.get(input, "stringData", {})) > 0
	msg := sprintf("Secret %s contains plain values: use an ExternalSecret, a SealedSecret or SOPS", [input.metadata.name])
}
REGO
cat > deploy/external.yaml <<'YAML'
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata: {name: linkly-db}
spec:
  secretStoreRef: {kind: ClusterSecretStore, name: vault}
  target: {name: linkly-db}
  dataFrom: [{extract: {key: linkly/db}}]
YAML
cat > deploy/oops.yaml <<'YAML'
apiVersion: v1
kind: Secret
metadata: {name: linkly-smtp}
stringData: {password: hunter2}
YAML
umask 077; age-keygen -o key.txt 2> /dev/null
printf 'apiVersion: v1\nkind: Secret\nmetadata: {name: linkly-api}\nstringData: {token: abc}\n' > deploy/encrypted.yaml
sops encrypt --age "$(age-keygen -y key.txt)" --encrypted-regex '^(data|stringData)
  

 -i deploy/encrypted.yaml
conftest test deploy/
FAIL - deploy/oops.yaml - main - Secret linkly-smtp contains plain values: use an ExternalSecret, a SealedSecret or SOPS

3 tests, 2 passed, 0 warnings, 1 failure, 0 exceptions

Test yourself

With External Secrets Operator, where is the secret value's source of truth?

  1. The Git repository
  2. The external store (Vault, a cloud secrets manager); Git holds only a reference
  3. The ExternalSecret object
  4. etcd only

What happens if the Sealed Secrets controller's private keys are lost?

  1. kubeseal regenerates them
  2. No SealedSecret in Git can be decrypted any more; back the keys up
  3. Secrets fall back to plain text
  4. Nothing, the public key is enough

By default, can a SealedSecret for namespace linkly be applied in namespace dev to get the value?

  1. Yes
  2. No: the strict scope binds the ciphertext to the name and namespace
  3. Only with kubectl --force
  4. Only for cluster admins