Skip to content
LF Cert Trainer

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

1/11Encrypting Secret data at rest
0:00 / 4:14

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/etcd on 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

The API server encrypts on the way to etcdplaincipherread at startbackupkubectlsees the real valuekube-apiserverencrypts and decryptsetcdstores ciphertextEncryption configproviders and keysSnapshot fileciphertext too
The API server encrypts on the way to etcd. kubectl and Pods always see the real value. With an encryption configuration, the API server encrypts a Secret just before it stores it, so etcd, its data folder and every snapshot hold only ciphertext.

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:

  • resources lists what to encrypt. secrets is the usual answer. You can add configmaps, a custom resource such as widgets.example.com, or wildcards such as *.apps and *.*.
  • providers is an ordered list of ways to encrypt. Each provider that uses a local key has a list of keys, each with a name and a base64 secret.
  • identity: {} is the provider that does nothing: it stores and reads values unchanged. Without any configuration file, the API server behaves as if identity were 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

The first provider writes, every provider can readwritestoresread old valuereadskube-apiserverwalks the list1. aescbc, key1encrypts every write2. identityreads plain valuesNew Secretk8s:enc:aescbc:v1:key1Old Secretstill plain text
The first provider writes, every provider can read. New writes use the first provider in the list. Values already in etcd are read by whichever listed provider matches their prefix, which is why identity stays in the list until every old Secret has been rewritten.

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:

  1. Writes use the first provider in the list, and the first key of that provider.
  2. 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 only identity can read it.

From those two rules:

Providers, in orderNew writesOld plain SecretsOld aescbc Secrets
aescbc, identityEncryptedStill readableReadable
aescbc onlyEncryptedRead failsReadable
identity, aescbcPlainReadableReadable
No config filePlainReadableRead 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

ProviderWhat it isKeyWhen to use it
identityNo encryptionNoneThe default, and the fallback for reading old values
aescbcAES in CBC mode16, 24 or 32 bytesThe example in the docs and in most exam tasks. The docs now rate it weak and no longer recommend it
aesgcmAES in GCM mode16, 24 or 32 bytesOnly with automated key rotation: the key must change every 200,000 writes
secretboxXSalsa20 and Poly1305Exactly 32 bytesThe strongest choice among the local-key providers
kms (v2)Envelope encryption with an external key serviceHeld by the KMSProduction: 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, aesgcm or secretbox the 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 yaml returns 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=true the 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

Rotating the key without losing a Secret1. Add key2second in the list2. Restartevery API server3. Move key2 firstit now encrypts4. Restartevery API server5. Rewrite Secretskubectl replace6. Remove key1after a backup
Rotating the key without losing a Secret. The new key is added where it can only decrypt, then promoted to encrypt, and the old key leaves only after every Secret has been rewritten. Skipping a step leaves Secrets that no key can open.

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:

  1. Add key2 as the second key of the provider. It can now decrypt, but key1 still encrypts.
  2. Restart every API server. After this, all of them can read values written with either key.
  3. Move key2 to the first position. It now encrypts; key1 stays second and can still decrypt.
  4. Restart every API server again.
  5. Rewrite all Secrets so that each is stored again with key2: kubectl get secrets --all-namespaces -o json | kubectl replace -f -
  6. Remove key1, after you have backed up the new key and checked a Secret in etcd for the key2 prefix.

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 identity first. Everything looks configured and nothing is encrypted. The first provider is the one that writes.
  • Removing identity too early. With aescbc as the only provider, every Secret that is still plain in etcd becomes unreadable. Keep identity last until all Secrets have been rewritten.
  • Forgetting the mount. The flag names a path inside the container. Without the volumeMounts entry and the hostPath volume, the API server cannot find the file and crash loops.
  • A key of the wrong length. The secret is the base64 of 16, 24 or 32 random bytes (exactly 32 for secretbox). 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 secret to 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.bak next 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 identity last 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.