Skip to content
Workload and AI agent identity · Chapter 2 of 20 Day 1: The problem – finding and exploiting static secrets

Day 1: The problem – finding and exploiting static secrets

Goal: set up a service with a classic database password, find every place where that password ends up afterwards, steal it with an overly generous ServiceAccount and use it · Duration: about 100 min · Prerequisite: Preparation completed, cluster with id1 and id2 running

Where we start

Check that both nodes are ready:

# on id1
kubectl get nodes

Expected (age differs):

NAME   STATUS   ROLES           AGE   VERSION
id1    Ready    control-plane   …     v1.37.2+k3s1
id2    Ready    <none>          …     v1.37.2+k3s1

You also need jq and step on id1 (step version shows 0.31.0).

Introduction

Before you learn how identity works without passwords, you should feel why you want it. Today you build a small shop of the kind that runs in a thousand companies: a PostgreSQL database whose password sits in a Kubernetes Secret, and a till application that is allowed to read Secrets. Everything is done “right” – the password is not in the image and not in the database manifest, but in a Secret, as every guide recommends.

Then you switch sides. You look for everywhere the password lies, who can read it and what an attacker can do with it after capturing nothing more than the badge of a single pod. At the end you try to change the password – and notice how hard that is.

Today you will learn:

  • why a Kubernetes Secret is a labelled envelope and not a vault,
  • where a password from a Secret turns up afterwards: in annotations, in the container runtime, in the cluster’s datastore, in pod definitions and in Git,
  • to read a ServiceAccount’s permissions with kubectl auth can-i,
  • to call the Kubernetes API directly with curl using a pod’s token – from inside and from outside,
  • why changing a static password is more than changing a Secret.

Background

The picture for today: the key under the doormat

The office building has a warehouse room. So that the till can get at the goods, somebody had a key copied and put it in an envelope marked “SECRET”. The envelope lies at reception, and whoever is allowed to collect envelopes at reception gets it.

  • The database password is the master key to the warehouse. It works for anyone who has it – the lock doesn’t ask who is unlocking it.
  • The Kubernetes Secret is the envelope. It covers the key but doesn’t lock it away: Base64 is a notation, not encryption.
  • Reception is the Kubernetes API. It hands out envelopes to anyone who has the right to them – humans and pods alike.
  • Every copy is another key under another doormat: in the datastore, in the container, in a note on the envelope, in Git.
  • Changing the key means replacing the lock – and finding every copy. Putting a new key in the envelope changes nothing about the lock.

The terms

Term Meaning in the picture
static secret password or key that is valid for a long time and shared by several parties master key
Kubernetes Secret API object that stores values Base64-encoded labelled envelope
Base64 notation for arbitrary bytes in letters and digits envelope, not a lock
last-applied-configuration annotation in which kubectl apply stores the whole manifest sticky note on the envelope
RBAC rules about who may read or change which API objects who may collect what at reception
datastore the cluster’s database (etcd, SQLite with k3s) filing cabinet behind reception
rotation replace a secret with a new one and invalidate the old one replace the lock

Remember: A Secret in Kubernetes is an envelope, not a vault. Whoever gets the envelope has the key – and the key opens for anyone.

Why static secrets are so persistent

A static password has three properties that seem harmless individually and are dangerous together. It is transferable: whoever knows it can use it from anywhere; the lock sees only the password, not the human or service behind it. It is long-lived: passwords are rarely changed, because every change means work and a risk of outage. And it multiplies: every pipeline, every manifest, every debugging session leaves a copy behind.

The counter-idea of this course is an identity that is bound to its holder, short-lived and verifiable – a badge with a photo and an expiry date instead of a key. Today, though, you look at the keys first.

Drill 1: Build the shop and open the envelope

Where you are: on id1.

What you have afterwards: a namespace laden with a PostgreSQL database on id2 whose password comes from a Secret – and that password in plain text on your screen.

Step 1 – Create a working folder.

# on id1
mkdir -p ~/kurs/01

Step 2 – Describe the namespace. A namespace separates the shop’s objects from everything else in the cluster:

# on id1
nano ~/kurs/01/namespace.yaml
~/kurs/01/namespace.yaml new file
apiVersion: v1
kind: Namespace
metadata:
  name: laden
# on id1
kubectl apply -f ~/kurs/01/namespace.yaml

Expected:

