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 cluster double-registration invalidating Proxmox credentials (#1319)

rcourtmanfe0706f61432
diff --git a/internal/api/config_handlers.go b/internal/api/config_handlers.go
index f14ca3e42b..f7c8871bac 100644
--- a/internal/api/config_handlers.go
+++ b/internal/api/config_handlers.go
@@ -5226,6 +5226,26 @@ type AutoRegisterRequest struct {
Password string `json:"password,omitempty"` // Password for authentication (never stored)
}
+func refreshClusterCredentialsFromAutoRegister(instance *config.PVEInstance, nodeConfig NodeConfigRequest, req *AutoRegisterRequest) {
+ if instance == nil {
+ return
+ }
+
+ tokenName := strings.TrimSpace(nodeConfig.TokenName)
+ tokenValue := strings.TrimSpace(nodeConfig.TokenValue)
+ if tokenName == "" || tokenValue == "" {
+ return
+ }
+
+ instance.User = ""
+ instance.Password = ""
+ instance.TokenName = tokenName
+ instance.TokenValue = tokenValue
+ if req != nil && req.Source != "" {
+ instance.Source = req.Source
+ }
+}
+
// HandleAutoRegister receives token details from the setup script and auto-configures the node
func (h *ConfigHandlers) HandleAutoRegister(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
@@ -5760,6 +5780,12 @@ func (h *ConfigHandlers) HandleAutoRegister(w http.ResponseWriter, r *http.Reque
}
}
+ refreshClusterCredentialsFromAutoRegister(existingInstance, nodeConfig, &req)
+ log.Info().
+ Str("cluster", clusterName).
+ Str("tokenName", existingInstance.TokenName).
+ Msg("Refreshed existing cluster credentials from auto-registration")
+
// Save and reload
if h.getPersistence(r.Context()) != nil {
if err := h.getPersistence(r.Context()).SaveNodesConfig(h.getConfig(r.Context()).PVEInstances, h.getConfig(r.Context()).PBSInstances, h.getConfig(r.Context()).PMGInstances); err != nil {
diff --git a/internal/api/config_handlers_auto_register_test.go b/internal/api/config_handlers_auto_register_test.go
index 826c3b70b1..3c8d34a8eb 100644
--- a/internal/api/config_handlers_auto_register_test.go
+++ b/internal/api/config_handlers_auto_register_test.go
@@ -420,6 +420,110 @@ func TestAutoRegisterDuplicateHostnameSeparateNodes(t *testing.T) {
}
}
+func TestHandleAutoRegisterClusterMergeRefreshesStoredToken(t *testing.T) {
+ tempDir := t.TempDir()
+ t.Setenv("PULSE_DATA_DIR", tempDir)
+
+ originalDetectPVECluster := detectPVECluster
+ detectPVECluster = func(clientConfig proxmox.ClientConfig, nodeName string, existingEndpoints []config.ClusterEndpoint) (bool, string, []config.ClusterEndpoint) {
+ endpoints := []config.ClusterEndpoint{
+ {
+ NodeID: "node-1",
+ NodeName: "pve-a",
+ Host: "https://10.0.1.10:8006",
+ Fingerprint: "fp-a",
+ Online: true,
+ },
+ }
+ if strings.Contains(clientConfig.Host, "10.0.1.11") {
+ endpoints = append(endpoints, config.ClusterEndpoint{
+ NodeID: "node-2",
+ NodeName: "pve-b",
+ Host: "https://10.0.1.11:8006",
+ Fingerprint: "fp-b",
+ Online: true,
+ })
+ }
+ return true, "cluster-a", endpoints
+ }
+ defer func() { detectPVECluster = originalDetectPVECluster }()
+
+ cfg := &config.Config{
+ DataPath: tempDir,
+ ConfigPath: tempDir,
+ }
+ handler := newTestConfigHandlers(t, cfg)
+
+ const authToken = "TEMP-TOKEN"
+ tokenHash := internalauth.HashAPIToken(authToken)
+
+ handler.codeMutex.Lock()
+ handler.setupCodes[tokenHash] = &SetupCode{
+ ExpiresAt: time.Now().Add(5 * time.Minute),
+ NodeType: "pve",
+ }
+ handler.codeMutex.Unlock()
+
+ firstReq := AutoRegisterRequest{
+ Type: "pve",
+ Host: "https://10.0.1.10:8006",
+ TokenID: "pulse-monitor@pam!pulse-pve-a-pulse-example-com",
+ TokenValue: "old-secret",
+ ServerName: "pve-a",
+ Source: "agent",
+ AuthToken: authToken,
+ }
+ firstBody, _ := json.Marshal(firstReq)
+ firstHTTPReq := httptest.NewRequest(http.MethodPost, "/api/auto-register", bytes.NewReader(firstBody))
+ firstRec := httptest.NewRecorder()
+ handler.HandleAutoRegister(firstRec, firstHTTPReq)
+ if firstRec.Code != http.StatusOK {
+ t.Fatalf("first registration failed: status=%d body=%s", firstRec.Code, firstRec.Body.String())
+ }
+
+ handler.codeMutex.Lock()
+ handler.setupCodes[tokenHash] = &SetupCode{
+ ExpiresAt: time.Now().Add(5 * time.Minute),
+ NodeType: "pve",
+ }
+ handler.codeMutex.Unlock()
+
+ secondReq := AutoRegisterRequest{
+ Type: "pve",
+ Host: "https://10.0.1.11:8006",
+ TokenID: "pulse-monitor@pam!pulse-pve-b-pulse-example-com",
+ TokenValue: "new-secret",
+ ServerName: "pve-b",
+ Source: "agent",
+ AuthToken: authToken,
+ }
+ secondBody, _ := json.Marshal(secondReq)
+ secondHTTPReq := httptest.NewRequest(http.MethodPost, "/api/auto-register", bytes.NewReader(secondBody))
+ secondRec := httptest.NewRecorder()
+ handler.HandleAutoRegister(secondRec, secondHTTPReq)
+ if secondRec.Code != http.StatusOK {
+ t.Fatalf("second registration failed: status=%d body=%s", secondRec.Code, secondRec.Body.String())
+ }
+
+ if len(cfg.PVEInstances) != 1 {
+ t.Fatalf("expected 1 merged cluster instance, got %d", len(cfg.PVEInstances))
+ }
+
+ instance := cfg.PVEInstances[0]
+ if instance.TokenName != secondReq.TokenID {
+ t.Fatalf("merged cluster TokenName = %q, want %q", instance.TokenName, secondReq.TokenID)
+ }
+ if instance.TokenValue != secondReq.TokenValue {
+ t.Fatalf("merged cluster TokenValue = %q, want %q", instance.TokenValue, secondReq.TokenValue)
+ }
+ if instance.Host != firstReq.Host {
+ t.Fatalf("merged cluster Host = %q, want original primary host %q", instance.Host, firstReq.Host)
+ }
+ if len(instance.ClusterEndpoints) != 2 {
+ t.Fatalf("expected 2 cluster endpoints after merge, got %d", len(instance.ClusterEndpoints))
+ }
+}
+
// TestExtractHostIP verifies the IP extraction from host URLs.
func TestExtractHostIP(t *testing.T) {
tests := []struct {
diff --git a/internal/hostagent/proxmox_setup.go b/internal/hostagent/proxmox_setup.go
index c1eca28e04..cca1b31884 100644
--- a/internal/hostagent/proxmox_setup.go
+++ b/internal/hostagent/proxmox_setup.go
@@ -106,8 +106,45 @@ func proxmoxTokenScope(candidates ...string) string {
return "server"
}
+func proxmoxMonitorTokenScope(hostname, pulseURL string) string {
+ parts := make([]string, 0, 2)
+ seen := make(map[string]struct{}, 2)
+
+ for _, candidate := range []string{hostname, pulseURL} {
+ host := proxmoxTokenHostCandidate(candidate)
+ if host == "" {
+ continue
+ }
+ slug := proxmoxTokenSlug(host)
+ if slug == "" {
+ continue
+ }
+ if _, exists := seen[slug]; exists {
+ continue
+ }
+ seen[slug] = struct{}{}
+ parts = append(parts, slug)
+ }
+
+ if len(parts) == 0 {
+ return "server"
+ }
+
+ scope := parts[0]
+ const maxScopeLen = 48
+ for _, part := range parts[1:] {
+ candidate := scope + "-" + part
+ if len(candidate) > maxScopeLen {
+ break
+ }
+ scope = candidate
+ }
+
+ return scope
+}
+
func (p *ProxmoxSetup) monitorTokenName() string {
- return "pulse-" + proxmoxTokenScope(p.pulseURL, p.hostname)
+ return "pulse-" + proxmoxMonitorTokenScope(p.hostname, p.pulseURL)
}
func tokenAlreadyExists(err error, output string) bool {
diff --git a/internal/hostagent/proxmox_setup_test.go b/internal/hostagent/proxmox_setup_test.go
index 3576dda023..e38b69aee0 100644
--- a/internal/hostagent/proxmox_setup_test.go
+++ b/internal/hostagent/proxmox_setup_test.go
@@ -41,8 +41,8 @@ func TestParsePBSTokenValue_RegexFallback(t *testing.T) {
}
func TestMonitorTokenName_Deterministic(t *testing.T) {
- p := &ProxmoxSetup{pulseURL: "https://Pulse.EXAMPLE.com:7655"}
- if got := p.monitorTokenName(); got != "pulse-pulse-example-com" {
+ p := &ProxmoxSetup{hostname: "node-01", pulseURL: "https://Pulse.EXAMPLE.com:7655"}
+ if got := p.monitorTokenName(); got != "pulse-node-01-pulse-example-com" {
t.Fatalf("monitorTokenName() = %q", got)
}
@@ -50,6 +50,20 @@ func TestMonitorTokenName_Deterministic(t *testing.T) {
if got := p.monitorTokenName(); got != "pulse-pulse-host" {
t.Fatalf("monitorTokenName() with hostname fallback = %q", got)
}
+
+ p = &ProxmoxSetup{pulseURL: "https://Pulse.EXAMPLE.com:7655"}
+ if got := p.monitorTokenName(); got != "pulse-pulse-example-com" {
+ t.Fatalf("monitorTokenName() with Pulse URL fallback = %q", got)
+ }
+}
+
+func TestMonitorTokenName_UniquePerNodeForSamePulse(t *testing.T) {
+ first := (&ProxmoxSetup{hostname: "pve-a", pulseURL: "https://pulse.example.com:7655"}).monitorTokenName()
+ second := (&ProxmoxSetup{hostname: "pve-b", pulseURL: "https://pulse.example.com:7655"}).monitorTokenName()
+
+ if first == second {
+ t.Fatalf("expected unique token names per node, got %q for both", first)
+ }
}
func TestSetupPVEToken_RotatesExistingToken(t *testing.T) {