Skip to main content

Removing identity From EncryptionConfiguration Before Secrets Are Rewritten Makes Plaintext Secrets Unreadable

High severityKubernetes About 20 minutes to fix

Overview

Encryption at rest works, so you remove the identity fallback, and old Secrets stop reading. They were never rewritten.

At a glance

Symptom
The change looks small.
Likely cause
The providers list is ordered.
Impact
The affected Secrets are unreadable through the API until a provider can read them again.
Verification signal
After step 4, read several Secrets from etcd with the hexdump check, taking some of the oldest in different namespaces.
Safe correction
Restore reads first, in an order that never writes plain text.
Rollback or recovery
Every change here is a file edit that can be reversed.

Encryption at rest for Secrets is switched on, and the hardening guidance says to remove identity: {} from the provider list so the API server can never read plain text. If that line goes before every existing Secret has been rewritten, the API server stops being able to read any Secret that is still stored in the clear. Put identity: {} back as the last provider, restart each API server in turn, then rewrite the Secrets and remove it properly.

Symptom

The change looks small. A file such as /etc/kubernetes/enc/enc.yaml loses one line, and the API servers pick it up. Reads of Secrets that were written before encryption was enabled then start to fail, while newer Secrets read normally.

So the cluster is not down, but it is uneven. Some Secrets read and some do not. Which ones fail depends on when each was last written, not on its namespace or its owner. Anything that needs one of the unreadable Secrets, such as a Pod that references it, has to wait until the Secret can be read again.

Why it looks safe to remove the fallback

The Kubernetes documentation for encrypting data at rest lists identity after the real provider as a fallback that allows reading unencrypted Secrets, for example during the initial migration. Its section on preventing plain text retrieval then says that once all Secrets are encrypted, the line can be removed, which stops the API server accessing a plain-text Secret even by accident.

The step is sound once the precondition is true. The trap is that nothing in the configuration records the precondition. Turning encryption on only encrypts what is written afterwards, so a cluster can have a working first provider and still hold years of plain-text Secrets. The first-provider check in the documentation does not tell you whether a previous migration to encrypted storage has succeeded.

Root cause

The providers list is ordered. The first provider encrypts every write. For reads, each provider that matches the stored data tries to decrypt it in order. If no provider can read the stored data, the documentation says an error is returned that prevents clients from accessing that resource.

Plain-text data is read by identity. Take that provider away and a Secret still stored in the clear has no provider that can read it. The same page warns that the change prevents the API server retrieving resources that are marked as encrypted at rest but are actually stored in the clear. Existing Secrets are only converted when something replaces them.

Automatic reload makes it faster. With --encryption-provider-config-automatic-reload=true, the API server polls the file every minute and applies changes without a restart. A bad edit can therefore land within about a minute, with no restart to mark the moment.

Impact

The affected Secrets are unreadable through the API until a provider can read them again. The documentation’s caution about unreadable resources spells out the worst case. If you cannot restore a working configuration, your only recourse is to delete the entry from etcd directly, which discards that Secret.

A cluster with more than one control plane node adds a second risk. The documentation says the encryption configuration should be identical on every control plane node, because a difference may mean a kube-apiserver cannot decrypt data. Edit one node first and its API server may fail reads that another node still serves.

How to confirm it is the missing provider

Three read-only checks settle it. First, find the configuration file the API server uses. On each control plane node, the kube-apiserver static Pod manifest names it with --encryption-provider-config:

sudo grep -- --encryption-provider-config /etc/kubernetes/manifests/kube-apiserver.yaml

Then read that file and look at the provider list for secrets. If an encrypting provider such as aescbc is listed and identity is not, the fallback has gone:

sudo cat /etc/kubernetes/enc/enc.yaml

Finally, look at what etcd holds for one of the failing Secrets. The documentation reads one Secret out of etcd and pipes it through hexdump -C. A Secret encrypted by aescbc starts with the prefix k8s:enc:aescbc:v1: followed by the key name. A Secret whose content shows as readable text instead is stored in the clear:

ETCDCTL_API=3 etcdctl --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 get /registry/secrets/default/secret1 | hexdump -C

Replace secret1 and default with the failing Secret’s name and namespace. Do not paste this output into tickets or chat. For a plain-text Secret it contains the value.

The shortcuts that make it worse

