Repository navigation
fix(rbac): CIS 5.1.6 token_automount rule - #108
Conversation
Detect unnecessary ServiceAccount token automount across live clusters and manifests. Flags ServiceAccounts with automountServiceAccountToken unset/null/true (default SA scores MEDIUM, others LOW), and workloads (Pod, Deployment, StatefulSet, DaemonSet, ReplicaSet, Job, CronJob) that override with true. Fixes: - Exporter crashes (table/sarif/junit/policyreport) when handling non-binding findings - Deployment pod-template overrides now detected (fixture 18, unit test added) Extends k8s_client.py and cli.py to parse ServiceAccounts and workload pod specs. Adds _finding_target() helper for uniform exporter handling. Remediation includes CIS 5.1.6 reference and target-aware wording. 53 tests pass, all 5 output modes work. Verified by independent adversarial verifier (gpt-5.6-sol): Round 1 caught 2 bugs, Round 2 confirmed both fixed. Co-Authored-By: Claude Haiku 4.5 <[email protected]> Co-Authored-By: gpt-5.6-sol (adversarial verification) Signed-off-by: KK Nkama <[email protected]>
0f61ea8 to
7777805
Compare
rrskris
left a comment
There was a problem hiding this comment.
Merging this. I went looking for the usual shortcuts in an automount rule and didn't find them — several details here are ones people routinely get wrong.
What I verified rather than assumed:
_effective_automount()gets the Kubernetes default right.value is not Falsecorrectly treats unset and explicit null as "token is mounted", and your docstring calls out theautomountServiceAccountToken:bare-key-parses-as-None case. That's the actual failure mode in hand-written YAML.- No double-reporting. Pods are only flagged when they set
trueover an SA that explicitly setfalse— the genuine pod-spec-wins override. Pods that merely inherit are left to the SA-level finding. Correct precedence, and the right call on noise. _finding_key()— the sharpest catch in the PR. Token findings carry norole/binding, so without your fallback toservice_account/workloadevery automount finding would collapse to(token_automount, None, None)and collide indiff_latest_scans(). That bug would have shown up much later as "the scan diff keeps losing automount findings". Nicely spotted.- Manifests mode really does cover controllers. I checked, expecting the docs row to overclaim — it doesn't.
_WORKLOAD_TEMPLATE_KINDShandles all six kinds, and you unwrapCronJobthroughjobTemplate.spec.template.spec, which is the one nesting almost everyone gets wrong. - Remediation is properly wired, not left on the generic fallback: a
token_automountbranch in_build_rbac_remediation()producingServiceAccount 'x' in namespace 'y', a real action string, the CIS 5.1.6 citation, and theaudit_notereworded so it says "the automount opt-out for …" instead of nonsensically "the narrowing of …". Contrast with what a rule type looks like when that wiring is skipped — the remediation readsRole '?'. - Degradation is graceful. Fetching SAs/pods in their own
try/exceptmeans a scanner withoutpods/serviceaccountslist permission still returns the binding-based rules instead of failing the whole scan. Right instinct.
I ran your check_issue7_acceptance.py and it passes — all 5 output modes exit 0. That check is more valuable than it looks: token findings are the first findings with no role/binding, so SARIF/JUnit/PolicyReport builders were a real regression risk. Please don't delete it — but it belongs in tests/ as a pytest case rather than a subprocess script in control-plane/, so it runs in CI instead of only when someone remembers. Follow-up, not a blocker; I'll move it if you'd rather not.
One gap I'll fix myself
The rule ships dark for in-cluster/live mode. deploy/cronjob.yaml and the kaaval-scanner ClusterRole in docs/ci-integration.md grant only rbac.authorization.k8s.io resources, so list_service_account_for_all_namespaces() and list_pod_for_all_namespaces() both raise, you log the warning, and the rule silently finds nothing for anyone following the documented deployment. --manifests mode is unaffected.
Not your miss to chase — it's a two-line RBAC addition in two files and maintainer follow-up territory. I'm handling it right after this merge. It does mean docs/ci-integration.md's "Kaaval's own scanner role is intentionally narrow enough that Kaaval would not flag it" needs re-checking once pods/serviceaccounts read is added, which is a nice bit of dogfooding.
Worth watching, no action now
Per-SA enumeration means every namespace contributes at least one MEDIUM for its default SA, plus a LOW per additional SA. On a 50-namespace cluster that's 50+ findings that will sit above genuinely quieter rules in a raw count. CIS 5.1.6 is legitimately per-SA and the Contextual Risk Score ranks them below real escalation paths, so this is correct as built — but if the findings list starts feeling swamped, aggregating "N namespaces have an automounting default SA" into one finding is the lever. Something to see in practice before changing.
Thanks — third merged PR and the care level is going up, not down.
#119) The token_automount rule added in #108 reads ServiceAccount automount settings and pod specs. Both fetches are wrapped in their own try/except, so a scanner without those permissions logs a warning and silently returns no automount findings — meaning the rule shipped dark for anyone following the documented in-cluster deployment. Adds read-only get/list on core serviceaccounts and pods to both the kaaval-scanner-reader ClusterRole in deploy/cronjob.yaml and the documented kaaval-scanner role in docs/ci-integration.md. Also corrects a stale claim next to that role. The docs asserted Kaaval "would not flag it"; scanning deploy/cronjob.yaml shows it does, and has since the segmentation_violation rule landed in #52 — a namespaced ServiceAccount granted cluster scope via ClusterRoleBinding is exactly what that rule looks for. Verified the two added grants introduce no new findings (3 before, 3 after), so the least-privilege story holds; the parenthetical was simply wrong. Replaced it with the actual result and why it is expected. Signed-off-by: Vamshi Krishna Santhapuri <[email protected]>
Adds Unreleased entries for the five PRs merged today, following the existing "Contributed by [@user](link) (#PR)" convention: - #106 Trivy report ingestion adapter (@Diyaaa-12) - #108 CIS 5.1.6 token_automount rule (@donkk11) - #103 Helm chart skeleton (@girosole60) - #100 severity_changed bucket in the RBAC scan diff (@floze-the-genius) - #118 / #119 doc and scanner-RBAC corrections Notes the doc corrections explicitly rather than folding them in silently: the --output table had been missing sarif and junit since those shipped, api.md never documented GET /rbac/scan/diff, and the claim that Kaaval would not flag its own scanner role stopped being true when segmentation_violation landed. Signed-off-by: Vamshi Krishna Santhapuri <[email protected]>
Per @rrskris's review on kaaval#108: check_issue7_acceptance.py was a standalone script that only ran when someone remembered to invoke it by hand. Moved its logic into tests/test_issue7_acceptance.py so it runs in CI like everything else. Same assertions, same fixtures, now parametrized across the 5 output modes instead of a manual loop. Verified locally against current main (6/6 passed): ./.venv/bin/pytest tests/test_issue7_acceptance.py -v Signed-off-by: KK Nkama <[email protected]>
Summary
Implements detection of unnecessary ServiceAccount token automount (CIS Kubernetes Benchmark v1.12.0 5.1.6). Flags ServiceAccounts with automountServiceAccountToken unset/null/true (default SA scores MEDIUM, others LOW) and workloads that override with true.
What Changed
control-plane/app/rbac_service.py_finding_target()helper incontrol-plane/app/cli.pyfor all 5 export formatscontrol-plane/app/k8s_client.pycontrol-plane/app/remediation.pyDesign Notes
get_rbac_graph_data()extended withservice_accountsandpodskeysVerification
Limitations