Skip to content

fix(C-0225): harden RBAC and preserve default account provisioning - #206

Merged
matthyx merged 1 commit into
kubescape:mainfrom
ANAMASGARD:fix/c0225-serviceaccount-security-168
Dec 2, 2026
Merged

matthyx merged 1 commit into
kubescape:mainfrom
ANAMASGARD:fix/c0225-serviceaccount-security-168

Conversation

@ANAMASGARD

@ANAMASGARD ANAMASGARD commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Fixes #168.

Summary

  • Deny RoleBindings and ClusterRoleBindings that target all ServiceAccounts or all ServiceAccounts in a namespace. Both groups include the default account.
  • Require the API server's authenticated loopback identity and the bootstrap label for the built-in RBAC exemption.
  • Keep the explicit automountServiceAccountToken: false requirement. Add a companion mutating policy and binding that set the missing field on creation so the Kubernetes controller can provision the default account. Document installation on Kubernetes 1.36 and later and the webhook equivalent for older clusters.

Verification

  • Reproduced the default ServiceAccount provisioning failure on upstream main in kind with Kubernetes v1.36.1.
  • Passed all 23 C-0225 cases and all 8 helper unit tests. The C-0225 cases cover both ServiceAccount group forms on both binding kinds, bootstrap labels on user requests, and unrelated groups.
  • Verified namespace provisioning with both policies active. The controller created and recreated the default ServiceAccount with automount disabled, the validator denied enabling it, and a server-side dry-run Pod received no projected token.
  • Verified that the API server recreated its built-in ServiceAccount issuer-discovery binding after a restart with C-0225 enforced.
  • Verified 1,094 policy cases across 72 groups. The current runner hit run-control-tests: wait-for-delete ignores test-namespace #201 in C-0050 and left its binding active for later tests. The 124 affected cases passed in isolated reruns with the cleanup fix from fix(tests): wait for test Pods in their namespace #204.
  • Rendered all four release overlays and ran git diff --check.

Rebased validation

Rebased onto main (4bd6dec), including the test cleanup fix, and pushed signed-off head aae1f4f. All 14 test-runner unit tests passed. On a disposable Kind Kubernetes v1.36.1 cluster with the PR policies active, creating a fresh namespace provisioned its default ServiceAccount and returned:

serviceaccount/default condition met
serviceaccount/default condition met
default automountServiceAccountToken=false

The hosted full policy suite is a separate check.

Summary by CodeRabbit

  • New Features
    • Added enforcement that prevents RBAC bindings from granting access to the default ServiceAccount or ServiceAccount-wide groups, with a limited exception for API-server bootstrap operations.
    • Newly created default ServiceAccounts now have token automounting disabled when it is not explicitly configured. Explicitly enabling it remains disallowed.
  • Documentation
    • Updated setup guidance for installing the policies, handling existing or recreated default ServiceAccounts, and using an alternative on clusters without the required Kubernetes API.
  • Tests
    • Added coverage for the updated binding and token-automount rules.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

C-0225 now rejects additional ServiceAccount group subjects in bindings and restricts the bootstrap exemption. A companion mutating policy disables token automounting on newly created default ServiceAccounts. Workflow checks and documentation cover policy behavior and setup.

Changes: C-0225 ServiceAccount policy

Layer / File(s) Summary
Binding subject and exemption checks
controls/C-0225/policy.yaml, controls/C-0225/tests.json, docs/policies-based-on-kubescape-controls/kubescape-c-0225-deny-default-service-account-rbac-and-automount.md
The policy requires the API-server identity and bootstrap label for the exemption, and rejects bindings to ServiceAccount-wide groups. Tests and documentation reflect these checks.
Default ServiceAccount mutation
controls/C-0225/default-serviceaccount-mutation.yaml, controls/C-0225/policy.yaml
The companion mutating policy sets automountServiceAccountToken to false on default ServiceAccount creation when the field is unset. Policy comments state that validation requires an explicit false value.
Provisioning checks and installation guidance
.github/workflows/main.yaml, docs/policies-based-on-kubescape-controls/kubescape-c-0225-deny-default-service-account-rbac-and-automount.md
The workflow checks denial of a patch without the required setting and verifies the setting on a newly provisioned default ServiceAccount. Documentation describes installation order and a webhook alternative.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant NamespaceController
  participant MutatingAdmissionPolicy
  participant ValidatingAdmissionPolicy
  NamespaceController->>MutatingAdmissionPolicy: Create default ServiceAccount
  MutatingAdmissionPolicy->>ValidatingAdmissionPolicy: Set automountServiceAccountToken to false
  ValidatingAdmissionPolicy->>NamespaceController: Validate ServiceAccount request
Loading

Possibly related PRs

Suggested reviewers: matthyx

Merge Risk: Minimal · up to aae1f

This change tightens the C-0225 RBAC checks and adds a companion mutating policy so default ServiceAccounts can still be created. No outstanding merge-blocking risk was found in the supplied review context.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the C-0225 control and two changes: hardening RBAC and preserving default ServiceAccount provisioning. The objectives confirm both as the main work: closing the group-subject bypass, t…
Linked Issues check Passed Issue #168 has three coding objectives. The current head matches the previous review for each one.

