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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Make agent lookup fallback fail closed
rcourtman5b538a005d61
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.mdindex cc9e18c5fa..ffd9c56f12 100644--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md@@ -861,7 +861,10 @@ That same uninstall continuity must preserve canonical hostname fallback too:copied shell uninstall payloads must carry `--hostname`, copied PowerShelluninstall payloads must carry `PULSE_HOSTNAME`, and both installer runtimesmust prefer that explicit hostname during lookup fallback before querying local-machine identity.+machine identity. That fallback must also fail closed when hostname matches are+ambiguous: hostname matches may resolve only when they identify one and only+one agent, and display-name or short-hostname fallbacks must return not found+otherwise.That governed hostname lookup fallback must also normalize query transport:both installer runtimes must percent-encode the resolved hostname beforecalling `/api/agents/agent/lookup`, so canonical identity recovery does notdiff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.mdindex c423a65e83..1e61268954 100644--- a/docs/release-control/v6/internal/subsystems/api-contracts.md+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md@@ -526,7 +526,10 @@ command.The same uninstall contract applies to hostname fallback identity: shellpayloads must carry `--hostname`, PowerShell payloads must carry`PULSE_HOSTNAME`, and the uninstall scripts must prefer that explicit hostname-when performing governed `/api/agents/agent/lookup` fallback.+when performing governed `/api/agents/agent/lookup` fallback. That lookup must+fail closed on ambiguous hostname matches: installer-driven recovery may only+resolve a hostname when the match is unique, and display-name or short-hostname+fallbacks must return not found rather than picking an arbitrary agent.That lookup fallback transport must be canonicalized on both installer paths:shell and PowerShell uninstall flows must percent-encode the selected hostnamebefore issuing `/api/agents/agent/lookup`, so API-owned identity recovery doesdiff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.mdindex a297145392..eba77507c5 100644--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md@@ -620,6 +620,10 @@ Unified Agent route family remains the primary auth/management surface:adjacent storage and recovery-linked transport must treat `/api/agents/agent/*`as the owned route family, while `/api/agents/host/*` and legacy`host-agent:*` scope names remain compatibility aliases only.+That same owned route family must also fail closed on ambiguous hostname+lookups: `/api/agents/agent/lookup` may resolve a unique hostname match, but+it must not pick an arbitrary agent when exact, display-name, or short-hostname+matches are duplicated across the live inventory.That same shared `internal/api/` dependency also assumes adjacent recovery andstorage-linked transport continues to describe those legacy names ascompatibility aliases rather than active product surfaces, so route/authdiff --git a/internal/api/agent_ingest.go b/internal/api/agent_ingest.goindex d9aa08caaa..6f6ba178ad 100644--- a/internal/api/agent_ingest.go+++ b/internal/api/agent_ingest.go@@ -166,16 +166,43 @@ func (h *UnifiedAgentHandlers) HandleLookup(w http.ResponseWriter, r *http.Reque}if !found && hostname != "" {- // First pass: exact match (case-insensitive)- for _, candidate := range hosts {- if strings.EqualFold(candidate.Hostname, hostname) || strings.EqualFold(candidate.DisplayName, hostname) {- host = candidate+ // First pass: exact hostname match (case-insensitive) when unique.+ var exactMatch *models.Host+ for i := range hosts {+ candidate := &hosts[i]+ if strings.EqualFold(candidate.Hostname, hostname) {+ if exactMatch != nil {+ exactMatch = nil+ break+ }+ exactMatch = candidate+ }+ }+ if exactMatch != nil {+ host = *exactMatch+ found = true+ }++ // Second pass: display-name match, but only when unambiguous.+ if !found {+ var displayMatch *models.Host+ for i := range hosts {+ candidate := &hosts[i]+ if strings.EqualFold(candidate.DisplayName, hostname) {+ if displayMatch != nil {+ displayMatch = nil+ break+ }+ displayMatch = candidate+ }+ }+ if displayMatch != nil {+ host = *displayMatchfound = true- break}}- // Second pass: short hostname match (if exact match failed)+ // Third pass: short hostname match, but only when unique.if !found {// Helper to get short hostname (before first dot)getShortName := func(h string) string {@@ -186,13 +213,21 @@ func (h *UnifiedAgentHandlers) HandleLookup(w http.ResponseWriter, r *http.Reque}shortLookup := getShortName(hostname)- for _, candidate := range hosts {+ var shortMatch *models.Host+ for i := range hosts {+ candidate := &hosts[i]if strings.EqualFold(getShortName(candidate.Hostname), shortLookup) {- host = candidate- found = true- break+ if shortMatch != nil {+ shortMatch = nil+ break+ }+ shortMatch = candidate}}+ if shortMatch != nil {+ host = *shortMatch+ found = true+ }}}diff --git a/internal/api/contract_test.go b/internal/api/contract_test.goindex 01cf1899c9..3faa649e93 100644--- a/internal/api/contract_test.go+++ b/internal/api/contract_test.go@@ -706,6 +706,33 @@ func TestContract_UnifiedAgentReportResponseJSONSnapshot(t *testing.T) {assertJSONSnapshot(t, got, want)}+func TestContract_UnifiedAgentLookupFailsClosedOnAmbiguousHostname(t *testing.T) {+ handler := newUnifiedAgentHandlerForTests(t,+ models.Host{ID: "host-1", Hostname: "webserver.corp.example.com", DisplayName: "Web One"},+ models.Host{ID: "host-2", Hostname: "webserver.other.example.com", DisplayName: "Web Two"},+ )++ req := httptest.NewRequest(http.MethodGet, "/api/agents/agent/lookup?hostname=webserver", nil)+ rec := httptest.NewRecorder()++ handler.HandleLookup(rec, req)++ if rec.Code != http.StatusNotFound {+ t.Fatalf("expected not found status for ambiguous hostname lookup, got %d: %s", rec.Code, rec.Body.String())+ }++ var resp struct {+ Error string `json:"error"`+ Code string `json:"code"`+ }+ if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {+ t.Fatalf("decode ambiguous lookup error response: %v", err)+ }+ if resp.Code != "agent_not_found" {+ t.Fatalf("expected agent_not_found code, got %q", resp.Code)+ }+}+func TestContract_HostsShareResolvedIdentityTreatsLoopbackAliasAsSameNode(t *testing.T) {if !hostsShareResolvedIdentity("https://localhost:7655", "https://127.0.0.1:7655") {t.Fatal("expected localhost and loopback IP to resolve as the same host identity")diff --git a/internal/api/unified_agent_lookup_test.go b/internal/api/unified_agent_lookup_test.goindex 49938aa4f4..a97c7498a2 100644--- a/internal/api/unified_agent_lookup_test.go+++ b/internal/api/unified_agent_lookup_test.go@@ -179,6 +179,15 @@ func TestHandleLookupByHostname(t *testing.T) {expectedHostID: "host-1",expectedStatus: http.StatusOK,},+ {+ name: "ambiguous exact hostname lookup fails closed",+ hosts: []models.Host{+ {ID: "host-1", Hostname: "server.example.com", DisplayName: "Server One"},+ {ID: "host-2", Hostname: "server.example.com", DisplayName: "Server Two"},+ },+ queryHostname: "server.example.com",+ expectedStatus: http.StatusNotFound,+ },{name: "display name match",hosts: []models.Host{@@ -226,25 +235,32 @@ func TestHandleLookupByHostname(t *testing.T) {expectedStatus: http.StatusOK,},{- name: "first match wins in sorted order",+ name: "exact hostname wins over display-name collisions",hosts: []models.Host{- // After sorting by Hostname: "aaa" < "zzz", so host-2 is checked first{ID: "host-1", Hostname: "zzz", DisplayName: "target"},- {ID: "host-2", Hostname: "aaa", DisplayName: "target"},+ {ID: "host-2", Hostname: "target", DisplayName: "aaa"},},queryHostname: "target",expectedHostID: "host-2",expectedStatus: http.StatusOK,},{- name: "display name matched before hostname of later host",+ name: "ambiguous display-name lookup fails closed",hosts: []models.Host{- {ID: "host-1", Hostname: "other", DisplayName: "target"},- {ID: "host-2", Hostname: "target", DisplayName: "Other"},+ {ID: "host-1", Hostname: "other-1", DisplayName: "target"},+ {ID: "host-2", Hostname: "other-2", DisplayName: "target"},},queryHostname: "target",- expectedHostID: "host-1",- expectedStatus: http.StatusOK,+ expectedStatus: http.StatusNotFound,+ },+ {+ name: "ambiguous short hostname lookup fails closed",+ hosts: []models.Host{+ {ID: "host-1", Hostname: "webserver.corp.example.com", DisplayName: "Web 1"},+ {ID: "host-2", Hostname: "webserver.other.example.com", DisplayName: "Web 2"},+ },+ queryHostname: "webserver",+ expectedStatus: http.StatusNotFound,},{name: "short hostname with FQDN query",