namespace/laden created

Step 3 – Create the Secret. This is how it appears in countless repositories:

# on id1
nano ~/kurs/01/lager-secret.yaml
~/kurs/01/lager-secret.yaml new file
apiVersion: v1
kind: Secret
metadata:
  name: lager-zugang
  namespace: laden
type: Opaque
stringData:
  POSTGRES_PASSWORD: Sommer2026!
Field Value Meaning
kind Secret an object for confidential values
type Opaque arbitrary key-value pairs, without a fixed format
stringData POSTGRES_PASSWORD: … values in plain text; the API server encodes them in Base64 when storing them
# on id1
kubectl apply -f ~/kurs/01/lager-secret.yaml

Expected:

secret/lager-zugang created

Step 4 – Start the database. The official PostgreSQL image reads the password for the user postgres from the environment variable POSTGRES_PASSWORD on first start. On top of that comes a Service so that other pods find the database under the name lager:

# on id1
nano ~/kurs/01/lager.yaml
~/kurs/01/lager.yaml new file
apiVersion: v1
kind: Pod
metadata:
  name: lager
  namespace: laden
  labels:
    app: lager
spec:
  nodeName: id2
  containers:
    - name: postgres
      image: postgres:17-alpine
      envFrom:
        - secretRef:
            name: lager-zugang
      ports:
        - containerPort: 5432
---
apiVersion: v1
kind: Service
metadata:
  name: lager
  namespace: laden
spec:
  selector:
    app: lager
  ports:
    - port: 5432
Field Value Meaning
nodeName id2 the database runs in the annex
envFrom.secretRef lager-zugang every key of the Secret becomes an environment variable
--- separates two objects in one file
Service selector app: lager routes connections to pods with this label
# on id1
kubectl apply -f ~/kurs/01/lager.yaml

Expected:

pod/lager created
service/lager created
# on id1
kubectl wait -n laden --for=condition=Ready pod/lager --timeout=180s

-n laden selects the namespace. The image is about 100 MB; the first download takes a moment.

Expected:

pod/lager condition met

Step 5 – Look at the envelope.

# on id1
kubectl get secret -n laden lager-zugang -o yaml

-o yaml prints the whole object as the API server stored it.

Expected:

apiVersion: v1
data:
  POSTGRES_PASSWORD: U29tbWVyMjAyNiE=
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{},"name":"lager-zugang","namespace":"laden"},"stringData":{"POSTGRES_PASSWORD":"Sommer2026!"},"type":"Opaque"}
  creationTimestamp: "2026-12-01T08:14:17Z"
  name: lager-zugang
  namespace: laden
  resourceVersion: "899"
  uid: db81c693-7f2a-4954-b13c-af2c6c5f3ed4
type: Opaque

What you see: two things. Under data, the password is Base64-encoded. And in the last-applied-configuration annotation it is in plain text: "POSTGRES_PASSWORD":"Sommer2026!". kubectl apply remembers the whole manifest there so that it can detect differences at the next apply – stringData included.

Step 6 – Fetch the Base64 value on its own.

# on id1
kubectl get secret -n laden lager-zugang -o jsonpath="{.data.POSTGRES_PASSWORD}"

-o jsonpath="{…}" prints only the field whose path is in the curly braces.

Expected:

U29tbWVyMjAyNiE=

Step 7 – Decode it.

# on id1
echo U29tbWVyMjAyNiE= | base64 -d

base64 -d turns Base64 back into the original bytes. This one pipe is itself the lesson: that’s all it takes.

Expected:

Sommer2026!

What you see: no key, no password, no computing time – Base64 is reversible for anyone. Whoever may get Secrets reads plain text.

Remember: Base64 is the envelope, not the lock. And kubectl apply sticks a copy of the plain text on the outside of the envelope as a note.

If it doesn’t work

If kubectl wait doesn’t succeed, kubectl describe pod -n laden lager shows under Events whether the image is still being pulled (Pulling) or the download fails (ErrImagePull). Without internet access from id2, the pod won’t start.

Save your progress? Sign up for free

Drill 2: The key in the annex

Where you are: on id2.

What you have afterwards: the password, read not via the API but directly from the node’s container runtime.

A Kubernetes node starts the containers. To do that, it has to know the environment variables, so the password too. Whoever is root on the node doesn’t need the API at all.

Step 1 – Find the container. Log in to id2 (ssh id2):

