Kubernetes: assess, fix and retest a cluster

Unit 5 of 5 in Cloud identity, network and data protection.

You will be able to assess what a pod can see and reach, find risky workload settings, measure network paths and permissions, and prove a guard rail holds.

Lesson

A checklist, and the assess, fix, retest loop

The Kubernetes security checklist is a baseline list, not an exhaustive one, and it says the order of topics does not reflect priority. Its items include that the Kubernetes API, the kubelet API and etcd are not exposed publicly on the internet, that encryption at rest is configured for the Secret API, and that service account tokens are not mounted in pods that do not need them.

The labs in this unit are one series on one cluster. You first build a small cluster on your own computer with nine weaknesses planted on purpose and written into a register, so you know the answer key. Then you measure it from the inside, scan its workloads, map its network paths and audit its permissions, and the capstone fixes the cluster in order of risk, adds a guard rail and re-measures with the same scripts. A fix without a re-measurement is a belief, not evidence.

What one pod can see and reach

A pod gets things mounted and injected for it. The checklist says tokens should not be mounted into pods that do not need them and that this is configured with automountServiceAccountToken. The pod-assessment lab asks four questions from inside a probe pod in your cluster: which files are mounted, which environment variables are injected, what the pod may do at the API server and what the network lets it reach. It asks the API what is allowed instead of reading any secret, and writes a blast radius statement.

The checklist also says to avoid the system:masters group. The RBAC good practices page explains why: a member of that group bypasses all RBAC checks and has unrestricted access that cannot be revoked by removing role bindings.

Workload settings and the Pod Security Standards

The Pod Security Standards define three cumulative policies. Privileged is unrestricted and allows known privilege escalations. Baseline is minimally restrictive and prevents known escalations while allowing a default pod configuration. Restricted is heavily restricted and follows current pod hardening best practice. Privileged pods can bypass typical container isolation, for example by using the node's host network.

The built-in Pod Security Admission controller enforces them, stable since Kubernetes v1.25. You set labels on a namespace to pick a level and a mode. In enforce mode a violating pod is rejected, in audit mode a violation adds an annotation to the audit log and the pod is allowed, and in warn mode the user sees a warning and the pod is allowed. A namespace can use more than one mode, which gives a safe rollout order: warn and audit first, then enforce.

The workload-scan lab writes a jq program over every pod that finds privileged, host-level, host path and Docker socket settings, missing limits and writable root filesystems, then scores itself against the register and cross-checks with a server-side dry run. It also separates your workloads from the expected privileges of system components, which is how you avoid a report full of noise.

Network policy: default allow, default deny

Kubernetes says that by default, if no policies exist in a namespace, all ingress and egress traffic is allowed to and from pods in it, and that a pod is non-isolated until a policy selects it. Policies are additive: they do not conflict, and if any policy applies to a pod, traffic is allowed if any policy allows it. A default deny policy selects all pods and allows nothing, and you then add the allowed paths. The page cautions that a default deny egress policy also blocks DNS, so workloads that need name lookup need a separate allow.

The page also says your cluster must use a network plugin that supports NetworkPolicy enforcement. That is the trap the reachability lab measures: you write a script that tests who can reach whom, apply default-deny, and diff the matrix. If the matrix does not change, the policy was accepted by the API but not enforced by the network plugin. The lab tests its own tester with a port that must say blocked.

RBAC: the verb is where the danger lives

The Kubernetes RBAC good practices page lists the basics: assign permissions at the namespace level where possible with RoleBindings rather than ClusterRoleBindings, avoid wildcard permissions because they also cover object types created in the future, and do not use cluster-admin accounts except where needed. It also names escalation routes such as the escalate verb, which lets users effectively raise their own privileges.

The RBAC audit lab applies deliberately over-permissive rules, finds the risky permissions with a tool, explains why each verb is dangerous, scopes them down and re-runs the audit. The capstone then removes a cluster-admin binding on a default service account, turns off token mounting, writes a network policy, enforces Pod Security Admission and tries to bring a weak pod back to prove the guard rail blocks it.

Key points

Sources

Practice in the labs

Build a deliberately weak cluster on your own computer, then assess it from the inside, scan its workloads, measure its network paths and audit its permissions. Docker and kind needed; about 4 hours in total, so spread it over sessions. Keep the cluster for the capstone.

Knowledge check

  1. What is the default network behavior in a Kubernetes namespace with no network policies? Answer: All ingress and egress traffic to and from pods is allowed. Kubernetes says that by default, if no policies exist in a namespace, all ingress and egress traffic is allowed to and from pods in that namespace. A default deny policy changes that.
  2. You apply a default-deny policy and the reachability matrix does not change. What is the likely explanation? Answer: The network plugin does not enforce NetworkPolicy, so the API accepted a policy nothing enforces. Kubernetes says the cluster must use a network plugin that supports NetworkPolicy enforcement. Measuring before and after is how you catch a policy that is accepted but not enforced.
  3. In Pod Security Admission, what does warn mode do with a violating pod? Answer: Shows the user a warning and still allows the pod. Enforce rejects, audit adds an audit annotation and allows, and warn shows a user-facing warning and allows. That is why warn and audit are used first when rolling out a stricter level.
  4. Why does the Kubernetes RBAC guide advise against wildcard permissions? Answer: They give rights to every existing object type and to types created in the future. Because Kubernetes is extensible, the guide says wildcard access covers not only the object types that exist now but also those created later, which is rarely what anyone intended.

Back to the syllabus or see all classes.

Related