(1) Group-subject bypass: controls/C-0225/policy.yaml Rule 1 adds `(subject.kind != 'Group' || !subj…
Out of Scope Changes check Passed Each changed area supports a linked objective. policy.yaml implements Rule 1 and the Rule 2 comment update. The mutation policy and binding implement objective 3. tests.json covers the group-subject a…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the C-0225 control and two changes: hardening RBAC and preserving default ServiceAccount provisioning. The objectives confirm both as the main work: closing the group-subject bypass, tightening the RBAC exemption, and adding a companion mutating policy. The title is specific and matches the changeset. The title uses the conventional commit prefix 'fix(...)', which is acceptable.

Full details: Linked Issues check

Explanation

Issue #168 has three coding objectives. The current head matches the previous review for each one.

(1) Group-subject bypass: controls/C-0225/policy.yaml Rule 1 adds (subject.kind != 'Group' || !subject.name.matches('^system:serviceaccounts(:[^:]+)?$')). This rejects both system:serviceaccounts and system:serviceaccounts:<namespace>.

(2) Exemption trust boundary: the kubernetes.io/bootstrapping: rbac-defaults exemption now also requires request.userInfo.username == 'system:apiserver' and 'system:masters' in request.userInfo.groups. This addresses the review and tightening request.

(3) Controller-created default account: controls/C-0225/default-serviceaccount-mutation.yaml adds a MutatingAdmissionPolicy and binding. On CREATE of a default ServiceAccount with no automount field, it applies automountServiceAccountToken: false. Rule 2 then no longer blocks the namespace controller. Docs and tests.json cover the new cases and the webhook alternative.

I did not re-run the runtime checks described in the PR. The code evidence supports all three objectives.

Full details: Out of Scope Changes check

Explanation

Each changed area supports a linked objective. policy.yaml implements Rule 1 and the Rule 2 comment update. The mutation policy and binding implement objective 3. tests.json covers the group-subject and exemption objectives. The docs change describes the new install steps and the webhook equivalent. The .github/workflows/main.yaml change adds a C-0225 namespace-provisioning test that exercises objective 3. The PR also reports release overlay rendering and a runner cleanup fix (#204). These items are verification or test-harness work tied to this PR. I found no concrete unrelated change.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 10f639f1-5df9-45a5-af4f-3b9726432e55
Commits

Reviewing files that changed from the base of the PR and between 593f590 and aae1f4f.

Files selected for processing: 1

  • .github/workflows/main.yaml

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@matthyx matthyx 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.

Reviewed head 593f590 against main (74e4b2e). The change looks sound, but I can't approve it yet because CI is red on this commit and the new check never ran.

Blocker (CI/validation): main-policy-automation failed in ./scripts/run-all-control-tests.sh (C-0050 leaked its binding into later tests, the known #201 problem). #204 fixed that and merged on 2026-11-29, after this branch's base f29cc53; main has been green since. The workflow exits at that step, so the new "Test C-0225 namespace provisioning" step never ran. Please rebase onto current main and get a green run, including that step.

Nonblocking:

  • The description should show the Before/After with the same command and input, for example the denied default ServiceAccount provisioning on main against the passing run on this PR. It currently only says it was reproduced.
  • The exemption now requires username system:apiserver plus system:masters and the bootstrap label. That matches the apiserver loopback identity, so I found no issue. The fix is also covered by new tests for the group and bootstrap-label cases.

Other checks: #168 is the linked issue and no duplicate PR exists (#118, #127 and #162 are earlier, merged C-0225 work). The author has 3 open PRs (#205, #206, #207), which is at the policy limit. I did not run the tests locally; this is a static review plus CI log review.

Verdict: request changes. I expect to approve once CI is green on a rebased head.

@matthyx matthyx moved this to Waiting on Author in KS PRs tracking Nov 30, 2026
Signed-off-by: Gaurav Chaudhary <chaudharygaurav2004@gmail.com>
@ANAMASGARD
ANAMASGARD force-pushed the fix/c0225-serviceaccount-security-168 branch from 593f590 to aae1f4f Compare December 2, 2026 10:20

@matthyx matthyx 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.

Re-reviewed head aae1f4f (rebased on current main 4bd6dec). The earlier blocker is resolved: main-policy-automation is green, including the new "Test C-0225 namespace provisioning" step, and DCO, CodeRabbit, GitGuardian and FOSSA are green. The system:apiserver + system:masters + bootstrap-label exemption, the new group denial and the companion mutating policy look sound, and the tests cover them. The description now includes verification and rebased validation. Validation here was static review plus CI results; I did not run tests locally.

@matthyx
matthyx merged commit 267535b into kubescape:main Dec 2, 2026
7 checks passed
@matthyx matthyx moved this from Waiting on Author to To Archive in KS PRs tracking Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Archive

Development

Successfully merging this pull request may close these issues.

C-0225: close group-subject bypass and preserve default ServiceAccount provisioning

2 participants