All guides

Kubernetes Secrets: encryption at rest and who can read them

Secret values are base64-encoded strings, and Kubernetes stores them unencrypted in etcd by default. Encryption at rest protects that copy. Anyone who is allowed to create a pod in a namespace can still read every Secret in it.

A person holding a phone above a laptop keyboard

What base64 does and does not do

The values for all keys in the data field of a Secret have to be base64-encoded strings. The Kubernetes documentation decodes the data of its example Secret, secret-tiger-docker, like this:

Shell

kubectl get secret secret-tiger-docker -o jsonpath='{.data.*}' | base64 -d

The same page describes a base64 value as obscured but not secret.

By default, Secrets are stored unencrypted in etcd, the API server's underlying data store. A Secret can be read or changed by anyone who has API access, and equally by anyone who can reach etcd.

Encryption at rest on self-managed clusters

The kube-apiserver process accepts the argument --encryption-provider-config, the path to an EncryptionConfiguration file. Each entry in the file names resources and an ordered list of providers. The first provider in the list encrypts resources written to storage. On a read, each provider that matches the stored data tries in order to decrypt it.

  • identity writes resources as-is, with no encryption. It is the default.
  • aescbc, aesgcm and secretbox take their key from the configuration file, so the key material is accessible from the control plane host. The documentation says a locally managed key protects against an etcd compromise and fails to protect against a host compromise. It does not recommend aescbc, and recommends aesgcm only with an automated key rotation scheme.
  • kms uses envelope encryption. Data is encrypted with a data encryption key, and that key is encrypted with a key encryption key that a remote KMS stores and manages. To reach the plain text, an attacker would need to compromise etcd and the KMS provider.

Kubernetes deprecated KMS v1 in v1.28 and turned it off by default in v1.29. KMS v2 is stable since v1.29, and Kubernetes recommends it.

YAML

apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
  - resources:
      - secrets
    providers:
      - kms:
          apiVersion: v2
          name: myKmsPluginFoo
          endpoint: unix:///tmp/socketfile-foo.sock
          timeout: 3s
      - identity: {}

The kms entry comes from the documented KMS v2 example. Replace name and endpoint with the name and the socket of your own plugin. The endpoint is the listen address of the KMS plugin's gRPC server, a UNIX domain socket. The configuration should be identical on every control plane node.

Existing Secrets and how to verify

Data is encrypted when it is written to etcd. A Secret stored before the change stays unencrypted until it is next written. The documented command reads all Secrets and writes them back:

Shell

kubectl get secrets --all-namespaces -o json | kubectl replace -f -

To verify, the documentation reads a Secret out of etcd with etcdctl. A value encrypted by the KMS v2 provider is prefixed with k8s:enc:kms:v2:.

The identity: {} entry at the end of the list allows unencrypted data to be read. Once all resources are encrypted, remove it. The API server then no longer honors unencrypted data.

What EKS, AKS and GKE do

  • Amazon EKS: clusters that run Kubernetes 1.28 or higher get envelope encryption for all Kubernetes API data by default. EKS uses KMS v2 with an AWS owned key, and you have the option to bring your own customer managed key from AWS KMS.
  • Azure AKS: Azure Storage encrypts all data at rest, and that encryption is always enabled. KMS provider encryption, with keys stored in Azure Key Vault, is an optional layer that encrypts Secrets before they are written to etcd.
  • Google GKE: GKE encrypts customer content at rest by default, Secrets included. Application-layer secrets encryption adds a key that you manage in Cloud KMS. You enable it with the --database-encryption-key flag of gcloud container clusters create, create-auto or update.

RBAC on Secrets

A Secret retrieved through the API is decrypted first.

A subject with list or watch on Secrets can read the data of every Secret in that namespace. The documentation says to restrict watch and list to the most privileged, system-level components, and to grant get only where a component's normal behavior requires it.

The resourceNames field restricts a rule to individual instances of a resource. This Role follows the documented ConfigMap example, with secrets as the resource:

YAML

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: secret-reader
rules:
- apiGroups: [""]
  resources: ["secrets"]
  resourceNames: ["db-user-pass"]
  verbs: ["get"]

Who else can read a Secret

  • Anyone who is authorized to create a pod in the namespace. With that right a user can read every Secret in the namespace. Indirect routes count as well, the right to create a Deployment for one.
  • Privileged containers. A container with privileged: true has access to every Secret in use on its node.

The documented way to isolate access to mounted Secrets is to use separate namespaces.

What to check in your cluster

  1. On your own control plane, look at the API server's arguments. With no --encryption-provider-config, encryption at rest is not enabled. The same is true when identity is the first provider in the file.
  2. Guard the KMS key. AKS documents that deleting the encryption key or the key vault makes the Secrets unrecoverable. EKS documents that deleting the customer managed key degrades the cluster beyond recovery.
  3. Follow the documented good practice for audit: write audit rules that raise an alert on specific events. One user reading several Secrets at the same time is the documented example.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides