Skip to content

fix(rbac): CIS 5.1.6 token_automount rule - #108

Merged
rrskris merged 1 commit into
kaaval:mainfrom
donkk11:fix/issue7-token-automount
Jul 26, 2026
Merged

rrskris merged 1 commit into
kaaval:mainfrom
donkk11:fix/issue7-token-automount

Conversation

@donkk11

@donkk11 donkk11 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

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

  • Detector in control-plane/app/rbac_service.py
  • Parser + _finding_target() helper in control-plane/app/cli.py for all 5 export formats
  • Live collector extension in control-plane/app/k8s_client.py
  • CIS 5.1.6 remediation in control-plane/app/remediation.py
  • Fixtures 13–18, 10 new unit tests

Design Notes

  • get_rbac_graph_data() extended with service_accounts and pods keys
  • Fixed exporter crash bug (table/sarif/junit/policyreport)
  • Workload pod-template overrides now detected

Verification

  • 53 tests pass; all 5 output modes work
  • Nkama evidence layer: clean_pass true
  • Independent verifier (gpt-5.6-sol): Round 1 caught 2 bugs, Round 2 confirmed fixed

Limitations

  • Static "necessary" judgment; live-cluster input is best-effort

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]>
@donkk11
donkk11 force-pushed the fix/issue7-token-automount branch from 0f61ea8 to 7777805 Compare July 22, 2026 00:59

@rrskris rrskris left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 False correctly treats unset and explicit null as "token is mounted", and your docstring calls out the automountServiceAccountToken: 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 true over an SA that explicitly set false — 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 no role/binding, so without your fallback to service_account/workload every automount finding would collapse to (token_automount, None, None) and collide in diff_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_KINDS handles all six kinds, and you unwrap CronJob through jobTemplate.spec.template.spec, which is the one nesting almost everyone gets wrong.
  • Remediation is properly wired, not left on the generic fallback: a token_automount branch in _build_rbac_remediation() producing ServiceAccount 'x' in namespace 'y', a real action string, the CIS 5.1.6 citation, and the audit_note reworded 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 reads Role '?'.
  • Degradation is graceful. Fetching SAs/pods in their own try/except means a scanner without pods/serviceaccounts list 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.

@rrskris
rrskris merged commit 6814251 into kaaval:main Jul 26, 2026
7 checks passed
rrskris added a commit that referenced this pull request Jul 26, 2026
#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]>
rrskris added a commit that referenced this pull request Jul 26, 2026
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]>
donkk11 added a commit to donkk11/kaaval that referenced this pull request Aug 7, 2026
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]>
rrskris pushed a commit that referenced this pull request Aug 13, 2026
Moves the standalone check_issue7_acceptance.py script into tests/ so the CIS 5.1.6 token_automount acceptance check runs in CI instead of by hand. Keeps the 5-output-mode regression guard (token findings carry no role/binding).

Closes #7 follow-up from the #108 review.

Thanks @donkk11.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants