A Kubernetes Secret is protected by nothing beyond what you configure
around it: by default, the API server (the component that receives every
request of the cluster) writes it in plain text into etcd (the database
where the cluster keeps its state), and any account with the get permission
on secrets reads it. This lesson, for someone who administers or deploys on a
cluster, sets up the three protections of a real cluster: encryption at
rest in etcd, an RBAC setup that limits reading, and volume mounting.
It then looks at the external tools (External Secrets Operator, CSI Secrets
Store, Sealed Secrets, SOPS) and at the criterion that lets you choose among
them.
What you will learn
- Prove that a Secret is readable in plain text in etcd, then enable encryption at rest and see the change
- Limit the reading of a Secret to one specific ServiceAccount and check it with
kubectl auth can-i - Reduce exposure in the Pod: a volume rather than an environment variable, no privileged Pod
- Sync a Secret from an external secret store with External Secrets Operator
- Decide when to choose CSI Secrets Store, Sealed Secrets or SOPS, and recognize what each protection does not cover
- Meet the requirements SOCLE-SEC-STO-1 (in French) (secrets encrypted at rest), SOCLE-SEC-GEN-3 (in French) (scoped access; tracing belongs to auditing) and SOCLE-CLD-K8S-10 (in French) (external secret store and mounting as files; the rotation it requires stays outside this lesson)
Prerequisites
This lesson builds on Secrets, which explains what a Secret is, why base64 is only an encoding, the Secret types and injection through a variable or a volume. It does not repeat them. You must also have read Kubernetes RBAC (roles and bindings) and know what etcd is.
To replay the steps, you need Docker, kind 0.33 or later,
kubectl and helm. Helm is installed in the section
How do you install Helm 4?
of the lesson "Mastering the Helm command", in the "Package and deliver" module.
Reading etcd with kubectl exec and etcdctl is the method of the etcd lesson.
The test cluster is disposable and is deleted at the end.
Where is a Secret exposed, and who protects it?
A Secret goes through four links between its creation and its use: the API server, etcd, the kubelet (the agent of each node) and the Pod. Each protection covers only some of them. The diagram follows this path: the API server filters readers with RBAC, writes the value into etcd (this is where encryption at rest acts), then the kubelet mounts it in the Pod as tmpfs, a filesystem held in memory. The external secret store, shown dashed, has two entry points: External Secrets Operator feeds the API server in place of a manual entry, while the CSI Driver mounts the value directly in the Pod, without going through the API server or etcd.
Base64 protects none of these links: it decodes in one command, as the "Secrets" lesson shows. One link is easy to forget: the etcd backup contains every Secret, and whoever reads it gets them without going through the API. Encryption at rest is the answer to that case.
How do you encrypt Secrets at rest in etcd?
Encryption at rest asks the API server to encrypt each Secret before
writing it into etcd. It is declared in an EncryptionConfiguration file that
the API server reads at startup. The providers list is ordered: the first
provider encrypts new writes, all of them serve for reading back. That is
what lets you move from one state to another without breaking existing Secrets.
The steps below create a kind cluster whose API server reads a configuration
file that does not yet encrypt anything (identity first, meaning no
encryption), observe the leak, then switch to secretbox, an
authenticated encryption from the NaCl library. Other providers exist, such as
aescbc: the provider order and the rewriting of Secrets are the same whichever
one you choose.
Create the test cluster
Work in an empty folder and keep the same terminal until the end: the KEY
variable and the etcd_get function defined below exist only in it. The
32-byte key is generated locally, and the file is mounted in the control
plane node. The option encryption-provider-config-automatic-reload avoids
restarting the API server when the file changes: without it, the change
would only be read at the next startup.
mkdir secrets-lab && cd secrets-labKEY=$(head -c 32 /dev/urandom | base64)
cat > enc.yaml <<EOFapiVersion: apiserver.config.k8s.io/v1kind: EncryptionConfigurationresources: - resources: - secrets providers: - identity: {} - secretbox: keys: - name: key1 secret: $KEYEOF
cat > kind.yaml <<EOFkind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes: - role: control-plane extraMounts: - hostPath: $PWD/enc.yaml containerPath: /etc/kubernetes/enc/encryption-config.yaml kubeadmConfigPatches: - | kind: ClusterConfiguration apiServer: extraArgs: - name: encryption-provider-config value: /etc/kubernetes/enc/encryption-config.yaml - name: encryption-provider-config-automatic-reload value: "true" extraVolumes: - name: enc hostPath: /etc/kubernetes/enc mountPath: /etc/kubernetes/enc readOnly: true pathType: DirectoryOrCreateEOF
kind create cluster --name sec-lab --config kind.yaml \ --image kindest/node:v1.37.0@sha256:a1ed56cfb0e7b93589bdf97c8cd566405a265939e3620fc4f5de89adff580ae5The cluster is created when kind, near the end of its output, suggests
trying the command kubectl cluster-info --context kind-sec-lab. If
creation fails with an error about kubeadm.k8s.io/v1beta3, your kind is too old:
kind version must return 0.33 or later (the v1beta4 format is handled
from 0.32, but 0.33 is the release that publishes the v1.37.0 image used
here), and the image digest guarantees that you replay the same node.
See that the value is readable in etcd
Create a Secret, then read it directly in etcd with etcdctl, the etcd
client tool, present in the etcd-sec-lab-control-plane Pod. The function
below avoids repeating the three certificates that etcdctl requires.
kubectl create secret generic db-credentials \ --from-literal=password='S3cr3t-Zeta'
etcd_get() { kubectl -n kube-system exec etcd-sec-lab-control-plane -- etcdctl \ --cacert=/etc/kubernetes/pki/etcd/ca.crt \ --cert=/etc/kubernetes/pki/etcd/server.crt \ --key=/etc/kubernetes/pki/etcd/server.key \ get "$1" --print-value-only}
etcd_get /registry/secrets/default/db-credentials | grep -a -o 'S3cr3t-Zeta'S3cr3t-Zetaetcd returns a binary record, not text. The -a option of grep reads it as
text anyway, and -o prints only the part that matches the pattern, not the
whole unreadable line.
The password appears as is. Whoever reads the control plane node's disk, or an etcd backup, gets it without going through the API or through RBAC.
Switch to secretbox
Rewrite the file with secretbox first: it will encrypt new writes,
while identity stays last to read back the old values that are still in
plain text. Use cat > (which writes into the same file) and not mv: the
node mounted that exact file, and a file replaced by another is not seen from
inside the container.
cat > enc.yaml <<EOFapiVersion: apiserver.config.k8s.io/v1kind: EncryptionConfigurationresources: - resources: - secrets providers: - secretbox: keys: - name: key1 secret: $KEY - identity: {}EOFThe API server reloads the file by itself, but not instantly: it rereads it
every minute, so wait two minutes. Then create a second Secret and look
at its prefix in etcd, which names the provider and the key used.
head -c 26 keeps only the first 26 bytes, the length of the prefix.
tr -c '[:print:]' '.' replaces with a dot everything that is not printable, so
the output stays readable: the -c of tr means "complement" (every character
except those in the list) and has nothing to do with the one in head.
LC_ALL=C makes it handle bytes one by one, which the macOS tr requires.
sleep 120kubectl create secret generic api-token --from-literal=token='Tok-Omega-42'etcd_get /registry/secrets/default/api-token | head -c 26 | LC_ALL=C tr -c '[:print:]' '.'; echok8s:enc:secretbox:v1:key1:The prefix k8s:enc:secretbox:v1:key1: proves that the value is encrypted.
If you still read k8s followed by dots and the word Secret, for example
k8s.....v1..Secret........, the old state is still active: wait and
create a Secret again.
Encrypt the Secrets already present
The change only affects new writes. db-credentials, created earlier, is
still in plain text in etcd. You must rewrite it so that it goes through
the new provider: reading all the Secrets and then replacing them is enough,
and it is the method given by the Kubernetes documentation.
kubectl get secrets -A -o json | kubectl replace -f -etcd_get /registry/secrets/default/db-credentials | head -c 26 | LC_ALL=C tr -c '[:print:]' '.'; echoetcd_get /registry/secrets/default/db-credentials | grep -a -c 'S3cr3t'k8s:enc:secretbox:v1:key1:0The first result shows the prefix. The second, grep -c (c for count),
counts the lines that contain the value: zero. On the API side, nothing
changes for users: kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 -d still returns S3cr3t-Zeta, because the
API server decrypts on read.
Local key or KMS: where does the encryption key live?
With secretbox, the key is in the file placed on the control plane node,
as in this exercise. It protects against reading etcd or a backup, not against
someone who is already root on the control plane node: they read the key
file too. To separate the key from the data, Kubernetes offers the KMS (Key
Management Service) provider, where the API server hands encryption over to an
external key store (cloud KMS, Vault), following the envelope encryption
principle explained in Encrypting secrets.
On a managed cluster, this setting belongs to the provider, and it is up to
you to check in its documentation that it is active. A secretbox with a local
key therefore does not prove SOCLE-CLD-K8S-8, which requires a KMS provider;
this lesson does not claim that requirement.
How do you limit who reads Secrets with RBAC?
Encryption protects the disk, not the API: an account allowed to read receives
the decrypted value. It is RBAC that decides who reads. The principle is
least privilege: a Role that grants the get verb on one named Secret
(--resource-name on the command line, resourceNames field in YAML), bound to
a single ServiceAccount (the identity of a Pod), and nothing else. A Role
only applies in its namespace: separating environments by namespace, here
production, prevents a development account from reading production Secrets, as
long as no ClusterRoleBinding gives it that right everywhere.
kubectl create namespace productionkubectl -n production create secret generic app-db --from-literal=password=xkubectl -n production create secret generic other --from-literal=k=vkubectl -n production create serviceaccount my-appkubectl -n production create role app-secrets-reader \ --verb=get --resource=secrets --resource-name=app-dbkubectl -n production create rolebinding app-secrets-reader \ --role=app-secrets-reader --serviceaccount=production:my-appkubectl auth can-i asks the API server whether an identity has a
permission, by impersonating that identity with --as. Four checks are
enough to prove the scope: reading the allowed Secret, the refusal on
another Secret, the refusal of list, and the real Forbidden error.
S=--as=system:serviceaccount:production:my-appkubectl auth can-i get secret/app-db -n production $Skubectl auth can-i get secret/other -n production $Skubectl auth can-i list secrets -n production $Skubectl -n production get secret other $SyesnonoError from server (Forbidden): secrets "other" is forbidden: User "system:serviceaccount:production:my-app" cannot get resource "secrets" in API group "" in the namespace "production"Two traps explain most leaks. The list verb (and watch) on secrets
returns the contents of all the Secrets in the namespace, not only their
names: never grant it to an application account. And the right to create a
Pod in a namespace is equivalent to reading its Secrets, since the Pod can
mount one: limit it as much as reading. To know who read what, enable
auditing, described in Kubernetes audit logs.
How do you reduce exposure in the Pod and on the node?
Once the Secret has left the API, the last exposure is the Pod. An
environment variable ends up in /proc/<pid>/environ, in debugging output
and in the error traces of many frameworks. A volume mounts the value as a
tmpfs file, with permissions set by defaultMode, and updates it without
restarting the Pod (an environment variable, by contrast, only changes on
restart). Prefer the volume, as the "Secrets" lesson explains.
The volume protects the node's disk, not the process: a compromised
container reads its own files. A privileged: true container can escape to
the node, and then reach the Secrets mounted on that node as well as the
kubelet's credentials. Forbid it with the
Pod Security Standards
and apply the
Security Context
to the Pod.
How do you sync an external secret store with External Secrets Operator?
External Secrets Operator (ESO) is a controller that reads a value in an external secret store and creates the matching Kubernetes Secret. In this role, it copies what exists in the store: it creates no value there and does not rotate it. Two resources drive it. The SecretStore says where and how to connect to the store (it only applies to its own namespace, and its ClusterSecretStore variant serves the whole cluster); the ExternalSecret says which key to read and which Secret to create. With a real store, your Git repository only contains these descriptions, never the value.
The exercise uses ESO's fake provider, a fake store defined in the
SecretStore itself: it lets you replay the whole chain without a cloud account
or a Vault server. It is for demonstration only. For a real store, replace
the fake block with vault, aws or another provider, whose
authentication is detailed in the HashiCorp Vault
guide.
-
Install ESO version 2.13.0
The chart deploys the controller, a validation webhook and the custom resource definitions (CRDs) that add
SecretStoreandExternalSecretto the API.Terminal window helm repo add external-secrets https://charts.external-secrets.iohelm install external-secrets external-secrets/external-secrets \--version 2.13.0 -n external-secrets --create-namespace --waitkubectl -n external-secrets get podsThe three Pods (
external-secrets,cert-controller,webhook) must be1/1 Running. -
Declare the store and the sync
Save this manifest as
eso.yaml.refreshIntervalsets how often ESO rereads the store.eso.yaml apiVersion: external-secrets.io/v1kind: SecretStoremetadata:name: coffre-facticenamespace: productionspec:provider:fake:data:- key: /production/dbvalue: "Zeta-Mot-De-Passe-1"---apiVersion: external-secrets.io/v1kind: ExternalSecretmetadata:name: db-credentialsnamespace: productionspec:refreshInterval: 1msecretStoreRef:name: coffre-facticekind: SecretStoretarget:name: db-credentialscreationPolicy: Ownerdata:- secretKey: passwordremoteRef:key: /production/dbTerminal window kubectl apply -f eso.yamlESO shows a warning: the
fakeprovider is no longer maintained, which is expected for a demonstration. The same warning comes back on every command that modifies the SecretStore. -
Check the sync
Terminal window kubectl -n production get externalsecretkubectl -n production get secret db-credentials \-o jsonpath='{.data.password}' | base64 -d; echoOutput NAME STORETYPE STORE REFRESH INTERVAL STATUS READY LAST SYNCdb-credentials SecretStore coffre-factice 1m SecretSynced True 8sZeta-Mot-De-Passe-1 -
Change the value in the store and force a reread
The source changes, the Secret follows. Without the annotation, ESO waits for the next
refreshInterval.Terminal window kubectl -n production patch secretstore coffre-factice --type=json \-p '[{"op":"replace","path":"/spec/provider/fake/data/0/value","value":"Zeta-Mot-De-Passe-2"}]'kubectl -n production annotate externalsecret db-credentials \force-sync=$(date +%s) --overwritesleep 6kubectl -n production get secret db-credentials \-o jsonpath='{.data.password}' | base64 -d; echoOutput Zeta-Mot-De-Passe-2
Keep in mind what this mechanism does not do. ESO syncs, it does not rotate: generating a new password and revoking the old one stays the job of the store. And the Secret created is a native Secret, stored in etcd: the encryption at rest and the RBAC of the previous sections still apply to it.
When should you choose CSI Secrets Store, Sealed Secrets or SOPS?
These three tools cover what ESO does not, or do it differently: the CSI Driver is an alternative to ESO when the value must not go through etcd, while Sealed Secrets and SOPS solve another problem. Each is recognized by the question it settles: where is the value read, and in what state does it travel to the cluster?
The Secrets Store CSI Driver (version 1.6.1 as of October 18, 2026) mounts
the value directly from the store into the Pod, as tmpfs files, without
going through a Secret object. It relies on a DaemonSet (one Pod per node) and
on one provider per store. Its benefit is that the value is not stored in
etcd, so it is not exposed to kubectl get secret (as long as you do not enable
the optional sync to a Secret, which recreates an object in etcd); its price is
that the application must reread the file when the value changes, and that it
restarts no Pod.
Rotation of the mounted content is disabled by default: it is enabled
with --enable-secret-rotation=true (enableSecretRotation=true in the Helm
chart), with a default interval of 2 minutes.
Sealed Secrets and SOPS solve another problem: versioning a Secret in
Git without exposing the value, in practice with GitOps (the Git
repository describes the state of the cluster, and a tool applies it). With
Sealed Secrets, a controller in the cluster holds the private key and only it
can decrypt the SealedSecret you commit; the guide
Sealed Secrets with Kubeseal (in French)
details installation and use. With SOPS, you encrypt the files themselves; the
SOPS guide covers the tool and the one on
Flux CD with SOPS (in French)
shows a decryption done by the cluster. In both cases, the Secret obtained
inside the cluster remains a native Secret: etcd encryption and RBAC are
still needed.
Which tool for which need?
The main selection criterion is not the security level of the tool, but what your application can do with the value and where the source of truth lives. The same organization often combines several lines: the baseline (etcd encryption, strict RBAC, auditing) always applies.
| Situation | Choice |
|---|---|
| A single cluster, few Secrets, no secret store | Native Secrets, etcd encryption, strict RBAC, auditing |
| An existing secret store (Vault, cloud) and an application that can only read an environment variable | External Secrets Operator |
| The value must not be in etcd and the application rereads a file | CSI Secrets Store, without sync to a Secret |
| Secret manifests to version in Git, no secret store | Sealed Secrets or SOPS |
| Several namespaces fed by the same secret store | External Secrets Operator with a ClusterSecretStore, declared once |
| Several clusters fed by the same secret store | One External Secrets Operator in each cluster, all pointed at that store |
| Ephemeral credentials, generated on demand | A store that generates them (Vault), exposed through ESO or the CSI Driver |
| Vault already in place and per-Pod injection, with no Secret object | Vault Agent Injector (sidecar described in the HashiCorp Vault guide), to compare with the CSI Driver |
An application that reads its configuration only once at startup does not benefit from a hot-reloaded mount: you then have to redeploy the Pod when the value changes. The identity that lets the cluster authenticate to the store without a stored password is covered by machine identity and workload identity.
Troubleshooting: what to do when the protection is not visible?
The failures in this lesson are silent: nothing breaks, the protection is simply absent. The table links each symptom to its probable cause and to the command that fixes it.
| Symptom | Probable cause | Solution |
|---|---|---|
| A Secret created after the change is still in plain text in etcd | The API server has not reloaded the file | Wait one more minute, then check that the option appears in the API server manifest: docker exec sec-lab-control-plane grep encryption-provider /etc/kubernetes/manifests/kube-apiserver.yaml |
Old Secrets are in plain text despite secretbox being first | Only new writes are encrypted | kubectl get secrets -A -o json | kubectl replace -f - |
| The modified file is not taken into account | It was replaced (mv, editor) instead of rewritten | Recreate the cluster: the node keeps the mount of the old file (the old inode), and rewriting the new one with cat > changes nothing |
kubectl auth can-i answers yes to list secrets | Another Role or ClusterRole grants that right | kubectl auth can-i --list --as=system:serviceaccount:production:my-app -n production lists its rights; kubectl get rolebindings,clusterrolebindings -A finds the binding |
The ExternalSecret stays in SecretSyncedError | The SecretStore is invalid or the remote key is missing | kubectl -n production describe externalsecret db-credentials |
Clean up the test cluster
Delete the kind cluster and the working folder, which contains the
exercise's encryption key: this key no longer protects anything, but never
reuse it elsewhere.
kind delete cluster --name sec-labcd .. && rm -rf secrets-labPut it into practice
In the lab, the API server writes the Secrets of the tresorerie namespace in clear text to etcd, and the facturation ServiceAccount can read every Secret in that namespace although it only needs cle-banque. You fix both problems. You enable encryption at rest with the secretbox provider, then rewrite the existing Secrets, cle-banque and cle-paie, so that they are encrypted too. You then reduce the rights of facturation to reading cle-banque only.
On the lab machine, the API server is a static Pod of a control plane node: you edit its manifest (/etc/kubernetes/manifests/kube-apiserver.yaml) instead of the kind cluster of the lesson, and it restarts for about a minute. Keep your backup copy of that manifest in another directory, never in the manifests directory, which the kubelet watches. The lesson shows the configuration file to write and the options to add. The file must be mounted in the API server container, as in the kind configuration above.
Validation reads etcd with etcdctl: it creates a test Secret and checks that it carries the prefix k8s:enc:secretbox:v1:, then does the same read for cle-banque and cle-paie, which must also stay readable through the API. It then asks the API server, with kubectl auth can-i, what facturation is allowed to do: read cle-banque, and nothing else, neither cle-paie, nor list nor watch on Secrets.
Key takeaways
- A Secret is in plain text in etcd by default: a direct read of the disk or of a backup reveals it
- Encryption at rest is declared with
EncryptionConfiguration: the first provider encrypts new writes, all serve for reading back, andkubectl get secrets -A -o json | kubectl replace -f -encrypts what already exists - A local key does not protect against root access to the control plane node: KMS separates the key from the data
- RBAC decides who reads:
geton a named Secret, neverlistfor an application, and the right to create a Pod amounts to reading - A volume exposes less than an environment variable, but does not protect against a compromised container
- ESO syncs, it does not rotate, and the Secret created stays in etcd; the CSI Secrets Store avoids etcd but requires an application that rereads its file
- Sealed Secrets and SOPS protect the Secret in Git, not in the cluster
- Expected proof (SOCLE-SEC-STO-1; only in part SOCLE-CLD-K8S-10 and SOCLE-SEC-GEN-3): the
k8s:enc:prefix read in etcd, a negativekubectl auth can-iand Secrets supplied by ESO or the CSI Driver. Three reservations, to know before declaring a requirement met: SOCLE-CLD-K8S-10 also asks for rotated secrets, which this lesson does not cover (ESO syncs without rotating, CSI rotation is disabled by default) and its demonstration relies on a fake store; the tracing of reads belongs to auditing; andsecretboxdoes not prove SOCLE-CLD-K8S-8, which requires a KMS
Knowledge check
Check that you have mastered the essentials of this guide. The questions cover only what was just explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Kubernetes supply chain security: securing the delivery chain
A new full quiz with random questions
Work again only on the questions you missed