# on id2
sudo crictl ps --name postgres
  • crictl is the tool for the container runtime (containerd with k3s); it came with k3s.
  • ps lists running containers.
  • --name postgres filters by container name.

Expected (truncated on the right):

CONTAINER           IMAGE               CREATED             STATE               NAME
b61141be1a763       fa7e2700e9061       18 seconds ago      Running             postgres

Note the container ID from the first column – yours is different.

Step 2 – Fetch the container’s configuration. Put in your container ID:

# on id2
sudo crictl inspect b61141be1a763 > lager-inspect.json

crictl inspect prints everything the runtime knows about the container, as JSON. You save it to a file so you can look into it selectively.

Step 3 – Read the environment variables.

# on id2
jq ".info.config.envs" lager-inspect.json

.info.config.envs is the path to the list of environment variables in this JSON structure.

Expected (excerpt – the first two of 16 entries):

[
  {
    "key": "POSTGRES_PASSWORD",
    "value": "Sommer2026!"
  },
  {
    "key": "KUBERNETES_SERVICE_PORT",
    "value": "443"
  },

What you see: the password is in plain text in the container configuration on the node. The other entries are variables Kubernetes gives every container – addresses of the API server and of the lager Service.

Step 4 – And in the running process? The runtime also knows the container’s process ID on the node:

# on id2
jq ".info.pid" lager-inspect.json

Expected:

43100

Linux shows the environment of every process in /proc/<pid>/environ, with the entries separated by null bytes. strings turns that into one line per entry. Put in your process ID:

# on id2
sudo strings /proc/43100/environ

Expected (excerpt – look for POSTGRES_PASSWORD):

KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=lager
LAGER_PORT_5432_TCP_PROTO=tcp
PWD=/
PG_MAJOR=17
PG_VERSION=17.12
PGDATA=/var/lib/postgresql/data

What you see: the password is missing here. The PostgreSQL image’s start script deletes the variables that begin with POSTGRES_ before it starts the database server. The image cleans up – the runtime doesn’t: the password was in crictl inspect all the same. And most applications don’t clean up at all.

Remember: Every node a pod runs on knows its Secrets in plain text. Root on a node means: all the keys of all the pods on that node.

Save your progress? Sign up for free

Drill 3: The filing cabinet behind reception

Where you are: on id1.

What you have afterwards: proof of whether the password is stored encrypted in the cluster’s datastore.

k3s stores all API objects in an SQLite database on the server. Large clusters use etcd; the question is the same: are Secrets stored there in plain text?

Step 1 – Find the datastore.

# on id1
sudo ls -l /var/lib/rancher/k3s/server/db/

Expected:

total 12772
-rw-r--r-- 1 root root 8413184 Dec  1 08:14 state.db
-rw-r--r-- 1 root root   32768 Dec  1 08:15 state.db-shm
-rw-r--r-- 1 root root 4626792 Dec  1 08:15 state.db-wal

What you see: the database state.db and two helper files from SQLite (write-ahead log and shared memory).

Step 2 – Search for the password.

# on id1
sudo grep -c "Sommer2026!" /var/lib/rancher/k3s/server/db/state.db

grep -c counts the lines in which the pattern occurs. In a binary file, “lines” are just sections between line breaks; what matters is zero or not zero.

Expected:

2

What you see: the password is in plain text in the datastore. The Base64 notation exists only in the API’s output; what gets stored is the value itself. Whoever has a backup of this file has all of the cluster’s Secrets.

If grep counts 0 for you, the change hasn’t been carried over from the write-ahead log into the main file yet. SQLite writes new data to state.db-wal first and transfers it later. In that case search this file:

# on id1
sudo grep -c "Sommer2026!" /var/lib/rancher/k3s/server/db/state.db-wal

In the author’s lab, a freshly created Secret without an annotation was at first only there (count 3 in the WAL file, 0 in state.db) – in plain text, even though kubectl shows it only Base64-encoded.

Step 3 – Is encryption switched on?

# on id1
sudo k3s secrets-encrypt status

Expected:

Encryption Status: Disabled, no configuration file found

What you see: encryption at rest is off by default in k3s. You can switch it on with --secrets-encryption – that protects the file on disk and in backups, but not the API: whoever may get secrets still gets plain text.

Step 4 – Who can get to the cabinet?

# on id1
ls -ld /var/lib/rancher/k3s/server /var/lib/rancher/k3s/server/db

This time without sudo, as a normal user.

Expected:

drwx------ 8 root root 4096 Dec  1 08:05 /var/lib/rancher/k3s/server
ls: cannot access '/var/lib/rancher/k3s/server/db': Permission denied

What you see: the file itself would be readable by everyone (-rw-r--r--), but the folder above it only lets root in (drwx------). The protection hangs on a single permission setting.

Remember: Without encryption at rest, every Secret lies in plain text in the datastore. And even with it, anyone who may read the Secret via the API gets plain text.

Save your progress? Sign up for free

Drill 4: A till with too many permissions

Where you are: on id1, then in a shell in the pod kasse.

What you have afterwards: a ServiceAccount that may read Secrets, and proof that every process in its pod can fetch all the Secrets in the namespace.

Applications often get more permissions than necessary – “so that it works”. The till is supposed to fetch its password from the Secret itself and gets the right to read Secrets for that. Not one Secret, but all of them in the namespace.

Step 1 – Describe ServiceAccount, Role and pod.

# on id1
nano ~/kurs/01/kasse.yaml
~/kurs/01/kasse.yaml new file
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kasse
  namespace: laden
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: kasse-liest-secrets
  namespace: laden
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: kasse-liest-secrets
  namespace: laden
subjects:
  - kind: ServiceAccount
    name: kasse
    namespace: laden
roleRef:
  kind: Role
  name: kasse-liest-secrets
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: Pod
metadata:
  name: kasse
  namespace: laden
spec:
  serviceAccountName: kasse
  containers:
    - name: shell
      image: curlimages/curl:8.17.0
      command: ["sleep", "infinity"]
Object Important fields Meaning
ServiceAccount kasse the till’s identity
Role resources: ["secrets"], verbs: ["get", "list"] may read individual Secrets and list all of them
RoleBinding subjects, roleRef connects the Role to the ServiceAccount
Pod serviceAccountName: kasse the pod gets this ServiceAccount’s badge
image: curlimages/curl small image with curl

Something often forgotten is missing: resourceNames. It would let you restrict the Role to the one Secret lager-zugang. Without it, the Role applies to all Secrets in the namespace.

# on id1
kubectl apply -f ~/kurs/01/kasse.yaml

Expected:

serviceaccount/kasse created
role.rbac.authorization.k8s.io/kasse-liest-secrets created
rolebinding.rbac.authorization.k8s.io/kasse-liest-secrets created
pod/kasse created
# on id1
kubectl wait -n laden --for=condition=Ready pod/kasse --timeout=180s

Expected:

pod/kasse condition met

Step 2 – What may the till do?

# on id1
kubectl auth can-i --list -n laden --as system:serviceaccount:laden:kasse
Option Meaning
auth can-i --list lists all permissions instead of answering a single question
-n laden for this namespace
--as system:serviceaccount:laden:kasse act as if you were this ServiceAccount – only an administrator may do that

Expected (excerpt – the lines with resources; below them follow paths such as /version that every authenticated user may read):

Resources                                       Non-Resource URLs   Resource Names   Verbs
selfsubjectreviews.authentication.k8s.io        []                  []               [create]
selfsubjectaccessreviews.authorization.k8s.io   []                  []               [create]
selfsubjectrulesreviews.authorization.k8s.io    []                  []               [create]
clustertrustbundles.certificates.k8s.io         []                  []               [get list watch]
secrets                                         []                  []               [get list]

What you see: the line secrets … [get list] has an empty list [] in the Resource Names column – that means: all Secrets. Every ServiceAccount has the other lines; they let it ask who it is and what it may do.

Step 3 – For comparison: the default ServiceAccount.

# on id1
kubectl auth can-i get secrets -n laden --as system:serviceaccount:laden:default

Expected:

no

What you see: without a Role of its own, a ServiceAccount may not read Secrets. The problem is not Kubernetes, but the Role you gave the till.

Step 4 – Switch into the till. You open a shell in the pod:

# on id1
kubectl exec -it -n laden kasse -- sh
  • -i keeps input open, -t gives you a terminal – together an interactive shell.
  • sh is the shell in the image.

The prompt changes. Everything up to exit you now type inside the pod.

Step 5 – Load the badge into a variable.

# im Pod kasse
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)

