From ea2be41255374eb80e609ca4ae8887342b3241c7 Mon Sep 17 00:00:00 2001 From: "pulse-triage[bot]" <249995291+pulse-triage[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:43:36 +0000 Subject: [PATCH] Use authenticating credentials in persisted agent lifecycle fixtures Containing CI for PR #4339 failed before either removal path ran: two fixtures saved token records with no Hash. Construct them through the production token-record constructor with the existing agent report/config scopes, retaining the deterministic identity and creation time. Assert the shared bearer survives removal in live and reloaded config while a dedicated bearer is revoked from both. Keep the credential-presence guard and all runtime source unchanged. The original incomplete-record regressions still exercise rejection; these lifecycle tests now exercise legitimate saved access instead of relying on invalid durable fixtures. Contract-Neutral: Test fixtures and authentication assertions only; production storage admission, agent-removal semantics, authorization scopes and subsystem contracts are unchanged. Change-source: pulse-maintainer --- .../agent_token_shared_lifecycle_test.go | 19 ++++++++++++++++- ...nitor_host_agent_removal_lifecycle_test.go | 21 +++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/internal/monitoring/agent_token_shared_lifecycle_test.go b/internal/monitoring/agent_token_shared_lifecycle_test.go index 00973a8902..1410b48fa1 100644 --- a/internal/monitoring/agent_token_shared_lifecycle_test.go +++ b/internal/monitoring/agent_token_shared_lifecycle_test.go @@ -76,8 +76,18 @@ func TestUninstallHostAgentKeepsCredentialUsedBySiblingHost(t *testing.T) { monitor.persistence = config.NewConfigPersistence(dataPath) now := time.Now().UTC() - token := config.APITokenRecord{ID: "shared-host-token", Name: "Shared host token", CreatedAt: now.Add(-time.Hour)} + const rawToken = "synthetic-shared-host-credential" + record, err := config.NewAPITokenRecord(rawToken, "Shared host token", []string{config.ScopeAgentReport, config.ScopeAgentConfigRead}) + if err != nil { + t.Fatalf("NewAPITokenRecord: %v", err) + } + token := *record + token.ID = "shared-host-token" + token.CreatedAt = now.Add(-time.Hour) monitor.config.APITokens = []config.APITokenRecord{token} + if !monitor.config.IsValidAPIToken(rawToken) { + t.Fatal("shared fixture credential does not authenticate before uninstall") + } if err := monitor.persistence.SaveAPITokens(monitor.config.APITokens); err != nil { t.Fatalf("SaveAPITokens: %v", err) } @@ -97,6 +107,9 @@ func TestUninstallHostAgentKeepsCredentialUsedBySiblingHost(t *testing.T) { t.Fatalf("UninstallHostAgent(first): %v", err) } requireAgentTokenPresent(t, monitor, token.ID) + if !monitor.config.IsValidAPIToken(rawToken) { + t.Fatal("sibling host lost its authenticating credential after uninstall") + } live := monitor.GetLiveHostsSnapshot() if len(live) != 1 || live[0].ID != second.ID { t.Fatalf("sibling host %q did not survive first host uninstall: %+v", second.ID, live) @@ -112,4 +125,8 @@ func TestUninstallHostAgentKeepsCredentialUsedBySiblingHost(t *testing.T) { if len(persisted) != 1 || persisted[0].ID != token.ID { t.Fatalf("shared credential was removed from durable inventory: %+v", persisted) } + restartedConfig := &config.Config{APITokens: persisted} + if !restartedConfig.IsValidAPIToken(rawToken) { + t.Fatal("sibling host credential does not authenticate from the durable inventory") + } } diff --git a/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go b/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go index da3e11503a..51adc4b61d 100644 --- a/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go +++ b/internal/monitoring/monitor_host_agent_removal_lifecycle_test.go @@ -161,12 +161,18 @@ func TestHostAgentRemovalLifecycleRevokesDedicatedCredentialAndRetainsDenial(t * monitor.persistence = config.NewConfigPersistence(dataPath) now := time.Now().UTC() - oldToken := config.APITokenRecord{ - ID: "dedicated-old-token", - Name: "Dedicated host token", - CreatedAt: now.Add(-time.Hour), + const rawToken = "synthetic-dedicated-host-credential" + record, err := config.NewAPITokenRecord(rawToken, "Dedicated host token", []string{config.ScopeAgentReport, config.ScopeAgentConfigRead}) + if err != nil { + t.Fatalf("NewAPITokenRecord: %v", err) } + oldToken := *record + oldToken.ID = "dedicated-old-token" + oldToken.CreatedAt = now.Add(-time.Hour) monitor.config.APITokens = []config.APITokenRecord{oldToken} + if !monitor.config.IsValidAPIToken(rawToken) { + t.Fatal("dedicated fixture credential does not authenticate before removal") + } if err := monitor.persistence.SaveAPITokens(monitor.config.APITokens); err != nil { t.Fatalf("SaveAPITokens: %v", err) } @@ -190,6 +196,9 @@ func TestHostAgentRemovalLifecycleRevokesDedicatedCredentialAndRetainsDenial(t * if len(monitor.config.APITokens) != 0 { t.Fatalf("dedicated credential remained in memory: %+v", monitor.config.APITokens) } + if monitor.config.IsValidAPIToken(rawToken) { + t.Fatal("removed host credential still authenticates in memory") + } reloadedTokens, err := monitor.persistence.LoadAPITokens() if err != nil { t.Fatalf("LoadAPITokens: %v", err) @@ -197,6 +206,10 @@ func TestHostAgentRemovalLifecycleRevokesDedicatedCredentialAndRetainsDenial(t * if len(reloadedTokens) != 0 { t.Fatalf("dedicated credential remained on disk: %+v", reloadedTokens) } + restartedConfig := &config.Config{APITokens: reloadedTokens} + if restartedConfig.IsValidAPIToken(rawToken) { + t.Fatal("removed host credential authenticates from the durable inventory") + } if _, err := monitor.ApplyHostReport(report, &oldToken); err == nil { t.Fatal("explicitly revoked credential bypassed the durable identity denial") }