feat(editor): add per-request access audit middleware - #530
Open
rot1024 wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds lightweight per-request access audit logging to the Editor API server so authenticated usage can be counted/aggregated downstream (Cloud Logging → BigQuery) without modifying the Editor web/client.
Changes:
- Introduces
accessAuditMiddlewareto emit one structured JSON audit line per authenticated request, with path-based exclusions for noisy/unauth endpoints. - Adds
Config.AccessAudit(envREEARTH_ACCESSAUDIT, defaulttrue) to allow disabling audit logging without code changes. - Adds unit tests validating enabled/disabled behavior, excluded paths, and JWT/DB attribute precedence.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| editor/server/internal/app/config/config.go | Adds AccessAudit config flag (default true) to toggle middleware. |
| editor/server/internal/app/app.go | Registers accessAuditMiddleware after auth/op resolution middleware. |
| editor/server/internal/app/access_audit.go | Implements the audit middleware and structured record format. |
| editor/server/internal/app/access_audit_test.go | Adds tests to validate audit emission and skipping rules. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+96
to
+106
| rec := accessAuditRecord{ | ||
| Ts: start.UTC().Format(time.RFC3339Nano), | ||
| Method: c.Request().Method, | ||
| Path: path, | ||
| Status: c.Response().Status, | ||
| LatencyMS: latency.Milliseconds(), | ||
| RemoteIP: c.RealIP(), | ||
| UserAgent: c.Request().UserAgent(), | ||
| RequestID: c.Response().Header().Get(echo.HeaderXRequestID), | ||
| Referer: c.Request().Referer(), | ||
| } |
| UserAgent string `json:"ua,omitempty"` | ||
| RequestID string `json:"request_id,omitempty"` | ||
| Referer string `json:"referer,omitempty"` | ||
| AuthMethod string `json:"auth,omitempty"` // "jwt" | "mock" | "debug" |
Comment on lines
+25
to
+31
| // disable color codes so the plain substring match works reliably | ||
| _ = os.Setenv("NO_COLOR", "1") | ||
| buf := &bytes.Buffer{} | ||
| log.SetOutput(buf) | ||
| t.Cleanup(func() { | ||
| log.SetOutput(os.Stdout) | ||
| }) |
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
accessAuditMiddlewaretoeditor-apithat emits one structured JSON line per authenticated request (prefixed witheditor_access_audit). Runs afterattachOpMiddleware, so both the JWTAuthInfo(sub/email/name) and the DB-resolveduser(id / email / name) are already available.Config.AccessAudit(envREEARTH_ACCESSAUDIT, default true) so the middleware can be disabled without a code change.ts, sub, user_id, email, name, method, path, status, latency_ms, remote_ip, ua, request_id, referer, auth(jwt/mock). Never logs theAuthorizationheader or the raw JWT./api/ping,/api/published/…,/api/published_data/…,/p/…,/assets/…,/favicon…,/debug/pprof,/health,/robots.txt).Test plan
go build ./...ineditor/serverpassesgo vet ./internal/app/...cleangofmtclean on changed filesgo test ./internal/app/... -run TestAccessAudit: 5/5 passAuthInfogo test ./internal/app/config/...: pass with new fieldtextPayload:"editor_access_audit "should show one line per authenticated Editor request;Authorizationshould not appear anywhere.