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.

11.4 KB
Raw Blame
package config

import "testing"

func TestConsolidatePVEInstancesMergesDuplicateClusters(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{Name: "c1", ClusterName: "cluster-A", IsCluster: true, ClusterEndpoints: []ClusterEndpoint{{NodeName: "n1", Host: "https://10.0.0.1:8006"}}},
		{Name: "c2", ClusterName: "cluster-A", IsCluster: true, ClusterEndpoints: []ClusterEndpoint{{NodeName: "n1", Host: "https://10.0.0.1:8006"}, {NodeName: "n2", Host: "https://10.0.0.2:8006"}}},
		{Name: "c3", ClusterName: "cluster-B", IsCluster: true},
	})

	if !changed {
		t.Fatalf("expected consolidation change")
	}
	if len(instances) != 2 {
		t.Fatalf("expected 2 instances after consolidation, got %d", len(instances))
	}
	if len(instances[0].ClusterEndpoints) != 2 {
		t.Fatalf("expected 2 endpoints in primary cluster, got %d", len(instances[0].ClusterEndpoints))
	}
}

func TestConsolidatePVEInstancesKeepsDistinctClustersWithSameName(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "site-a",
			ClusterName: "production",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "pve1", Host: "https://pve1:8006", IP: "10.0.0.1"},
			},
		},
		{
			Name:        "site-b",
			ClusterName: "production",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "pve1", Host: "https://pve1:8006", IP: "10.1.0.1"},
			},
		},
	})

	if changed {
		t.Fatalf("distinct same-name clusters must not be consolidated")
	}
	if len(instances) != 2 {
		t.Fatalf("instances = %d, want 2 distinct clusters", len(instances))
	}
}

func TestConsolidatePVEInstancesRemovesStandaloneCoveredByClusterEndpoint(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "homelab",
			ClusterName: "cluster-A",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "minipc", Host: "https://10.0.0.5:8006"},
			},
		},
		{
			Name:        "minipc-standalone",
			Host:        "10.0.0.5",
			GuestURL:    "https://minipc.example",
			Fingerprint: "fp-standalone",
			TokenName:   "pulse@pve!token",
			TokenValue:  "secret",
			VerifySSL:   true,
			Source:      "agent",
		},
	})

	if !changed {
		t.Fatalf("expected consolidation change")
	}
	if len(instances) != 1 {
		t.Fatalf("expected 1 instance after consolidation, got %d", len(instances))
	}
	if got := instances[0].ClusterEndpoints[0].GuestURL; got != "https://minipc.example" {
		t.Fatalf("GuestURL = %q, want https://minipc.example", got)
	}
	if got := instances[0].ClusterEndpoints[0].Fingerprint; got != "fp-standalone" {
		t.Fatalf("Fingerprint = %q, want fp-standalone", got)
	}
	if got := instances[0].TokenName; got != "pulse@pve!token" {
		t.Fatalf("TokenName = %q, want pulse@pve!token", got)
	}
	if got := instances[0].TokenValue; got != "secret" {
		t.Fatalf("TokenValue = %q, want secret", got)
	}
	if !instances[0].VerifySSL {
		t.Fatalf("expected VerifySSL to be promoted")
	}
	if got := instances[0].Source; got != "agent" {
		t.Fatalf("Source = %q, want agent", got)
	}
}

func TestConsolidatePVEInstancesPromotesCanonicalTokenMetadataFromStandalone(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "homelab",
			ClusterName: "cluster-A",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "minipc", Host: "https://10.0.0.5:8006"},
			},
		},
		{
			Name:       "minipc-standalone",
			Host:       "10.0.0.5",
			TokenName:  "pulse@pve!canonical-token",
			TokenValue: "secret",
			Source:     "script",
		},
	})

	if !changed {
		t.Fatalf("expected consolidation change")
	}
	if len(instances) != 1 {
		t.Fatalf("expected 1 instance after consolidation, got %d", len(instances))
	}
	if got := instances[0].TokenName; got != "pulse@pve!canonical-token" {
		t.Fatalf("TokenName = %q, want pulse@pve!canonical-token", got)
	}
	if got := instances[0].TokenValue; got != "secret" {
		t.Fatalf("TokenValue = %q, want secret", got)
	}
	if got := instances[0].Source; got != "script" {
		t.Fatalf("Source = %q, want script", got)
	}
}

