766a6c482f
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.
57 lines
2.7 KiB
SQL
57 lines
2.7 KiB
SQL
-- Migration: 000044_audit_log
|
|
-- Adds a generic per-tenant audit log table (issue #1303 PR 6).
|
|
--
|
|
-- Scope:
|
|
-- - PR 6 wires the table to RBAC events (member add/remove/role-change/
|
|
-- leave) and middleware-level enforcement denials.
|
|
-- - The schema is intentionally generic (action, target_type, target_id,
|
|
-- details JSONB) so future PRs (KB ops, agent ops, datasource sync)
|
|
-- can plug in new action constants without another migration.
|
|
--
|
|
-- Indexes:
|
|
-- - (tenant_id, id DESC) for the cursor-paginated feed query
|
|
-- ("show this tenant's audit log newest-first").
|
|
-- - (actor_user_id) for "what did Alice do" lookups.
|
|
-- - (tenant_id, action) for action-class filtering and powering the
|
|
-- 1-minute sliding-window dedup that LogDenied uses to keep a
|
|
-- probing client from filling the table.
|
|
DO $$ BEGIN RAISE NOTICE '[Migration 000044] Creating table: audit_logs'; END $$;
|
|
|
|
CREATE TABLE IF NOT EXISTS audit_logs (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
tenant_id BIGINT NOT NULL,
|
|
actor_user_id VARCHAR(36) NOT NULL DEFAULT '',
|
|
actor_role VARCHAR(32) NOT NULL DEFAULT '',
|
|
action VARCHAR(64) NOT NULL,
|
|
target_type VARCHAR(32) NOT NULL DEFAULT '',
|
|
target_id VARCHAR(64) NOT NULL DEFAULT '',
|
|
target_user_id VARCHAR(36) NOT NULL DEFAULT '',
|
|
request_path VARCHAR(512) NOT NULL DEFAULT '',
|
|
request_method VARCHAR(16) NOT NULL DEFAULT '',
|
|
outcome VARCHAR(16) NOT NULL DEFAULT 'success',
|
|
details JSONB NOT NULL DEFAULT '{}'::JSONB,
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Primary list query: newest-first cursor pagination per tenant. Using id
|
|
-- (BIGSERIAL, monotonic) as the cursor sidesteps the duplicate-timestamp
|
|
-- tie-breaking that a created_at cursor would require — same approach as
|
|
-- wiki_log_entries (migration 000040).
|
|
CREATE INDEX IF NOT EXISTS idx_audit_logs_tenant_id_desc
|
|
ON audit_logs (tenant_id, id DESC);
|
|
|
|
-- Filter by actor (e.g. "what did Alice do"). Useful for incident
|
|
-- response and for user-scoped self-audit screens (out of v1 scope but
|
|
-- the index is cheap and the column is already there).
|
|
CREATE INDEX IF NOT EXISTS idx_audit_logs_actor
|
|
ON audit_logs (actor_user_id);
|
|
|
|
-- Powers two queries: (a) the audit-log feed filtered by action class,
|
|
-- and (b) the LogDenied dedup `count rows where (tenant_id, action) and
|
|
-- created_at >= since`. Without this index the dedup count would scan
|
|
-- the entire table on every denied request.
|
|
CREATE INDEX IF NOT EXISTS idx_audit_logs_tenant_action
|
|
ON audit_logs (tenant_id, action);
|
|
|
|
DO $$ BEGIN RAISE NOTICE '[Migration 000044] audit_logs table ready'; END $$;
|