$( … ) substitutes the command’s output; afterwards the variable TOKEN contains the JWT. That saves you copying the long token into every command. The command prints nothing.

Step 6 – Collect all envelopes at reception.

# im Pod kasse
curl -s --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt -H "Authorization: Bearer $TOKEN" https://kubernetes.default.svc/api/v1/namespaces/laden/secrets -o /tmp/secrets.json
Option Value Meaning
-s without progress display
--cacert …/ca.crt trust the API server only if its certificate comes from the cluster CA
-H Authorization: Bearer $TOKEN send the badge along – this is what the Kubernetes API expects
Address …/api/v1/namespaces/laden/secrets the list of all Secrets in the namespace laden
-o /tmp/secrets.json write the response to a file

Step 7 – See what came back.

# im Pod kasse
cat /tmp/secrets.json

Expected (excerpt – uid, timestamps and the managedFields block are omitted):

{
  "kind": "SecretList",
  "apiVersion": "v1",
  "metadata": {
    "resourceVersion": "981"
  },
  "items": [
    {
      "metadata": {
        "name": "lager-zugang",
        "namespace": "laden",
        "annotations": {
          "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Secret\",\"metadata\":{\"annotations\":{},\"name\":\"lager-zugang\",\"namespace\":\"laden\"},\"stringData\":{\"POSTGRES_PASSWORD\":\"Sommer2026!\"},\"type\":\"Opaque\"}\n"
        },
      },
      "data": {
        "POSTGRES_PASSWORD": "U29tbWVyMjAyNiE="
      },
      "type": "Opaque"
    }
  ]
}

