Files
archie/internal/app/statestore/audit_retention.go
T
sam dcc12582ce
deploy / plan (push) Successful in 35s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 25s
ci/woodpecker/push/deploy Pipeline was canceled
feat(audit): set per-kind event retention from the instance (#1856)
The retention sweep applied one window to a code-fixed allowlist, and the
largest event kind (agent transcripts) was outside it, so the events table
grew whatever the instance set. The audit-retention document now takes
per-kind overrides, the dashboard offers each prunable kind its own window
from the server's list, and a kind without an override follows the default.
A security kind is refused where the document is written, and the sweep cuts
each kind at its own cutoff in one bounded, locked transaction.
2026-11-11 02:31:46 +11:00

84 lines
2.6 KiB
Go

package statestore
import (
"context"
"errors"
"time"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
)
// auditRetentionSweepInterval is how often the stored retention is applied.
const auditRetentionSweepInterval = time.Hour
// auditEventPruner is the store's bounded, lock-guarded event delete.
type auditEventPruner interface {
PruneAuditEvents(ctx context.Context, cutoffs map[string]time.Time) (int64, error)
}
// auditRetentionResources reads the instance's retention document.
type auditRetentionResources interface {
Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error)
}
// sweepAuditRetention applies the audit-retention setting until ctx ends. It
// reads the resource on every tick, so an edit reaches the next sweep without
// a restart, and an unreadable or invalid document skips the tick rather than
// falling back to a default that prunes.
func (b *server) sweepAuditRetention(ctx context.Context) {
ticker := time.NewTicker(auditRetentionSweepInterval)
defer ticker.Stop()
for {
b.pruneAuditEvents(ctx)
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
func (b *server) pruneAuditEvents(ctx context.Context) {
resources, ok := b.st.(auditRetentionResources)
if !ok {
return
}
resource, err := resources.Resource(ctx, storecontract.DefaultOrgID, controlplanestore.AuditRetentionKind)
if err != nil {
if ctx.Err() == nil {
b.log.WarnContext(ctx, "audit retention not applied: setting unreadable, sweep skipped", "err", err)
}
return
}
policy, err := controlplanestore.DecodeRetentionPolicy(resource.Value)
if err != nil {
if ctx.Err() == nil {
b.log.WarnContext(ctx, "audit retention not applied: setting invalid, sweep skipped", "err", err)
}
return
}
cutoffs := policy.Cutoffs(time.Now(), audit.PrunableEventKinds())
if len(cutoffs) == 0 {
// Every prunable kind is kept forever: there is no cutoff to apply.
return
}
pruner, ok := b.st.(auditEventPruner)
if !ok {
return
}
rows, err := pruner.PruneAuditEvents(ctx, cutoffs)
switch {
case errors.Is(err, postgres.ErrAuditPruneBusy):
b.log.DebugContext(ctx, "audit retention sweep skipped: another sweep holds the lock")
case err != nil:
if ctx.Err() == nil {
b.log.WarnContext(ctx, "prune audit events", "err", err)
}
default:
b.log.InfoContext(ctx, "pruned audit observability events", "rows", rows, "kinds", len(cutoffs))
}
}