mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
Require the five sections emitted by every supported archive before starting a transaction. Preserve null webhook inventories, deliberate empty configurations, legacy optional fields and best-effort forward compatibility. Exercise encrypted rejected imports, byte-identical persistent state, unchanged runtime, failure audits and legacy recovery. Contract-Neutral: Rejects incomplete non-backup payloads before the existing replacement transaction; emitted archive formats, supported complete legacy backups, API response shape and tenant authority remain unchanged. Change-source: pulse-maintainer
42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// validateImportSections distinguishes an actual configuration backup from JSON
|
|
// that would silently decode to a zero-value ExportData. Every emitted format,
|
|
// including 4.0, contains these sections. Later additions remain optional, and
|
|
// unknown fields/versions retain their existing best-effort compatibility.
|
|
func validateImportSections(data []byte) error {
|
|
var sections struct {
|
|
Nodes json.RawMessage `json:"nodes"`
|
|
Alerts json.RawMessage `json:"alerts"`
|
|
Email json.RawMessage `json:"email"`
|
|
Webhooks json.RawMessage `json:"webhooks"`
|
|
System json.RawMessage `json:"system"`
|
|
}
|
|
if err := json.Unmarshal(data, §ions); err != nil {
|
|
return fmt.Errorf("invalid configuration backup: %w", err)
|
|
}
|
|
for _, section := range []struct {
|
|
name string
|
|
data json.RawMessage
|
|
}{
|
|
{"nodes", sections.Nodes}, {"alerts", sections.Alerts},
|
|
{"email", sections.Email}, {"system", sections.System},
|
|
} {
|
|
if len(section.data) == 0 || bytes.Equal(bytes.TrimSpace(section.data), []byte("null")) {
|
|
return fmt.Errorf("configuration backup is missing the %s section", section.name)
|
|
}
|
|
}
|
|
// An empty webhook inventory is legitimately encoded as null by older
|
|
// exports as well as by a fresh installation. Absence is not the same.
|
|
if len(sections.Webhooks) == 0 {
|
|
return fmt.Errorf("configuration backup is missing the webhooks section")
|
|
}
|
|
return nil
|
|
}
|