What you see: a SecretList with all Secrets of the namespace – here only one, in a real environment all the team’s database, API and registry passwords. Every process in the pod can do this: the till itself, but also a library with a backdoor or an attacker who has found a hole in the till.

Step 8 – And in another namespace?

# im Pod kasse
curl -s --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt -H "Authorization: Bearer $TOKEN" https://kubernetes.default.svc/api/v1/namespaces/kube-system/secrets

Expected:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "secrets is forbidden: User \"system:serviceaccount:laden:kasse\" cannot list resource \"secrets\" in API group \"\" in the namespace \"kube-system\"",
  "reason": "Forbidden",
  "details": {
    "kind": "secrets"
  },
  "code": 403
}

What you see: 403 Forbidden – unlike the 401 on the preparation day, the API server now knows exactly who is asking (system:serviceaccount:laden:kasse), but the till may do nothing here. The Role applies only in its own namespace.

Leave the shell in the pod:

# im Pod kasse
exit

Remember: Whoever may list secrets in a namespace has all the keys of that namespace. A Role for a single Secret needs resourceNames – and preferably no Secret at all.

Save your progress? Sign up for free

Drill 5: The stolen badge

Where you are: on id1.

What you have afterwards: the till’s badge outside the cluster, the password fetched with it and a login to the database.

An attacker doesn’t stay in the pod. They take the badge with them and use it from a machine of their own. You play this out on id1 – here id1 stands for “any machine that can reach the API server over the network”.

Step 1 – Get the badge and CA certificate out of the pod.

# on id1
kubectl exec -n laden kasse -- cat /var/run/secrets/kubernetes.io/serviceaccount/token > kasse.jwt
# on id1
kubectl exec -n laden kasse -- cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt > cluster-ca.crt

Neither command prints anything. In a real incident, the attacker would have read the files through a hole in the application – for example through a path such as ../../var/run/secrets/….

Step 2 – Load the badge into a variable.

# on id1
TOKEN=$(cat kasse.jwt)

Step 3 – Fetch the Secret from outside. This time via the address at which the API server is reachable on the network:

# on id1
curl -s --cacert cluster-ca.crt -H "Authorization: Bearer $TOKEN" https://172.16.0.126:6443/api/v1/namespaces/laden/secrets/lager-zugang -o gestohlen.json

The path now ends in /secrets/lager-zugang – a single Secret instead of the list.

Step 4 – Pull out the value.

# on id1
jq -r ".data.POSTGRES_PASSWORD" gestohlen.json

-r prints the string without quotation marks.

Expected:

U29tbWVyMjAyNiE=

What you see: the badge works outside the pod exactly as it does inside. The API server checks signature, issuer, audience and expiry – not where the badge is shown. And the token, as you saw in the Preparation, is valid for a year.

