Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Redact legacy action audit verification details

rcourtmand3670d7882be
diff --git a/internal/unifiedresources/audit_redaction.go b/internal/unifiedresources/audit_redaction.go
index 27b29581b4..1eb689c4d3 100644
--- a/internal/unifiedresources/audit_redaction.go
+++ b/internal/unifiedresources/audit_redaction.go
@@ -90,6 +90,17 @@ func redactActionVerificationResult(result *ActionVerificationResult) *ActionVer
return redacted
}
+func redactActionExecutionResult(result *ExecutionResult) *ExecutionResult {
+ if result == nil {
+ return nil
+ }
+ redacted := *result
+ redacted.Output = RedactAuditText(redacted.Output)
+ redacted.ErrorMessage = RedactAuditText(redacted.ErrorMessage)
+ redacted.Verification = redactActionVerificationResult(redacted.Verification)
+ return &redacted
+}
+
// RedactAuditRecord returns a copy of the input ActionAuditRecord with
// known secret shapes scrubbed from the operator-authored reason, the
// params map's string values, execution output, and error text. Verification
@@ -111,13 +122,7 @@ func RedactAuditRecord(record ActionAuditRecord) ActionAuditRecord {
}
record.Request.Params = redactedParams
}
- if record.Result != nil {
- redacted := *record.Result
- redacted.Output = RedactAuditText(redacted.Output)
- redacted.ErrorMessage = RedactAuditText(redacted.ErrorMessage)
- redacted.Verification = redactActionVerificationResult(redacted.Verification)
- record.Result = &redacted
- }
+ record.Result = redactActionExecutionResult(record.Result)
record.Verification = redactActionVerificationResult(record.Verification)
record.VerificationOutcome.EvidenceSummary = RedactAuditText(record.VerificationOutcome.EvidenceSummary)
return record
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 8755f00c65..3d1a596b54 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -389,10 +389,17 @@ func TestActionExecutionContractStaysAPIOwned(t *testing.T) {
// across future refactors.
"record = RedactAuditRecord(normalized)",
"normalizedRecord = RedactAuditRecord(normalizedRecord)",
+ // SQLite audit reads must also pass through the shared
+ // redaction boundary so legacy rows cannot expose verification
+ // command/output/note details after read-time normalization.
+ "func redactActionAuditRecordFromStore(record ActionAuditRecord) ActionAuditRecord",
+ "return redactActionAuditRecordFromStore(record), nil",
+ "func (s *SQLiteResourceStore) migrateActionAuditRedaction() error",
},
filepath.Join(".", "audit_redaction.go"): {
"func RedactAuditText(s string) string",
"func RedactAuditRecord(record ActionAuditRecord) ActionAuditRecord",
+ "func redactActionExecutionResult(result *ExecutionResult) *ExecutionResult",
},
filepath.Join("..", "api", "actions.go"): {
"type ActionExecutor interface",
diff --git a/internal/unifiedresources/store.go b/internal/unifiedresources/store.go
index 1e7b47f1f6..9fe1087287 100644
--- a/internal/unifiedresources/store.go
+++ b/internal/unifiedresources/store.go
@@ -433,6 +433,9 @@ func (s *SQLiteResourceStore) initSchema() error {
if err := s.migrateActionAuditsSchema(); err != nil {
return err
}
+ if err := s.migrateActionAuditRedaction(); err != nil {
+ return err
+ }
return nil
}
@@ -454,6 +457,73 @@ func (s *SQLiteResourceStore) migrateActionAuditsSchema() error {
return nil
}
+func (s *SQLiteResourceStore) migrateActionAuditRedaction() error {
+ rows, err := s.db.Query(`
+ SELECT id, result_json
+ FROM action_audits
+ WHERE result_json IS NOT NULL AND result_json != '' AND result_json != 'null'
+ `)
+ if err != nil {
+ return fmt.Errorf("query action audit redaction migration rows: %w", err)
+ }
+ defer rows.Close()
+
+ type redactionUpdate struct {
+ id string
+ resultJSON string
+ }
+ var updates []redactionUpdate
+ for rows.Next() {
+ var id, resultJSON string
+ if err := rows.Scan(&id, &resultJSON); err != nil {
+ return fmt.Errorf("scan action audit redaction migration row: %w", err)
+ }
+ var result ExecutionResult
+ if err := json.Unmarshal([]byte(resultJSON), &result); err != nil {
+ return fmt.Errorf("unmarshal action audit result for redaction migration %q: %w", id, err)
+ }
+ redactedResult := redactActionExecutionResult(&result)
+ redactedJSON, err := json.Marshal(redactedResult)
+ if err != nil {
+ return fmt.Errorf("marshal action audit result for redaction migration %q: %w", id, err)
+ }
+ if string(redactedJSON) != resultJSON {
+ updates = append(updates, redactionUpdate{id: id, resultJSON: string(redactedJSON)})
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return fmt.Errorf("iterate action audit redaction migration rows: %w", err)
+ }
+ if len(updates) == 0 {
+ return nil
+ }
+
+ tx, err := s.db.Begin()
+ if err != nil {
+ return fmt.Errorf("begin action audit redaction migration: %w", err)
+ }
+ committed := false
+ defer func() {
+ if !committed {
+ _ = tx.Rollback()
+ }
+ }()
+ for _, update := range updates {
+ if _, err := tx.Exec(`
+ UPDATE action_audits
+ SET result_json = ?
+ WHERE id = ?
+ `, update.resultJSON, update.id); err != nil {
+ return fmt.Errorf("update action audit result redaction %q: %w", update.id, err)
+ }
+ }
+ if err := tx.Commit(); err != nil {
+ return fmt.Errorf("commit action audit redaction migration: %w", err)
+ }
+ committed = true
+ return nil
+}
+
func (s *SQLiteResourceStore) migrateResourceChangesSchema() error {
columns, err := s.tableColumns("resource_changes")
if err != nil {
@@ -1107,7 +1177,11 @@ func scanActionAuditRecord(scanner actionAuditScanner) (ActionAuditRecord, error
record.Request.RequestID = requestID
record.Request.ResourceID = CanonicalResourceID(record.Request.ResourceID)
_ = actionID
- return normalizeActionAuditRecordFromStore(record), nil
+ return redactActionAuditRecordFromStore(record), nil
+}
+
+func redactActionAuditRecordFromStore(record ActionAuditRecord) ActionAuditRecord {
+ return RedactAuditRecord(normalizeActionAuditRecordFromStore(record))
}
func normalizeActionAuditRecordFromStore(record ActionAuditRecord) ActionAuditRecord {
diff --git a/internal/unifiedresources/store_test.go b/internal/unifiedresources/store_test.go
index f9fb2417a5..8489cce000 100644
--- a/internal/unifiedresources/store_test.go
+++ b/internal/unifiedresources/store_test.go
@@ -458,6 +458,48 @@ func insertRawActionAuditForTest(t *testing.T, store *SQLiteResourceStore, recor
}
}
+func rawActionAuditResultJSONForTest(t *testing.T, store *SQLiteResourceStore, id string) string {
+ t.Helper()
+ var resultJSON string
+ if err := store.db.QueryRow(`SELECT result_json FROM action_audits WHERE id = ?`, id).Scan(&resultJSON); err != nil {
+ t.Fatalf("query raw action audit result_json: %v", err)
+ }
+ return resultJSON
+}
+
+func assertActionAuditVerificationDetailsRedactedForTest(t *testing.T, record ActionAuditRecord, rawDetails ...string) {
+ t.Helper()
+ verification := CanonicalActionVerification(record)
+ if verification == nil {
+ t.Fatalf("expected canonical verification to remain present: %+v", record)
+ }
+ if verification.Command != auditVerificationCommandRedacted {
+ t.Fatalf("verification command was not redacted: %+v", verification)
+ }
+ if verification.Output != auditVerificationOutputRedacted {
+ t.Fatalf("verification output was not redacted: %+v", verification)
+ }
+ if verification.Note != auditVerificationNoteRedacted {
+ t.Fatalf("verification note was not redacted: %+v", verification)
+ }
+ if record.Result == nil || record.Result.Verification == nil {
+ t.Fatalf("expected result verification to remain aligned: %+v", record.Result)
+ }
+ if record.Result.Verification.Command != verification.Command || record.Result.Verification.Output != verification.Output || record.Result.Verification.Note != verification.Note {
+ t.Fatalf("result verification was not aligned with canonical verification: result=%+v canonical=%+v", record.Result.Verification, verification)
+ }
+
+ wire, err := json.Marshal(record)
+ if err != nil {
+ t.Fatalf("marshal action audit record for leak check: %v", err)
+ }
+ for _, rawDetail := range rawDetails {
+ if strings.Contains(string(wire), rawDetail) {
+ t.Fatalf("raw verification detail %q leaked through action audit read: %s", rawDetail, string(wire))
+ }
+ }
+}
+
func TestRecordChange_RoundTrip(t *testing.T) {
store := newTestStore(t)
now := time.Now().UTC().Truncate(time.Second)
@@ -1344,9 +1386,12 @@ func TestActionAuditRecord_RoundTrip(t *testing.T) {
}
}
-func TestActionAuditRecord_RoundTripLegacyResultVerificationCanonicalizesSQLiteRead(t *testing.T) {
+func TestActionAuditRecord_RoundTripLegacyResultVerificationRedactsSQLiteReads(t *testing.T) {
store := newTestStore(t)
now := time.Date(2026, 3, 18, 14, 0, 0, 0, time.UTC)
+ rawCommand := "systemctl is-active nginx --token legacy-command-secret"
+ rawOutput := "active legacy-output-secret"
+ rawNote := "legacy note password=legacy-note-secret"
insertRawActionAuditForTest(t, store, ActionAuditRecord{
ID: "action-legacy-verification",
@@ -1369,9 +1414,10 @@ func TestActionAuditRecord_RoundTripLegacyResultVerificationCanonicalizesSQLiteR
Verification: &ActionVerificationResult{
Ran: true,
Success: true,
- Command: "systemctl is-active nginx",
- Output: "active",
+ Command: rawCommand,
+ Output: rawOutput,
RanAt: now.Add(30 * time.Second),
+ Note: rawNote,
},
},
})
@@ -1383,11 +1429,86 @@ func TestActionAuditRecord_RoundTripLegacyResultVerificationCanonicalizesSQLiteR
if !ok {
t.Fatal("expected raw legacy action audit row")
}
- if got.Verification == nil || got.Verification.Command != "systemctl is-active nginx" {
- t.Fatalf("legacy result.verification was not restored onto top-level verification: %+v", got.Verification)
+ if got.Verification == nil || !got.Verification.Ran || !got.Verification.Success || !got.Verification.RanAt.Equal(now.Add(30*time.Second)) {
+ t.Fatalf("legacy verification semantics were not preserved: %+v", got.Verification)
}
- if got.Result == nil || got.Result.Verification == nil || got.Result.Verification.Command != got.Verification.Command {
- t.Fatalf("legacy result verification was not kept aligned: result=%+v canonical=%+v", got.Result, got.Verification)
+ assertActionAuditVerificationDetailsRedactedForTest(t, got, rawCommand, rawOutput, rawNote)
+
+ results, err := store.GetActionAudits("vm:legacy-verification", now.Add(-time.Hour), 10)
+ if err != nil {
+ t.Fatalf("GetActionAudits: %v", err)
+ }
+ if len(results) != 1 {
+ t.Fatalf("expected 1 raw legacy action audit row, got %d", len(results))
+ }
+ assertActionAuditVerificationDetailsRedactedForTest(t, results[0], rawCommand, rawOutput, rawNote)
+}
+
+func TestActionAuditRecord_LegacyResultVerificationMigrationRedactsSQLiteAtRest(t *testing.T) {
+ dir := t.TempDir()
+ now := time.Date(2026, 3, 18, 15, 0, 0, 0, time.UTC)
+ rawCommand := "systemctl is-active nginx --token legacy-command-at-rest"
+ rawOutput := "active legacy-output-at-rest"
+ rawNote := "legacy note password=legacy-note-at-rest"
+
+ store, err := NewSQLiteResourceStore(dir, "testorg")
+ if err != nil {
+ t.Fatalf("NewSQLiteResourceStore: %v", err)
+ }
+ insertRawActionAuditForTest(t, store, ActionAuditRecord{
+ ID: "action-legacy-at-rest",
+ CreatedAt: now,
+ UpdatedAt: now.Add(time.Minute),
+ State: ActionStateCompleted,
+ Request: ActionRequest{
+ RequestID: "req-legacy-at-rest",
+ ResourceID: "vm:legacy-at-rest",
+ CapabilityName: "restart",
+ RequestedBy: "agent:test",
+ },
+ Plan: ActionPlan{
+ ActionID: "action-legacy-at-rest",
+ RequestID: "req-legacy-at-rest",
+ Allowed: true,
+ },
+ Result: &ExecutionResult{
+ Success: true,
+ Verification: &ActionVerificationResult{
+ Ran: true,
+ Success: true,
+ Command: rawCommand,
+ Output: rawOutput,
+ RanAt: now.Add(30 * time.Second),
+ Note: rawNote,
+ },
+ },
+ })
+ before := rawActionAuditResultJSONForTest(t, store, "action-legacy-at-rest")
+ for _, rawDetail := range []string{rawCommand, rawOutput, rawNote} {
+ if !strings.Contains(before, rawDetail) {
+ t.Fatalf("raw setup did not persist %q before migration: %s", rawDetail, before)
+ }
+ }
+ if err := store.Close(); err != nil {
+ t.Fatalf("close store before migration reopen: %v", err)
+ }
+
+ store, err = NewSQLiteResourceStore(dir, "testorg")
+ if err != nil {
+ t.Fatalf("NewSQLiteResourceStore after raw legacy insert: %v", err)
+ }
+ defer store.Close()
+
+ after := rawActionAuditResultJSONForTest(t, store, "action-legacy-at-rest")
+ for _, rawDetail := range []string{rawCommand, rawOutput, rawNote} {
+ if strings.Contains(after, rawDetail) {
+ t.Fatalf("raw verification detail %q survived SQLite migration: %s", rawDetail, after)
+ }
+ }
+ for _, marker := range []string{auditVerificationCommandRedacted, auditVerificationOutputRedacted, auditVerificationNoteRedacted} {
+ if !strings.Contains(after, marker) {
+ t.Fatalf("expected migration marker %q in result_json, got %s", marker, after)
+ }
}
}