Skip to content

Add a CI verification check - #2567

Merged
archlitchi merged 1 commit into
Project-HAMi:masterfrom
jianzhangbjz:ci-rbac
Aug 17, 2026
Merged

Add a CI verification check#2567
archlitchi merged 1 commit into
Project-HAMi:masterfrom
jianzhangbjz:ci-rbac

Conversation

@jianzhangbjz

@jianzhangbjz jianzhangbjz commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Add a CI verification tool (hack/tools/rbaccheck/) that uses Go AST analysis to verify production code only calls Kubernetes API verbs that are granted by the scheduler ClusterRole.

The existing bash-based verify-rbac.sh greps for Nodes().Update( to catch one specific violation. The new Go tool:

  • Parses charts/hami/templates/scheduler/clusterrole.yaml to extract all
    (resource, verb) pairs granted by the scheduler ClusterRole
  • Walks Go source files using go/ast to precisely match method call chains
    like CoreV1().<Resource>().<Verb>(...)
  • Checks all resource types (Nodes, Pods, Events, ConfigMaps, etc.), not just
    Nodes
  • Avoids false positives by matching at the AST level rather than text grep

This catches violations that bash/grep misses — for example Events().Update() in pkg/util/util.go:325, where the scheduler ClusterRole only grants create, get, list on events.

Fixed PR: #2569

Which issue(s) this PR fixes:
Fixes #2329 (comment)

Special notes for your reviewer:
The tool runs via go run ./hack/tools/rbaccheck/ ./pkg/ as part of make verify. It depends on gopkg.in/yaml.v3 which is already a direct dependency.

Does this PR introduce a user-facing change?:
No — CI-only change.

This PR was Assisted by Claude Code.

Summary by CodeRabbit

Security

  • Added automated validation to ensure Kubernetes operations use only approved permissions.
  • Verification reports unauthorized access clearly, including affected locations and remediation guidance.

Quality

  • Integrated access-control checks into the full verification process.
  • Existing validation checks continue to run alongside the new security checks.

@github-actions github-actions Bot removed the kind/feature new function label Aug 11, 2026
@hami-robot hami-robot Bot added the size/M label Aug 11, 2026
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change adds a Go RBAC checker and integrates it into repository verification. The checker loads scheduler ClusterRole permissions, scans production Go files under pkg, and reports unauthorized Kubernetes client methods.

Changes

RBAC verification

Layer / File(s) Summary
RBAC policy extraction
hack/tools/rbaccheck/main.go
Defines RBAC mappings, loads scheduler ClusterRole rules, converts verbs to client methods, and discovers the repository root.
Production code scan and reporting
hack/tools/rbaccheck/main.go
Walks configured Go paths, skips excluded and test files, detects unauthorized Kubernetes client calls, and returns failures for violations or configuration errors.
Verification sequence integration
hack/verify-rbac.sh, hack/verify-all.sh
Runs the checker from the repository root and adds it after import-alias verification.

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

Suggested labels: enhancement

Suggested reviewers: ouyangluwei163

Sequence Diagram(s)

sequenceDiagram
  participant Verification as verify-all.sh
  participant Wrapper as verify-rbac.sh
  participant Checker as rbaccheck
  participant ClusterRole as Scheduler ClusterRole
  participant Source as Production Go files
  Verification->>Wrapper: invoke RBAC verification
  Wrapper->>Checker: run checker for pkg
  Checker->>ClusterRole: load permissions
  ClusterRole-->>Checker: resource and verb rules
  Checker->>Source: parse Kubernetes client calls
  Source-->>Checker: resource method calls
  Checker-->>Wrapper: report violations and exit status
  Wrapper-->>Verification: return verification result
Loading

Poem

A rabbit checks each verb with care,
Go calls hop through files laid bare.
Cluster rules define the way,
Unauthorized calls cannot stay.
Verification guards the day.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding a CI verification check for Kubernetes RBAC usage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@hack/verify-all.sh`:
- Line 35: Resolve the node RBAC mismatch before enabling the verify-rbac
invocation in the verification flow: update the production Nodes().Update()
calls in device.go to use a semantically equivalent Patch operation, or grant
the scheduler’s ClusterRole update permission for nodes if full updates are
required. Ensure verify-rbac.sh can complete successfully during make verify.

In `@hack/verify-rbac.sh`:
- Around line 44-49: Change EXCLUDE_DIRS from a quoted string to a shell array
containing each find exclusion predicate and its argument separately, then
expand it with "${EXCLUDE_DIRS[@]}" in the find command inside the
DISALLOWED_VERBS loop so pkg/vendor and pkg/.git are actually excluded.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 79eaa672-58dc-4b02-b444-b1c4388e50e8

📥 Commits

Reviewing files that changed from the base of the PR and between 634bf2b and e9d43f7.

📒 Files selected for processing (2)
  • hack/verify-all.sh
  • hack/verify-rbac.sh

Comment thread hack/verify-all.sh
Comment thread hack/verify-rbac.sh Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@hack/verify-rbac.sh`:
- Around line 94-96: Update the matches scan in verify-rbac.sh to stop
suppressing find or grep failures: remove the stderr redirection and
unconditional success fallback, and ensure traversal or matching errors
propagate as a non-zero script result while preserving the existing file and
pattern filtering.
- Around line 93-94: Update both grep invocations in the verification flow using
the pattern from verb_to_pattern so they pass fixed-string mode with grep -F --,
ensuring literal Go call patterns are matched without regular-expression
interpretation.
- Around line 57-67: Extend verb_to_pattern and ALL_VERBS to cover NodeInterface
methods UpdateStatus, DeleteCollection, Apply, and ApplyStatus, mapping them to
update, deletecollection, and patch as specified. Add separate nodes/status
handling, and make the verification logic recognize method calls whose arguments
span multiple lines.
- Around line 93-96: Update the verb-to-pattern scanning flow in verify-rbac.sh
to use Go AST/type information rather than grep text matching, so it detects
line-broken method chains, node methods including UpdateStatus and
DeleteCollection, and generated Apply/ApplyStatus calls mapped to the patch
permission. If AST-based detection cannot be implemented, explicitly document
these limitations in the script.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 50df41d8-68d5-43c3-8234-b44e21be35e2

📥 Commits

Reviewing files that changed from the base of the PR and between e9d43f7 and 220a175.

📒 Files selected for processing (1)
  • hack/verify-rbac.sh

Comment thread hack/verify-rbac.sh Outdated
Comment thread hack/verify-rbac.sh Outdated
Comment thread hack/verify-rbac.sh Outdated
Comment thread hack/verify-rbac.sh Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@hack/tools/rbaccheck/main.go`:
- Around line 97-117: Update the filepath.Walk call in the main traversal flow
to capture its returned error, and set exitCode to a nonzero value when
traversal fails. Preserve the existing per-file violation handling and
directory-skipping behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 92776584-a191-4f3c-bd8f-7df37ae7f255

📥 Commits

Reviewing files that changed from the base of the PR and between 220a175 and fe92278.

📒 Files selected for processing (2)
  • hack/tools/rbaccheck/main.go
  • hack/verify-rbac.sh

Comment thread hack/tools/rbaccheck/main.go Outdated
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 62.57% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mesutoezdil

Copy link
Copy Markdown
Contributor

This is being closed because it does not comply with the contribution guidelines.

@jianzhangbjz

jianzhangbjz commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Hi @mesutoezdil , I have updated this commit and PR description. Please have a look, thanks!

@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

/reopen

@hami-robot hami-robot Bot reopened this Aug 11, 2026
@hami-robot

hami-robot Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@jianzhangbjz: Reopened this PR.

Details

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@mesutoezdil

Copy link
Copy Markdown
Contributor

Hi @mesutoezdil , could you give more details about this closing reason? Thanks!

we have some rules here:
https://github.com/Project-HAMi/HAMi/blob/master/CONTRIBUTING.md#ai-assistance-notice

@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

Hi @mesutoezdil , got it, thanks! I have updated it, please have a look, thanks!

@mesutoezdil mesutoezdil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

checked this tool logic against current master. left two notes inline, first one is important.

Comment thread hack/verify-rbac.sh Outdated
Comment thread hack/tools/rbaccheck/main.go Outdated
@mesutoezdil

Copy link
Copy Markdown
Contributor

and look at the messages from coderrabbit and resolve it pls

@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

and look at the messages from coderrabbit and resolve it pls

I guess the CodeRabbit passed for the latest commit.

Comment thread hack/verify-rbac.sh Outdated
@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

Hi @mesutoezdil , I've updated it. Could you help have a look? Thanks!

@mesutoezdil

Copy link
Copy Markdown
Contributor

resolve the conflicts.
look at the feedbacks of coderrabbit

Comment thread hack/tools/rbaccheck/main.go
1,require CoreV1() in rbaccheck call chain to avoid false positives
2,check RBAC against the role of the binary that runs the code

Signed-off-by: Jian Zhang <jiazha666@gmail.com>
@mesutoezdil

Copy link
Copy Markdown
Contributor

/lgtm

@jianzhangbjz

Copy link
Copy Markdown
Contributor Author

Hi @Shouren , could you help approve it? Thanks!

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot

hami-robot Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, jianzhangbjz

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the approved label Aug 17, 2026
@archlitchi
archlitchi merged commit 57c967d into Project-HAMi:master Aug 17, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants