mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-06 18:39:42 +00:00
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
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user