Files
archie/internal/infrastructure/controlplanestore/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

167 lines
5.6 KiB
Go

package controlplanestore
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/audit"
)
const (
AuditRetentionKind = "audit-retention"
// MinimumAuditRetention is the shortest retention the resource accepts:
// below a week the audit trail is unlikely to outlive the investigation
// that needs it.
MinimumAuditRetention = 7 * 24 * time.Hour
// auditRetentionSeed keeps observability events for 90 days.
auditRetentionSeed = "2160h"
)
// auditRetention is the audit-retention document. Zero keeps the observability
// trail forever; any other value is a Go duration of at least a week.
// EventRetention overrides the default for named prunable kinds (for example
// agent_transcript); an omitted kind inherits the default. The sweep may only
// delete kinds the audit domain marks prunable, so a security record cannot be
// given a window here.
type auditRetention struct {
RuntimeEventRetention string `json:"runtime_event_retention"`
EventRetention map[string]string `json:"event_retention,omitempty"`
}
func auditRetentionDefinition() Definition {
return Definition{
Kind: AuditRetentionKind, Title: "Audit retention", ApplyMode: "live",
Document: auditRetention{},
Seed: func(config.Config) any { return auditRetention{RuntimeEventRetention: auditRetentionSeed} },
Validate: validateAuditRetention,
Normalize: normalizeAuditRetention,
}
}
func validateAuditRetention(input []byte) error {
return validateAs(input, func(document auditRetention) error {
if _, err := parseRetention("runtime_event_retention", document.RuntimeEventRetention); err != nil {
return err
}
for kind, value := range document.EventRetention {
if !audit.PrunableEventKind(kind) {
return fmt.Errorf("event_retention: %q is not an event kind whose retention can be set", kind)
}
if _, err := parseRetention("event_retention."+kind, value); err != nil {
return err
}
}
return nil
})
}
// RetentionPolicy is a decoded audit-retention document: how long observability
// events are kept by default, and the per-kind windows that replace it.
type RetentionPolicy struct {
Default time.Duration
Overrides map[string]time.Duration
}
// Cutoffs returns the delete-before instant for every kind in kinds that has a
// window; a kind kept forever is absent, and an empty result means the sweep
// has nothing to do. The sweep and the control plane share this decoder, so a
// value the control plane accepted is one the sweep can run.
func (p RetentionPolicy) Cutoffs(now time.Time, kinds []string) map[string]time.Time {
cutoffs := make(map[string]time.Time, len(kinds))
for _, kind := range kinds {
keep := p.Default
if override, ok := p.Overrides[kind]; ok {
keep = override
}
if keep > 0 {
cutoffs[kind] = now.Add(-keep)
}
}
return cutoffs
}
// DecodeRetentionPolicy decodes a stored audit-retention document.
func DecodeRetentionPolicy(document []byte) (RetentionPolicy, error) {
decoded, err := auditRetentionDocument(document)
if err != nil {
return RetentionPolicy{}, err
}
policy := RetentionPolicy{Overrides: make(map[string]time.Duration, len(decoded.EventRetention))}
if policy.Default, err = parseRetention("runtime_event_retention", decoded.RuntimeEventRetention); err != nil {
return RetentionPolicy{}, err
}
for kind, value := range decoded.EventRetention {
if !audit.PrunableEventKind(kind) {
return RetentionPolicy{}, fmt.Errorf("event_retention: %q is not an event kind whose retention can be set", kind)
}
window, err := parseRetention("event_retention."+kind, value)
if err != nil {
return RetentionPolicy{}, err
}
policy.Overrides[kind] = window
}
return policy, nil
}
func auditRetentionDocument(document []byte) (auditRetention, error) {
var decoded auditRetention
reader := json.NewDecoder(bytes.NewReader(document))
reader.DisallowUnknownFields()
if err := reader.Decode(&decoded); err != nil {
return auditRetention{}, fmt.Errorf("decode %s: %w", AuditRetentionKind, err)
}
return decoded, nil
}
// normalizeAuditRetention stores every window in whole hours: the audit page
// parses "<n>h" only, so a value such as 604800s would read as zero there and
// the next save would disable pruning. Zero stays "0".
func normalizeAuditRetention(input []byte) ([]byte, error) {
decoded, err := auditRetentionDocument(input)
if err != nil {
return nil, err
}
retention, err := parseRetention("runtime_event_retention", decoded.RuntimeEventRetention)
if err != nil {
return nil, err
}
decoded.RuntimeEventRetention = formatRetention(retention)
if len(decoded.EventRetention) > 0 {
normalized := make(map[string]string, len(decoded.EventRetention))
for kind, value := range decoded.EventRetention {
window, err := parseRetention("event_retention."+kind, value)
if err != nil {
return nil, err
}
normalized[kind] = formatRetention(window)
}
decoded.EventRetention = normalized
}
return json.Marshal(decoded)
}
func formatRetention(retention time.Duration) string {
if retention == 0 {
return "0"
}
return strconv.FormatInt(int64(retention/time.Hour), 10) + "h"
}
func parseRetention(field, value string) (time.Duration, error) {
retention, err := time.ParseDuration(value)
if err != nil {
return 0, fmt.Errorf("%s must be a duration such as 2160h: %w", field, err)
}
if retention < 0 {
return 0, fmt.Errorf("%s must not be negative", field)
}
if retention > 0 && retention < MinimumAuditRetention {
return 0, fmt.Errorf("%s must be at least 168h, or 0 to keep forever", field)
}
return retention, nil
}