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.

fix(recovery): tolerate malformed persisted metadata

rcourtman2c3cf7d6b2d3
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index 250a24dd1a..f18f3d26d5 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -213,6 +213,11 @@ Recovery surfaces must resolve rollup and point item types through one shared
item-type helper instead of repeating `display.itemType` / `subjectType` /
`subjectRef.type` fallback chains across state, summary, details, and table
renderers.
+That same recovery-store decode boundary must fail soft on malformed persisted
+metadata. If a stored recovery row contains bad `subject_ref_json`,
+`repository_ref_json`, or `details_json`, the list endpoints should log and
+drop only the malformed derived field for that row rather than returning `500`
+for the entire recovery points or rollups surface.
That same hook-boundary normalization also owns the runtime recovery display
model. Canonical recovery points and rollups must expose `display.itemLabel`
and `display.itemType` to recovery consumers, while legacy transport fields
diff --git a/internal/recovery/store/store.go b/internal/recovery/store/store.go
index a3eeb3c817..0c2dc8a59b 100644
--- a/internal/recovery/store/store.go
+++ b/internal/recovery/store/store.go
@@ -508,6 +508,23 @@ func unmarshalJSON[T any](raw sql.NullString, target *T) error {
return json.Unmarshal([]byte(raw.String), target)
}
+func decodeRecoveryJSONField[T any](rowID string, field string, raw sql.NullString, target *T) bool {
+ if !raw.Valid || strings.TrimSpace(raw.String) == "" {
+ return false
+ }
+ if err := json.Unmarshal([]byte(raw.String), target); err != nil {
+ log.Warn().
+ Err(err).
+ Str("row_id", strings.TrimSpace(rowID)).
+ Str("field", strings.TrimSpace(field)).
+ Msg("Ignoring malformed recovery JSON field")
+ var zero T
+ *target = zero
+ return false
+ }
+ return true
+}
+
func normalizeLimit(limit int) int {
if limit <= 0 {
return defaultLimit
@@ -917,24 +934,18 @@ func (s *Store) ListPoints(ctx context.Context, opts recovery.ListPointsOptions)
}
var subjectRef recovery.ExternalRef
- if err := unmarshalJSON(subjectRefRaw, &subjectRef); err != nil {
- return nil, 0, err
- }
+ _ = decodeRecoveryJSONField(p.ID, "subject_ref_json", subjectRefRaw, &subjectRef)
if subjectRef.Type != "" {
p.SubjectRef = &subjectRef
}
var repoRef recovery.ExternalRef
- if err := unmarshalJSON(repoRefRaw, &repoRef); err != nil {
- return nil, 0, err
- }
+ _ = decodeRecoveryJSONField(p.ID, "repository_ref_json", repoRefRaw, &repoRef)
if repoRef.Type != "" {
p.RepositoryRef = &repoRef
}
var details map[string]any
- if err := unmarshalJSON(detailsRaw, &details); err != nil {
- return nil, 0, err
- }
+ _ = decodeRecoveryJSONField(p.ID, "details_json", detailsRaw, &details)
if len(details) > 0 {
p.Details = details
}
@@ -1256,9 +1267,7 @@ func (s *Store) ListRollups(ctx context.Context, opts recovery.ListPointsOptions
}
var subjectRef recovery.ExternalRef
- if err := unmarshalJSON(subjectRefRaw, &subjectRef); err != nil {
- return nil, 0, err
- }
+ _ = decodeRecoveryJSONField(subjectKey, "subject_ref_json", subjectRefRaw, &subjectRef)
var subjectRefPtr *recovery.ExternalRef
if subjectRef.Type != "" {
subjectRefPtr = &subjectRef
diff --git a/internal/recovery/store/store_rollups_test.go b/internal/recovery/store/store_rollups_test.go
index f8f9c1d726..4b08e38305 100644
--- a/internal/recovery/store/store_rollups_test.go
+++ b/internal/recovery/store/store_rollups_test.go
@@ -227,3 +227,64 @@ func TestStore_ListRollups(t *testing.T) {
t.Fatalf("ListRollups(itemType) = %#v total=%d, want only dataset rollup", got4, total4)
}
}
+
+func TestStore_ListRollups_IgnoresMalformedSubjectRefJSON(t *testing.T) {
+ t.Parallel()
+
+ dir := t.TempDir()
+ dbPath := filepath.Join(dir, "recovery.db")
+
+ store, err := Open(dbPath)
+ if err != nil {
+ t.Fatalf("Open() error = %v", err)
+ }
+ t.Cleanup(func() { _ = store.Close() })
+
+ now := time.Date(2026, 2, 19, 12, 0, 0, 0, time.UTC)
+
+ point := recovery.RecoveryPoint{
+ ID: "rollup-bad-json",
+ Provider: recovery.ProviderTrueNAS,
+ Kind: recovery.KindSnapshot,
+ Mode: recovery.ModeSnapshot,
+ Outcome: recovery.OutcomeSuccess,
+ StartedAt: &now,
+ CompletedAt: &now,
+ SubjectRef: &recovery.ExternalRef{
+ Type: "truenas-dataset",
+ Name: "tank/apps",
+ ID: "tank/apps",
+ },
+ }
+
+ if err := store.UpsertPoints(context.Background(), []recovery.RecoveryPoint{point}); err != nil {
+ t.Fatalf("UpsertPoints() error = %v", err)
+ }
+
+ if _, err := store.db.ExecContext(
+ context.Background(),
+ `UPDATE recovery_points
+ SET subject_ref_json = '{'
+ WHERE id = ?`,
+ point.ID,
+ ); err != nil {
+ t.Fatalf("corrupt recovery rollup json: %v", err)
+ }
+
+ got, total, err := store.ListRollups(context.Background(), recovery.ListPointsOptions{Page: 1, Limit: 50})
+ if err != nil {
+ t.Fatalf("ListRollups() error = %v, want graceful degradation", err)
+ }
+ if total != 1 || len(got) != 1 {
+ t.Fatalf("ListRollups() total=%d len=%d, want 1/1", total, len(got))
+ }
+ if got[0].SubjectRef != nil {
+ t.Fatalf("SubjectRef = %#v, want nil after malformed json fallback", got[0].SubjectRef)
+ }
+ if got[0].Display == nil || got[0].Display.SubjectLabel != "tank/apps" {
+ t.Fatalf("Display = %#v, want preserved normalized subject label", got[0].Display)
+ }
+ if got[0].Display == nil || got[0].Display.ItemType != "dataset" {
+ t.Fatalf("Display = %#v, want preserved normalized item type", got[0].Display)
+ }
+}
diff --git a/internal/recovery/store/store_test.go b/internal/recovery/store/store_test.go
index 0b4aca23e7..32527ed467 100644
--- a/internal/recovery/store/store_test.go
+++ b/internal/recovery/store/store_test.go
@@ -65,3 +65,77 @@ func TestStore_UpsertAndList(t *testing.T) {
t.Fatalf("subjectRef = %#v, want k8s-pvc", got[0].SubjectRef)
}
}
+
+func TestStore_ListPoints_IgnoresMalformedJSONFields(t *testing.T) {
+ t.Parallel()
+
+ dir := t.TempDir()
+ dbPath := filepath.Join(dir, "recovery.db")
+
+ store, err := Open(dbPath)
+ if err != nil {
+ t.Fatalf("Open() error = %v", err)
+ }
+ t.Cleanup(func() { _ = store.Close() })
+
+ now := time.Date(2026, 2, 18, 12, 0, 0, 0, time.UTC)
+
+ point := recovery.RecoveryPoint{
+ ID: "point-bad-json",
+ Provider: recovery.ProviderKubernetes,
+ Kind: recovery.KindSnapshot,
+ Mode: recovery.ModeSnapshot,
+ Outcome: recovery.OutcomeSuccess,
+ StartedAt: &now,
+ CompletedAt: &now,
+ SubjectRef: &recovery.ExternalRef{
+ Type: "k8s-pvc",
+ Namespace: "default",
+ Name: "data",
+ },
+ RepositoryRef: &recovery.ExternalRef{
+ Type: "velero-backup-storage-location",
+ Name: "repo-a",
+ },
+ Details: map[string]any{"foo": "bar"},
+ }
+
+ if err := store.UpsertPoints(context.Background(), []recovery.RecoveryPoint{point}); err != nil {
+ t.Fatalf("UpsertPoints() error = %v", err)
+ }
+
+ if _, err := store.db.ExecContext(
+ context.Background(),
+ `UPDATE recovery_points
+ SET subject_ref_json = '{',
+ repository_ref_json = '{',
+ details_json = '{'
+ WHERE id = ?`,
+ point.ID,
+ ); err != nil {
+ t.Fatalf("corrupt recovery point json: %v", err)
+ }
+
+ got, total, err := store.ListPoints(context.Background(), recovery.ListPointsOptions{Page: 1, Limit: 50})
+ if err != nil {
+ t.Fatalf("ListPoints() error = %v, want graceful degradation", err)
+ }
+ if total != 1 || len(got) != 1 {
+ t.Fatalf("ListPoints() total=%d len=%d, want 1/1", total, len(got))
+ }
+ if got[0].SubjectRef != nil {
+ t.Fatalf("SubjectRef = %#v, want nil after malformed json fallback", got[0].SubjectRef)
+ }
+ if got[0].RepositoryRef != nil {
+ t.Fatalf("RepositoryRef = %#v, want nil after malformed json fallback", got[0].RepositoryRef)
+ }
+ if got[0].Details != nil {
+ t.Fatalf("Details = %#v, want nil after malformed json fallback", got[0].Details)
+ }
+ if got[0].Display == nil || got[0].Display.SubjectLabel != "default/data" {
+ t.Fatalf("Display = %#v, want preserved normalized subject label", got[0].Display)
+ }
+ if got[0].Display == nil || got[0].Display.ItemType != "pvc" {
+ t.Fatalf("Display = %#v, want preserved normalized item type pvc", got[0].Display)
+ }
+}