Challenge
DifficultyHard
Learner activity2 solved of 4 attempts

Encrypt Kubernetes Secrets at Rest in etcd

Omkar Shelke
by  Omkar Shelke · on
KubernetesSecurity
The Secrets of a kubeadm cluster are stored as plaintext in etcd. Turn on encryption at rest in the API server, make sure the Secrets that already exist get encrypted too, and prove it by reading etcd directly.

Scenario

A security audit of your kubeadm cluster found that its Secrets are stored as plaintext in etcd. Anyone who gets a copy of the etcd data directory or an etcd backup can read every Secret value, without the Kubernetes API and without RBAC.

The cluster has three Secrets, and all of them were written before encryption was turned on:

NamespaceSecretCreated by
securedb-credentialsthe app team
billingpayment-gatewaythe app team
kube-systembootstrap-token-<random>kubeadm init

The API server stores each Secret in etcd under the key /registry/secrets/<namespace>/<name>. You can list these keys on cplane-01:

sudo etcdctl get /registry/secrets/ --prefix --keys-only \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  --cert=/etc/kubernetes/pki/etcd/server.crt \
  --key=/etc/kubernetes/pki/etcd/server.key

The auditors used one of the Secrets as an example. Read the value of db-credentials with the same command, without --prefix --keys-only:

sudo etcdctl get /registry/secrets/secure/db-credentials \
  --endpoints=https://127.0.0.1:2379 \
  --cacert=/etc/kubernetes/pki/etcd/ca.crt \
  --cert=/etc/kubernetes/pki/etcd/server.crt \
  --key=/etc/kubernetes/pki/etcd/server.key

The values admin and SuperSecret123 are readable in the output. The other two Secrets have the same problem.

Secrets in etcd before and after encryption at rest

Task

Turn on encryption at rest for Secrets in the API server. Use a real encryption provider: aescbc, aesgcm, or secretbox. The identity provider stores data as plaintext, so it does not count.

When you are done, all three Secrets from the table above must be stored encrypted in etcd. The same goes for any Secret that you create while you work. kubectl must still return the original values of the Secrets. Do not delete or change any Secret data.

Important

A mistake in the API server configuration can stop the API server, and kubectl stops working with it. Keep a copy of every file you change, outside the directory you change it in, so that you can go back.


Hint 1: Where encryption is configured

The API server reads its encryption settings from an EncryptionConfiguration file. A command-line flag of the API server points to that file. The file lists the resources to encrypt and an ordered list of providers.

Think about the order of the providers. The first provider in the list encrypts all new writes. The API server can read data with any provider in the list, including data that was written before encryption was on.

The key of aescbc, aesgcm, or secretbox is 32 random bytes, base64-encoded.

Docs:

Hint 2: Changing a static Pod

In a kubeadm cluster, the API server runs as a static Pod. The kubelet starts it from a manifest in /etc/kubernetes/manifests/ and restarts it on its own when the manifest changes.

The container sees only the host paths that are mounted into it. A file on the host is invisible to the API server until a volume makes it available.

The kubelet treats every file in the manifests directory as a Pod, so keep your backup somewhere else.

Docs:

Hint 3: When the API server does not come back

While the API server is down, kubectl cannot tell you why. The container runtime still can. List all containers, including the ones that exited, and read the logs of the last kube-apiserver container:

sudo crictl ps -a --name kube-apiserver
sudo crictl logs <container-id>

The last lines of the log name the setting that the API server could not accept.

Hint 4: The Secrets that already exist

The API server encrypts data when it writes it to etcd. A Secret that was written before encryption was on stays as it is until something writes it again. The docs page about encryption at rest has a section on how to make sure that all Secrets are encrypted.

The check of this challenge lists the Secrets that are still plaintext in etcd. An encrypted value starts with k8s:enc:, followed by the provider name.


âš’ Test Cases

Problem
Solution
Start
Black Friday