Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(hub): scope container/systemd/SMART/ZFS upserts to the reporting system (#2835)

Sven van Ginkel927ac992de04
diff --git a/internal/hub/systems/record_ownership_test.go b/internal/hub/systems/record_ownership_test.go
new file mode 100644
index 000000000..8145e9bed
--- /dev/null
+++ b/internal/hub/systems/record_ownership_test.go
@@ -0,0 +1,93 @@
+//go:build testing
+
+package systems
+
+import (
+ "testing"
+
+ "ganigeorgiev.com/pocketbase/dbx"
+ "ganigeorgiev.com/pocketbase/pocketbase/core"
+ "github.com/henrygd/beszel/internal/entities/container"
+ "github.com/henrygd/beszel/internal/entities/smart"
+ "github.com/henrygd/beszel/internal/entities/systemd"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// Record IDs are derived from agent-supplied values, so one system must never be
+// able to take over or overwrite a record that belongs to another system.
+
+func newOtherSystem(t *testing.T, app core.App) string {
+ t.Helper()
+ col, err := app.FindCachedCollectionByNameOrId("systems")
+ require.NoError(t, err)
+ rec := core.NewRecord(col)
+ rec.Set("name", "other-system")
+ rec.Set("host", "127.0.0.2")
+ rec.Set("port", "45876")
+ require.NoError(t, app.SaveNoValidate(rec))
+ return rec.Id
+}
+
+func TestCreateContainerRecordsDoesNotTakeOverOtherSystemsRecord(t *testing.T) {
+ sys, app := newTestSystemWithHub(t)
+ other := newOtherSystem(t, app)
+
+ require.NoError(t, createContainerRecords(app, []*container.Stats{{Id: "shared-id", Name: "victim"}}, other))
+ require.NoError(t, createContainerRecords(app, []*container.Stats{{Id: "shared-id", Name: "attacker"}}, sys.Id))
+
+ var rec struct {
+ System string `db:"system"`
+ Name string `db:"name"`
+ }
+ require.NoError(t, app.DB().Select("system", "name").From("containers").
+ Where(dbx.HashExp{"id": "shared-id"}).One(&rec))
+ assert.Equal(t, other, rec.System)
+ assert.Equal(t, "victim", rec.Name)
+}
+
+func TestCreateSystemdStatsRecordsDoesNotTakeOverOtherSystemsRecord(t *testing.T) {
+ sys, app := newTestSystemWithHub(t)
+ other := newOtherSystem(t, app)
+
+ // Plant a record owned by another system at the ID this system would compute.
+ id := MakeStableHashId(sys.Id, "a.service")
+ _, err := app.DB().Insert("systemd_services", dbx.Params{
+ "id": id, "system": other, "name": "victim.service", "updated": 1,
+ }).Execute()
+ require.NoError(t, err)
+
+ require.NoError(t, createSystemdStatsRecords(app, []*systemd.Service{{Name: "a.service"}}, sys.Id))
+
+ var rec struct {
+ System string `db:"system"`
+ Name string `db:"name"`
+ }
+ require.NoError(t, app.DB().Select("system", "name").From("systemd_services").
+ Where(dbx.HashExp{"id": id}).One(&rec))
+ assert.Equal(t, other, rec.System)
+ assert.Equal(t, "victim.service", rec.Name)
+}
+
+func TestSaveSmartDevicesDoesNotTakeOverOtherSystemsRecord(t *testing.T) {
+ sys, app := newTestSystemWithHub(t)
+ other := newOtherSystem(t, app)
+
+ col, err := app.FindCachedCollectionByNameOrId("smart_devices")
+ require.NoError(t, err)
+ id := MakeStableHashId(sys.Id, "AAA")
+ planted := core.NewRecord(col)
+ planted.Set("id", id)
+ planted.Set("system", other)
+ planted.Set("name", "victim-disk")
+ require.NoError(t, app.SaveNoValidate(planted))
+
+ require.NoError(t, sys.saveSmartDevices(map[string]smart.SmartData{
+ "AAA": {SerialNumber: "AAA", DiskName: "attacker-disk"},
+ }, false))
+
+ rec, err := app.FindRecordById("smart_devices", id)
+ require.NoError(t, err)
+ assert.Equal(t, other, rec.GetString("system"))
+ assert.Equal(t, "victim-disk", rec.GetString("name"))
+}
diff --git a/internal/hub/systems/system.go b/internal/hub/systems/system.go
index 263545f44..bbd6514bc 100644
--- a/internal/hub/systems/system.go
+++ b/internal/hub/systems/system.go
@@ -445,7 +445,7 @@ func createSystemdStatsRecords(app core.App, data []*systemd.Service, systemId s
params["memPeak"+suffix] = service.MemPeak
}
queryString := fmt.Sprintf(
- "INSERT INTO systemd_services (id, system, name, state, sub, cpu, cpuPeak, memory, memPeak, updated) VALUES %s ON CONFLICT(id) DO UPDATE SET system = excluded.system, name = excluded.name, state = excluded.state, sub = excluded.sub, cpu = excluded.cpu, cpuPeak = excluded.cpuPeak, memory = excluded.memory, memPeak = excluded.memPeak, updated = excluded.updated",
+ "INSERT INTO systemd_services (id, system, name, state, sub, cpu, cpuPeak, memory, memPeak, updated) VALUES %s ON CONFLICT(id) DO UPDATE SET name = excluded.name, state = excluded.state, sub = excluded.sub, cpu = excluded.cpu, cpuPeak = excluded.cpuPeak, memory = excluded.memory, memPeak = excluded.memPeak, updated = excluded.updated WHERE systemd_services.system = excluded.system",
strings.Join(valueStrings, ","),
)
if _, err := app.DB().NewQuery(queryString).Bind(params).Execute(); err != nil {
@@ -599,7 +599,8 @@ func (sys *System) updateNetworkMonitorsRecords(app core.App, monitorResults map
return nil
}
-// createContainerRecords creates container records
+// createContainerRecords creates container records. Record IDs are agent-supplied,
+// so an ID owned by another system is left untouched rather than taken over.
func createContainerRecords(app core.App, data []*container.Stats, systemId string) error {
if len(data) == 0 {
return nil
@@ -629,7 +630,7 @@ func createContainerRecords(app core.App, data []*container.Stats, systemId stri
params["updateAvailable"+suffix] = container.UpdateAvailable
}
queryString := fmt.Sprintf(
- "INSERT INTO containers (id, system, name, image, ports, status, health, cpu, memory, net, updatable, updated) VALUES %s ON CONFLICT(id) DO UPDATE SET system = excluded.system, name = excluded.name, image = excluded.image, ports = excluded.ports, status = excluded.status, health = excluded.health, cpu = excluded.cpu, memory = excluded.memory, net = excluded.net, updatable = excluded.updatable, updated = excluded.updated",
+ "INSERT INTO containers (id, system, name, image, ports, status, health, cpu, memory, net, updatable, updated) VALUES %s ON CONFLICT(id) DO UPDATE SET name = excluded.name, image = excluded.image, ports = excluded.ports, status = excluded.status, health = excluded.health, cpu = excluded.cpu, memory = excluded.memory, net = excluded.net, updatable = excluded.updatable, updated = excluded.updated WHERE containers.system = excluded.system",
strings.Join(valueStrings, ","),
)
_, err := app.DB().NewQuery(queryString).Bind(params).Execute()
diff --git a/internal/hub/systems/system_smart.go b/internal/hub/systems/system_smart.go
index e82456522..3b900178f 100644
--- a/internal/hub/systems/system_smart.go
+++ b/internal/hub/systems/system_smart.go
@@ -125,6 +125,9 @@ func (sys *System) upsertSmartDeviceRecord(app core.App, collection *core.Collec
}
record = core.NewRecord(collection)
record.Set("id", recordID)
+ } else if record.GetString("system") != sys.Id {
+ // ID collision with another system's device: never take it over.
+ return nil
}
name := device.DiskName
diff --git a/internal/hub/systems/system_zfs.go b/internal/hub/systems/system_zfs.go
index f9f1c6142..217ae0c5d 100644
--- a/internal/hub/systems/system_zfs.go
+++ b/internal/hub/systems/system_zfs.go
@@ -138,6 +138,9 @@ func (sys *System) upsertZfsPoolRecord(app core.App, collection *core.Collection
}
record = core.NewRecord(collection)
record.Set("id", recordID)
+ } else if record.GetString("system") != sys.Id {
+ // ID collision with another system's pool: never take it over.
+ return nil
}
record.Set("system", sys.Id)
@@ -193,6 +196,9 @@ func (sys *System) syncZfsPoolHealth(app core.App, pools map[string]*system.ZfsP
}
continue
}
+ if record.GetString("system") != sys.Id {
+ continue
+ }
if record.GetString("health") == pool.Health && record.GetBool("raw") == pool.Raw && record.GetString("display_name") == pool.DisplayName {
continue
}