Files
Enterprise-Knowledge-Base/internal/application/service/audit_log.go
T
wizardchen ff1f03ce0d feat(rbac): daily retention sweep for audit_logs (default 90 days)
The audit_logs table grows monotonically until something purges it.
PR 6 explicitly listed retention as out of scope for v1; this picks
that up.

Adds an `audit.retention_days` config (default 90, 0 disables) and a
small background goroutine `AuditLogRetentionRunner` that fires once
~10 minutes after boot and then every 24h, calling
`AuditLogService.Purge` which DELETEs rows older than the cutoff in a
single statement. The dependency surface is intentionally minimal —
no robfig/cron, no asynq, no migration — because retention has no
wall-clock alignment requirement.

Wiring is the same shape as the existing data source scheduler:
container `Provide(NewAuditLogRetentionRunner)`, an `Invoke` that
calls `Start`, and a `ResourceCleaner.RegisterWithName` for graceful
shutdown.

Defaults preserve operator intent:
  - `audit:` section omitted from YAML  -> retention_days=90.
  - explicit `audit.retention_days: 0` in YAML  -> purge disabled.
  - `WEKNORA_AUDIT_RETENTION_DAYS=N` env override (incl. N=0).
  - `retention_days < 0` is rejected by ValidateConfig.

Tests cover the service Purge contract (no-op when disabled, cutoff
math, error propagation) and the runner lifecycle (Start no-op when
disabled, idempotent Start/Stop, ticker cadence, runOnce swallows
errors). 10 new tests, all green.

docs/rbac.md updated with the new YAML / env / behaviour.

Refs: #1303
2026-05-18 17:28:58 +08:00

152 lines
5.4 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)
}
// Purge deletes rows whose created_at is strictly older than
// `retentionDays` ago. retentionDays <= 0 short-circuits — operators
// who configured no retention pay zero database round-trips.
//
// The cutoff is computed off the service's clock (s.now) so tests
// can drive deterministic horizons without touching wall time.
//
// We intentionally do NOT batch the DELETE: at the volumes audit_logs
// realistically reaches in a 24h window, a single DELETE-with-index
// finishes in well under a second on Postgres. If the table ever
// grows large enough that a single sweep blocks vacuum, the repo
// helper is the place to add LIMIT-style chunking — the service stays
// simple.
func (s *auditLogService) Purge(ctx context.Context, retentionDays int) (int64, error) {
if retentionDays <= 0 {
return 0, nil
}
cutoff := s.now().Add(-time.Duration(retentionDays) * 24 * time.Hour)
return s.repo.DeleteOlderThan(ctx, cutoff)
}