feat(rbac): per-tenant audit log for RBAC events (PR 6, #1303)
PR 1-5 of the multi-tenant RBAC series enforce the role matrix; what they don't do is keep a durable record of who did what. Today the RBAC middleware logs "[rbac] role insufficient ..." to stderr - perishable, not searchable. Operators cannot answer "who removed Alice from tenant T-foo on Tuesday" or "did we 403 a probing user 200 times last hour" after the fact. This PR adds a generic per-tenant audit log that: - Captures mutating RBAC actions (member add/remove/role-change/leave) durably from the tenant_member service after the repo write succeeds. - Captures enforcement rejections (RequireRole / RequireOwnershipOrRole denies under EnableRBAC=true) via a gin-context-injected hook. - Sliding-window dedup (1 minute) on (tenant, actor, action, path) for denied events so a probing client cannot flood audit_logs. - Default-on - no extra env flag. Schema is generic enough for future KB / agent / datasource events to reuse via new action namespaces without another migration. Schema (migration 000044) mirrors wiki_log_entries' shape: a single audit_logs table with action, target_type/id, request path/method, outcome, and a JSONB details column. Three indexes: (tenant_id, id DESC) for the per-tenant cursor feed, actor_user_id for "what did Alice do", (tenant_id, action) for both the action filter and the dedup lookup. Sqlite mirror added for Lite mode. Wiring is nil-safe: the audit field on tenantMemberService and the gin-context lookup in rbac.go both tolerate a nil service so existing tests construct without a stub and dormant deployments degrade gracefully if the provider is misconfigured. The query API is GET /tenants/:id/audit-log, gated by Admin+ on top of the existing PathTenantMatch (denial histories should not surface to ordinary members). Cursor-paginated by descending id (sidesteps the duplicate-timestamp tie-breaking that BIGSERIAL trivially solves). Frontend: TenantMembers.vue gets a t-tabs wrapper with Members (existing) and Audit log (new) tabs; the audit tab is gated to Admin+ to mirror the server, lazy-loads on first open, paginates via the next_cursor pattern, and renders coloured chips per action/outcome so an operator can scan for anomalies. i18n keys added in all four locales. Tests: 14 new tests across service (6), middleware (4), and handler (4). Service tests pin the dedup window behaviour (writes again after expiry, dedup is per-actor-per-path, degrades on lookup error). Middleware tests pin both reject sites' audit hook and the "dormant mode does not fire" + "nil audit does not panic" invariants. Handler tests pin the cursor envelope and that an empty page returns next_cursor=0. Out of scope: per-action ACL on the audit log, retention/TTL job, KB/agent/datasource event wiring (schema supports it - just no emitters yet), async writes, frontend filter UI.
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// stubAuditRepo collects Create calls and answers CountSinceForDedup
|
||||
// from the in-memory state. Embeds the interface so any unstubbed
|
||||
// method will nil-panic, surfacing a contract drift loudly instead of
|
||||
// silently returning zero-values.
|
||||
type stubAuditRepo struct {
|
||||
interfaces.AuditLogRepository
|
||||
|
||||
mu sync.Mutex
|
||||
created []*types.AuditLog
|
||||
// countErr lets a test inject a transient lookup failure.
|
||||
countErr error
|
||||
}
|
||||
|
||||
func (s *stubAuditRepo) Create(_ context.Context, entry *types.AuditLog) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.created = append(s.created, entry)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubAuditRepo) CountSinceForDedup(
|
||||
_ context.Context, tenantID uint64, actorUserID string,
|
||||
action types.AuditAction, requestPath string, since time.Time,
|
||||
) (int64, error) {
|
||||
if s.countErr != nil {
|
||||
return 0, s.countErr
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var n int64
|
||||
for _, e := range s.created {
|
||||
if e.TenantID == tenantID &&
|
||||
e.ActorUserID == actorUserID &&
|
||||
e.Action == action &&
|
||||
e.RequestPath == requestPath &&
|
||||
!e.CreatedAt.Before(since) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// fakeClock returns a controllable time source so the dedup-window
|
||||
// tests can simulate "1 minute later" without sleeping.
|
||||
type fakeClock struct{ t time.Time }
|
||||
|
||||
func (f *fakeClock) Now() time.Time { return f.t }
|
||||
func (f *fakeClock) Advance(by time.Duration) { f.t = f.t.Add(by) }
|
||||
|
||||
func newSvcForTest() (*auditLogService, *stubAuditRepo, *fakeClock) {
|
||||
clock := &fakeClock{t: time.Date(2026, 5, 14, 10, 0, 0, 0, time.UTC)}
|
||||
repo := &stubAuditRepo{}
|
||||
svc := &auditLogService{repo: repo, now: clock.Now}
|
||||
return svc, repo, clock
|
||||
}
|
||||
|
||||
func newDeniedCtx(t *testing.T, method, path string) *gin.Context {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(method, path, nil)
|
||||
return c
|
||||
}
|
||||
|
||||
func TestAuditLog_Log_FillsCreatedAtAndOutcome(t *testing.T) {
|
||||
svc, repo, clock := newSvcForTest()
|
||||
entry := &types.AuditLog{
|
||||
TenantID: 7,
|
||||
Action: types.AuditActionMemberAdded,
|
||||
// CreatedAt left zero, Outcome left empty
|
||||
}
|
||||
if err := svc.Log(context.Background(), entry); err != nil {
|
||||
t.Fatalf("Log: %v", err)
|
||||
}
|
||||
if len(repo.created) != 1 {
|
||||
t.Fatalf("expected 1 written entry, got %d", len(repo.created))
|
||||
}
|
||||
if !entry.CreatedAt.Equal(clock.Now()) {
|
||||
t.Fatalf("expected CreatedAt to default to clock time, got %v", entry.CreatedAt)
|
||||
}
|
||||
if entry.Outcome != types.AuditOutcomeSuccess {
|
||||
t.Fatalf("expected Outcome to default to success, got %q", entry.Outcome)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditLog_Log_RejectsEmptyAction(t *testing.T) {
|
||||
// Schema requires action; the service guards the contract upfront so
|
||||
// callers get a clean error instead of a constraint violation later.
|
||||
svc, _, _ := newSvcForTest()
|
||||
err := svc.Log(context.Background(), &types.AuditLog{TenantID: 7})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error when Action is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditLog_LogDenied_DedupesRepeatedRejectsWithinWindow(t *testing.T) {
|
||||
// First denied write hits the table; second within the window does
|
||||
// NOT — the dedup primitive is the headline durability guarantee
|
||||
// against probing clients filling the audit table at line rate.
|
||||
svc, repo, _ := newSvcForTest()
|
||||
c := newDeniedCtx(t, "PUT", "/api/v1/tenants/7")
|
||||
|
||||
if err := svc.LogDenied(context.Background(), c, 7, "u-viewer", "viewer", types.TenantRoleAdmin); err != nil {
|
||||
t.Fatalf("LogDenied: %v", err)
|
||||
}
|
||||
if err := svc.LogDenied(context.Background(), c, 7, "u-viewer", "viewer", types.TenantRoleAdmin); err != nil {
|
||||
t.Fatalf("LogDenied second call: %v", err)
|
||||
}
|
||||
if len(repo.created) != 1 {
|
||||
t.Fatalf("expected dedup to drop second write, got %d entries", len(repo.created))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditLog_LogDenied_WritesAgainAfterWindowExpires(t *testing.T) {
|
||||
// The dedup is a sliding window, not a once-per-tuple lock — once
|
||||
// the trailing window is empty, the next denied call must record.
|
||||
svc, repo, clock := newSvcForTest()
|
||||
c := newDeniedCtx(t, "PUT", "/api/v1/tenants/7")
|
||||
|
||||
_ = svc.LogDenied(context.Background(), c, 7, "u-viewer", "viewer", types.TenantRoleAdmin)
|
||||
clock.Advance(denyDedupWindow + time.Second)
|
||||
_ = svc.LogDenied(context.Background(), c, 7, "u-viewer", "viewer", types.TenantRoleAdmin)
|
||||
|
||||
if len(repo.created) != 2 {
|
||||
t.Fatalf("expected window-expiry to allow second write, got %d entries", len(repo.created))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditLog_LogDenied_DedupIsPerActorAndPath(t *testing.T) {
|
||||
// Two distinct actors hitting the same endpoint, or the same actor
|
||||
// hitting two distinct endpoints, must each record. The dedup key
|
||||
// is (tenant, actor, action, path) — anything else collides.
|
||||
svc, repo, _ := newSvcForTest()
|
||||
c1 := newDeniedCtx(t, "PUT", "/api/v1/tenants/7")
|
||||
c2 := newDeniedCtx(t, "PUT", "/api/v1/agents/abc")
|
||||
|
||||
_ = svc.LogDenied(context.Background(), c1, 7, "u-viewer-a", "viewer", types.TenantRoleAdmin)
|
||||
_ = svc.LogDenied(context.Background(), c1, 7, "u-viewer-b", "viewer", types.TenantRoleAdmin)
|
||||
_ = svc.LogDenied(context.Background(), c2, 7, "u-viewer-a", "viewer", types.TenantRoleAdmin)
|
||||
|
||||
if len(repo.created) != 3 {
|
||||
t.Fatalf("expected 3 distinct (actor,path) writes, got %d", len(repo.created))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditLog_LogDenied_DegradesGracefullyOnDedupLookupError(t *testing.T) {
|
||||
// If the dedup count returns an error (DB hiccup, transient), we
|
||||
// must NOT silently skip the audit row. Better to write a
|
||||
// duplicate than to lose a denied event during incident response.
|
||||
svc, repo, _ := newSvcForTest()
|
||||
repo.countErr = errors.New("transient")
|
||||
c := newDeniedCtx(t, "PUT", "/api/v1/tenants/7")
|
||||
|
||||
if err := svc.LogDenied(context.Background(), c, 7, "u-viewer", "viewer", types.TenantRoleAdmin); err != nil {
|
||||
t.Fatalf("LogDenied with dedup error: %v", err)
|
||||
}
|
||||
if len(repo.created) != 1 {
|
||||
t.Fatalf("expected fallthrough write on dedup error, got %d entries", len(repo.created))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user