Files
Enterprise-Knowledge-Base/internal/application/service/tenant_member.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

311 lines
10 KiB
Go

package service
import (
"context"
"encoding/json"
"errors"
"time"
apprepo "github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// Sentinel errors returned by tenantMemberService. Callers compare with
// errors.Is to render appropriate HTTP responses (404 / 409 / 403).
var (
// ErrMembershipNotFound is returned when no active membership row
// matches the (user, tenant) pair the caller requested.
ErrMembershipNotFound = errors.New("tenant membership not found")
// ErrMembershipAlreadyExists is returned by AddMember when the
// (user, tenant) pair already has an active membership.
ErrMembershipAlreadyExists = errors.New("tenant membership already exists")
// ErrInvalidTenantRole is returned when the caller passes a role
// value that is not one of the four defined TenantRole constants.
ErrInvalidTenantRole = errors.New("invalid tenant role")
// ErrLastOwner is returned when an operation would leave the tenant
// without an active Owner. Demoting the last Owner or removing them
// is forbidden; an explicit ownership transfer must happen first.
ErrLastOwner = errors.New("cannot demote or remove the last active owner of the tenant")
)
// tenantMemberService implements interfaces.TenantMemberService.
type tenantMemberService struct {
repo interfaces.TenantMemberRepository
audit interfaces.AuditLogService // optional; nil ⇒ no audit, business ops still succeed
}
// NewTenantMemberService constructs the service. Wired up via the DI
// container alongside the other application services. The auditService
// is optional — passing nil disables durable audit but keeps the
// underlying mutations working, so a container reshuffle that
// constructs tenant_member before audit_log won't crash and tests
// don't need to stub the dependency unless they care about audit
// behaviour.
func NewTenantMemberService(
repo interfaces.TenantMemberRepository,
audit interfaces.AuditLogService,
) interfaces.TenantMemberService {
return &tenantMemberService{repo: repo, audit: audit}
}
// emitAudit is the per-mutation audit hook. Best-effort: a nil audit
// service or a write failure is logged inside the audit service itself
// and never bubbles up to the caller. RBAC mutations succeed even if
// audit is unavailable; the alternative (failing the business op when
// the audit table is down) is far worse.
func (s *tenantMemberService) emitAudit(ctx context.Context, entry *types.AuditLog) {
if s.audit == nil {
return
}
_ = s.audit.Log(ctx, entry)
}
// auditActorRole picks up the caller's role at write-time. Empty if
// auth middleware didn't set it (e.g. service-internal flows like
// EnsureOwner during register, where there is no "caller").
func auditActorRole(ctx context.Context) string {
return string(types.TenantRoleFromContext(ctx))
}
// auditActor returns the calling user id from context, "" when no
// authenticated caller is present (service-internal paths).
func auditActor(ctx context.Context) string {
uid, _ := types.UserIDFromContext(ctx)
return uid
}
// AddMember inserts a new active membership row. Returns
// ErrMembershipAlreadyExists if the user is already an active member of
// the tenant, and ErrInvalidTenantRole for unknown roles.
func (s *tenantMemberService) AddMember(
ctx context.Context,
userID string,
tenantID uint64,
role types.TenantRole,
invitedBy *string,
) (*types.TenantMember, error) {
if !role.IsValid() {
return nil, ErrInvalidTenantRole
}
existing, err := s.repo.Get(ctx, userID, tenantID)
if err != nil {
return nil, err
}
if existing != nil {
return nil, ErrMembershipAlreadyExists
}
member := &types.TenantMember{
UserID: userID,
TenantID: tenantID,
Role: role,
Status: types.TenantMemberStatusActive,
InvitedBy: invitedBy,
JoinedAt: time.Now(),
}
if err := s.repo.Create(ctx, member); err != nil {
return nil, err
}
s.emitAudit(ctx, &types.AuditLog{
TenantID: tenantID,
ActorUserID: auditActor(ctx),
ActorRole: auditActorRole(ctx),
Action: types.AuditActionMemberAdded,
TargetType: "tenant_member",
TargetUserID: userID,
Outcome: types.AuditOutcomeSuccess,
})
return member, nil
}
// EnsureOwner is idempotent: if the user already has an active membership
// in the tenant it is returned unchanged; otherwise a new owner row is
// created. Used by Register/OIDC paths so re-running Register on an
// existing user (e.g. after a partial failure) does not double-insert.
func (s *tenantMemberService) EnsureOwner(
ctx context.Context,
userID string,
tenantID uint64,
) (*types.TenantMember, error) {
existing, err := s.repo.Get(ctx, userID, tenantID)
if err != nil {
return nil, err
}
if existing != nil {
return existing, nil
}
member := &types.TenantMember{
UserID: userID,
TenantID: tenantID,
Role: types.TenantRoleOwner,
Status: types.TenantMemberStatusActive,
JoinedAt: time.Now(),
}
if err := s.repo.Create(ctx, member); err != nil {
return nil, err
}
logger.Infof(ctx, "Bootstrapped owner membership for user=%s tenant=%d", userID, tenantID)
return member, nil
}
// GetMembership returns the active membership or (nil, nil) when absent.
func (s *tenantMemberService) GetMembership(
ctx context.Context,
userID string,
tenantID uint64,
) (*types.TenantMember, error) {
return s.repo.Get(ctx, userID, tenantID)
}
// ListByUser proxies to the repository; included on the service so HTTP
// handlers depend only on the service interface.
func (s *tenantMemberService) ListByUser(ctx context.Context, userID string) ([]*types.TenantMember, error) {
return s.repo.ListByUser(ctx, userID)
}
// ListByTenant proxies to the repository.
func (s *tenantMemberService) ListByTenant(ctx context.Context, tenantID uint64) ([]*types.TenantMember, error) {
return s.repo.ListByTenant(ctx, tenantID)
}
// HasAnyMembers proxies to the repository.
func (s *tenantMemberService) HasAnyMembers(ctx context.Context, tenantID uint64) (bool, error) {
return s.repo.HasAnyMembers(ctx, tenantID)
}
// UpdateRole enforces the "cannot demote the last Owner" invariant before
// delegating to the repository. Re-promoting an existing Owner is a no-op
// from the invariant's perspective.
func (s *tenantMemberService) UpdateRole(
ctx context.Context,
userID string,
tenantID uint64,
newRole types.TenantRole,
) error {
if !newRole.IsValid() {
return ErrInvalidTenantRole
}
current, err := s.repo.Get(ctx, userID, tenantID)
if err != nil {
return err
}
if current == nil {
return ErrMembershipNotFound
}
if current.Role == newRole {
return nil
}
oldRole := current.Role
// Owner demotion is the dangerous path: two concurrent demotions of
// two different Owners with the old "Get → Count → Update" sequence
// could each observe count=2 and both commit, leaving the tenant
// ownerless. Route through the repo's atomic helper instead, which
// takes a row-level UPDATE lock on every other active Owner before
// committing the role change.
if current.Role == types.TenantRoleOwner && newRole != types.TenantRoleOwner {
err := s.repo.DemoteOwnerAtomically(ctx, userID, tenantID, newRole)
switch {
case errors.Is(err, apprepo.ErrLastOwner):
return ErrLastOwner
case err != nil:
return err
}
s.emitRoleChangeAudit(ctx, tenantID, userID, oldRole, newRole)
return nil
}
if err := s.repo.UpdateRole(ctx, userID, tenantID, newRole); err != nil {
return err
}
s.emitRoleChangeAudit(ctx, tenantID, userID, oldRole, newRole)
return nil
}
// emitRoleChangeAudit packs the old/new role into Details so the
// audit-log UI can render "promoted Alice from contributor to admin"
// without a separate column per role transition.
func (s *tenantMemberService) emitRoleChangeAudit(
ctx context.Context,
tenantID uint64,
targetUserID string,
oldRole, newRole types.TenantRole,
) {
details, _ := json.Marshal(map[string]string{
"old_role": string(oldRole),
"new_role": string(newRole),
})
s.emitAudit(ctx, &types.AuditLog{
TenantID: tenantID,
ActorUserID: auditActor(ctx),
ActorRole: auditActorRole(ctx),
Action: types.AuditActionMemberRoleChanged,
TargetType: "tenant_member",
TargetUserID: targetUserID,
Outcome: types.AuditOutcomeSuccess,
Details: types.JSON(details),
})
}
// RemoveMember enforces the "cannot remove the last Owner" invariant
// before soft-deleting the membership. For Owner removals it routes
// through the repo's transactional helper so the count + delete commit
// atomically (no TOCTOU between checking owner count and deleting).
//
// The audit row distinguishes "voluntary leave" (caller == target,
// driven by POST /leave) from "kicked" (caller != target, driven by
// DELETE /tenants/:id/members/:user_id). Both go through this same
// service method but the recorded action differs so an audit reader
// can tell the two apart.
func (s *tenantMemberService) RemoveMember(ctx context.Context, userID string, tenantID uint64) error {
current, err := s.repo.Get(ctx, userID, tenantID)
if err != nil {
return err
}
if current == nil {
return ErrMembershipNotFound
}
if current.Role == types.TenantRoleOwner {
err := s.repo.RemoveOwnerAtomically(ctx, userID, tenantID)
switch {
case errors.Is(err, apprepo.ErrLastOwner):
return ErrLastOwner
case err != nil:
return err
}
s.emitRemovalAudit(ctx, tenantID, userID)
return nil
}
if err := s.repo.SoftDelete(ctx, userID, tenantID); err != nil {
return err
}
s.emitRemovalAudit(ctx, tenantID, userID)
return nil
}
// emitRemovalAudit picks AuditActionMemberLeft when the caller is
// removing themselves, AuditActionMemberRemoved otherwise. Caller
// detection uses the user-id from the request context — the same
// source the LeaveTenant handler uses to derive its `userID` arg.
func (s *tenantMemberService) emitRemovalAudit(
ctx context.Context,
tenantID uint64,
targetUserID string,
) {
action := types.AuditActionMemberRemoved
if actor := auditActor(ctx); actor != "" && actor == targetUserID {
action = types.AuditActionMemberLeft
}
s.emitAudit(ctx, &types.AuditLog{
TenantID: tenantID,
ActorUserID: auditActor(ctx),
ActorRole: auditActorRole(ctx),
Action: action,
TargetType: "tenant_member",
TargetUserID: targetUserID,
Outcome: types.AuditOutcomeSuccess,
})
}