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.

Prefer Linux distribution metadata over package heuristics

pulse-triage[bot]abacf3b482cc
diff --git a/internal/hostagent/enroll_test.go b/internal/hostagent/enroll_test.go
index 9aa95ea0e9..203bec03b6 100644
--- a/internal/hostagent/enroll_test.go
+++ b/internal/hostagent/enroll_test.go
@@ -1,8 +1,10 @@
package hostagent
import (
+ "compress/gzip"
"context"
"encoding/json"
+ "encoding/pem"
"errors"
"fmt"
"net/http"
@@ -15,6 +17,8 @@ import (
"testing"
"time"
+ "github.com/rcourtman/pulse-go-rewrite/internal/hostmetrics"
+ agentshost "github.com/rcourtman/pulse-go-rewrite/pkg/agents/host"
"github.com/rs/zerolog"
gohost "github.com/shirou/gopsutil/v4/host"
)
@@ -656,3 +660,124 @@ func TestEnrollPresentsTheIdentityNewReports(t *testing.T) {
t.Fatalf("New identity = %q/%q/%q, want the trimmed hostname, Proxmox VE, and the runtime architecture", agent.hostname, agent.osName, agent.architecture)
}
}
+
+// #2854: inject gopsutil's raspi-config heuristic, but read Debian's actual
+// metadata through a real synthetic /etc -> /usr/lib symlink. Neither the
+// reporter's Pi nor gopsutil's absolute-file heuristic is executed here.
+func TestEnrollAndReportUseDebianOSReleaseIdentity(t *testing.T) {
+ root := t.TempDir()
+ for _, dir := range []string{"etc", "usr/lib"} {
+ if err := os.MkdirAll(filepath.Join(root, dir), 0700); err != nil {
+ t.Fatal(err)
+ }
+ }
+ const release = "PRETTY_NAME=\"Debian GNU/Linux 13 (trixie)\"\nNAME=\"Debian GNU/Linux\"\nVERSION_ID=\"13\"\nDEBIAN_VERSION_FULL=13.7\nID=debian\n"
+ if err := os.WriteFile(filepath.Join(root, "usr/lib/os-release"), []byte(release), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink("../usr/lib/os-release", filepath.Join(root, "etc/os-release")); err != nil {
+ t.Fatal(err)
+ }
+ collector := realFSCollector()
+ collector.goos = "linux"
+ collector.hostInfoFn = func(context.Context) (*gohost.InfoStat, error) {
+ return &gohost.InfoStat{
+ Hostname: "pi-host", HostID: "synthetic-machine-id",
+ Platform: "raspbian", PlatformFamily: "debian", PlatformVersion: "13.7",
+ KernelArch: "aarch64",
+ }, nil
+ }
+ collector.readFileFn = func(path string) ([]byte, error) {
+ if path == "/etc/os-release" {
+ return os.ReadFile(filepath.Join(root, "etc/os-release"))
+ }
+ if strings.HasPrefix(path, root+string(os.PathSeparator)) {
+ return os.ReadFile(path)
+ }
+ return nil, os.ErrNotExist
+ }
+ collector.metricsFn = func(context.Context, []string) (hostmetrics.Snapshot, error) {
+ return hostmetrics.Snapshot{CPUCount: 4, CPUUsagePercent: 7.5}, nil
+ }
+ var enrollment enrollPayload
+ var received agentshost.Report
+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case enrollEndpoint:
+ if r.Header.Get("Authorization") != "Bearer synthetic-bootstrap" {
+ t.Error("enrollment credential changed")
+ }
+ if err := json.NewDecoder(r.Body).Decode(&enrollment); err != nil {
+ t.Error(err)
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ json.NewEncoder(w).Encode(enrollResponse{AgentID: "canonical-agent", RuntimeToken: "synthetic-runtime"})
+ case agentReportEndpoint:
+ if r.Header.Get("Authorization") != "Bearer synthetic-runtime" {
+ t.Error("report credential changed")
+ }
+ reader, err := gzip.NewReader(r.Body)
+ if err != nil {
+ t.Error(err)
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ defer reader.Close()
+ if err := json.NewDecoder(reader).Decode(&received); err != nil {
+ t.Error(err)
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ json.NewEncoder(w).Encode(map[string]bool{"success": true})
+ default:
+ t.Errorf("unexpected request: %s", r.URL.Path)
+ w.WriteHeader(http.StatusNotFound)
+ }
+ }))
+ defer server.Close()
+ caPath := filepath.Join(root, "test-ca.pem")
+ if err := os.WriteFile(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: server.Certificate().Raw}), 0600); err != nil {
+ t.Fatal(err)
+ }
+ stateDir := filepath.Join(root, "state")
+ result, err := Enroll(context.Background(), EnrollmentConfig{
+ PulseURL: server.URL, APIToken: "synthetic-bootstrap", StateDir: stateDir,
+ HostnameOverride: " pi-label ", Collector: collector, CACertPath: caPath,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ agent, err := New(Config{
+ PulseURL: server.URL, APIToken: result.APIToken, AgentID: result.AgentID,
+ StateDir: stateDir, HostnameOverride: " pi-label ", Collector: collector,
+ CACertPath: caPath, LogLevel: zerolog.Disabled, DisableCeph: true,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ report, err := agent.buildReport(context.Background())
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := agent.sendReport(context.Background(), report); err != nil {
+ t.Fatal(err)
+ }
+ if received.Host.OSName != "debian" || received.Host.OSVersion != "13.7" {
+ t.Fatalf("received OS = %q %q, want debian 13.7", received.Host.OSName, received.Host.OSVersion)
+ }
+ if received.Host.Hostname != "pi-label" || received.Host.MachineID != "synthetic-machine-id" || received.Host.Architecture != "aarch64" || received.Agent.ID != "canonical-agent" {
+ t.Fatalf("host/agent identity changed: %+v / %q", received.Host, received.Agent.ID)
+ }
+ if enrollment.Hostname != received.Host.Hostname || enrollment.OS != received.Host.OSName || enrollment.Arch != received.Host.Architecture || enrollment.CommandsEnabled || received.Agent.CommandsEnabled {
+ t.Fatalf("enrollment/report identity or commands-off control changed: %+v / %+v", enrollment, received.Agent)
+ }
+ if received.Host.CPUCount != 4 || received.Metrics.CPUUsagePercent != 7.5 {
+ t.Fatal("OS identity correction changed collected metrics")
+ }
+ if saved, err := os.ReadFile(filepath.Join(stateDir, runtimeTokenFile)); err != nil || string(saved) != "synthetic-runtime" {
+ t.Fatalf("saved synthetic credential changed: %q / %v", saved, err)
+ }
+ t.Logf("trusted synthetic enrollment/report: %s %s, hostname=%s, machine=%s, agent=%s, arch=%s, commands=false, CPU=7.5",
+ received.Host.OSName, received.Host.OSVersion, received.Host.Hostname, received.Host.MachineID, received.Agent.ID, received.Host.Architecture)
+}
diff --git a/internal/hostagent/os_identity.go b/internal/hostagent/os_identity.go
index bae5bce9e6..45bde8b952 100644
--- a/internal/hostagent/os_identity.go
+++ b/internal/hostagent/os_identity.go
@@ -2,6 +2,7 @@ package hostagent
import (
"context"
+ "os"
"regexp"
"strings"
"time"
@@ -9,6 +10,7 @@ import (
var unraidVersionPattern = regexp.MustCompile(`\b\d+(?:\.\d+)+(?:[-+._][A-Za-z0-9]+)*\b|\b\d+\b`)
var proxmoxPVEVersionPattern = regexp.MustCompile(`(?i)\bpve-manager/([^/\s]+)`)
+var linuxOSReleaseIDPattern = regexp.MustCompile(`^[a-z0-9._-]+$`)
const proxmoxPVEOSName = "Proxmox VE"
const proxmoxPVEVersionCommandTimeout = 10 * time.Second
@@ -37,9 +39,47 @@ func resolveHostOSIdentity(collector SystemCollector, osName, osVersion string)
return resolvedDetectedHostOSIdentity(name, version, currentVersion, false)
}
+ // Package-presence heuristics can misidentify ordinary distributions: for
+ // example, Debian can install raspi-config without becoming Raspbian (#2854).
+ // Keep appliance detection above authoritative distro metadata, and retain
+ // the collector's version (which can be more precise than VERSION_ID).
+ if name := detectLinuxDistributionOSName(collector); name != "" {
+ return name, currentVersion
+ }
+
return currentName, currentVersion
}
+func detectLinuxDistributionOSName(collector SystemCollector) string {
+ data, err := collector.ReadFile("/etc/os-release")
+ if os.IsNotExist(err) {
+ data, err = collector.ReadFile("/usr/lib/os-release")
+ }
+ if err != nil {
+ return ""
+ }
+
+ // os-release is data, not a shell script. Only its machine-readable ID is
+ // needed; do not evaluate expansions or substitute NAME/ID_LIKE (a family).
+ // /etc takes precedence as a whole, even when its ID is unavailable.
+ id := ""
+ for _, line := range strings.Split(string(data), "\n") {
+ key, value, ok := strings.Cut(strings.TrimSpace(line), "=")
+ if !ok || strings.TrimSpace(key) != "ID" {
+ continue
+ }
+ value = strings.TrimSpace(value)
+ if len(value) >= 2 && (value[0] == '"' || value[0] == '\'') && value[len(value)-1] == value[0] {
+ value = value[1 : len(value)-1]
+ }
+ if !linuxOSReleaseIDPattern.MatchString(value) {
+ return ""
+ }
+ id = value
+ }
+ return id
+}
+
func resolvedDetectedHostOSIdentity(name, version, currentVersion string, allowVersionFallback bool) (string, string) {
version = strings.TrimSpace(version)
if version == "" && allowVersionFallback {
diff --git a/internal/hostagent/os_identity_test.go b/internal/hostagent/os_identity_test.go
new file mode 100644
index 0000000000..40d283d40e
--- /dev/null
+++ b/internal/hostagent/os_identity_test.go
@@ -0,0 +1,139 @@
+package hostagent
+
+import (
+ "context"
+ "os"
+ "testing"
+)
+
+func TestResolveHostOSIdentityUsesLinuxDistributionMetadata(t *testing.T) {
+ tests := []struct {
+ name string
+ platform string
+ version string
+ release string
+ releaseErr error
+ vendor string
+ wantName string
+ }{
+ {name: "Debian with raspi-config", platform: "raspbian", version: "13.7", release: "ID=debian\nVERSION_ID=13\n", wantName: "debian"},
+ {name: "actual Raspbian", platform: "raspbian", version: "13.7", release: "ID=raspbian\n", wantName: "raspbian"},
+ {name: "Ubuntu is not its Debian family", platform: "debian", version: "24.04", release: "ID=ubuntu\nID_LIKE=debian\n", wantName: "ubuntu"},
+ {name: "double quoted ID", platform: "linux", release: "ID=\"debian\"\n", wantName: "debian"},
+ {name: "single quoted ID", platform: "linux", release: "ID='ubuntu'\n", wantName: "ubuntu"},
+ {name: "whitespace and CRLF", platform: "linux", release: " # comment\r\n ID = \"debian\" \r\n", wantName: "debian"},
+ {name: "distribution punctuation", platform: "linux", release: "ID=opensuse-tumbleweed\n", wantName: "opensuse-tumbleweed"},
+ {name: "last ID wins", platform: "linux", release: "ID=debian\nID=ubuntu\n", wantName: "ubuntu"},
+ {name: "vendor file when etc is absent", platform: "raspbian", releaseErr: os.ErrNotExist, vendor: "ID=debian\n", wantName: "debian"},
+ {name: "etc takes precedence over vendor", platform: "linux", release: "ID=ubuntu\n", vendor: "ID=debian\n", wantName: "ubuntu"},
+ {name: "unavailable files", platform: "raspbian", releaseErr: os.ErrNotExist, wantName: "raspbian"},
+ {name: "unreadable etc", platform: "raspbian", releaseErr: os.ErrPermission, vendor: "ID=debian\n", wantName: "raspbian"},
+ {name: "empty etc does not merge vendor", platform: "raspbian", vendor: "ID=debian\n", wantName: "raspbian"},
+ {name: "NAME is not a distro ID", platform: "raspbian", release: "NAME=Debian\nID_LIKE=debian\n", wantName: "raspbian"},
+ {name: "empty ID", platform: "raspbian", release: "ID=\"\"\n", wantName: "raspbian"},
+ {name: "unclosed quote", platform: "raspbian", release: "ID=\"debian\n", wantName: "raspbian"},
+ {name: "mismatched quotes", platform: "raspbian", release: "ID=\"debian'\n", wantName: "raspbian"},
+ {name: "extra quote", platform: "raspbian", release: "ID=\"debian\"\"\n", wantName: "raspbian"},
+ {name: "multiple words", platform: "raspbian", release: "ID=Debian GNU/Linux\n", wantName: "raspbian"},
+ {name: "expansion is not evaluated", platform: "raspbian", release: "ID=\"$(printf debian)\"\n", wantName: "raspbian"},
+ {name: "NUL is invalid", platform: "raspbian", release: "ID=debian\x00\n", wantName: "raspbian"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ collector := &mockCollector{
+ readFileFn: func(path string) ([]byte, error) {
+ switch path {
+ case "/etc/os-release":
+ return []byte(tt.release), tt.releaseErr
+ case "/usr/lib/os-release":
+ if tt.vendor != "" {
+ return []byte(tt.vendor), nil
+ }
+ }
+ return nil, os.ErrNotExist
+ },
+ commandCombinedOutputFn: func(_ context.Context, _ string, _ ...string) (string, error) {
+ t.Fatal("distribution detection must not run commands")
+ return "", os.ErrNotExist
+ },
+ }
+ name, version := resolveHostOSIdentity(collector, " "+tt.platform+" ", " "+tt.version+" ")
+ if name != tt.wantName || version != tt.version {
+ t.Fatalf("identity = %q %q, want %q %q", name, version, tt.wantName, tt.version)
+ }
+ })
+ }
+}
+
+func TestResolveHostOSIdentityLeavesNonLinuxUnchanged(t *testing.T) {
+ for _, goos := range []string{"windows", "freebsd", "darwin"} {
+ t.Run(goos, func(t *testing.T) {
+ collector := &mockCollector{
+ goos: goos,
+ readFileFn: func(string) ([]byte, error) {
+ t.Fatal("non-Linux identity must not read Linux release files")
+ return nil, os.ErrNotExist
+ },
+ }
+ name, version := resolveHostOSIdentity(collector, " "+goos+" ", " 1.2 ")
+ if name != goos || version != "1.2" {
+ t.Fatalf("identity = %q %q", name, version)
+ }
+ })
+ }
+ if name, version := resolveHostOSIdentity(nil, " linux ", " 1.2 "); name != "linux" || version != "1.2" {
+ t.Fatalf("nil collector identity = %q %q", name, version)
+ }
+}
+
+func TestResolveHostOSIdentityKeepsAppliancePrecedence(t *testing.T) {
+ tests := []struct {
+ name string
+ files map[string]string
+ pve bool
+ wantName string
+ wantVersion string
+ }{
+ {name: "Synology", files: map[string]string{"/etc.defaults/VERSION": "productversion=7.2.2\nbuildnumber=72806\n"}, wantName: "Synology DSM", wantVersion: "7.2.2-72806"},
+ {name: "QNAP", files: map[string]string{"/etc/config/uLinux.conf": "Version=5.2.0\n"}, wantName: "QNAP QTS", wantVersion: "5.2.0"},
+ {name: "Unraid", files: map[string]string{"/etc/unraid-version": "version=7.2.2\n"}, wantName: "Unraid", wantVersion: "7.2.2"},
+ {name: "Proxmox", pve: true, wantName: proxmoxPVEOSName, wantVersion: "9.1.9"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ collector := &mockCollector{
+ readFileFn: func(path string) ([]byte, error) {
+ if path == "/etc/os-release" {
+ return []byte("ID=debian\nVERSION_ID=13\n"), nil
+ }
+ if data, ok := tt.files[path]; ok {
+ return []byte(data), nil
+ }
+ return nil, os.ErrNotExist
+ },
+ statFn: func(path string) (os.FileInfo, error) {
+ if tt.pve && path == "/etc/pve" {
+ return nil, nil
+ }
+ return nil, os.ErrNotExist
+ },
+ lookPathFn: func(name string) (string, error) {
+ if tt.pve && name == "pveversion" {
+ return "/synthetic/pveversion", nil
+ }
+ return "", os.ErrNotExist
+ },
+ commandCombinedOutputFn: func(_ context.Context, name string, _ ...string) (string, error) {
+ if !tt.pve || name != "/synthetic/pveversion" {
+ t.Fatalf("unexpected command: %s", name)
+ }
+ return "pve-manager/9.1.9/synthetic", nil
+ },
+ }
+ name, version := resolveHostOSIdentity(collector, "raspbian", "13.7")
+ if name != tt.wantName || version != tt.wantVersion {
+ t.Fatalf("identity = %q %q, want %q %q", name, version, tt.wantName, tt.wantVersion)
+ }
+ })
+ }
+}