Skip to content
Hands-on IT IT Drills

Appendix A: Cheat Sheet

The most important audit and hardening commands from the book, by topic. “Check as the attacker” means --as=system:serviceaccount:<ns>:<name>.

RBAC (Days 1–4)

# Who am I / what am I allowed to do (from inside or via --as)
kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa> -n <ns>

# Who may read Secrets cluster-wide?
kubectl auth can-i list secrets --as=system:serviceaccount:<ns>:<sa> -A

# ServiceAccounts as subjects in ClusterRoleBindings (workloads with cluster-wide rights)
kubectl get clusterrolebindings -o json | python3 -c '
import sys,json
for b in json.load(sys.stdin)["items"]:
    for s in b.get("subjects") or []:
        if s.get("kind")=="ServiceAccount":
            print(b["metadata"]["name"],"->",b["roleRef"]["name"],
                  "(",s["namespace"]+"/"+s["name"],")")'

# Track down dangerous verbs: impersonate / escalate / bind / nodes-proxy
kubectl get clusterroles -o json | python3 -c '
import sys,json
G={"impersonate","escalate","bind"}
for r in json.load(sys.stdin)["items"]:
    for rule in r.get("rules") or []:
        if G & set(rule.get("verbs") or []) or "nodes/proxy" in (rule.get("resources") or []):
            print(r["metadata"]["name"], rule.get("resources"), rule.get("verbs"))'

Hardening: minimal Role (namespaced) instead of ClusterRole, use resourceNames, automountServiceAccountToken: false as the default.

Pods & Nodes (Days 5–7)

# Privileged pods
kubectl get pods -A -o json | python3 -c '
import sys,json
[print(p["metadata"]["namespace"]+"/"+p["metadata"]["name"]) for p in json.load(sys.stdin)["items"]
 for c in p["spec"]["containers"] if (c.get("securityContext") or {}).get("privileged")]'

# hostPath / host namespaces
kubectl get pods -A -o json | python3 -c '
import sys,json
for p in json.load(sys.stdin)["items"]:
    s=p["spec"]; n=p["metadata"]["namespace"]+"/"+p["metadata"]["name"]
    if any(v.get("hostPath") for v in s.get("volumes") or []): print("hostPath",n)
    if s.get("hostPID") or s.get("hostNetwork") or s.get("hostIPC"): print("hostNS",n)'

# Enforce PSA
kubectl label namespace <ns> pod-security.kubernetes.io/enforce=restricted --overwrite

Hardening: PSA default cluster-wide (baseline at minimum, restricted where possible); nodes/proxy for the system only; port 10250 reachable only from the control plane via firewall/NetworkPolicy.

Network (Day 8)

# Default deny (ingress) per namespace
kubectl -n <ns> apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: default-deny-ingress, namespace: <ns>}
spec: {podSelector: {}, policyTypes: ["Ingress"]}
EOF

# Test reachability
kubectl -n <ns> exec <pod> -- nc -zv -w4 <ziel-svc> <port>

Services & Secrets (Days 9–10)

# NodePort/LoadBalancer inventory
kubectl get svc -A --field-selector spec.type=NodePort
kubectl get svc -A -o json | python3 -c '
import sys,json
[print(s["spec"]["type"], s["metadata"]["namespace"]+"/"+s["metadata"]["name"])
 for s in json.load(sys.stdin)["items"] if s["spec"]["type"] in ("NodePort","LoadBalancer")]'

# Plaintext secrets in Deployment env
kubectl get deploy -A -o json | python3 -c '
import sys,json
[print(d["metadata"]["namespace"]+"/"+d["metadata"]["name"], e["name"])
 for d in json.load(sys.stdin)["items"]
 for c in d["spec"]["template"]["spec"]["containers"] for e in (c.get("env") or [])
 if "value" in e and any(x in e["name"].upper() for x in ("PASS","SECRET","KEY","TOKEN"))]'

# Encryption at rest (RKE2)
sudo rke2 secrets-encrypt status

Hardening: secretKeyRef or mounted instead of plaintext env; data services with auth; encryption at rest on.

Admission & Images (Days 11–14)

# Check Kyverno policies (mode Deny? scope?)
kubectl get validatingpolicy -o custom-columns=\
NAME:.metadata.name,ACTION:.spec.validationActions

# Version triage against a CVE
kubectl version -o json | python3 -c '
import sys,json,re
v=json.load(sys.stdin)["serverVersion"]["gitVersion"]
maj,mi=map(int,re.match(r"v(\d+)\.(\d+)",v).groups())
print(v, "fixed>=1.22:", (maj,mi)>=(1,22))'

Hardening: image guardrails (allowed registries, no :latest, signatures), policies on Deny, scope by excluding the system namespaces, operations: [CREATE, UPDATE], failurePolicy: Fail.

The golden rules

  1. Least privilege: roles as narrow as possible, no cluster-admin for workloads.
  2. PSA default cluster-wide: no privileged, no hostPath, no host namespace.
  3. Default deny on the network, then open selectively.
  4. Secrets never in plaintext, encrypted at rest.
  5. Admission guardrails on Deny, with no gaps in scope.
  6. Patch – and additionally use policies as a protective wall against the next CVE.