Lesson 18 of 20 · Practice: Vault, egress and tenant labs
Lab: attack your own tenant setup
A hands-on lab on one kind cluster: set up a tenant with two namespaces, real certificate users, group RBAC, quota, limits, Pod Security and NetworkPolicy, then attack it like an insider: try to escalate, read Secrets you have no access to, test a CI token, and break each guardrail on purpose to see exactly how it fails.
Open the lab on GitHublab-01-tenant-rbac: manifests, a user-creation script and a can-i test matrix
What this lab is for
You already know how to build a tenant: Teams on a shared Kubernetes cluster walks through every object (namespace labels, group RBAC, quota, LimitRange, NetworkPolicy), and Multi-tenancy in Kubernetes Administration explains the models. This lab spends its time on the other half: proving the guardrails hold, the way an insider or an attacker would test them.
Rule for the whole lab: before each command, predict the result (allowed or forbidden, and why). A wrong guess is the thing to go and learn.
tenant "acme" another tenant
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ acme-dev │ │ acme-prod │ │ other-team │
│ exec allowed │ │ CI may patch │ └──────────────┘
└──────────────┘ └──────────────┘
alice (acme-admins) · bob (acme-devs) · carol (viewer) · acme-ci (ServiceAccount)
You've built a playground with fences, gates and rules. Now you put on a pupil's cap and try every trick to get out of bounds. Every gap you find today is one nobody else finds tomorrow.
What you need
- Docker, kind,
kubectl,openssl, about 2 CPUs and 4 GiB free. - The companion repo:
git clone https://github.com/infra-ops-lab/k8s-security-labs
1. Build the tenant (fast path)
kind create cluster --name ops-lab
kubectl create namespace other-team
cd k8s-security-labs/lab-01-tenant-rbac && export LAB=$PWD
$LAB/scripts/make-user.sh alice acme-admins # real x509 user through the CSR API, context alice@ops-lab
$LAB/scripts/make-user.sh bob acme-devs
kubectl apply -f $LAB/manifests/platform/ # namespaces, RBAC, quota, limits, NetworkPolicy
What you just applied, and where each piece is explained:
| File | What it sets up | Explained in |
|---|---|---|
02-namespaces.yaml |
acme-dev, acme-prod, tenant label, PSA restricted |
Pod Security & admission control |
04a–04d RBAC |
admin → acme-admins; custom tenant-developer (no Secrets) → acme-devs; exec only in dev; view → carol; CI may only patch Deployments in prod |
RBAC design at scale |
06, 07 |
ResourceQuota (no LoadBalancer/NodePort) and LimitRange defaults | Teams on a shared Kubernetes cluster |
08-networkpolicy.yaml |
Default-deny ingress, allow from the same tenant | Network policies & mTLS |
scripts/make-user.sh |
Key → CSR → approve → kubeconfig context | Authentication methods |
Check who you are as each person:
kubectl --context alice@ops-lab auth whoami # alice, groups [acme-admins system:authenticated]
kubectl --context bob@ops-lab auth whoami
2. The access matrix
$LAB/scripts/check-rbac.sh # each check, the answer, and the expected answer
Cover the "expect" column and predict first. A few of the checks:
| Who | Action | Expected | Why |
|---|---|---|---|
| bob (acme-devs) | get secrets in acme-prod |
no | tenant-developer has no Secrets |
| bob | create pods/exec in acme-prod |
no | the exec Role exists only in dev |
| acme-ci | create deployments in acme-prod |
no | CI may only patch existing ones |
| alice (acme-admins) | update resourcequotas in acme-dev |
no | admin can't change quota |
| alice | patch namespaces/acme-dev |
no | the Namespace is cluster-scoped; she can't remove PSA labels |
Always pass the groups when impersonating
kubectl auth can-i ... --as=bob tests user bob with no groups, so bindings to acme-devs don't count and you get a wrong "no". Use --as=bob --as-group=acme-devs, or test with bob's real certificate (--context bob@ops-lab).
3. Attack 1: escalate as the tenant admin
# give herself cluster-admin inside her namespace
kubectl --context alice@ops-lab -n acme-dev create rolebinding oops --clusterrole=cluster-admin --user=alice
# give "edit" to a new teammate
kubectl --context alice@ops-lab -n acme-dev create rolebinding dave-edit --clusterrole=edit --user=dave
kubectl --context alice@ops-lab -n acme-dev delete rolebinding dave-edit
# bind a role in another tenant's namespace
kubectl --context alice@ops-lab -n other-team create rolebinding x --clusterrole=view --user=alice
Results: forbidden, allowed, forbidden. Escalation prevention: you may bind a role only if you already hold every permission in it (or have the bind verb on that role). alice holds everything in edit inside acme-dev, but not cluster-admin, and nothing in other-team.
4. Attack 2: read a Secret without Secret access
As the cluster admin, create a Secret that bob must not see:
kubectl -n acme-dev create secret generic db-pass --from-literal=password=hunter2
kubectl --context bob@ops-lab -n acme-dev get secret db-pass # Forbidden. Good?
Now bob uses a right he does have, creating pods:
kubectl --context bob@ops-lab -n acme-dev apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: { name: peek }
spec:
securityContext: { runAsNonRoot: true, runAsUser: 1000, seccompProfile: { type: RuntimeDefault } }
containers:
- name: c
image: busybox:1.37
command: ["sh", "-c", "cat /s/password; echo; sleep 3600"]
securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ["ALL"] } }
volumeMounts: [ { name: s, mountPath: /s, readOnly: true } ]
volumes: [ { name: s, secret: { secretName: db-pass } } ]
EOF
kubectl --context bob@ops-lab -n acme-dev logs peek # hunter2
kubectl --context bob@ops-lab -n acme-dev delete pod peek
"Can create pods" means "can read every Secret, and act as every ServiceAccount, in that namespace." Pod Security doesn't stop this: the pod is perfectly restricted. The real fixes are: keep high-value secrets out of namespaces where many people can create pods, deliver them from an external store bound to one ServiceAccount (Vault on Kubernetes), and treat workload-creation rights as secret access when you review RBAC (RBAC design at scale lists the other hidden paths).
5. Attack 3: what can the CI token do?
TOKEN=$(kubectl create token acme-ci -n acme-prod --duration=10m)
kubectl --token="$TOKEN" auth whoami # system:serviceaccount:acme-prod:acme-ci
kubectl --token="$TOKEN" -n acme-prod get deployments # allowed
kubectl --token="$TOKEN" -n acme-prod get pods # Forbidden
kubectl --token="$TOKEN" -n acme-prod create deployment x --image=busybox:1.37 # Forbidden
A leaked CI token can roll out new versions of existing Deployments, and nothing else. That's the point of a narrow Role. (Think about what it can still do: patch a Deployment's image to anything. Signed-image admission from Supply chain security closes that gap.)
6. Break every guardrail on purpose
First, deploy a workload as bob:
kubectl --context bob@ops-lab -n acme-dev apply -f $LAB/manifests/tenant/09-workload-web.yaml
kubectl --context bob@ops-lab -n acme-dev rollout status deploy/web
6a. Over quota
kubectl --context bob@ops-lab -n acme-dev scale deploy/web --replicas=12
kubectl -n acme-dev get deploy web # READY stops below 12
kubectl -n acme-dev get events --field-selector reason=FailedCreate
# ... exceeded quota: acme-quota, requested: requests.cpu=100m, used: requests.cpu=1, limited: requests.cpu=1
6b. Over the LimitRange maximum
kubectl --context bob@ops-lab -n acme-dev scale deploy/web --replicas=2
kubectl --context bob@ops-lab -n acme-dev apply -f $LAB/manifests/tenant/10b-break-limitrange.yaml # asks for a 2-CPU limit, max is 1
kubectl -n acme-dev get events --field-selector reason=FailedCreate | tail -3
# ... maximum cpu usage per Container is 1, but limit is 2
6c. A Pod Security violation
kubectl --context bob@ops-lab -n acme-dev apply -f $LAB/manifests/tenant/10c-break-podsecurity.yaml # privileged: true
kubectl -n acme-dev get deploy rooty # created, 0 ready
kubectl -n acme-dev get events --field-selector reason=FailedCreate | grep -i podsecurity
All three look the same from the outside: a Deployment with 0 ready and no pod events. The reason is one level up, on the ReplicaSet, as a FailedCreate event. Pod Security in enforce mode checks pods, so the Deployment is accepted and only the pods are refused. One command shows every blocked workload:
kubectl get events -A --field-selector reason=FailedCreate \
-o custom-columns=NS:.involvedObject.namespace,KIND:.involvedObject.kind,NAME:.involvedObject.name,MSG:.message
6d. Cross-tenant network
kubectl -n other-team run probe --rm -it --restart=Never --image=busybox:1.37 -- wget -qO- -T 3 http://web.acme-dev:8080
# expect a timeout: another tenant
Then run the same probe from acme-prod (same tenant): it should answer hello-acme. The probe pod must be restricted-compliant there, so writing its --overrides is a good exercise. On kind, NetworkPolicy is enforced by kindnet in recent releases; on an older kind, install a CNI that enforces it, or this test passes for the wrong reason.
7. One design limit: resourceNames
Give alice read access to exactly one cluster-scoped object by name:
kubectl auth can-i get namespaces/acme-dev --as=alice --as-group=acme-admins # no: Namespace is cluster-scoped
A ClusterRole with resourceNames: [acme-dev] and verbs: [get] on namespaces, bound with a ClusterRoleBinding, makes the get work. But a plain list stays forbidden, and you can't make "list only my namespaces" work with RBAC: RBAC never filters a list. With resourceNames, list works only for that one exact name (--field-selector metadata.name=acme-dev), and create can't be limited at all. The repo's step 12 shows the same limit with a custom report type. RBAC design at scale has the design options.
8. Clean up
kubectl delete ns acme-dev acme-prod other-team
kubectl delete -f $LAB/manifests/platform/04b-rbac-tenant-developer.yaml --ignore-not-found
kubectl delete csr alice bob
for u in alice bob; do kubectl config delete-context $u@ops-lab; kubectl config delete-user $u; done
rm -rf $LAB/certs
kind delete cluster --name ops-lab # when you're done with lesson 19 too
Check yourself
1. A developer has no Secret access but can create pods. Are the Secrets in that namespace safe?
No. A pod can mount any Secret and run as any ServiceAccount in its namespace. Workload-creation rights equal Secret access there.
2. How do you remove a user's access if they log in with a client certificate?
Delete the bindings that name that user. If their access comes through a group, there's no clean way: the group is written inside the certificate, which can't be revoked before it expires. That's why certificate lifetimes should be short and people should log in with OIDC (lesson 19 shows the day-2 cases).
3. You lower a namespace's quota below current use. What happens to running pods?
Nothing. Quota is checked at admission, when objects are created. New pods are refused until usage drops.
4. Why can't alice remove the Pod Security labels from acme-dev?
The Namespace object is cluster-scoped. Her admin rights come from a RoleBinding inside the namespace, which grants nothing on the Namespace object itself.
Recap
- Build with the fast path, then test like an attacker: escalation, Secrets through a pod, the CI token, cross-tenant traffic.
- Escalation prevention stops binding roles you don't hold; pod creation is still a path to every Secret in the namespace.
- Quota, LimitRange and Pod Security all fail the same way: Deployment 0 ready,
FailedCreateon the ReplicaSet. resourceNamescan't turnlistinto "list only mine", and can't limitcreate.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.