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:
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 variablesThe 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:
PushSecretwrites 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 linklylistsSecretSyncedor the reason it isn't.
NAME STORETYPE STORE REFRESH INTERVAL STATUS READY
linkly-db ClusterSecretStore vault 15m SecretSynced TrueSealed 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.
kubectl create secret generic linkly-db -n linkly \
--from-literal=password='c0rrect-h0rse' --dry-run=client -o yaml \
| kubeseal --format yaml > linkly-db.sealed.yamlapiVersion: 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-wideandcluster-wideloosen 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-encryptand 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):
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)