Skip to content

feat(audit): JSON-lines audit-log parser -> per-subject usage set (#93) - #101

Closed
girosole60 wants to merge 1 commit into
kaaval:mainfrom
girosole60:feat/audit-ingest-93
Closed

girosole60 wants to merge 1 commit into
kaaval:mainfrom
girosole60:feat/audit-ingest-93

Conversation

@girosole60

@girosole60 girosole60 commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements parse_audit_usage(lines) -> dict[subject_key, set[tuple]] in a new control-plane/app/audit_ingest.py — a pure, file-based adapter for the used side of the granted-vs-used least-privilege diff (issue #93, part of #50).

Design notes

  • Subject keys match the Effective Access Graph convention (Effective Access Graph: build_effective_access() subject-keyed rule aggregation #84/Design: audit-log adapter interface (file-based first) #92): the Kubernetes identity string user.username (e.g. system:serviceaccount:ns:sa or a plain username). This keeps the parsed usage set directly diffable against build_effective_access().
  • Pure function: no cluster / DB / file I/O — caller passes an iterable of lines (open file object or list of strings).
  • Resilience (mirrors the pattern in tests/test_error_surfacing.py): a single malformed line, missing user info, or missing verb is skipped + counted via logger.warning; the batch never crashes. Empty input yields {}. Non-resource requests (no objectRef.resource) are not recorded as usage.

Acceptance criteria coverage

  • Handles valid events, unknown verbs, missing user info, malformed JSON lines (skipped + counted), empty file.
  • Subject keys match the Design: audit-log adapter interface (file-based first) #92 convention (user.username).
  • Table-driven tests with realistic (names-scrubbed) apiserver audit-event fixtures — tests/test_audit_ingest.py.

Verify locally

cd control-plane
KAAVAL_ADMIN_PASSWORD=test-admin-password python -m pytest tests/test_audit_ingest.py -v

All 14 tests pass.

Follow-ons (out of scope here)

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

Welcome, and thanks — the parser itself is good work. I checked out the branch and ran the full suite locally: 82 passed, including your table-driven cases. Design calls I agree with: recording unknown verbs instead of dropping them (surface what actually happened), and excluding non-resource URLs from usage.

Three things block the merge, none of them about the parser code:

  1. This branch is stacked on the Helm-chart branch (#102) — the diff carries the entire deploy/helm/ tree. #102 needs changes (see my review there), and the audit parser doesn't depend on it. Please rebase onto main so this PR is only audit_ingest.py + test_audit_ingest.py.

  2. DCO check is failing: neither commit has a Signed-off-by trailer. Every commit needs one (git commit -s); after the rebase, git commit --amend -s covers it.

  3. Git author identity: the commits are authored as "哈哈哈哈哈". The DCO sign-off is a certification of authorship, so it needs to be a name you actually go by (a consistent handle like your GitHub username is fine) — an obvious joke string undermines what the sign-off certifies. git config user.name girosole60 before amending works.

One small code note for the rebase, take it or leave it: the os.environ.setdefault("KAAVAL_ADMIN_PASSWORD", ...) at the top of the test file works, but the suite convention is to pass the env var at invocation (see the other test files / CI) — the module under test doesn't read it, so the line can likely just be dropped.

Happy to merge quickly once it's rebased and signed off.

@rrskris

rrskris commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Same three asks as before, now with a concrete example of what "fixed" looks like: #103 is your resubmission of the Helm skeleton after #102's review, and it's landed well — correct appVersion, a real postgresql dependency block, no invented services, scope held to exactly what the issue asked for. That's the bar.

For this one:

  1. Rebase onto main — feat: add RBAC scan diff endpoint #82 (the diff endpoint this stacks on) is merged now, so the diff here should shrink to just audit_ingest.py + test_audit_ingest.py.
  2. DCO sign-off on the commit(s) — still failing.
  3. Git identity — same note as on feat(helm): add Helm chart skeleton (Chart.yaml, values.yaml, templates) (#95) #102: "哈哈哈哈哈" isn't a name the DCO certification can mean anything against. A real handle (your GitHub username is fine) fixes it.

The parser itself already tested clean locally (82 passed on the unrebased branch) — this is purely the mechanical cleanup, not a rewrite. Happy to merge quickly once it's through.

@girosole60

Copy link
Copy Markdown
Contributor Author

All three items from the earlier review are addressed:

  1. ✅ Rebased onto main — diff is now only audit_ingest.py + test_audit_ingest.py
  2. ✅ DCO sign-off present on commit (Signed-off-by: girosole60 <[email protected]>)
  3. ✅ Author identity set to girosole60
  4. ✅ Removed os.environ.setdefault("KAAVAL_ADMIN_PASSWORD", ...) from test file

Please take another look when you get a chance. 🙏

@rrskris

rrskris commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Two of the three are genuinely done, and I checked rather than assumed:

  1. ✅ Rebased — the diff is now exactly audit_ingest.py + test_audit_ingest.py, no Helm tree.
  2. ✅ Sign-off present and the DCO check passes.
  3. ❌ Author identity is unchanged.

On item 3 — your comment says it's addressed, but the commit on this branch is still:

dd0c731b  author: 哈哈哈哈哈 <[email protected]>
          Signed-off-by: girosole60 <[email protected]>

I think what happened is you set git config user.name, which changes the committer and the sign-off trailer, but the author field on an existing commit isn't rewritten by --amend -s alone. So the trailer now certifies girosole60 while git attributes the authorship to 哈哈哈哈哈 — the two disagree, and it's that disagreement rather than the name itself that undermines the certification.

Also worth fixing for your own sake: [email protected] is a local machine default, so these commits won't be attributed to your GitHub account in the contributor graph. You'd be doing the work and not getting credit for it.

The amend needs --author explicitly:

git commit --amend --author="girosole60 <[email protected]>" --no-edit
git config user.name girosole60
git config user.email [email protected]   # so future commits are right
git push --force-with-lease

Everything else here is ready — the parser was good work in the first review and the rebase is clean. This is the last thing between it and a merge, and CI is green.

For what it's worth, this is the only place it's still outstanding: you fixed the identity correctly on #103 and #107, and #103 is now merged — your first. Same one-line fix here and this follows it.

…kaaval#93)

Implements parse_audit_usage(lines) -> dict[subject_key, set[tuple]]
in control-plane/app/audit_ingest.py as a pure, file-based adapter.

- Subject keys match the Effective Access Graph convention (kaaval#84/kaaval#92):
  user.username (e.g. system:serviceaccount:ns:sa or a plain username).
- Resilient: malformed lines, missing user info, and missing verbs are
  skipped + counted via logger.warning; never raises on a single bad line.
- Non-resource requests and empty input are handled.
- Table-driven tests with realistic (names-scrubbed) apiserver fixtures;
  all 14 pass with the issue's verify command.

Signed-off-by: girosole60 <[email protected]>
@girosole60

Copy link
Copy Markdown
Contributor Author

Rebased onto main (6543c0e) and fixed the remaining blocker.

  • Author identity — both author and committer are now girosole60 <[email protected]>; the joke string is gone, and the sign-off and the author now agree. Sign-off trailer is present exactly once.
  • Also dropped the now-unused import os left behind when the os.environ.setdefault(...) line you flagged was removed — it had become the only remaining reference to os in the file.
  • Verified locally from the rebased branch: KAAVAL_ADMIN_PASSWORD=test-admin-password python -m pytest tests/test_audit_ingest.py -q → 14 passed.
  • Diff is exactly control-plane/app/audit_ingest.py + control-plane/tests/test_audit_ingest.py (2 files, +333).

All checks are green on this head (dco, control-plane, dashboard, invigil, CodeQL). Ready for another look whenever you have a moment.

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