Step 5 – Break into the warehouse. With the password, the attacker starts a pod with the PostgreSQL client:

# on id1
kubectl run einbrecher -n laden --restart=Never --image=postgres:17-alpine --env=PGPASSWORD=Sommer2026! -- psql -h lager -U postgres -c "select current_user;"
Option Value Meaning
run einbrecher starts a pod with this name
--restart=Never a one-off pod, no restart after it ends
--image= postgres:17-alpine the image brings the psql client
--env= PGPASSWORD=Sommer2026! psql reads the password from this variable
-- everything after it is the command in the container
psql -h lager -U postgres -c "…" connects to the lager Service as user postgres and runs a query

Expected:

pod/einbrecher created
# on id1
kubectl wait -n laden --for=jsonpath="{.status.phase}"=Succeeded pod/einbrecher --timeout=60s

--for=jsonpath=…=Succeeded waits until the field status.phase has the value Succeeded – the pod is done and has exited without errors.

Expected:

pod/einbrecher condition met
# on id1
kubectl logs -n laden einbrecher

Expected:

 current_user 
--------------
 postgres
(1 row)

What you see: logged in as postgres, the database superuser. The database cannot tell whether the till or a burglar is unlocking it – it only sees the password.

Step 6 – The next doormat. Look at the pod you just started:

# on id1
kubectl get pod -n laden einbrecher -o yaml

Expected (excerpt – look for the line with PGPASSWORD):

    - name: PGPASSWORD
      value: Sommer2026!

What you see: the password is now also in the pod definition – readable by anyone who may get pods in this namespace, a permission handed out far more often than get secrets.

# on id1
kubectl delete pod -n laden einbrecher

Expected:

pod "einbrecher" deleted from laden namespace

Remember: A stolen badge works everywhere until it expires. A stolen password works everywhere until someone replaces the lock – and every use leaves a new copy behind.

Save your progress? Sign up for free

Drill 6: Git doesn’t forget

Where you are: on id1.

What you have afterwards: a small repository in which a deleted Secret can still be found in full.

Manifests end up in Git. At some point someone notices that a Secret was checked in too, deletes the file and commits. Done?

Step 1 – Prepare Git. Git needs a name and an address for the commits. Put in your own:

# on id1
git config --global user.name "Thomas Zachmann"
# on id1
git config --global user.email [email protected]

Step 2 – Create a repository.

# on id1
git init -b main ~/kurs/01/laden-repo

-b main names the first branch main.

Expected:

Initialized empty Git repository in /home/ubuntu/kurs/01/laden-repo/.git/

Step 3 – Copy the manifests in.

# on id1
cp ~/kurs/01/lager-secret.yaml ~/kurs/01/lager.yaml ~/kurs/01/laden-repo/

Step 4 – Stage and commit.

# on id1
git -C ~/kurs/01/laden-repo add .

-C <ordner> runs Git as if you were standing in that folder. add . stages all files.

# on id1
git -C ~/kurs/01/laden-repo commit -m "Lager-Datenbank"

Expected:

[main (root-commit) 06d434f] Lager-Datenbank
 2 files changed, 36 insertions(+)
 create mode 100644 lager-secret.yaml
 create mode 100644 lager.yaml

Step 5 – “Remove the Secret”.

# on id1
git -C ~/kurs/01/laden-repo rm lager-secret.yaml

Expected:

rm 'lager-secret.yaml'
# on id1
git -C ~/kurs/01/laden-repo commit -m "Secret entfernt"

Expected:

[main 7232346] Secret entfernt
 1 file changed, 8 deletions(-)
 delete mode 100644 lager-secret.yaml

Step 6 – It’s gone from the working directory.

# on id1
ls ~/kurs/01/laden-repo

Expected:

lager.yaml

Step 7 – But not from the history.

# on id1
git -C ~/kurs/01/laden-repo log -p -S Sommer2026 --oneline
Option Meaning
log shows the commits, newest first
-p with the changes (patch) of each commit
-S Sommer2026 only commits in which this string was added or removed
--oneline each commit in one header line

Expected (excerpt – the older commit):

06d434f Lager-Datenbank
diff --git a/lager-secret.yaml b/lager-secret.yaml
new file mode 100644
index 0000000..7a1737f
--- /dev/null
+++ b/lager-secret.yaml
@@ -0,0 +1,8 @@
+apiVersion: v1
+kind: Secret
+metadata:
+  name: lager-zugang
+  namespace: laden
+type: Opaque
+stringData:
+  POSTGRES_PASSWORD: Sommer2026!

