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.
247 lines
9.8 KiB
Go
247 lines
9.8 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"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"
|
|
agentsdocker "github.com/rcourtman/pulse-go-rewrite/pkg/agents/docker"
|
|
)
|
|
|
|
// Docker analog of issue #1753: two standalone sites reuse one short hostname
|
|
// and one pasted unified install token. The host-agent side of this estate was
|
|
// fixed by machine-qualifying the host token bindings; these tests walk the
|
|
// same estate through /api/agents/docker/report and fail if the Docker records
|
|
// collapse into one identity, or if removing one Docker host revokes the
|
|
// shared token while a sibling record still authenticates with it - the same
|
|
// 401 "Unauthorized access attempt" chain the reporter hit on the host path.
|
|
|
|
func issue1753DockerInstallTokenRecord(t *testing.T, raw, site string) config.APITokenRecord {
|
|
t.Helper()
|
|
record, err := config.NewAPITokenRecord(raw, "Unified agent install ("+site+")", agenttokens.HostScopes(false))
|
|
if err != nil {
|
|
t.Fatalf("NewAPITokenRecord: %v", err)
|
|
}
|
|
record.Metadata = map[string]string{
|
|
"install_type": "host",
|
|
"issued_via": agentbinding.IssuedViaConfig,
|
|
agenttokens.RuntimeRoleMetadataKey: agenttokens.CredentialKindMonitoringCollector,
|
|
}
|
|
return *record
|
|
}
|
|
|
|
func issue1753DockerReport(hostname, machineID string) agentsdocker.Report {
|
|
return agentsdocker.Report{
|
|
Agent: agentsdocker.AgentInfo{
|
|
// The unified agent's Docker module reports the machine ID as its
|
|
// agent ID (the hostagent fallback chain, #985/#986).
|
|
ID: machineID,
|
|
Version: "6.4.2",
|
|
Type: "unified",
|
|
IntervalSeconds: 30,
|
|
},
|
|
Host: agentsdocker.HostInfo{
|
|
Hostname: hostname,
|
|
MachineID: machineID,
|
|
Runtime: "docker",
|
|
},
|
|
Containers: []agentsdocker.Container{
|
|
{ID: "container-" + machineID, Name: "app", State: "running"},
|
|
},
|
|
Timestamp: time.Now().UTC(),
|
|
}
|
|
}
|
|
|
|
func issue1753PostDockerReport(t *testing.T, router *Router, token string, report agentsdocker.Report) (int, map[string]any) {
|
|
t.Helper()
|
|
body, err := json.Marshal(report)
|
|
if err != nil {
|
|
t.Fatalf("marshal docker report: %v", err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodPost, "/api/agents/docker/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 issue1753TokenNames(cfg *config.Config) map[string]bool {
|
|
config.Mu.RLock()
|
|
defer config.Mu.RUnlock()
|
|
names := make(map[string]bool, len(cfg.APITokens))
|
|
for _, record := range cfg.APITokens {
|
|
names[record.Name] = true
|
|
}
|
|
return names
|
|
}
|
|
|
|
// TestIssue1753DockerSharedTokenTwoSitesSameShortHostname models the reporter's
|
|
// estate on the Docker module: both sites' unified agents report host and
|
|
// Docker inventory with the same short hostname and one shared install token.
|
|
//
|
|
// The Docker path deliberately requires a unique token per module, so the
|
|
// second site's Docker reports must converge to the documented, actionable
|
|
// rejection ("generate a new token") - never to a silent fold that leaves one
|
|
// record flip-flopping between machines. The first site's Docker identity must
|
|
// stay stable throughout, and removing the Docker record must not revoke the
|
|
// shared token the host records still authenticate with (the 401 chain from
|
|
// the issue).
|
|
func TestIssue1753DockerSharedTokenTwoSitesSameShortHostname(t *testing.T) {
|
|
const shared = "issue1753-docker-shared.13571357"
|
|
|
|
cfg := newTestConfigWithTokens(t, issue1753DockerInstallTokenRecord(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")
|
|
|
|
// Alternate reports the way two live 30s-interval agents do. Site B's
|
|
// very first Docker report may legitimately fold (a changed machine ID is
|
|
// indistinguishable from a recreated container until the old machine
|
|
// reports again), but from then on it must be rejected with the
|
|
// unique-token guidance rather than adopted.
|
|
var ackIDA string
|
|
var siteBRejected bool
|
|
for round := 0; round < 4; round++ {
|
|
code, payload := issue1753PostReport(t, router, shared,
|
|
issue1753Report("", "docker01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site A host report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
code, payload = issue1753PostReport(t, router, shared,
|
|
issue1753Report("", "docker01", "machine-id-site-b", "10.2.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site B host report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
|
|
code, payload = issue1753PostDockerReport(t, router, shared,
|
|
issue1753DockerReport("docker01", "machine-id-site-a"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("round %d: site A docker report rejected with %d (%v)", round, code, payload)
|
|
}
|
|
gotID, _ := payload["agentId"].(string)
|
|
if gotID == "" {
|
|
t.Fatalf("round %d: site A docker report acknowledged without an identity", round)
|
|
}
|
|
if ackIDA == "" {
|
|
ackIDA = gotID
|
|
} else if gotID != ackIDA {
|
|
t.Fatalf("round %d: site A docker identity flapped from %q to %q", round, ackIDA, gotID)
|
|
}
|
|
|
|
code, payload = issue1753PostDockerReport(t, router, shared,
|
|
issue1753DockerReport("docker01", "machine-id-site-b"))
|
|
switch {
|
|
case code == http.StatusOK && round == 0:
|
|
// The one ambiguous cycle: nothing distinguishes site B's first
|
|
// report from a recreated container yet.
|
|
case code == http.StatusOK:
|
|
t.Fatalf("round %d: site B's Docker module silently adopted an identity (%v) instead of the unique-token rejection; the record would flip-flop between machines", round, payload)
|
|
default:
|
|
message, _ := payload["error"].(string)
|
|
if !strings.Contains(message, "unique API token") {
|
|
t.Fatalf("round %d: site B docker report rejected with %d but without the unique-token guidance: %v", round, code, payload)
|
|
}
|
|
siteBRejected = true
|
|
}
|
|
}
|
|
if !siteBRejected {
|
|
t.Fatal("site B's Docker module was never steered to a unique token")
|
|
}
|
|
|
|
// Site A's record must have settled on site A's machine, not the last
|
|
// reporter's: the flip-flop is what let a later removal act on the wrong
|
|
// site in the original estate.
|
|
dockerHost, ok := monitor.GetDockerHost(ackIDA)
|
|
if !ok {
|
|
t.Fatalf("site A docker host %q missing from state", ackIDA)
|
|
}
|
|
if dockerHost.MachineID != "machine-id-site-a" {
|
|
t.Fatalf("site A docker record carries machine ID %q, want %q", dockerHost.MachineID, "machine-id-site-a")
|
|
}
|
|
|
|
// The reporter's removal step: the Docker record is removed while both
|
|
// sites' host records still authenticate with the shared token. The token
|
|
// must survive, and host reporting must keep working - its revocation is
|
|
// the 401 "Unauthorized access attempt" chain from the issue.
|
|
if _, err := monitor.RemoveDockerHost(ackIDA); err != nil {
|
|
t.Fatalf("RemoveDockerHost(%q): %v", ackIDA, err)
|
|
}
|
|
|
|
code, payload := issue1753PostReport(t, router, shared,
|
|
issue1753Report("", "docker01", "machine-id-site-a", "10.1.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("site A host report rejected with %d after Docker host removal (%v)", code, payload)
|
|
}
|
|
code, payload = issue1753PostReport(t, router, shared,
|
|
issue1753Report("", "docker01", "machine-id-site-b", "10.2.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("site B host report rejected with %d after Docker host removal (%v)", code, payload)
|
|
}
|
|
|
|
if names := issue1753TokenNames(cfg); !names["Unified agent install (shared)"] {
|
|
t.Fatalf("shared install token was revoked while both host records still use it (have %v)", names)
|
|
}
|
|
}
|
|
|
|
// TestIssue1753DockerHostRemovalKeepsUnifiedSiblingToken covers the same
|
|
// revocation chain without any hostname collision: a single unified install
|
|
// shares one token between the machine's host record and its Docker record.
|
|
// Removing only the Docker host (stop monitoring Docker, keep the host) must
|
|
// not revoke the token the host module still reports with.
|
|
func TestIssue1753DockerHostRemovalKeepsUnifiedSiblingToken(t *testing.T) {
|
|
const token = "issue1753-unified-single.24682468"
|
|
|
|
cfg := newTestConfigWithTokens(t, issue1753DockerInstallTokenRecord(t, token, "single"))
|
|
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")
|
|
|
|
code, payload := issue1753PostReport(t, router, token,
|
|
issue1753Report("", "nas01", "machine-id-nas01", "10.3.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("host report rejected with %d (%v)", code, payload)
|
|
}
|
|
|
|
code, payload = issue1753PostDockerReport(t, router, token,
|
|
issue1753DockerReport("nas01", "machine-id-nas01"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("docker report rejected with %d (%v)", code, payload)
|
|
}
|
|
dockerID, _ := payload["agentId"].(string)
|
|
if dockerID == "" {
|
|
t.Fatal("expected an acknowledged Docker host identity")
|
|
}
|
|
|
|
if _, err := monitor.RemoveDockerHost(dockerID); err != nil {
|
|
t.Fatalf("RemoveDockerHost(%q): %v", dockerID, err)
|
|
}
|
|
|
|
code, payload = issue1753PostReport(t, router, token,
|
|
issue1753Report("", "nas01", "machine-id-nas01", "10.3.0.10"))
|
|
if code != http.StatusOK {
|
|
t.Fatalf("host report rejected with %d after Docker host removal (%v); the shared unified token must survive", code, payload)
|
|
}
|
|
|
|
if names := issue1753TokenNames(cfg); !names["Unified agent install (single)"] {
|
|
t.Fatalf("unified install token was revoked while the host record still uses it (have %v)", names)
|
|
}
|
|
}
|