Commit 034a418
fix: audit middleware logs after auth to capture identity fields (#4502)
## Summary
- **Fix broken audit data**: The audit middleware was logging *before*
auth ran, so `sub`, `account`, and `scopes` were always empty (`"not set
in context"`). Restructured to use a shared `*AuditData` struct in
context — audit injects it, auth populates it after JWT validation,
audit reads it after the response completes.
- **Exclude health checks**: `/healthz` is now excluded from audit
logging, eliminating high-volume K8s probe noise with useless
empty-identity entries.
- **Capture response status**: Audit entries now include the HTTP status
code, improving the trail for security review.
## Changes
### `go/audit/main.go` — Core middleware rewrite
- Removed direct reads of `auth.CurrentSubjectContextKey{}` etc. (which
were always empty at the outer middleware layer)
- Added `AuditData` struct and `AuditDataFromContext()` for
cross-middleware communication via a shared mutable pointer in context
- Moved log emission to **after** `next.ServeHTTP` so auth has populated
the data
- Added `statusRecorder` wrapper to capture HTTP response status
- Added `WithExcludePaths()` option for skipping paths like `/healthz`
- Removed import of `go/auth` — dependency direction is now `auth →
audit`
### `go/auth/middleware.go` — Populate audit data
- Added audit data population in `processOverrides`, which is the final
handler before the route handler runs (covers JWT-validated, bypass, and
override paths)
- When `*AuditData` is present in context, writes `Subject`,
`AccountName`, and `Scopes` from the finalized auth context
### Service files (api-server, gateway, revlink)
- Added `audit.WithExcludePaths("/healthz")` to all three services
### Tests and docs
- Rewrote `go/audit/main_test.go` with 7 focused tests covering:
authenticated requests, unauthenticated requests, path exclusion, status
code capture, implicit 200, and nil-safety
- Updated `go/audit/README.md` to document the new architecture
## Deviations from Approved Plan
> No approved plan is associated with this PR.
Made with [Cursor](https://cursor.com)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches cross-middleware auth/audit interaction and wraps
`http.ResponseWriter`, which can affect request handling
(streaming/WebSocket) if edge cases are missed, though behavior is
well-covered by new tests.
>
> **Overview**
> Fixes audit logging to capture authenticated identity by having
`audit.NewAuditMiddleware` inject a shared `*AuditData` into request
context, letting `auth.NewAuthMiddleware` populate
`sub`/`account`/`scopes` after JWT validation, and emitting the audit
log **after** the handler completes.
>
> Audit logs now include the HTTP response `status` (via a
`ResponseWriter` recorder that preserves `Hijacker`/`Flusher` behavior)
and support `WithExcludePaths` to skip noisy endpoints like `/healthz`;
services are updated to exclude health checks, and tests/docs are
expanded to cover these behaviors.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
24c0ae98b56bc362e471137ab7cdc14ee5a0a9bb. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
GitOrigin-RevId: a0424a1d86901cfd20f938291e1874b100408a631 parent b525bfe commit 034a418
2 files changed
Lines changed: 153 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
192 | 193 | | |
193 | 194 | | |
194 | 195 | | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
195 | 208 | | |
196 | 209 | | |
197 | 210 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
671 | 673 | | |
672 | 674 | | |
673 | 675 | | |
674 | | - | |
675 | 676 | | |
676 | 677 | | |
677 | 678 | | |
| |||
713 | 714 | | |
714 | 715 | | |
715 | 716 | | |
716 | | - | |
717 | 717 | | |
718 | 718 | | |
719 | 719 | | |
| |||
995 | 995 | | |
996 | 996 | | |
997 | 997 | | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
0 commit comments