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") }