Files
Enterprise-Knowledge-Base/internal/application/service/audit_log.go
T
wizardchen 766a6c482f 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.
2026-05-18 17:28:58 +08:00

131 lines
4.5 KiB
Go

package service
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/gin-gonic/gin"
)
// auditLogService is the high-level wrapper around AuditLogRepository.
// It owns:
// - timestamp defaulting (Log fills CreatedAt if zero so callers
// don't have to).
// - the 1-minute sliding-window dedup that LogDenied applies to keep
// a probing client from filling the audit_logs table.
//
// The service is intentionally nil-tolerant when consumed: the
// tenant_member service holds it as an optional field, so a future
// container reshuffle that constructs tenant_member before audit_log
// won't crash. Callers should still aim to inject a real instance —
// the nil path is a degraded mode, not a default.
type auditLogService struct {
repo interfaces.AuditLogRepository
now func() time.Time
}
// NewAuditLogService constructs the production service.
func NewAuditLogService(repo interfaces.AuditLogRepository) interfaces.AuditLogService {
return &auditLogService{repo: repo, now: time.Now}
}
// denyDedupWindow caps how often LogDenied will write a durable row
// for the same (tenant, actor, path, action) tuple. 1 minute is
// short enough that bursts across distinct paths still each show up
// in the table (one row per path per minute), long enough that a
// single hammered endpoint at 100 RPS produces 1 row/minute, not 6000.
const denyDedupWindow = 1 * time.Minute
// Log is the canonical write path. The repo's Create defaults
// CreatedAt at the SQL level, but we also fill it here so tests and
// callers that read entry.CreatedAt right after Log() get a sensible
// value without round-tripping through the database.
func (s *auditLogService) Log(ctx context.Context, entry *types.AuditLog) error {
if entry == nil {
return fmt.Errorf("audit log: nil entry")
}
if entry.Action == "" {
return fmt.Errorf("audit log: action is required")
}
if entry.Outcome == "" {
entry.Outcome = types.AuditOutcomeSuccess
}
if entry.CreatedAt.IsZero() {
entry.CreatedAt = s.now()
}
if err := s.repo.Create(ctx, entry); err != nil {
// Log loudly but do NOT propagate the error to the caller in
// the production wiring (see callers in tenant_member service
// and rbac middleware — both ignore the return). Audit failure
// must never break the underlying business operation.
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"audit_action": entry.Action,
"audit_target": entry.TargetID,
})
return err
}
return nil
}
// LogDenied records a middleware-level rejection. Subject to a
// 1-minute sliding-window dedup keyed by
// (tenant_id, actor_user_id, action, request_path) so a probing
// client cannot flood the table.
//
// The non-durable advisory log line `[rbac] role insufficient: ...`
// continues to fire on every reject (in middleware/rbac.go) — the
// dedup only suppresses durable writes, not stderr observability.
func (s *auditLogService) LogDenied(
ctx context.Context,
c *gin.Context,
tenantID uint64,
actorUserID, actorRole string,
requiredRole types.TenantRole,
) error {
requestPath := ""
requestMethod := ""
if c != nil && c.Request != nil {
requestPath = c.Request.URL.Path
requestMethod = c.Request.Method
}
// Dedup probe: skip the durable write if this exact tuple already
// has a row in the trailing window. Failure here is non-fatal —
// degraded behaviour is "write a duplicate" which is preferable to
// "skip the audit because the count failed".
since := s.now().Add(-denyDedupWindow)
if n, err := s.repo.CountSinceForDedup(
ctx, tenantID, actorUserID, types.AuditActionAccessDenied, requestPath, since,
); err == nil && n > 0 {
return nil
}
details, _ := json.Marshal(map[string]string{"required_role": string(requiredRole)})
return s.Log(ctx, &types.AuditLog{
TenantID: tenantID,
ActorUserID: actorUserID,
ActorRole: actorRole,
Action: types.AuditActionAccessDenied,
RequestPath: requestPath,
RequestMethod: requestMethod,
Outcome: types.AuditOutcomeDenied,
Details: types.JSON(details),
})
}
// List proxies to the repository. The handler layer applies the
// PathTenantMatch + Admin guard before this is reached, so we don't
// re-check tenant scope here.
func (s *auditLogService) List(
ctx context.Context,
tenantID uint64,
q *interfaces.AuditLogQuery,
) ([]*types.AuditLog, error) {
return s.repo.List(ctx, tenantID, q)
}