Files
archie/internal/infrastructure/controlplanestore/audit_retention.go
T

104 lines
3.3 KiB
Go

package controlplanestore
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
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.
type auditRetention struct {
RuntimeEventRetention string `json:"runtime_event_retention"`
}
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 {
_, err := parseRuntimeEventRetention(document.RuntimeEventRetention)
return err
})
}
// RuntimeEventRetention decodes a stored audit-retention document and returns
// how long observability events are kept. Zero means keep forever. The sweep
// uses this same parser, so a value the control plane accepted is one the
// sweep can run.
func RuntimeEventRetention(document []byte) (time.Duration, error) {
decoded, err := auditRetentionDocument(document)
if err != nil {
return 0, err
}
return parseRuntimeEventRetention(decoded.RuntimeEventRetention)
}
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 the retention 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 := parseRuntimeEventRetention(decoded.RuntimeEventRetention)
if err != nil {
return nil, err
}
decoded.RuntimeEventRetention = formatRuntimeEventRetention(retention)
return json.Marshal(decoded)
}
func formatRuntimeEventRetention(retention time.Duration) string {
if retention == 0 {
return "0"
}
return strconv.FormatInt(int64(retention/time.Hour), 10) + "h"
}
func parseRuntimeEventRetention(value string) (time.Duration, error) {
retention, err := time.ParseDuration(value)
if err != nil {
return 0, fmt.Errorf("runtime_event_retention must be a duration such as 2160h: %w", err)
}
if retention < 0 {
return 0, fmt.Errorf("runtime_event_retention must not be negative")
}
if retention > 0 && retention < MinimumAuditRetention {
return 0, fmt.Errorf("runtime_event_retention must be at least 168h, or 0 to keep forever")
}
return retention, nil
}