Under pressure, two fixes tempt. The first is to put identity: {} back at the top of the list so the errors stop. That does restore reads, but the first provider encrypts every write. With identity first, new and updated Secrets are written as plain text, and the documentation says this provider gives no confidentiality protection. The cluster goes back to storing credentials in the clear, and the next audit finds it.

The second is to delete the unreadable entries from etcd. The documentation names that as the last resort when no working configuration can be restored. It is not a first move. It destroys the Secret, and anything that depended on it needs the value recreated from wherever the credential came from.

Neither is needed. The fallback only has to be present again, and last. For the surrounding etcd protections that should stay on while you work, see why an etcd client port without certificate authentication exposes every Secret.

Correction

Restore reads first, in an order that never writes plain text. Work on one control plane node at a time, and keep the encrypting provider first throughout. The precondition for the whole procedure is a working copy of the configuration that was in place before the bad edit, and an etcd snapshot.

  1. Back up. Copy the encryption configuration file on every control plane node to a root-only directory outside /etc/kubernetes/manifests/. Take an etcd snapshot with ETCDCTL_API=3 etcdctl --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 snapshot save /var/backups/etcd-before-enc-fix.db, then check it with etcdutl --write-out=table snapshot status /var/backups/etcd-before-enc-fix.db. Encrypt the snapshot, because it holds every Secret.
  2. Put the fallback back, as the last provider. Under secrets, list the encrypting provider first and add - identity: {} after it, as in the documentation’s configuration. Keep the keys exactly as they were. Precondition: the file is identical on every control plane node when you finish. Rollback: restore the backed-up file.
  3. Restart each kube-apiserver in turn. Do one node, confirm that the Secret now reads (kubectl get secret secret1 -n default -o yaml), then move to the next. If automatic reload is on, wait for the poll interval instead.
  4. Rewrite every Secret. As an administrator who can read and write all Secrets, run kubectl get secrets --all-namespaces -o json | kubectl replace -f -. The documentation says to retry on a conflicting write and that it is safe to run more than once. On a large cluster, run it per namespace. Rollback: none is needed, because the command writes the same data with the first provider.
  5. Check, then remove the fallback. Only when the spot checks below pass, remove identity: {}, and apply the change to each API server in turn, exactly as for step 3.

Following along? Tap a step’s number to tick it off.

Verification

After step 4, read several Secrets from etcd with the hexdump check, taking some of the oldest in different namespaces. Each should start with k8s:enc:aescbc:v1: and the key name from the file. The documentation gives this check one Secret at a time, so sample widely rather than assuming the cluster is done.

Then run kubectl get secret secret1 -n default -o yaml and confirm the data is returned as before. After the fallback is removed, repeat the reads through the API for the same Secrets. Any that fail mean the rewrite missed them, so restore the fallback and run step 4 again.

Rollback

Every change here is a file edit that can be reversed. Restore the backed-up encryption configuration on each node, then restart the API servers one at a time so every node is the same. The Secret rewrite needs no undoing.

The etcd snapshot is for damaged data only. Restoring it rewinds the whole cluster to the moment it was taken, so it is not a way to undo a configuration edit.

Stop it happening next time

  • Treat identity removal as the last step of a migration, never part of enabling encryption. Make the ticket require the rewrite to be run and sampled first.
  • Change one control plane node at a time and compare the file on every node afterwards, as the documentation requires identical configurations.
  • If automatic reload is enabled, remember that a saved edit applies within about a minute. Make the change on a copy, check it, then move it into place.
  • Rotating keys has its own order, and the documentation covers it: add the new key second, restart, make it first, restart, rewrite the Secrets, then remove the old key. A related renewal trap is described in kubeadm certs renew without a static Pod restart.
3

Apply the safer control

Before you change production

Confirm the affected scope, export the current configuration, and test the replacement control in a non-production environment first.

Fix commands and configuration

identity: {}
/etc/kubernetes/enc/enc.yaml
identity
4

When to escalate

Validation and rollback are covered in the sections above.

Escalate

Escalate when the blast radius is uncertain, the control cannot be tested safely, or remediation requires an outage or security exception.

The KBY Brief

Fixed? Get the next one before it hits you: The KBY Brief

Practical fixes for the systems you run, by email about twice a week. Unsubscribe at any time.

About this page

Credited to

Dates

Published

Corrections

No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

Found this useful? Follow KBY for more like it.

After remediation

Further reading stays below the corrective workflow and is selected by platform, category and shared technical keywords.

More from KBY that goes with this config trap.

Discover more

Connected KBY resources