Conversation
`kubectl auth can-i` reports a denial twice, on stdout and through exit status 1, so `|| echo "no"` appended a second line instead of supplying a missing one. The resulting "no\nno" was spliced into a JSON string literal, where a bare newline is illegal, and jq rejected the whole document. The audit aborted at check 4 of 41 and wrote no report. Only an identity that cannot create pods reaches the fallback, so this never fires for an admin and always fires for a read-only audit. Take the first line, so the value is single-line whether "no" was printed once or twice. No check logic changes: `allowed` still keys off `result == "yes"`.
nvvqi
requested review from
JordanNanos,
Prathmesh234,
functionstackx and
samharshe
as code owners
September 18, 2026 03:30
Author
|
Closing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A read-only Kubernetes audit aborts at check 4 of 41 and writes no report. The RBAC probe builds a two-line value that is then spliced into a JSON string literal, so
jqrejects the document.Found on v0.2.1 and still present at
3f426ba(currentmaster).What happens
The stray
noon its own line after each warning is the tell.Cause
cluster-audit-k8s.sh:493, and the scope-less variant on 495:result=$(kubectl auth can-i "$verb" "$resource" "$scope" 2>/dev/null || echo "no")kubectl auth can-ireports its answer twice: it printsyes/noon stdout and encodes the same answer in its exit status. On a denial it printsnoand exits 1, so|| echo "no"appends a second line rather than supplying a missing one, andresultbecomes"no\nno".Line 505 splices that into a JSON string literal:
KUBECTL_AUTH_JSON_ENTRIES+=("{\"name\":\"${name}\",...,\"result\":\"${result}\"}")A bare newline is illegal inside a JSON string, so
jqrejects the whole document rather than the one field, and the audit stops with 37 checks unrun.Why it stayed hidden
It cannot happen to an admin.
create podsanswersyesand exits 0, the||never fires, andresultstays on one line. It takes an identity that cannot create pods — which is to say a read-only audit, the case a third-party collector most needs to be safe in.Fix
Take the first line, so
resultis single-line whethernowas printed once or twice:result=$({ kubectl auth can-i "$verb" "$resource" "$scope" 2>/dev/null || echo "no"; } | head -1)No check logic changes:
allowedstill keys offresult == "yes".Tests
tests/audit/test_k8s_auth_check.pyruns the realadd_kubectl_auth_checkagainst a stubkubectl, following thebashtestpattern already used for this collector. It asserts the appended entry parses as JSON for a denial at both call sites, that a grant is still recorded as allowed, and that the probe passes through the arguments it was given.Without the fix the two denial cases fail with
JSONDecodeError: Invalid control character. With it,python3 -m pytest -q tests/auditis green (1304 passed, 835 subtests).One more thing, not in this PR
The entry on line 505 is assembled by string concatenation, so any interpolated value containing a newline, a double quote or a backslash breaks the document. Building it with
jq -n --argwould escape those automatically and demote this class of failure to a display bug. Happy to send that separately if you want it.Note
Low Risk
Narrow change to RBAC probe string handling plus regression tests; no change to permission semantics beyond fixing malformed JSON on denials.
Overview
Fixes a read-only Kubernetes audit crash where denied
kubectl auth can-ianswers produced invalid RBAC JSON andjqaborted the whole report (often around check 4 of 41).In
add_kubectl_auth_check, the probe now keeps only the first line of thecan-ioutput (head -1) so a denial that printsnoand exits 1 no longer becomesno\nnowhen spliced into the manual JSON string. Allowed/denied logic is unchanged (result == "yes").Adds
tests/audit/test_k8s_auth_check.py, which runs the real shell helper against a stubkubectl(deny/allow and argument passthrough) so denials stay JSON-parseable at both scoped and cluster-wide call sites.Reviewed by Cursor Bugbot for commit f83640b. Bugbot is set up for automated code reviews on this repo. Configure here.