mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
No Router built by the internal/api tests was ever freed. At the end of a run
all 1,358 were still reachable, with 26,000 goroutines and 2.86 GiB live, so
peak memory grew with every new test until the release correctness job ran out
of memory on its 7 GiB runner.
Most test sites never stopped their Router's workers. The few that did were
pinned anyway, because the setup-token cleanup worker in ConfigHandlers had no
way to stop and holds callbacks back into the Router. That worker now joins
the Router's shutdown, and package tests build Routers through newTestRouter,
which stops them with the test. Two new tests fail if a Router stays reachable
after shutdown or a package test calls NewRouter directly.
Default command policies now share one compiled pattern set. A private copy
cost 230 KiB and 1.4 ms per call and is now 2.2 KiB and 1.6 us. It accounted
for a quarter of the live test heap. In production each organization's AI
service holds one, so this also saves about 230 KiB per organization.
Measured on pulse-dev at 88c2a21f65 with the same go test -count=1 -p 1 run:
peak RSS of the largest child 6.41 GiB before, 2.44 GiB after. That largest
child is now the compiler: a cold test build alone peaks at 2.34 GiB and the
test binary alone at 1.48 GiB. All package tests pass. CPU time fell about 5%;
wall clock followed the host's load and is not comparable between these runs.
Contract-Neutral: Stops test Router workers and shares compiled policy patterns; no API, policy decision or storage change.
237 lines
8.7 KiB
Go
237 lines
8.7 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/api/agentbinding"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/api/agenttokens"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
|
|
agentshost "github.com/rcourtman/pulse-go-rewrite/pkg/agents/host"
|
|
)
|
|
|
|
// Issue #1753 (2026-09-01 comment): two standalone Proxmox sites reuse the
|
|
// short node name pve01. After upgrading to v6.4.2 and reinstalling both
|
|
// Unified Agents, the reporter sees "Unauthorized access attempt" server log
|
|
// lines for /api/agents/agent/report. This test models that estate end to end
|
|
// through the real router (global auth middleware, RequireAuth, RequireScope,
|
|
// ApplyHostReport) and fails if either agent's report path ever loses
|
|
// authentication, including across the uninstall/reinstall cycle the reporter
|
|
// performed.
|
|
|
|
func issue1753InstallTokenRecord(t *testing.T, raw, site string) config.APITokenRecord {
|
|
t.Helper()
|
|
record, err := config.NewAPITokenRecord(raw, "Proxmox agent install ("+site+")", agenttokens.ProxmoxScopes(false))
|
|
if err != nil {
|
|
t.Fatalf("NewAPITokenRecord: %v", err)
|
|
}
|
|
record.Metadata = map[string]string{
|
|
"install_type": "pve",
|
|
"issued_via": agentbinding.IssuedViaConfig,
|
|
agenttokens.RuntimeRoleMetadataKey: agenttokens.CredentialKindMonitoringCollector,
|
|
}
|
|
return *record
|
|
}
|
|
|
|
func issue1753Report(agentID, hostname, machineID, reportIP string) agentshost.Report {
|
|
return agentshost.Report{
|
|
Agent: agentshost.AgentInfo{
|
|
ID: agentID,
|
|
Version: "6.4.2",
|
|
Type: "unified",
|
|
IntervalSeconds: 30,
|
|
Hostname: hostname,
|
|
},
|
|
Host: agentshost.HostInfo{
|
|
Hostname: hostname,
|
|
MachineID: machineID,
|
|
Platform: "proxmox",
|
|
OSName: "debian",
|
|
ReportIP: reportIP,
|
|
},
|
|
Timestamp: time.Now().UTC(),
|
|
}
|
|
}
|
|
|
|
func issue1753PostReport(t *testing.T, router *Router, token string, report agentshost.Report) (int, map[string]any) {
|
|
t.Helper()
|
|
body, err := json.Marshal(report)
|
|
if err != nil {
|
|
t.Fatalf("marshal report: %v", err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodPost, "/api/agents/agent/report", bytes.NewReader(body))
|
|
req.Header.Set("X-API-Token", token)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
|
|
var payload map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &payload)
|
|
return rec.Code, payload
|
|
}
|
|
|
|
func TestIssue1753TwoStandaloneSitesSameShortHostnameKeepReportAuth(t *testing.T) {
|
|
const (
|
|
tokenSiteA = "issue1753-site-a-token.12345678"
|
|
tokenSiteB = "issue1753-site-b-token.87654321"
|
|
)
|
|
|
|
cfg := newTestConfigWithTokens(t,
|
|
issue1753InstallTokenRecord(t, tokenSiteA, "site-a"),
|
|
issue1753InstallTokenRecord(t, tokenSiteB, "site-b"),
|
|
)
|
|
monitor, err := monitoring.New(cfg)
|
|
if err != nil {
|
|
t.Fatalf("new monitor: %v", err)
|
|
}
|
|
defer monitor.Stop()
|
|
router := newTestRouter(t, cfg, monitor, nil, nil, func() error { return nil }, "6.4.2")
|
|
|
|
var ackIDA, ackIDB string
|
|
for round := 0; round < 4; round++ {
|
|
code, payload := issue1753PostReport(t, router, tokenSiteA,
|
|
issue1753Report("", "pve01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site A report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
ackIDA, _ = payload["agentId"].(string)
|
|
|
|
code, payload = issue1753PostReport(t, router, tokenSiteB,
|
|
issue1753Report("", "pve01", "machine-id-site-b", "10.2.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site B report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
ackIDB, _ = payload["agentId"].(string)
|
|
}
|
|
|
|
if ackIDA == "" || ackIDB == "" {
|
|
t.Fatalf("expected both sites to receive acknowledged agent IDs, got %q and %q", ackIDA, ackIDB)
|
|
}
|
|
if ackIDA == ackIDB {
|
|
t.Fatalf("both standalone sites were acknowledged with the same agent identity %q; uninstalling either agent would act on the other site's record", ackIDA)
|
|
}
|
|
|
|
// The reporter's cycle: site B uninstalls its agent (the uninstaller calls
|
|
// the unregister endpoint), then reinstalls with a freshly minted token.
|
|
uninstallBody, _ := json.Marshal(map[string]string{"agentId": ackIDB})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/agents/agent/uninstall", bytes.NewReader(uninstallBody))
|
|
req.Header.Set("X-API-Token", tokenSiteB)
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("site B uninstall failed with %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
// Site A must keep reporting with its original token after site B's
|
|
// uninstall: revoking site A's credential here is the cross-site failure.
|
|
code, payload := issue1753PostReport(t, router, tokenSiteA,
|
|
issue1753Report(ackIDA, "pve01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("site A report rejected with %d after site B uninstall (%v)", code, payload)
|
|
}
|
|
|
|
// Site B reinstalls: fresh token, same short hostname, same machine.
|
|
const tokenSiteB2 = "issue1753-site-b-second.11112222"
|
|
recordB2 := issue1753InstallTokenRecord(t, tokenSiteB2, "site-b-reinstall")
|
|
config.Mu.Lock()
|
|
cfg.APITokens = append(cfg.APITokens, recordB2)
|
|
cfg.SortAPITokens()
|
|
config.Mu.Unlock()
|
|
|
|
for round := 0; round < 3; round++ {
|
|
code, payload = issue1753PostReport(t, router, tokenSiteB2,
|
|
issue1753Report("", "pve01", "machine-id-site-b", "10.2.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: reinstalled site B report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
code, payload = issue1753PostReport(t, router, tokenSiteA,
|
|
issue1753Report(ackIDA, "pve01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site A report rejected with %d after site B reinstall (%v)", round, code, payload)
|
|
}
|
|
}
|
|
|
|
// Both live tokens must still be present in the inventory: a silent
|
|
// revocation here is what surfaces as 401 "Unauthorized access attempt".
|
|
config.Mu.RLock()
|
|
defer config.Mu.RUnlock()
|
|
remaining := map[string]bool{}
|
|
for _, record := range cfg.APITokens {
|
|
remaining[record.Name] = true
|
|
}
|
|
for _, name := range []string{"Proxmox agent install (site-a)", "Proxmox agent install (site-b-reinstall)"} {
|
|
if !remaining[name] {
|
|
t.Fatalf("token %q was removed from the inventory (have %v)", name, remaining)
|
|
}
|
|
}
|
|
}
|
|
|
|
func issue1753UninstallAgent(t *testing.T, router *Router, token, agentID string) int {
|
|
t.Helper()
|
|
body, _ := json.Marshal(map[string]string{"agentId": agentID})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/agents/agent/uninstall", bytes.NewReader(body))
|
|
req.Header.Set("X-API-Token", token)
|
|
rec := httptest.NewRecorder()
|
|
router.Handler().ServeHTTP(rec, req)
|
|
return rec.Code
|
|
}
|
|
|
|
// TestIssue1753SharedInstallTokenTwoSites models the other common install
|
|
// pattern: the operator pastes one existing API token into both sites'
|
|
// install commands, so both agents authenticate with the same credential.
|
|
func TestIssue1753SharedInstallTokenTwoSites(t *testing.T) {
|
|
const shared = "issue1753-shared-token.99990000"
|
|
|
|
cfg := newTestConfigWithTokens(t, issue1753InstallTokenRecord(t, shared, "shared"))
|
|
monitor, err := monitoring.New(cfg)
|
|
if err != nil {
|
|
t.Fatalf("new monitor: %v", err)
|
|
}
|
|
defer monitor.Stop()
|
|
router := newTestRouter(t, cfg, monitor, nil, nil, func() error { return nil }, "6.4.2")
|
|
|
|
var ackIDA, ackIDB string
|
|
for round := 0; round < 4; round++ {
|
|
code, payload := issue1753PostReport(t, router, shared,
|
|
issue1753Report("", "pve01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site A report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
ackIDA, _ = payload["agentId"].(string)
|
|
|
|
code, payload = issue1753PostReport(t, router, shared,
|
|
issue1753Report("", "pve01", "machine-id-site-b", "10.2.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site B report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
ackIDB, _ = payload["agentId"].(string)
|
|
}
|
|
|
|
if ackIDA == ackIDB {
|
|
t.Fatalf("both sites sharing one token were acknowledged with the same agent identity %q", ackIDA)
|
|
}
|
|
|
|
// Site B uninstalls, then both keep going. The shared token must survive
|
|
// the uninstall because site A still uses it.
|
|
if code := issue1753UninstallAgent(t, router, shared, ackIDB); code != http.StatusOK {
|
|
t.Fatalf("site B uninstall failed with %d", code)
|
|
}
|
|
code, payload := issue1753PostReport(t, router, shared,
|
|
issue1753Report(ackIDA, "pve01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("site A report rejected with %d after shared-token uninstall (%v)", code, payload)
|
|
}
|
|
|
|
config.Mu.RLock()
|
|
tokens := len(cfg.APITokens)
|
|
config.Mu.RUnlock()
|
|
if tokens != 1 {
|
|
t.Fatalf("expected the shared token to survive site B's uninstall, inventory now holds %d tokens", tokens)
|
|
}
|
|
}
|