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
This commit is contained in:
@@ -39,6 +39,12 @@ type AuditLogRepository interface {
|
||||
requestPath string,
|
||||
since time.Time,
|
||||
) (int64, error)
|
||||
// DeleteOlderThan removes audit rows whose created_at is strictly
|
||||
// before cutoff and returns the affected row count. It is the
|
||||
// retention primitive driven by the daily background sweep.
|
||||
// Implementations should delete in a single statement (no per-row
|
||||
// fetch) so the long-tail cost stays at "one DELETE per sweep".
|
||||
DeleteOlderThan(ctx context.Context, cutoff time.Time) (int64, error)
|
||||
}
|
||||
|
||||
// AuditLogService is the high-level audit API the rest of the codebase
|
||||
@@ -60,4 +66,9 @@ type AuditLogService interface {
|
||||
requiredRole types.TenantRole,
|
||||
) error
|
||||
List(ctx context.Context, tenantID uint64, q *AuditLogQuery) ([]*types.AuditLog, error)
|
||||
// Purge deletes rows whose created_at is strictly older than the
|
||||
// retention horizon. retentionDays <= 0 makes the call a no-op,
|
||||
// which keeps the daily sweep cheap when retention is disabled.
|
||||
// Returns rows deleted; transient repo errors propagate.
|
||||
Purge(ctx context.Context, retentionDays int) (int64, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user