func TestConsolidatePVEInstancesKeepsStandaloneWithoutExplicitOverlap(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "homelab",
			ClusterName: "cluster-A",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "minipc", Host: "https://minipc.local:8006"},
			},
		},
		{
			Name: "minipc-standalone",
			Host: "https://10.0.0.5:8006",
		},
	})

	if changed {
		t.Fatalf("expected no consolidation change without explicit endpoint overlap")
	}
	if len(instances) != 2 {
		t.Fatalf("expected both instances to remain, got %d", len(instances))
	}
}

func TestConsolidatePVEInstancesMergesDuplicateClusterAuthIntoPrimary(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "c1",
			ClusterName: "cluster-A",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "n1", Host: "https://10.0.0.5:8006"},
			},
		},
		{
			Name:        "c2",
			ClusterName: "cluster-A",
			IsCluster:   true,
			TokenName:   "pulse@pve!token",
			TokenValue:  "secret",
			Fingerprint: "fp-1",
			VerifySSL:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "n1", Host: "https://10.0.0.5:8006"},
				{NodeName: "n2", Host: "https://10.0.0.6:8006"},
			},
		},
	})

	if !changed {
		t.Fatalf("expected consolidation change")
	}
	if len(instances) != 1 {
		t.Fatalf("expected 1 instance after consolidation, got %d", len(instances))
	}
	if got := instances[0].TokenName; got != "pulse@pve!token" {
		t.Fatalf("TokenName = %q, want pulse@pve!token", got)
	}
	if got := instances[0].TokenValue; got != "secret" {
		t.Fatalf("TokenValue = %q, want secret", got)
	}
	if got := instances[0].Fingerprint; got != "fp-1" {
		t.Fatalf("Fingerprint = %q, want fp-1", got)
	}
	if !instances[0].VerifySSL {
		t.Fatalf("expected VerifySSL to be promoted")
	}
	if len(instances[0].ClusterEndpoints) != 2 {
		t.Fatalf("expected 2 endpoints after consolidation, got %d", len(instances[0].ClusterEndpoints))
	}
}

func TestConsolidatePVEInstancesKeepsSameNameClustersWithCollidingPrivateIPs(t *testing.T) {
	// Two customer sites reuse the same corosync cluster name and the same
	// RFC1918 addressing, so discovered member IPs collide. The TOFU-captured
	// TLS fingerprints are the evidence that these are different machines.
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "site-a",
			Host:        "https://site-a.example:8006",
			ClusterName: "production",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "pve01", Host: "https://192.168.1.10:8006", IP: "192.168.1.10", Fingerprint: "fp-site-a"},
			},
		},
		{
			Name:        "site-b",
			Host:        "https://site-b.example:8006",
			ClusterName: "production",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "pve01", Host: "https://192.168.1.10:8006", IP: "192.168.1.10", Fingerprint: "fp-site-b"},
			},
		},
	})

	if changed {
		t.Fatalf("same-name clusters with contradicting endpoint fingerprints must not be consolidated")
	}
	if len(instances) != 2 {
		t.Fatalf("instances = %d, want 2 distinct clusters", len(instances))
	}
}

func TestPVEClusterInstancesShareIdentityRejectsSameNameWithContradictingFingerprints(t *testing.T) {
	siteA := PVEInstance{
		Name:        "enacon",
		ClusterName: "production",
		IsCluster:   true,
		ClusterEndpoints: []ClusterEndpoint{
			{NodeName: "pve01", Host: "https://192.168.1.246:8006", IP: "192.168.1.246", Fingerprint: "AA:AA"},
		},
	}
	siteB := PVEInstance{
		Name:        "rewo",
		ClusterName: "production",
		IsCluster:   true,
		ClusterEndpoints: []ClusterEndpoint{
			{NodeName: "pve01", Host: "https://192.168.1.246:8006", IP: "192.168.1.246", Fingerprint: "BB:BB"},
		},
	}

	if PVEClusterInstancesShareIdentity(siteA, siteB) {
		t.Fatal("same-name clusters with contradicting TLS fingerprints must not share identity")
	}
}

