Sub topic 12
Encryption at rest and other safeguards
This chapter assumes you can create Secrets, know that base64 is encoding and not encryption, and have read the RBAC chapter. It covers the API server setting that encrypts Secrets in etcd, and the other controls that limit exposure.
What encryption at rest protects
By default, the API server writes Secret objects to etcd as plain base64 data. Anyone who can read etcd files, a disk snapshot, or a backup can recover every Secret. Encryption at rest makes the API server encrypt the data before it writes to etcd and decrypt it when it reads back.
It does not protect against a user with get permission on Secrets. The API server decrypts the data for them. It also does not protect against a compromised API server process, which holds the keys.
Configuring encryption
You give the API server an EncryptionConfiguration file and point to it with the --encryption-provider-config flag. On a kubeadm cluster, you must also mount the file into the static Pod of kube-apiserver.
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key1
secret: <base64-encoded 32-byte key>
- identity: {}Provider order matters:
- The first provider encrypts all new writes.
- On reads, the API server tries each provider in order until one works.
identitymeans no encryption. Listing it last lets the cluster still read Secrets that were stored before you enabled encryption.
Generate a key with head -c 32 /dev/urandom | base64. Restrict the config file to root, because it holds the key in plain text.
Encrypting existing Secrets
Only writes are encrypted. Existing Secrets stay in plain text until they are rewritten. Force a rewrite, then check etcd directly.
kubectl get secrets --all-namespaces -o json | kubectl replace -f -
ETCDCTL_API=3 etcdctl get /registry/secrets/default/db-credentials | hexdump -C | headEncrypted values start with a prefix such as k8s:enc:aescbc:v1:key1. If you still see your plain value, encryption is not active for that object.
Rotating keys
- Add a new key as the first entry under
aescbc, and keep the old key below it. - Restart every API server so they all can read the new key.
- Rewrite all Secrets with the
replacecommand above. - Remove the old key and restart again.
Stronger options and other safeguards
aescbc keeps the key on the control plane node next to etcd. A KMS provider (v2) encrypts data keys with a key held in an external service, so a stolen disk alone is not enough. Managed services such as EKS, GKE, and AKS offer this as a setting.
Combine encryption with these controls:
- Grant
get,list, andwatchon Secrets sparingly.listreturns every value in a namespace. - Enable audit logging for Secret access, at the
Metadatalevel so values do not land in logs. - Prefer volume mounts over environment variables. Variables leak through crash dumps, child processes, and
kubectl describe-style tooling. - Never commit Secret manifests to Git unless they are sealed or encrypted.
- Restrict access to etcd with mutual TLS and network rules, and encrypt backups.
Exercise
You enabled an aescbc provider listed first, with identity second. A Secret created last week is still readable in etcd as plain base64. Why, and how do you fix it?
Show answer
Encryption applies only on write, so the old Secret was never rewritten. Rewrite it with kubectl get secrets --all-namespaces -o json | kubectl replace -f -, then confirm the etcd value starts with k8s:enc:aescbc.