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:
@@ -172,6 +172,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(service.NewTenantService))
|
||||
must(container.Provide(service.NewTenantMemberService))
|
||||
must(container.Provide(service.NewAuditLogService))
|
||||
must(container.Provide(service.NewAuditLogRetentionRunner))
|
||||
must(container.Provide(service.NewKnowledgeBaseService))
|
||||
must(container.Provide(service.NewOrganizationService))
|
||||
must(container.Provide(service.NewKBShareService)) // KBShareService must be registered before KnowledgeService and KnowledgeTagService
|
||||
@@ -262,6 +263,8 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(service.NewDataSourceService))
|
||||
must(container.Invoke(startDataSourceScheduler))
|
||||
logger.Debugf(ctx, "[Container] Data source sync framework registered")
|
||||
must(container.Invoke(startAuditLogRetention))
|
||||
logger.Debugf(ctx, "[Container] Audit log retention runner registered")
|
||||
must(container.Provide(chatpipeline.NewEventManager))
|
||||
must(container.Invoke(chatpipeline.NewPluginSearch))
|
||||
must(container.Invoke(chatpipeline.NewPluginRerank))
|
||||
@@ -1322,3 +1325,22 @@ func startDataSourceScheduler(scheduler *datasource.Scheduler, cleaner interface
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// startAuditLogRetention spins up the daily audit_logs purge sweep
|
||||
// and registers shutdown cleanup. Mirrors the data-source-scheduler
|
||||
// pattern: container init kicks the goroutine, ResourceCleaner stops
|
||||
// it during graceful shutdown so a SIGTERM during a sweep doesn't
|
||||
// orphan the goroutine.
|
||||
//
|
||||
// retention_days <= 0 is the configured way to disable retention;
|
||||
// the runner short-circuits Start() on that path so we don't need
|
||||
// to gate the wiring here.
|
||||
func startAuditLogRetention(
|
||||
runner *service.AuditLogRetentionRunner, cleaner interfaces.ResourceCleaner,
|
||||
) {
|
||||
runner.Start(context.Background())
|
||||
cleaner.RegisterWithName("AuditLogRetentionRunner", func() error {
|
||||
runner.Stop()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user