func TestConsolidatePVEInstancesKeepsStandaloneWithContradictingFingerprint(t *testing.T) {
	// A standalone whose address collides with a cluster endpoint at another
	// site must not fold into that cluster when its TLS fingerprint disagrees.
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:        "site-a",
			ClusterName: "cluster-A",
			IsCluster:   true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "pve01", Host: "https://192.168.1.10:8006", Fingerprint: "fp-cluster"},
			},
		},
		{
			Name:        "other-site-standalone",
			Host:        "192.168.1.10",
			Fingerprint: "fp-standalone",
		},
	})

	if changed {
		t.Fatalf("standalone with contradicting fingerprint must not be consolidated into the cluster")
	}
	if len(instances) != 2 {
		t.Fatalf("expected both instances to remain, got %d", len(instances))
	}
}

// TestConsolidatePVEInstancesPreservesDisabledVerifySSLOnStandaloneMerge covers
// #2140: a standalone/auto-registered entry that captured a fingerprint is
// created with VerifySSL true. Folding it into a cluster whose operator
// disabled "Verify SSL certificate" must not re-enable the setting. The
// captured pin still moves onto the cluster endpoint, so verification is not
// silently downgraded.
func TestConsolidatePVEInstancesPreservesDisabledVerifySSLOnStandaloneMerge(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:              "homelab",
			ClusterName:       "cluster-A",
			IsCluster:         true,
			VerifySSL:         false,
			VerifySSLExplicit: true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "minipc", Host: "https://10.0.0.5:8006"},
			},
		},
		{
			Name:        "minipc-standalone",
			Host:        "10.0.0.5",
			Fingerprint: "fp-standalone",
			TokenName:   "pulse@pve!token",
			TokenValue:  "secret",
			VerifySSL:   true,
			Source:      "agent",
		},
	})

	if !changed {
		t.Fatalf("expected consolidation change")
	}
	if len(instances) != 1 {
		t.Fatalf("expected 1 instance after consolidation, got %d", len(instances))
	}
	if instances[0].VerifySSL {
		t.Fatalf("standalone merge re-enabled a disabled VerifySSL (#2140)")
	}
	if got := instances[0].ClusterEndpoints[0].Fingerprint; got != "fp-standalone" {
		t.Fatalf("ClusterEndpoint Fingerprint = %q, want fp-standalone (pin must survive)", got)
	}
}

// TestConsolidatePVEInstancesPreservesDisabledVerifySSLOnDuplicateClusterMerge
// is the duplicate-cluster twin of the #2140 regression above.
func TestConsolidatePVEInstancesPreservesDisabledVerifySSLOnDuplicateClusterMerge(t *testing.T) {
	instances, changed := ConsolidatePVEInstances([]PVEInstance{
		{
			Name:              "c1",
			ClusterName:       "cluster-A",
			IsCluster:         true,
			VerifySSL:         false,
			VerifySSLExplicit: true,
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "n1", Host: "https://10.0.0.5:8006"},
			},
		},
		{
			Name:        "c2",
			ClusterName: "cluster-A",
			IsCluster:   true,
			VerifySSL:   true,
			Fingerprint: "fp-1",
			ClusterEndpoints: []ClusterEndpoint{
				{NodeName: "n1", Host: "https://10.0.0.5:8006"},
				{NodeName: "n2", Host: "https://10.0.0.6:8006"},
			},
		},
	})

	if !changed {
		t.Fatalf("expected consolidation change")
	}
	if len(instances) != 1 {
		t.Fatalf("expected 1 instance after consolidation, got %d", len(instances))
	}
	if instances[0].VerifySSL {
		t.Fatalf("duplicate-cluster merge re-enabled a disabled VerifySSL (#2140)")
	}
	if got := instances[0].Fingerprint; got != "fp-1" {
		t.Fatalf("Fingerprint = %q, want fp-1 (pin must survive)", got)
	}
}