Jelajahi Sumber

fix(pve): recreate the client when the TLS settings change (#1880)

Nacho 4 minggu lalu
induk
melakukan
3d776402ab

+ 15 - 4
server-go/internal/adapters/proxmox.go

@@ -21,6 +21,7 @@ package adapters
 import (
 	"context"
 	"log"
+	"strconv"
 	"strings"
 	"time"
 
@@ -80,6 +81,19 @@ type pveInstClient struct {
 	c    *pve.Client
 }
 
+// pveClientsKey: huella de la config que obliga a recrear los clientes. Debe
+// incluir TODO lo que cambia el transporte, no solo url/token: con los ajustes
+// TLS fuera de la clave, editar caPem/fingerprint/insecure no surtia efecto
+// hasta reiniciar el servicio (#1879).
+func pveClientsKey(instances []pve.Instance) string {
+	var b strings.Builder
+	for _, in := range instances {
+		b.WriteString(in.ID + "|" + in.URL + "|" + in.TokenID + "|" + in.Secret + "|" +
+			in.CAPEM + "|" + in.FingerprintSHA256 + "|" + strconv.FormatBool(in.Insecure) + "\n")
+	}
+	return b.String()
+}
+
 // pveClientsCached: clientes de TODAS las instancias configuradas (#764),
 // recreados si la lista cambió (patrón AdGuard). Sin instancias → nil.
 func (l *Live) pveClientsCached() []pveInstClient {
@@ -87,10 +101,7 @@ func (l *Live) pveClientsCached() []pveInstClient {
 		return nil
 	}
 	instances := pve.LoadInstances(l.db.DB)
-	key := ""
-	for _, in := range instances {
-		key += in.ID + "|" + in.URL + "|" + in.TokenID + "|" + in.Secret + "\n"
-	}
+	key := pveClientsKey(instances)
 	if key == "" {
 		l.pveClients = nil
 		l.pveKey = ""

+ 38 - 0
server-go/internal/adapters/proxmox_key_test.go

@@ -0,0 +1,38 @@
+// proxmox_key_test.go — #1879: la clave de caché de los clientes PVE tiene que
+// incluir los ajustes TLS. Con solo url/token/secret, editar caPem,
+// fingerprintSha256 o insecure no recreaba el cliente y el cambio no surtía
+// efecto hasta reiniciar el servicio.
+package adapters
+
+import (
+	"testing"
+
+	"github.com/gnacho/netpulse/server-go/internal/pve"
+)
+
+func TestPveClientsKeyIncludesTLS(t *testing.T) {
+	base := pve.Instance{ID: "ofi", Name: "PVE", Config: pve.Config{
+		URL: "https://192.168.10.200:8006", TokenID: "netpulse@pve!netpulse", Secret: "s3cret",
+	}}
+	withCA := base
+	withCA.CAPEM = "-----BEGIN CERTIFICATE-----\nAAA\n-----END CERTIFICATE-----"
+	withFP := base
+	withFP.FingerprintSHA256 = "EE:2A:D0:F1"
+	withInsecure := base
+	withInsecure.Insecure = true
+
+	key := pveClientsKey([]pve.Instance{base})
+	for name, inst := range map[string]pve.Instance{
+		"caPem":       withCA,
+		"fingerprint": withFP,
+		"insecure":    withInsecure,
+	} {
+		if got := pveClientsKey([]pve.Instance{inst}); got == key {
+			t.Errorf("%s no cambia la clave de caché: %q", name, got)
+		}
+	}
+	// Misma config → misma clave (no se recrean clientes en cada sondeo).
+	if got := pveClientsKey([]pve.Instance{base}); got != key {
+		t.Errorf("clave inestable: %q != %q", got, key)
+	}
+}