External Secrets Operator with Wardn
Let External Secrets Operator create the Secret from your secret store, keep the values out of git and out of Wardn, and still see in Wardn when a secret is missing, stale or has changed.
The situation
payments-api needs a database password and a payment-provider key. They live in your secret store, and the policy is that they are never in git and never in a tool that deploys. External Secrets Operator (ESO) fits: an ExternalSecret object, which holds no value, tells ESO which entries to read from the store and which Kubernetes Secret to write. The ExternalSecret goes in git. The Secret never does.
Wardn's part is narrow, and it is all by reference (Secret references):
| Who | Does |
|---|---|
| Your secret store | Holds the values. |
| ESO, in the cluster | Reads the store, writes the Secret, refreshes it on a timer. |
| Git | Holds the ExternalSecret manifest and the Deployment that reads the Secret. |
| Wardn | Applies the manifests, and has the operator check, from inside the cluster, that the ExternalSecret is syncing and that the Secret exists. It keeps only a fingerprint of the Secret, a keyed hash made in your cluster, so it can say a secret changed without ever seeing it. |
Wardn does not create, rotate or read a secret value, and it does not decrypt anything. There is no SOPS or sealed-secret support.
Repository layout
Settings
Before you start
These are installed once per cluster, outside Wardn. The operator refuses a custom resource whose kind it cannot find, so an ExternalSecret fails to apply if ESO is not there (Connect a cluster).
- External Secrets Operator and its CRDs.
- A store ESO can reach, as a
SecretStorein the namespace or aClusterSecretStore. This recipe assumes aClusterSecretStorenamedpayments-store. Its credentials are ESO's concern, not Wardn's, and not the app's repository.
Vault is a known limit
Wardn can check a vault reference itself when it names a Vault secret store. If you would rather the clusters read Vault, point ESO at it with ESO's Vault provider and declare the reference as eso, as below. Wardn then watches the Kubernetes Secret ESO creates, which is the thing the app reads.
The manifests
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: payments-api
annotations:
wardn.dev/sync-wave: "-5"
spec:
refreshInterval: 15m
secretStoreRef:
name: payments-store
kind: ClusterSecretStore
target:
name: payments-api
data:
- secretKey: DATABASE_PASSWORD
remoteRef:
key: payments/prod/database
property: password
- secretKey: PROVIDER_API_KEY
remoteRef:
key: payments/prod/provider
property: api_keyapiVersion: apps/v1
kind: Deployment
metadata:
name: payments-api
spec:
replicas: 2
selector:
matchLabels:
app: payments-api
template:
metadata:
labels:
app: payments-api
spec:
containers:
- name: payments-api
image: ghcr.io/example/payments-api:1.9.0
ports:
- containerPort: 8080
envFrom:
- secretRef:
name: payments-apiThe annotation applies the ExternalSecret in an early wave so it exists before the pods that read its Secret. It does not make the apply wait until the Secret is written, because an ExternalSecret has no standard readiness. A pod that starts first waits in CreateContainerConfigError and starts by itself when ESO has written the Secret.
Notes on ExternalSecret fields: target.name is the name of the Secret ESO writes (the ExternalSecret's own name if you leave it out), and refreshInterval is also what Wardn uses to judge staleness, one hour if you set none. An ExternalSecret with refreshInterval: 0 syncs once and never goes stale.
Do not commit a Secret
If a Secret with values is in the repository, Wardn renders it into the snapshot like any object. It masks the values in diffs and drift views but it stores them. Only the ExternalSecret belongs in git.
Let the deployment's service account read them
The operator reads the ExternalSecret and its Secret as the deployment's service account, wardn-deployer, never as itself. The built-in admin role covers Secrets but not necessarily ExternalSecret objects, so grant both, narrowed to names:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: wardn-secret-refs
namespace: payments
rules:
- apiGroups: ["external-secrets.io"]
resources: ["externalsecrets"]
resourceNames: ["payments-api"]
verbs: ["get"]
- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["payments-api"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: wardn-secret-refs
namespace: payments
roleRef: { apiGroup: rbac.authorization.k8s.io, kind: Role, name: wardn-secret-refs }
subjects:
- { kind: ServiceAccount, name: wardn-deployer, namespace: payments }Without them the reference is missing, with the detail The deployment's service account may not read the ExternalSecret. or ... the Secret.
Declare the reference
A deployment lists the secrets it depends on, by name. The list replaces the previous one, and a request never carries a value. The web app does not edit references yet, so this is the API:
api() { curl -fsS -H "Authorization: Bearer $WARDN_TOKEN" -H 'Content-Type: application/json' "$@"; }
api -X PUT "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/secret-refs" -d '{
"refs": [
{"name": "database-password", "source": "eso", "locator": "payments-api", "key": "DATABASE_PASSWORD"},
{"name": "provider-key", "source": "eso", "locator": "payments-api", "key": "PROVIDER_API_KEY"}
]
}'
api -X POST -H "Idempotency-Key: $(uuidgen)" "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/sync"locator is the ExternalSecret's name, or namespace/name; without a namespace it means the deployment's. key narrows a reference to one key of the Secret, so one fingerprint follows one value; leave it out and the fingerprint covers every key. Declaring needs deployment:manage. References reach the operator with the next apply, so the Sync now call is part of the step.
Check that it worked
api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/secret-refs" \
| jq '.refs[] | {name, source, status, fingerprint, checks: [.checks[] | {reporter, status, detail}]}'| Status | Meaning here |
|---|---|
present | The ExternalSecret is Ready, refreshed within two intervals, and its Secret has the key. The fingerprint is set. |
missing | The ExternalSecret does not exist, has not created its Secret, the Secret lacks the key, or nothing has checked yet. The detail says which. |
stale | The Secret exists, but the ExternalSecret has not reported a sync, is not Ready, or has not refreshed within two of its refresh intervals. The fingerprint is still the Secret that is there. |
The operator checks again after each apply and about every five minutes. A deployment on an environment is checked on each cluster, and the reference's status is the worst of them.
See a change
Rotate a value in the store. After ESO's next refresh the Secret changes, and at the operator's next check the fingerprint in the list above is a different value. Wardn shows the change in three places, none of which show a value:
- The next snapshot. Push a commit, or choose Sync now. The deployment's snapshot history lists the secrets that changed fingerprint since the snapshot before, with the old and new fingerprint (
secretson aGET /deployments/{id}/historyevent). The snapshot's page shows each reference under Settings and secrets, marked Fingerprints only. - A restore. Before you go back to an older snapshot, the preflight compares the secret now with what the older snapshot ran with: Secret changed since this snapshot ran is a warning, and Secret missing blocks the restore (Restore and rollback).
- A failing sync. If ESO cannot reach the store, the
ExternalSecretstops beingReadyand the reference turnsstale, with the reason in the check'sdetail. The Secret that is there keeps its fingerprint, and the app keeps running on it. Nothing is applied or reverted because of it.
api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/secret-refs/preflight?snapshotId=$SNAPSHOT_ID" | jq .problemsWardn does not restart pods when a Secret changes: the Deployment above reads the Secret through envFrom, which a pod reads when it starts. Mount the Secret as files, or restart the pods yourself, if the app must pick up a new value without a deploy.
Next
Secret references
Every source, status and permission, and how the fingerprint is made.
Restore and rollback
How the preflight uses a secret's fingerprint before it goes back.
Run a database migration before an apply
A job that reads the same Secret without it being in git.
Related: Security model, Jobs and waves, Connect a cluster.
Ignore fields an autoscaler owns
Keep the replica count out of drift when a HorizontalPodAutoscaler sets it, and know exactly when that needs no rule and when it needs an ignore rule.
Deploy from CI with an API token
Make an API token that can only read one environment, and a CI job that waits until a commit is running there and healthy, with a complete script.