What you see: both commits appear – the one that added the password and the one that deleted it. Anyone who clones the repository gets the whole history and with it the password. This is exactly how tools like gitleaks or trufflehog work: they search the history, not just the current state.

Remember: A secret that was ever in Git counts as leaked. Deleting doesn’t help; only a new password helps – and that is harder than it sounds.

Save your progress? Sign up for free

Diagnosis task

After the break-in, the team reacts quickly: it changes the password in lager-secret.yaml from Sommer2026! to Winter2026! and applies the file with kubectl apply:

secret/lager-zugang configured

An hour later, the till reports login errors at the database, and the security department finds that the burglar is still logging in. Find out what happened. Try it yourself first before you read the solution path.

Solution path:

  1. Does the old password still work?

    # on id1
    kubectl run pruefer -n laden --restart=Never --image=postgres:17-alpine --env=PGPASSWORD=Sommer2026! -- psql -h lager -U postgres -c "select 1 as alter_zugang;"
    # on id1
    kubectl wait -n laden --for=jsonpath="{.status.phase}"=Succeeded pod/pruefer --timeout=60s
    # on id1
    kubectl logs -n laden pruefer

    Expected:

     alter_zugang 
    --------------
                1
    (1 row)

    Yes – the burglar still gets in.

  2. Does the new one work?

    # on id1
    kubectl run pruefer2 -n laden --restart=Never --image=postgres:17-alpine --env=PGPASSWORD=Winter2026! -- psql -h lager -U postgres -c "select 1 as neuer_zugang;"
    # on id1
    kubectl wait -n laden --for=jsonpath="{.status.phase}"=Failed pod/pruefer2 --timeout=60s
    # on id1
    kubectl logs -n laden pruefer2

    Expected:

    psql: error: connection to server at "lager" (10.43.222.185), port 5432 failed: FATAL:  password authentication failed for user "postgres"

    No. So every service that re-reads the Secret fails – those are the login errors.

  3. What does the running container see? On id2, with your container ID from Drill 2:

    # on id2
    sudo crictl inspect b61141be1a763 > lager-inspect.json
    # on id2
    jq ".info.config.envs[0]" lager-inspect.json

    Expected:

    {
      "key": "POSTGRES_PASSWORD",
      "value": "Sommer2026!"
    }

    Environment variables are only set when a container starts. The running pod still knows the old password.

  4. Cause: the team refilled the envelope but didn’t replace the lock. The PostgreSQL image sets POSTGRES_PASSWORD only on the very first initialization of the database. After that, the password lives in the database itself; the Secret is just a copy. A real rotation would have needed ALTER USER postgres PASSWORD '…' in the database, then the new password in the Secret, then a restart of all services that use it – in exactly this order, without a gap. And the till’s stolen token would still be valid afterwards and could fetch the new password again right away.

Clean up the test pods:

# on id1
kubectl delete pod -n laden pruefer pruefer2

Challenge

Take an inventory: list every place where the password Sommer2026! is or was stored after today. Write down your list before you read the solution. There are at least seven.

Solution:

  1. ~/kurs/01/lager-secret.yaml on id1 (now Winter2026! – but the old one was there).
  2. The Secret in the data field, Base64-encoded.
  3. The Secret’s last-applied-configuration annotation, in plain text.
  4. The datastore state.db on id1, in plain text (Drill 3).
  5. The container configuration on id2 (crictl inspect, Drill 2).
  6. The database itself, as the password hash of the user postgres.
  7. The pod definitions einbrecher and pruefer (--env=PGPASSWORD=…), as long as they existed – and in the shell history on id1, because you typed the commands.
  8. The Git history of laden-repo.
  9. The file gestohlen.json on id1 and /tmp/secrets.json in the pod kasse.

On top of that come places you didn’t see today: backups of the datastore, logs of CI pipelines, chat messages (“can you send me the DB password?”). In an incident you would have to find every one of them.

Save your progress? Sign up for free

Checkpoint

# on id1
kubectl get pods -n laden

Expected (age differs):

