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:
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -21,6 +22,7 @@ type Config struct {
|
||||
KnowledgeBase *KnowledgeBaseConfig `yaml:"knowledge_base" json:"knowledge_base"`
|
||||
Tenant *TenantConfig `yaml:"tenant" json:"tenant"`
|
||||
Auth *AuthConfig `yaml:"auth" json:"auth"`
|
||||
Audit *AuditConfig `yaml:"audit" json:"audit"`
|
||||
OIDCAuth *OIDCAuthConfig `yaml:"oidc_auth" json:"oidc_auth"`
|
||||
Models []ModelConfig `yaml:"models" json:"models"`
|
||||
VectorDatabase *VectorDatabaseConfig `yaml:"vector_database" json:"vector_database"`
|
||||
@@ -182,6 +184,20 @@ type TenantConfig struct {
|
||||
EnableRBAC bool `yaml:"enable_rbac" json:"enable_rbac"`
|
||||
}
|
||||
|
||||
// AuditConfig governs durable audit log behaviour. Writes happen on
|
||||
// every member-management mutation and on RBAC denials (when
|
||||
// EnableRBAC is true); the table grows monotonically unless this
|
||||
// section turns on retention.
|
||||
type AuditConfig struct {
|
||||
// RetentionDays is how many days of audit history to keep. Older
|
||||
// rows are deleted by a daily background sweep.
|
||||
// > 0 — purge rows whose created_at < NOW() - retention_days.
|
||||
// = 0 — disable purge entirely (the pre-rollout default).
|
||||
// < 0 — invalid; ValidateConfig rejects it.
|
||||
// Default: 90 (set by applyAuditDefaults when the section is omitted).
|
||||
RetentionDays int `yaml:"retention_days" json:"retention_days"`
|
||||
}
|
||||
|
||||
// AuthConfig governs the user authentication entry points.
|
||||
type AuthConfig struct {
|
||||
// RegistrationMode controls who may call POST /auth/register.
|
||||
@@ -472,6 +488,7 @@ func LoadConfig() (*Config, error) {
|
||||
applyAgentEnvOverrides(&cfg)
|
||||
applyKnowledgeBaseEnvOverrides(&cfg)
|
||||
applyAuthAndTenantDefaults(&cfg)
|
||||
applyAuditDefaults(&cfg)
|
||||
|
||||
if err := ValidateConfig(&cfg); err != nil {
|
||||
return nil, err
|
||||
@@ -524,6 +541,11 @@ func ValidateConfig(cfg *Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.Audit != nil && cfg.Audit.RetentionDays < 0 {
|
||||
errs = append(errs, fmt.Sprintf("audit.retention_days must be >= 0 (got %d); use 0 to disable purge",
|
||||
cfg.Audit.RetentionDays))
|
||||
}
|
||||
|
||||
if cfg.Conversation != nil {
|
||||
if cfg.Conversation.EmbeddingTopK < 0 {
|
||||
errs = append(errs, "conversation.embedding_top_k must be >= 0")
|
||||
@@ -693,6 +715,38 @@ func applyAuthAndTenantDefaults(cfg *Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// applyAuditDefaults fills in defaults for the Audit config section
|
||||
// and applies the env override commonly used to extend or disable
|
||||
// retention without editing config.yaml.
|
||||
//
|
||||
// Defaults:
|
||||
// - When the `audit:` section is omitted entirely from YAML,
|
||||
// RetentionDays = 90 (purge rows older than 90 days).
|
||||
//
|
||||
// Operator intent is otherwise preserved: an explicit
|
||||
// `audit.retention_days: 0` in YAML means "disable the purge", which
|
||||
// is a supported posture for compliance use cases that handle archival
|
||||
// off-database.
|
||||
//
|
||||
// Env overrides (when set and parseable; out-of-range is ignored):
|
||||
// - WEKNORA_AUDIT_RETENTION_DAYS (non-negative integer)
|
||||
func applyAuditDefaults(cfg *Config) {
|
||||
// Section omitted entirely -> apply the default and no env wiring
|
||||
// is needed for the most common path.
|
||||
if cfg.Audit == nil {
|
||||
cfg.Audit = &AuditConfig{RetentionDays: 90}
|
||||
}
|
||||
|
||||
// Env override always wins, but only when explicitly set so a
|
||||
// stale shell variable doesn't suddenly disable the purge for a
|
||||
// future deployment that committed a real value.
|
||||
if value := strings.TrimSpace(os.Getenv("WEKNORA_AUDIT_RETENTION_DAYS")); value != "" {
|
||||
if n, err := strconv.Atoi(value); err == nil && n >= 0 {
|
||||
cfg.Audit.RetentionDays = n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// into actual prompt text content. Only xxx_id fields are used;
|
||||
// no fallback to default templates.
|
||||
func backfillConversationDefaults(cfg *Config) {
|
||||
|
||||
Reference in New Issue
Block a user