Skip to main content
Security & multi-tenancyLesson 5 of 13

Keeping Secrets Safe

A Kubernetes Secret keeps a password out of the image, but by itself it does not keep the password safe. Follow one DB password from kubectl apply into etcd, out to readers, into Git, and finally into an external vault that rotates it. How a Secret reaches a Pod as an env var or a file is covered in Config and Secrets.

Value in etcd?
Who can read it?
In Git?
RotationManual
Kubernetes clusterDeveloperkubectl applyczNjcjN0Git repomanifestsAPI serverencrypts on writeetcd/registry/secretsKMSholds the KEKExternal SecretsoperatorSecretdb-passSecret vaultVault · cloud storeOver-broad userlist secretsPodorders appapply Secretstore
RequestResponse / reportAppears in a later step

What you will learn

  • base64 is encoding, not encryption
  • Encryption at rest and KMS v2 envelope encryption
  • Why list on secrets or create on Pods exposes every value
  • Keeping secrets out of Git: Sealed Secrets, SOPS, external stores
  • Syncing, rotating and short-lived credentials from a vault

Ways to handle secrets compared​

ApproachWhere the value livesIn GitRotation
Plain Secretetcd (plain unless encrypted at rest)Plain base64 if committed: avoidManual kubectl apply
Sealed Secretsetcd, after the controller decryptsCiphertext (SealedSecret)Re-seal and commit a new value
SOPSetcd, after decrypt at deploy timeCiphertext valuesRe-encrypt and commit
External Secrets OperatorExternal vault, copied into a SecretOnly a reference (ExternalSecret)In the vault, picked up every refreshInterval
Secrets Store CSI DriverExternal vault, mounted as a file (Secret optional)Only a reference (SecretProviderClass)In the vault, file refreshed if rotation is enabled
Dynamic credentialsIssued per request by the vaultNothingAutomatic, each credential expires

Try it yourself​

kubectl create secret generic db-pass --from-literal=password=s3cr3t
kubectl get secret db-pass -o jsonpath='{.data.password}' | base64 -d # base64 is not encryption

# Lab cluster only, on a control plane node: read the raw etcd value
sudo 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/db-pass | hexdump -C | head # s3cr3t visible, or k8s:enc:… if encrypted

kubectl auth can-i list secrets -n default --as=system:serviceaccount:default:default # should be "no"
kubectl get secrets -A -o json | kubectl replace -f - # rewrite all Secrets after enabling encryption
kubectl get externalsecret -A # STATUS SecretSynced, REFRESH INTERVAL
kubectl get clustersecretstore,secretstore -A # Can the operator reach the vault?

Check yourself

  1. You turn on encryption at rest with an EncryptionConfiguration. Do the Secrets that already exist become encrypted in etcd?
  2. etcd is encrypted with KMS v2. A user has list on secrets in namespace shop. What do they get?
  3. A password is rotated in the vault, and the External Secrets Operator updates the Secret. The app still uses the old password. What is the most likely reason?

Further reading: Good practices for Kubernetes Secrets · Encrypting confidential data at rest · KMS provider for data encryption · External Secrets Operator · Secrets Store CSI Driver · Sealed Secrets · SOPS · Config and Secrets · Audit Logs