NAME    READY   STATUS    RESTARTS   AGE
kasse   1/1     Running   0          …
lager   1/1     Running   0          …
# on id1
kubectl auth can-i list secrets -n laden --as system:serviceaccount:laden:kasse

Expected:

yes

Leave the shop running. On Day 2 you rebuild the till so that its badge is only valid for the door it is meant for.

Test yourself

  1. A colleague says: “Our passwords are in Kubernetes Secrets, they’re encrypted.” What do you reply?
  2. How can you tell from the output of kubectl auth can-i --list that a ServiceAccount may read all Secrets of a namespace and not just one?
  3. Why does the token from a pod also work on a completely different machine?
  4. What is the difference between the Kubernetes API’s responses 401 Unauthorized and 403 Forbidden?
  5. Why isn’t it enough to change a Secret in Kubernetes to change a database password?
  6. A Secret was accidentally pushed to Git and deleted in the next commit. What needs to be done?

Answers:

  1. Base64 is not encryption; anyone can reverse it. Without encryption at rest switched on, the Secret lies in plain text in the datastore; and even with it, the API delivers plain text to anyone who may get secrets. On top of that come copies in annotations, on the nodes and in pod definitions.
  2. In the secrets line, the Resource Names column is empty ([]). If access were restricted to one Secret, its name would be there.
  3. The API server only checks signature, issuer, audience, expiry and whether the bound pod still exists – not where the request comes from. The token is a bearer instrument: whoever presents it counts as its owner.
  4. 401 means “I don’t know who you are” (no badge or an invalid one). 403 means “I know who you are, but you’re not allowed to do that”.
  5. The password is valid in the database itself; the Secret is just a copy. You have to change it in the database first, then in the Secret, then restart all services that use it – and the old one stays valid until the first step.
  6. The password counts as leaked and must be changed in the database. Rewriting the history is sensible in addition, but doesn’t replace the change – clones and forks keep the old history.

Interview note

“Are Kubernetes Secrets secure?” – Good answer: they are only as secure as RBAC, the nodes and the datastore. Base64 is not encryption; encryption at rest only protects the disk. Every node knows the Secrets of its pods, and list secrets exposes all the Secrets of a namespace. If they dig deeper: the real problem is the static password itself – better are short-lived, bound identities with which the service authenticates directly to the target, or dynamic credentials from a vault such as OpenBao.

Enterprise note

In larger environments, three things belong to basic hygiene: encryption of the datastore with a key outside the cluster (KMS plugin), a secret scanner in every pipeline and before every push (gitleaks, trufflehog), and a policy that forbids Roles with list on Secrets or without resourceNames (Kyverno, Gatekeeper). That lowers the risk but doesn’t eliminate it. The real step – services that no longer need a shared password at all – is the subject of the remaining days of this course.

Summary

  • A Kubernetes Secret stores values Base64-encoded; kubectl apply additionally puts a plain-text copy in the last-applied-configuration annotation.
  • Every node knows the Secrets of its pods in plain text (crictl inspect).
  • In the k3s datastore, Secrets lie in plain text without encryption at rest.
  • A Role with get/list on Secrets without resourceNames gives every process in the pod all the Secrets of the namespace.
  • A pod’s token also works outside the cluster – a stolen badge is enough to fetch passwords and use them to get into the database.
  • Git keeps deleted Secrets forever.
  • Changing a static password means replacing the lock, not refilling the envelope.

Commands at a glance

Command What for
kubectl get secret -n <ns> <name> -o yaml Secret with all fields and annotations
kubectl get secret … -o jsonpath="{.data.<key>}" fetch a single value
echo <wert> \| base64 -d decode Base64
sudo crictl ps --name <name> find a container on the node
sudo crictl inspect <id> > datei.json container configuration including environment
sudo strings /proc/<pid>/environ environment of a running process
sudo k3s secrets-encrypt status check encryption at rest
kubectl auth can-i --list -n <ns> --as <user> all permissions of a user or ServiceAccount
kubectl exec -it -n <ns> <pod> -- sh interactive shell in the pod
curl --cacert ca.crt -H "Authorization: Bearer $TOKEN" <api-url> call the Kubernetes API directly
kubectl run <name> --restart=Never --image=… -- <befehl> start a one-off pod
kubectl wait --for=jsonpath="{.status.phase}"=Succeeded pod/<name> wait until a pod is done
git log -p -S <text> find commits in which a text was added or removed

Contents

More