Lesson 52 · Section 5: Application Lifecycle Management
Lesson 52: Encrypting Secret data at rest: locking what etcd stores
Why a Secret is readable in etcd by default, how an EncryptionConfiguration makes the API server encrypt it before it is stored, and how to turn it on, prove it works, re-encrypt old Secrets and rotate the key. · 20 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why this matters
In the Secrets lesson you saw that the data of a Secret is base64. Base64 is an encoding: it turns bytes into
text that survives YAML, and anyone can turn it back with base64 -d. It hides nothing.
What surprises most people is the next layer down. When the API server stores a Secret in etcd, it stores it as it is. There is no encryption unless an administrator sets it up. So the passwords, tokens and TLS keys of every application in the cluster are readable by:
- anyone who can read etcd's data folder,
/var/lib/etcdon a kubeadm control plane node, - anyone who gets hold of an etcd snapshot, and backups tend to be copied to many places,
- anyone who can connect to etcd with a valid client certificate,
- anyone who finds the disk, or a copy of the virtual machine's disk image.
"At rest" means exactly this: the data while it sits in storage, as opposed to "in transit" (on the network, where TLS already protects it).
See the problem first
You can only trust a fix you have seen fail first. Create a Secret and read its key directly from etcd:
kubectl -n shop create secret generic db-pass --from-literal=password=S3cr3t-pw
P=/etc/kubernetes/pki/etcd
sudo ETCDCTL_API=3 etcdctl --cacert=$P/ca.crt --cert=$P/server.crt --key=$P/server.key \
get /registry/secrets/shop/db-pass | hexdump -C
00000000 2f 72 65 67 69 73 74 72 79 2f 73 65 63 72 65 74 |/registry/secret|
00000010 73 2f 73 68 6f 70 2f 64 62 2d 70 61 73 73 0a 6b |s/shop/db-pass.k|
00000020 38 73 00 0a 0c 0a 02 76 31 12 06 53 65 63 72 65 |8s.....v1..Secre|
00000030 74 12 5a 0a 3d 0a 07 64 62 2d 70 61 73 73 12 00 |t.Z.=..db-pass..|
[...]
00000080 70 61 73 73 77 6f 72 64 12 09 53 33 63 72 33 74 |password..S3cr3t|
00000090 2d 70 77 1a 06 4f 70 61 71 75 65 1a 00 22 00 0a |-pw..Opaque.."..|
The right-hand column shows password and then S3cr3t-pw. It is not even base64 here: base64 is only how the API
shows the bytes, and etcd holds the raw value. hexdump -C is used because the stored object is binary (protobuf),
and the hex view keeps your terminal readable.
How encryption at rest works
Parts:
- kubectl (sees the real value)
- kube-apiserver (encrypts and decrypts)
- etcd (stores ciphertext)
- Encryption config (providers and keys)
- Snapshot file (ciphertext too)
Connections:
- kubectl and kube-apiserver: plain
- kube-apiserver and etcd: cipher
- Encryption config to kube-apiserver: read at start
- etcd to Snapshot file: backup
The API server is the only client of etcd, so it is the one place where every write passes through. You start it with
one extra flag, --encryption-provider-config, that points to a file. That file is an EncryptionConfiguration:
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key1
secret: <32 random bytes, base64 encoded>
- identity: {}
Read it from the top:
resourceslists what to encrypt.secretsis the usual answer. You can addconfigmaps, a custom resource such aswidgets.example.com, or wildcards such as*.appsand*.*.providersis an ordered list of ways to encrypt. Each provider that uses a local key has a list ofkeys, each with anameand a base64secret.identity: {}is the provider that does nothing: it stores and reads values unchanged. Without any configuration file, the API server behaves as ifidentitywere the only provider.
This is not an object you kubectl apply. It is a file on the control plane node that the API server reads when it
starts, like a kubeconfig.
The order of providers is the whole trick
Parts:
- kube-apiserver (walks the list)
- 1. aescbc, key1 (encrypts every write)
- 2. identity (reads plain values)
- New Secret (k8s:enc:aescbc:v1:key1)
- Old Secret (still plain text)
Connections:
- kube-apiserver to 1. aescbc, key1: write
- 1. aescbc, key1 to New Secret: stores
- kube-apiserver to 2. identity: read old value
- 2. identity to Old Secret: reads
Two rules explain every behaviour you will see:
- Writes use the first provider in the list, and the first key of that provider.
- Reads try the listed providers and use the one that matches the stored value. Each encrypted value starts with
a prefix such as
k8s:enc:aescbc:v1:key1:, which names the provider and the key. A value with no such prefix is plain, and onlyidentitycan read it.
From those two rules:
| Providers, in order | New writes | Old plain Secrets | Old aescbc Secrets |
|---|---|---|---|
aescbc, identity | Encrypted | Still readable | Readable |
aescbc only | Encrypted | Read fails | Readable |
identity, aescbc | Plain | Readable | Readable |
| No config file | Plain | Readable | Read fails |
The third row is the classic mistake and the classic exam trap: the file exists, the flag is set, the key is valid,
and nothing is encrypted, because identity comes first. The same third row is also exactly how you turn
encryption off again: put identity first, rewrite every Secret, and only then remove the key.
Choosing a provider
| Provider | What it is | Key | When to use it |
|---|---|---|---|
identity | No encryption | None | The default, and the fallback for reading old values |
aescbc | AES in CBC mode | 16, 24 or 32 bytes | The example in the docs and in most exam tasks. The docs now rate it weak and no longer recommend it |
aesgcm | AES in GCM mode | 16, 24 or 32 bytes | Only with automated key rotation: the key must change every 200,000 writes |
secretbox | XSalsa20 and Poly1305 | Exactly 32 bytes | The strongest choice among the local-key providers |
kms (v2) | Envelope encryption with an external key service | Held by the KMS | Production: the key never sits on the control plane node |
For the exam, use the provider the task names. If you choose yourself on a real cluster, prefer kms v2, and
secretbox when you have no key service.
Under the hood · Local keys, KMS and what encryption at rest does not protect
- A local key is only as safe as the node. With
aescbc,aesgcmorsecretboxthe key is written in a file on the control plane node, the same machine that usually holds etcd. Someone with root on that node has both the ciphertext and the key. What you have protected is everything that leaves the node: snapshots, disk images, a separate etcd host. - KMS uses envelope encryption. The API server encrypts data with a data encryption key, and that key is itself encrypted by a key that lives in an external service (a cloud KMS or a hardware module), reached through a plugin on a local socket. Stealing the node's disk gives an attacker nothing they can decrypt. KMS v2 has been stable since Kubernetes v1.29, and KMS v1 is deprecated.
- It is not access control.
kubectl get secret db-pass -o yamlreturns the value to anyone RBAC allows, and a Pod that mounts the Secret gets it in plain form. Encryption at rest protects the storage, RBAC protects the API. You need both. - Every API server needs the same file. In a highly available control plane, each API server must have the same providers and keys. One server with a different key cannot read what the others wrote.
- Reloading. By default the file is read only at start. With
--encryption-provider-config-automatic-reload=truethe API server picks up changes to the file without a restart. - The key is now part of your backup. An etcd snapshot of an encrypted cluster is useless without the key. Store the encryption configuration somewhere safe, and separate from the snapshots.
Using it
Example · Check whether encryption is already on
A task may ask you to find out, or you may simply need to know before changing anything. Look for the flag:
sudo grep encryption-provider-config /etc/kubernetes/manifests/kube-apiserver.yaml
# or, for an API server that runs as a service:
ps aux | grep kube-apiserver | grep -o 'encryption-provider-config[^ ]*'
No flag means no encryption. If the flag is there, read the file it names and look at the first provider under
the resource you care about. Anything other than identity means new writes are encrypted.
Example · Turn on encryption for Secrets on a kubeadm cluster
1. Generate a key. 32 random bytes, base64 encoded:
head -c 32 /dev/urandom | base64
2. Write the configuration on the control plane node, in a folder of its own:
sudo mkdir -p /etc/kubernetes/enc
sudo vi /etc/kubernetes/enc/enc.yaml
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key1
secret: PASTE-THE-KEY-FROM-STEP-1
- identity: {}
sudo chmod 600 /etc/kubernetes/enc/enc.yaml # the file holds the key
3. Back up the manifest, then edit it. Keep the copy outside the manifests folder:
sudo cp /etc/kubernetes/manifests/kube-apiserver.yaml /root/kube-apiserver.yaml.bak
sudo vi /etc/kubernetes/manifests/kube-apiserver.yaml
Three additions, because the API server runs in a container and cannot see the node's files unless they are mounted:
spec:
containers:
- command:
- kube-apiserver
- --encryption-provider-config=/etc/kubernetes/enc/enc.yaml # 1: the flag
volumeMounts:
- name: enc # 2: the mount
mountPath: /etc/kubernetes/enc
readOnly: true
volumes:
- name: enc # 3: the volume
hostPath:
path: /etc/kubernetes/enc
type: DirectoryOrCreate
4. Wait for the API server. The kubelet sees the changed manifest and restarts it. For a minute kubectl answers
"connection refused", and that is expected:
watch sudo crictl ps --name kube-apiserver # wait for a new container that stays Running
kubectl get nodes
5. Encrypt what was already there:
kubectl get secrets --all-namespaces -o json | kubectl replace -f -
Example · Prove that a Secret is encrypted in etcd
Read the same key as before:
P=/etc/kubernetes/pki/etcd
sudo ETCDCTL_API=3 etcdctl --cacert=$P/ca.crt --cert=$P/server.crt --key=$P/server.key \
get /registry/secrets/shop/db-pass | hexdump -C | head -6
00000000 2f 72 65 67 69 73 74 72 79 2f 73 65 63 72 65 74 |/registry/secret|
00000010 73 2f 73 68 6f 70 2f 64 62 2d 70 61 73 73 0a 6b |s/shop/db-pass.k|
00000020 38 73 3a 65 6e 63 3a 61 65 73 63 62 63 3a 76 31 |8s:enc:aescbc:v1|
00000030 3a 6b 65 79 31 3a 6e 34 0b 9c ff b3 7a 98 9c a5 |:key1:n4....z...|
00000040 44 e6 bb 78 0a 2c 78 90 1d 3f b3 37 38 76 85 11 |D..x.,x..?.78v..|
The value starts with k8s:enc:aescbc:v1:key1: and the rest is noise: provider aescbc, key key1. Then check the
other direction, that the API still gives the real value back:
kubectl -n shop get secret db-pass -o jsonpath='{.data.password}' | base64 -d
If etcdctl is not installed on the node, run it inside the etcd Pod and pipe the output to hexdump on your side:
kubectl -n kube-system exec etcd-controlplane -- etcdctl \
--cacert=$P/ca.crt --cert=$P/server.crt --key=$P/server.key \
get /registry/secrets/shop/db-pass | hexdump -C | head -6
Example · Rotate the key
Parts:
- 1. Add key2 (second in the list)
- 2. Restart (every API server)
- 3. Move key2 first (it now encrypts)
- 4. Restart (every API server)
- 5. Rewrite Secrets (kubectl replace)
- 6. Remove key1 (after a backup)
Connections:
- 1. Add key2 to 2. Restart
- 2. Restart to 3. Move key2 first
- 3. Move key2 first to 4. Restart
- 4. Restart to 5. Rewrite Secrets
- 5. Rewrite Secrets to 6. Remove key1
A key must never be swapped in one step. An API server that meets a value encrypted with a key it does not have cannot read it, so the new key is introduced in two moves:
- Add
key2as the second key of the provider. It can now decrypt, butkey1still encrypts. - Restart every API server. After this, all of them can read values written with either key.
- Move
key2to the first position. It now encrypts;key1stays second and can still decrypt. - Restart every API server again.
- Rewrite all Secrets so that each is stored again with
key2:kubectl get secrets --all-namespaces -o json | kubectl replace -f - - Remove
key1, after you have backed up the new key and checked a Secret in etcd for thekey2prefix.
The configuration between steps 3 and 6:
providers:
- aescbc:
keys:
- name: key2
secret: <the new key>
- name: key1
secret: <the old key>
- identity: {}
On a cluster with a single API server, steps 1 and 2 can be folded into step 3, because there is no second server that could meet a key it has not loaded yet.
Tricks
Common mistakes
- Putting
identityfirst. Everything looks configured and nothing is encrypted. The first provider is the one that writes. - Removing
identitytoo early. Withaescbcas the only provider, every Secret that is still plain in etcd becomes unreadable. Keepidentitylast until all Secrets have been rewritten. - Forgetting the mount. The flag names a path inside the container. Without the
volumeMountsentry and thehostPathvolume, the API server cannot find the file and crash loops. - A key of the wrong length. The
secretis the base64 of 16, 24 or 32 random bytes (exactly 32 forsecretbox). Pasting a password, or encoding a string with a trailing newline, gives a length the API server rejects. - Not rewriting existing Secrets. Only writes are encrypted. Without
kubectl replace, old Secrets stay in plain form forever. - Expecting
kubectl get secretto look different. It never does. The only place to see the encryption is in etcd. - Losing the key. Deleting the configuration, or the key in it, while encrypted Secrets remain means those Secrets cannot be read by anyone, including you.
- Leaving a copy of the manifest in the manifests folder. The kubelet runs every file there, so
kube-apiserver.yaml.baknext to the original starts a second API server.
Recap
- By default a Secret is stored in etcd unencrypted; base64 is only an encoding.
- The API server encrypts, driven by an EncryptionConfiguration file named by
--encryption-provider-config. - The first provider encrypts new writes; every listed provider can decrypt. Keep
identitylast while old plain Secrets remain, and never put it first unless you are turning encryption off. - On kubeadm: write the file, add the flag, the mount and the volume to
kube-apiserver.yaml, wait for the restart. - Rewrite existing Secrets with
kubectl get secrets -A -o json | kubectl replace -f -. - Verify in etcd: the value must start with
k8s:enc:PROVIDER:v1:KEYNAME:. - It protects etcd's storage and snapshots, not the API. RBAC still decides who can read a Secret, and a KMS provider is what keeps the key off the node.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
See a Secret lying readable in etcd, switch on encryption at rest in the API server of a kubeadm cluster, and make sure the Secrets that already existed are encrypted too.
Build · Level 3 · challenge · target 25 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.