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.

Trust newly joined cluster members under fingerprint pinning

rcourtman192aee6acc2f
diff --git a/internal/api/config_handlers.go b/internal/api/config_handlers.go
index 6776733b67..6056835938 100644
--- a/internal/api/config_handlers.go
+++ b/internal/api/config_handlers.go
@@ -1005,10 +1005,45 @@ func ensureHostHasPort(host, port string) string {
return net.JoinHostPort(trimmed, port)
}
+// isNodeValidationTLSError reports whether a validation error came from TLS
+// certificate or fingerprint verification rather than the Proxmox API itself.
+func isNodeValidationTLSError(errStr string) bool {
+ return strings.Contains(errStr, "fingerprint") || strings.Contains(errStr, "x509") || strings.Contains(errStr, "certificate")
+}
+
+// isNodeValidationAuthError reports whether the node's API answered but denied
+// access — proof enough that a real Proxmox endpoint is listening.
+func isNodeValidationAuthError(errStr string) bool {
+ return strings.Contains(errStr, "401") || strings.Contains(errStr, "403") || strings.Contains(errStr, "permission")
+}
+
+// nodeValidationFailureReason maps a validation error to a short operator-facing
+// category so cluster discovery can distinguish "could not resolve or connect"
+// from "TLS rejected" without leaking raw error internals.
+func nodeValidationFailureReason(err error) string {
+ if err == nil {
+ return ""
+ }
+ errStr := err.Error()
+ switch {
+ case isNodeValidationTLSError(errStr):
+ return "TLS certificate validation failed"
+ case strings.Contains(errStr, "no such host"):
+ return "hostname did not resolve from the Pulse server"
+ case strings.Contains(errStr, "connection refused"):
+ return "connection refused"
+ case strings.Contains(errStr, "timeout") || strings.Contains(errStr, "deadline exceeded"):
+ return "connection timed out"
+ default:
+ return "not reachable"
+ }
+}
+
// validateNodeAPI tests if a cluster node has a working Proxmox API
// This helps filter out qdevice VMs and other non-Proxmox participants.
-// Returns (isValid, fingerprint) - fingerprint is captured for TOFU (Trust On First Use).
-func validateNodeAPI(clusterNode proxmox.ClusterStatus, baseConfig proxmox.ClientConfig) (bool, string) {
+// Returns (isValid, fingerprint, failureReason) - fingerprint is captured for
+// TOFU (Trust On First Use); failureReason is empty when the node validates.
+func validateNodeAPI(clusterNode proxmox.ClusterStatus, baseConfig proxmox.ClientConfig) (bool, string, string) {
// Determine the host to test - prefer IP if available, otherwise use node name
testHost := clusterNode.IP
if testHost == "" {
@@ -1017,7 +1052,7 @@ func validateNodeAPI(clusterNode proxmox.ClusterStatus, baseConfig proxmox.Clien
// Skip empty hostnames (shouldn't happen but be safe)
if testHost == "" {
- return false, ""
+ return false, "", "no hostname or IP reported for this cluster member"
}
scheme, defaultPort := deriveSchemeAndPort(baseConfig.Host)
@@ -1064,9 +1099,7 @@ func validateNodeAPI(clusterNode proxmox.ClusterStatus, baseConfig proxmox.Clien
// same fingerprint will fail. Fall back to a relaxed TLS check so we can
// still detect valid cluster members while keeping other errors (like
// auth) as hard failures.
- errStr := err.Error()
- isTLSMismatch := strings.Contains(errStr, "fingerprint") || strings.Contains(errStr, "x509") || strings.Contains(errStr, "certificate")
- if isTLSMismatch {
+ if isNodeValidationTLSError(err.Error()) {
log.Debug().
Str("node", clusterNode.Name).
Msg("Retrying cluster node validation without fingerprint pinning")
@@ -1079,7 +1112,7 @@ func validateNodeAPI(clusterNode proxmox.ClusterStatus, baseConfig proxmox.Clien
Str("node", clusterNode.Name).
Err(err).
Msg("Failed to create test client for cluster node")
- return false, ""
+ return false, "", nodeValidationFailureReason(err)
}
}
@@ -1094,26 +1127,59 @@ func validateNodeAPI(clusterNode proxmox.ClusterStatus, baseConfig proxmox.Clien
// If we reached the API but were denied (common when per-node permissions
// differ), treat it as valid. We only want to filter out hosts that aren't
// actually Proxmox endpoints.
- if strings.Contains(errMsg, "401") || strings.Contains(errMsg, "403") || strings.Contains(errMsg, "permission") {
+ if isNodeValidationAuthError(errMsg) {
log.Debug().
Str("node", clusterNode.Name).
Err(err).
Msg("Cluster node API responded but denied access; accepting for discovery")
- return true, capturedFingerprint
+ return true, capturedFingerprint, ""
+ }
+
+ // A pinned-fingerprint mismatch surfaces here, not from NewClient: the
+ // verifier runs during the TLS handshake of the first request. Cluster
+ // members normally carry their own per-node certificates, so retry with
+ // the member's own captured fingerprint before rejecting it.
+ if isNodeValidationTLSError(errMsg) {
+ retryConfig := testConfig
+ retryConfig.Fingerprint = capturedFingerprint
+ if capturedFingerprint == "" {
+ retryConfig.VerifySSL = false
+ }
+ if retryNodeValidation(retryConfig) {
+ log.Debug().
+ Str("node", clusterNode.Name).
+ Msg("Cluster node validated with its own certificate after pinned-fingerprint mismatch")
+ return true, capturedFingerprint, ""
+ }
}
log.Debug().
Str("node", clusterNode.Name).
Err(err).
Msg("Node failed Proxmox API validation - likely not a Proxmox node")
- return false, ""
+ return false, "", nodeValidationFailureReason(err)
}
log.Debug().
Str("node", clusterNode.Name).
Msg("Node passed Proxmox API validation")
- return true, capturedFingerprint
+ return true, capturedFingerprint, ""
+}
+
+// retryNodeValidation re-runs the lightweight validation call with an adjusted
+// TLS configuration. Auth denials still count as success (see validateNodeAPI).
+func retryNodeValidation(cfg proxmox.ClientConfig) bool {
+ retryClient, err := proxmox.NewClient(cfg)
+ if err != nil {
+ return false
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ if _, err := retryClient.GetNodes(ctx); err != nil {
+ return isNodeValidationAuthError(err.Error())
+ }
+ return true
}
// findExistingGuestURL looks up the GuestURL for a node from existing endpoints
@@ -1348,7 +1414,7 @@ func defaultDetectPVECluster(clientConfig proxmox.ClientConfig, nodeName string,
// Reachability enriches a member endpoint but never defines cluster
// membership. Powered-off nodes remain canonical members even
// though their individual API cannot answer during discovery.
- isValid, nodeFingerprint := validatePVEClusterNode(clusterNode, clientConfig)
+ isValid, nodeFingerprint, failureReason := validatePVEClusterNode(clusterNode, clientConfig)
existingEndpoint, hadExistingEndpoint := findExistingClusterEndpoint(clusterNode.Name, existingEndpoints)
if !isValid {
log.Info().
@@ -1387,6 +1453,9 @@ func defaultDetectPVECluster(clientConfig proxmox.ClientConfig, nodeName string,
}
if !isValid {
endpoint.PulseError = "Proxmox API endpoint unavailable during cluster discovery"
+ if failureReason != "" {
+ endpoint.PulseError = fmt.Sprintf("Proxmox API endpoint unavailable during cluster discovery (%s)", failureReason)
+ }
}
// Populate Host field with hostname (if available) for TLS certificate validation
diff --git a/internal/api/config_handlers_discovery_test.go b/internal/api/config_handlers_discovery_test.go
index 46887f5b1e..17b9fd593e 100644
--- a/internal/api/config_handlers_discovery_test.go
+++ b/internal/api/config_handlers_discovery_test.go
@@ -123,7 +123,7 @@ func TestValidateNodeAPI(t *testing.T) {
IP: "127.0.0.1",
}
- isValid, fingerprint := validateNodeAPI(clusterNode, clientConfig)
+ isValid, fingerprint, _ := validateNodeAPI(clusterNode, clientConfig)
if isValid {
t.Error("expected isValid=false for invalid connection, got true")
@@ -137,7 +137,7 @@ func TestValidateNodeAPI(t *testing.T) {
configInvalid := proxmox.ClientConfig{
Host: "::invalid-url::",
}
- isValid2, _ := validateNodeAPI(clusterNode, configInvalid)
+ isValid2, _, _ := validateNodeAPI(clusterNode, configInvalid)
if isValid2 {
t.Error("expected isValid=false for invalid url")
}
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 216b6877c6..630e9067ad 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -116,11 +116,11 @@ func TestContract_DefaultDetectPVEClusterRetainsUnreachableMembers(t *testing.T)
originalValidate := validatePVEClusterNode
t.Cleanup(func() { validatePVEClusterNode = originalValidate })
- validatePVEClusterNode = func(node proxmox.ClusterStatus, _ proxmox.ClientConfig) (bool, string) {
+ validatePVEClusterNode = func(node proxmox.ClusterStatus, _ proxmox.ClientConfig) (bool, string, string) {
if node.Name == "pve-a" {
- return true, "fingerprint-a"
+ return true, "fingerprint-a", ""
}
- return false, ""
+ return false, "", "not reachable"
}
lastSeen := time.Now().Add(-time.Hour).UTC()
diff --git a/internal/api/issue1664_cluster_fingerprint_test.go b/internal/api/issue1664_cluster_fingerprint_test.go
new file mode 100644
index 0000000000..e9d6c4753c
--- /dev/null
+++ b/internal/api/issue1664_cluster_fingerprint_test.go
@@ -0,0 +1,73 @@
+package api
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
+)
+
+// Issue #1664: a node joining a PVE cluster after setup could never be
+// trusted when the primary was fingerprint-pinned. The pinned fingerprint
+// mismatch surfaces from the first API call, not from client construction,
+// so validateNodeAPI judged the member "not a Proxmox node" and discarded
+// the fingerprint it had already captured.
+func TestValidateNodeAPI_PinnedPrimaryFingerprintMismatch(t *testing.T) {
+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/api2/json/nodes" {
+ w.Header().Set("Content-Type", "application/json")
+ fmt.Fprint(w, `{"data":[{"node":"member-b","status":"online"}]}`)
+ return
+ }
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ defer server.Close()
+
+ // The base config carries the primary's pinned fingerprint, which can
+ // never match the member's own self-signed certificate.
+ baseConfig := proxmox.ClientConfig{
+ Host: server.URL,
+ TokenName: "root@pam!pulse",
+ TokenValue: "secret",
+ Fingerprint: "DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF",
+ }
+ clusterNode := proxmox.ClusterStatus{
+ Name: "member-b",
+ ID: "node/member-b",
+ Online: 1,
+ IP: "127.0.0.1",
+ }
+
+ isValid, fingerprint, reason := validateNodeAPI(clusterNode, baseConfig)
+ if !isValid {
+ t.Fatalf("expected member with its own certificate to validate despite primary pin, got invalid (reason %q)", reason)
+ }
+ if fingerprint == "" {
+ t.Fatal("expected captured member fingerprint to be returned for TOFU persistence")
+ }
+ if reason != "" {
+ t.Fatalf("expected empty failure reason on success, got %q", reason)
+ }
+}
+
+func TestNodeValidationFailureReason(t *testing.T) {
+ cases := []struct {
+ err error
+ want string
+ }{
+ {errors.New("certificate fingerprint mismatch: expected aa, got bb"), "TLS certificate validation failed"},
+ {errors.New(`Get "https://b:8006": dial tcp: lookup b: no such host`), "hostname did not resolve from the Pulse server"},
+ {errors.New("dial tcp 10.0.0.5:8006: connect: connection refused"), "connection refused"},
+ {errors.New("context deadline exceeded"), "connection timed out"},
+ {errors.New("500 internal server error"), "not reachable"},
+ {nil, ""},
+ }
+ for _, tc := range cases {
+ if got := nodeValidationFailureReason(tc.err); got != tc.want {
+ t.Errorf("nodeValidationFailureReason(%v) = %q, want %q", tc.err, got, tc.want)
+ }
+ }
+}
diff --git a/pkg/proxmox/cluster_client.go b/pkg/proxmox/cluster_client.go
index 4ad7b5d5ec..24dc17d5f9 100644
--- a/pkg/proxmox/cluster_client.go
+++ b/pkg/proxmox/cluster_client.go
@@ -227,12 +227,14 @@ func (cc *ClusterClient) refreshTOFUFingerprintAndRetry(ctx context.Context, end
return nil, lastErr, false
}
+ // A member without a stored per-endpoint fingerprint fails against the
+ // base config's pinned fingerprint (the primary's). Cluster members carry
+ // their own certificates, so capture the member's fingerprint on first
+ // use just like discovery does, instead of pinning it to the primary's
+ // certificate forever.
cc.mu.RLock()
_, hasTOFU := cc.endpointFingerprints[endpoint]
cc.mu.RUnlock()
- if !hasTOFU {
- return nil, lastErr, false
- }
newFingerprint, err := tlsutil.FetchFingerprint(endpoint)
if err != nil {
@@ -248,10 +250,17 @@ func (cc *ClusterClient) refreshTOFUFingerprintAndRetry(ctx context.Context, end
cc.endpointFingerprints[endpoint] = newFingerprint
cc.mu.Unlock()
- log.Warn().
- Str("cluster", cc.name).
- Str("endpoint", endpoint).
- Msg("Detected TLS certificate change; refreshed TOFU fingerprint")
+ if hasTOFU {
+ log.Warn().
+ Str("cluster", cc.name).
+ Str("endpoint", endpoint).
+ Msg("Detected TLS certificate change; refreshed TOFU fingerprint")
+ } else {
+ log.Warn().
+ Str("cluster", cc.name).
+ Str("endpoint", endpoint).
+ Msg("Trusting cluster member certificate on first use; the pinned primary fingerprint cannot match per-node certificates")
+ }
cfg := cc.config
cfg.Host = endpoint
diff --git a/pkg/proxmox/cluster_client_api_test.go b/pkg/proxmox/cluster_client_api_test.go
index bc3b8af1f6..516f373a48 100644
--- a/pkg/proxmox/cluster_client_api_test.go
+++ b/pkg/proxmox/cluster_client_api_test.go
@@ -2,6 +2,7 @@ package proxmox
import (
"context"
+ "errors"
"fmt"
"net/http"
"net/http/httptest"
@@ -551,3 +552,64 @@ func TestClusterClient_GetTaskLog(t *testing.T) {
t.Errorf("unexpected first line: %+v", lines[0])
}
}
+
+// Issue #1664: refreshTOFUFingerprintAndRetry only refreshed endpoints that
+// already had a per-endpoint fingerprint entry, so it recovered certificate
+// rotation on known members but never first trust of a newly joined member.
+// A new member has no entry and fell back to the primary's pinned
+// fingerprint forever.
+func TestClusterClient_TOFUFirstUse_TrustsNewMemberCert(t *testing.T) {
+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/api2/json/nodes" {
+ w.Header().Set("Content-Type", "application/json")
+ fmt.Fprint(w, `{"data":[{"node":"member-b","status":"online"}]}`)
+ return
+ }
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ defer server.Close()
+
+ cfg := ClientConfig{
+ Host: server.URL,
+ TokenName: "root@pam!pulse",
+ TokenValue: "secret",
+ Fingerprint: "DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF:DE:AD:BE:EF",
+ }
+ // Single endpoint skips the initial health check; the member endpoint has
+ // no per-endpoint fingerprint entry, exactly like a freshly joined node.
+ cc := NewClusterClient("issue1664", cfg, []string{server.URL}, nil)
+
+ mismatch := errors.New("certificate fingerprint mismatch: expected DE:AD, got EF:B5")
+ client, err, refreshed := cc.refreshTOFUFingerprintAndRetry(context.Background(), server.URL, 2*time.Second, mismatch)
+ if !refreshed {
+ t.Fatal("expected TOFU refresh to run for an endpoint with no stored fingerprint")
+ }
+ if err != nil {
+ t.Fatalf("expected retry with freshly captured fingerprint to succeed, got %v", err)
+ }
+ if client == nil {
+ t.Fatal("expected a working client after first-use trust")
+ }
+
+ cc.mu.RLock()
+ stored := cc.endpointFingerprints[server.URL]
+ cc.mu.RUnlock()
+ if stored == "" {
+ t.Fatal("expected member fingerprint to be stored for future connections")
+ }
+}
+
+// Non-mismatch errors must not trigger TOFU capture.
+func TestClusterClient_TOFURefresh_IgnoresNonTLSMismatch(t *testing.T) {
+ cfg := ClientConfig{Host: "https://127.0.0.1:1", TokenName: "u@p!t", TokenValue: "v"}
+ cc := NewClusterClient("issue1664b", cfg, []string{"https://127.0.0.1:1"}, nil)
+
+ plainErr := errors.New("connection refused")
+ _, err, refreshed := cc.refreshTOFUFingerprintAndRetry(context.Background(), "https://127.0.0.1:1", time.Second, plainErr)
+ if refreshed {
+ t.Fatal("expected no TOFU refresh for a non-mismatch error")
+ }
+ if !errors.Is(err, plainErr) {
+ t.Fatalf("expected original error back, got %v", err)
+ }
+}