mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 01:32:00 +01:00
Schema v6 shipped audit_logging_persistent and audit_events_30d as Pro adoption signals. Neither discriminated. pkg/server installs the SQLite audit logger on every install for defense in depth and gates only the read/export endpoints, so the boolean was true on all 8 installs that had taken rc.8 and 0 rows in the retained table have ever had it false. The event count measured that background write volume: three of those eight unlicensed community installs were pegged at the receiver's 100000 clamp ceiling, with the rest between 4863 and 67509. Schema v7 replaces both with audit_reads_30d, a count of requests that cleared the license gate on an audit read or export surface. A read requires a human action, so unlike store presence or write volume it cannot settle into a constant. The recorder is wrapped INSIDE RequireLicenseFeature so unentitled requests never count, and the persisted marker carries a timestamp and a coarse activity class from a fixed allowlist. Query filters, actors, ranges, and every audit row read stay on the install. The retired columns are left in the live database. They hold real rc.8 rows and migrations only add, so dropping them would be a pointless risk; nothing writes them once the receiver struct loses the fields. Adds the guard this class needed. LicensedFeatureAdoptionFields registers every field that exists to measure licensed-feature adoption, and TestLicensedFeatureAdoptionFieldsDiscriminate builds an unused install through the real production snapshot paths, installs a real SQLite audit logger exactly as pkg/server does, records a baseline audit event, and fails if any registered field is non-zero. Pinning a console logger there would have made the guard pass while the payload lied, so it deliberately does not. The guard was verified by reintroducing the v6 sourcing and confirming it fails with the field named. A companion test pins the three retired fields so they cannot return under their old names. This is the third instance of one bug class. v6 removed pulse_intelligence_patrol_autofixes_30d, hardcoded to zero with no increment site, and then introduced two fields that were constant in the other direction. Three occurrences is a guard, not a habit. Verified end to end on a running unlicensed install: the payload that reported audit_logging_persistent true under v6 now reports audit_reads_30d 0, and seeding two in-window reads, one outside the window, and one with an invalid activity class yields 2.
156 lines
4.8 KiB
Go
156 lines
4.8 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// normalizeAuditReadActivityRecord drops records that carry no recognized
|
|
// activity class. Unknown classes are rejected rather than coerced so a future
|
|
// caller cannot silently widen what this history means.
|
|
func normalizeAuditReadActivityRecord(record AuditReadActivityRecord) (AuditReadActivityRecord, bool) {
|
|
if record.Timestamp.IsZero() {
|
|
return AuditReadActivityRecord{}, false
|
|
}
|
|
record.Timestamp = record.Timestamp.UTC()
|
|
switch strings.TrimSpace(record.Activity) {
|
|
case AuditReadActivityList, AuditReadActivityExport, AuditReadActivityVerify, AuditReadActivitySummary:
|
|
record.Activity = strings.TrimSpace(record.Activity)
|
|
default:
|
|
return AuditReadActivityRecord{}, false
|
|
}
|
|
return record, true
|
|
}
|
|
|
|
func normalizeAuditReadActivityRecords(records []AuditReadActivityRecord) []AuditReadActivityRecord {
|
|
if records == nil {
|
|
return make([]AuditReadActivityRecord, 0)
|
|
}
|
|
normalized := make([]AuditReadActivityRecord, 0, len(records))
|
|
for _, record := range records {
|
|
record, ok := normalizeAuditReadActivityRecord(record)
|
|
if !ok {
|
|
continue
|
|
}
|
|
normalized = append(normalized, record)
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
func pruneAuditReadActivityRecords(records []AuditReadActivityRecord, cutoff time.Time, maxRecords int) []AuditReadActivityRecord {
|
|
normalized := normalizeAuditReadActivityRecords(records)
|
|
if !cutoff.IsZero() {
|
|
kept := normalized[:0]
|
|
for _, record := range normalized {
|
|
if record.Timestamp.Before(cutoff) {
|
|
continue
|
|
}
|
|
kept = append(kept, record)
|
|
}
|
|
normalized = kept
|
|
}
|
|
if maxRecords > 0 && len(normalized) > maxRecords {
|
|
normalized = normalized[len(normalized)-maxRecords:]
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
func newEmptyAuditReadActivityHistoryData() *AuditReadActivityHistoryData {
|
|
return &AuditReadActivityHistoryData{
|
|
Version: 1,
|
|
Events: make([]AuditReadActivityRecord, 0),
|
|
}
|
|
}
|
|
|
|
// RecordAuditReadActivity appends a content-free marker that an entitled
|
|
// operator reached one of the license-gated audit read or export surfaces.
|
|
func (c *ConfigPersistence) RecordAuditReadActivity(record AuditReadActivityRecord) error {
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
now := time.Now().UTC()
|
|
if record.Timestamp.IsZero() {
|
|
record.Timestamp = now
|
|
}
|
|
record, ok := normalizeAuditReadActivityRecord(record)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return recordActivityHistoryLocked(c, c.auditReadActivityFile,
|
|
"audit read activity history", "Resetting unreadable audit read activity history",
|
|
newEmptyAuditReadActivityHistoryData,
|
|
func(history *AuditReadActivityHistoryData) {
|
|
history.Events = append(history.Events, record)
|
|
history.Events = pruneAuditReadActivityRecords(
|
|
history.Events,
|
|
record.Timestamp.Add(-auditReadActivityHistoryRetention),
|
|
maxAuditReadActivityHistoryRecords,
|
|
)
|
|
history.Version = 1
|
|
history.LastSaved = now
|
|
})
|
|
}
|
|
|
|
// SaveAuditReadActivityHistory replaces the persisted audit-read history.
|
|
func (c *ConfigPersistence) SaveAuditReadActivityHistory(events []AuditReadActivityRecord) error {
|
|
normalized := normalizeAuditReadActivityRecords(events)
|
|
data := AuditReadActivityHistoryData{
|
|
Version: 1,
|
|
LastSaved: time.Now().UTC(),
|
|
Events: normalized,
|
|
}
|
|
return saveHistoryData(c, c.auditReadActivityFile, data, len(normalized),
|
|
"audit read activity history", "Audit read activity history")
|
|
}
|
|
|
|
// LoadAuditReadActivityHistory reads the persisted audit-read history.
|
|
func (c *ConfigPersistence) LoadAuditReadActivityHistory() (*AuditReadActivityHistoryData, error) {
|
|
return loadHistoryData(
|
|
c.fs,
|
|
&c.mu,
|
|
c.auditReadActivityFile,
|
|
c.crypto,
|
|
newEmptyAuditReadActivityHistoryData,
|
|
func(data *AuditReadActivityHistoryData) {
|
|
data.Events = normalizeAuditReadActivityRecords(data.Events)
|
|
},
|
|
func(data *AuditReadActivityHistoryData) error {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal audit read activity history migration rewrite: %w", err)
|
|
}
|
|
return rewriteEncryptedJSONLocked(c, c.auditReadActivityFile, jsonData, "audit read activity history migration rewrite")
|
|
},
|
|
func(data *AuditReadActivityHistoryData) int {
|
|
return len(data.Events)
|
|
},
|
|
func(data *AuditReadActivityHistoryData) time.Time {
|
|
return data.LastSaved
|
|
},
|
|
"Audit read activity history",
|
|
)
|
|
}
|
|
|
|
// CountAuditReadActivitySince returns how many audit reads happened at or after
|
|
// the cutoff. Count only: the activity classes are not broken out, so the
|
|
// telemetry payload cannot describe what an operator looked at.
|
|
func (c *ConfigPersistence) CountAuditReadActivitySince(since time.Time) int {
|
|
if c == nil {
|
|
return 0
|
|
}
|
|
history, err := c.LoadAuditReadActivityHistory()
|
|
if err != nil || history == nil {
|
|
return 0
|
|
}
|
|
count := 0
|
|
for _, record := range history.Events {
|
|
if record.Timestamp.Before(since) {
|
|
continue
|
|
}
|
|
count++
|
|
}
|
|
return count
|
|
}
|