Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Keep agent install token issuance atomic

pulse-triage[bot]0c2d3f6d806b
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index e31272158e..e6bb50bc20 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -6684,11 +6684,14 @@ indefinitely with 400s that no reinstall can escape (#1772).
### Agent token issuance and revocation remain continuous across persistence failure
-An operator-created API token carrying agent authority becomes live and is
-returned only after its expanded inventory commits. A failed creation write
-restores every prior token and the primary-token projection, so the runtime
-does not retain an undisclosed agent credential or evict an older working
-credential from its sorted inventory.
+An operator-created API token carrying agent authority, including the shared
+host and Proxmox install tokens issued by `internal/api/agenttokens`, becomes
+live and is returned only after its expanded inventory commits. A failed
+creation write restores every prior token and the primary-token projection,
+so the runtime does not retain an undisclosed install credential or evict an
+older working credential from its sorted inventory. The install-token proof is
+`TestIssueAndPersistRollsBackCompleteInventoryWhenPersistenceFails` in
+`internal/api/agenttokens/install_test.go`.
The shared API-token deletion boundary may mark an agent credential revoked
only after the reduced token inventory is durably committed. If persistence
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index b8e3bf6af0..2018139f7d 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -10015,6 +10015,13 @@ sorted inventory and accidentally removing an older valid token.
`internal/api/security_tokens_lifecycle_test.go` pins this failed-commit
boundary.
+`POST /api/agent-install-command` and the hosted install-command boundary use
+the shared `internal/api/agenttokens.IssueAndPersist` commit rule. They return
+neither the generated secret nor its record when persistence fails, restore
+the complete prior inventory and legacy primary-token projection, and must not
+truncate the newest-first inventory. The forced-write-failure proof lives in
+`internal/api/agenttokens/install_test.go`.
+
`DELETE /api/security/tokens/{id}` removes exactly the requested token and
returns `204` only after the resulting token inventory is durably persisted.
If persistence fails, the runtime restores the complete prior inventory,
diff --git a/docs/release-control/v6/internal/subsystems/security-privacy.md b/docs/release-control/v6/internal/subsystems/security-privacy.md
index 9a02f96c90..a2befd3a6e 100644
--- a/docs/release-control/v6/internal/subsystems/security-privacy.md
+++ b/docs/release-control/v6/internal/subsystems/security-privacy.md
@@ -2368,10 +2368,14 @@ a non-root agent as unhealthy on that evidence alone.
### API token creation and revocation are durable credential transitions
Creation may not admit an unreturned secret or evict an older valid token when
-persistence fails. The token-management API retains the complete pre-creation
-inventory until the expanded inventory commits, then restores that inventory
-and its primary-token projection before returning an error on failed writes.
-The generated secret is returned only after a successful commit.
+persistence fails. Both token-management creation and shared agent
+install-token issuance retain the complete pre-creation inventory until the
+expanded inventory commits, then restore that inventory and its primary-token
+projection before returning an error on failed writes. The generated secret
+and record are returned only after a successful commit. The agent-install
+failure proof is
+`TestIssueAndPersistRollsBackCompleteInventoryWhenPersistenceFails` in
+`internal/api/agenttokens/install_test.go`.
Revocation may not create different live and restart-time credential sets.
The token-management API therefore retains a complete pre-mutation inventory
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index a4fd01a3df..860bf5ab6b 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -5644,6 +5644,12 @@ the commit boundary. If expansion cannot be written, it restores the complete
prior inventory and legacy primary-token projection and returns no generated
credential. Because token records sort newest-first, rollback restores the
snapshot rather than truncating the sorted list and evicting an older token.
+The same rule governs shared host, Proxmox, and hosted agent install-token
+issuance in `internal/api/agenttokens`: failed persistence restores the entire
+live inventory and primary projection, and the install-command response
+contains no credential material. Its failure proofs live in
+`internal/api/agenttokens/install_test.go` and
+`internal/api/security_tokens_lifecycle_test.go`.
The shared `internal/api` token revocation path treats its persisted inventory
as the commit boundary. It snapshots the complete in-memory inventory before
diff --git a/internal/api/agenttokens/install.go b/internal/api/agenttokens/install.go
index bb58b68c7a..f2be1118a0 100644
--- a/internal/api/agenttokens/install.go
+++ b/internal/api/agenttokens/install.go
@@ -110,11 +110,16 @@ func IssueAndPersist(cfg *config.Config, persistence *config.ConfigPersistence,
config.Mu.Lock()
defer config.Mu.Unlock()
+ previousTokens := append([]config.APITokenRecord(nil), cfg.APITokens...)
cfg.APITokens = append(cfg.APITokens, *record)
cfg.SortAPITokens()
if persistence != nil {
if err := persistence.SaveAPITokens(cfg.APITokens); err != nil {
- cfg.APITokens = cfg.APITokens[:len(cfg.APITokens)-1]
+ // The generated record sorts newest-first, so truncating the sorted
+ // inventory would discard an older valid token and keep the secret
+ // that the failed request never returned. Restore the full snapshot.
+ cfg.APITokens = previousTokens
+ cfg.SortAPITokens()
return "", nil, fmt.Errorf("%w: %w", ErrPersist, err)
}
}
diff --git a/internal/api/agenttokens/install_test.go b/internal/api/agenttokens/install_test.go
index 9f6c9742a3..f5f3e26434 100644
--- a/internal/api/agenttokens/install_test.go
+++ b/internal/api/agenttokens/install_test.go
@@ -2,7 +2,10 @@ package agenttokens
import (
"errors"
+ "os"
+ "path/filepath"
"testing"
+ "time"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
@@ -29,6 +32,49 @@ func TestIssueAndPersistInstallToken(t *testing.T) {
}
}
+func TestIssueAndPersistRollsBackCompleteInventoryWhenPersistenceFails(t *testing.T) {
+ now := time.Now().UTC()
+ tokens := []config.APITokenRecord{
+ {ID: "newest", Name: "newest", Hash: "hash-newest", CreatedAt: now, Scopes: []string{config.ScopeWildcard}},
+ {ID: "oldest", Name: "oldest", Hash: "hash-oldest", CreatedAt: now.Add(-time.Minute), Scopes: []string{config.ScopeWildcard}},
+ }
+ cfg := &config.Config{APITokens: append([]config.APITokenRecord(nil), tokens...)}
+ cfg.SortAPITokens()
+
+ stateDir := filepath.Join(t.TempDir(), "state")
+ persistence := config.NewConfigPersistence(stateDir)
+ if err := os.RemoveAll(stateDir); err != nil {
+ t.Fatalf("remove persistence directory: %v", err)
+ }
+ if err := os.WriteFile(stateDir, []byte("not a directory"), 0o600); err != nil {
+ t.Fatalf("create persistence blocker: %v", err)
+ }
+
+ raw, record, err := IssueAndPersist(cfg, persistence, IssueOptions{TokenName: "must-not-survive"})
+ if !errors.Is(err, ErrPersist) {
+ t.Fatalf("IssueAndPersist error = %v, want ErrPersist", err)
+ }
+ if raw != "" || record != nil {
+ t.Fatalf("failed issue returned credential material: raw=%q record=%#v", raw, record)
+ }
+ if len(cfg.APITokens) != len(tokens) {
+ t.Fatalf("token count = %d, want %d: %#v", len(cfg.APITokens), len(tokens), cfg.APITokens)
+ }
+ for index, want := range tokens {
+ if cfg.APITokens[index].ID != want.ID {
+ t.Fatalf("token[%d].ID = %q, want %q", index, cfg.APITokens[index].ID, want.ID)
+ }
+ }
+ if cfg.APIToken != "hash-newest" {
+ t.Fatalf("legacy primary token = %q, want rollback to %q", cfg.APIToken, "hash-newest")
+ }
+ for _, token := range cfg.APITokens {
+ if token.Name == "must-not-survive" {
+ t.Fatalf("failed issue left generated install token active: %#v", token)
+ }
+ }
+}
+
func TestIssueAndPersistRejectsReservedOwnerMetadata(t *testing.T) {
_, _, err := IssueAndPersist(&config.Config{}, nil, IssueOptions{
TokenName: "invalid",
diff --git a/internal/api/security_tokens_lifecycle_test.go b/internal/api/security_tokens_lifecycle_test.go
index 3e8f4e0732..26d707bea5 100644
--- a/internal/api/security_tokens_lifecycle_test.go
+++ b/internal/api/security_tokens_lifecycle_test.go
@@ -50,6 +50,49 @@ func TestSecurityTokensCreateRollsBackCompleteInventoryWhenPersistenceFails(t *t
}
}
+func TestAgentInstallCommandReturnsOnlyDurablyCommittedCredential(t *testing.T) {
+ now := time.Now().UTC()
+ tokens := []config.APITokenRecord{
+ {ID: "newest", Name: "newest", Hash: "hash-newest", CreatedAt: now, Scopes: []string{config.ScopeWildcard}},
+ {ID: "oldest", Name: "oldest", Hash: "hash-oldest", CreatedAt: now.Add(-time.Minute), Scopes: []string{config.ScopeWildcard}},
+ }
+ stateDir := filepath.Join(t.TempDir(), "state")
+ cfg := &config.Config{
+ DataPath: stateDir,
+ AuthUser: "admin",
+ AuthPass: "hashed-password",
+ APITokens: append([]config.APITokenRecord(nil), tokens...),
+ }
+ cfg.SortAPITokens()
+ handler := newTestConfigHandlers(t, cfg)
+
+ req := httptest.NewRequest(http.MethodPost, "/api/agent-install-command", bytes.NewBufferString(`{"type":"host","name":"must-not-survive"}`))
+ persistence := handler.Persistence(req.Context())
+ if err := persistence.SaveAPITokens(tokens); err != nil {
+ t.Fatalf("save initial tokens: %v", err)
+ }
+ if err := os.RemoveAll(stateDir); err != nil {
+ t.Fatalf("remove persistence directory: %v", err)
+ }
+ if err := os.WriteFile(stateDir, []byte("not a directory"), 0o600); err != nil {
+ t.Fatalf("create persistence blocker: %v", err)
+ }
+
+ rec := httptest.NewRecorder()
+ handler.HandleAgentInstallCommand(rec, req)
+
+ if rec.Code != http.StatusInternalServerError {
+ t.Fatalf("status = %d, want %d (body=%q)", rec.Code, http.StatusInternalServerError, rec.Body.String())
+ }
+ if strings.Contains(rec.Body.String(), "must-not-survive") {
+ t.Fatalf("failed install-token response disclosed generated record: %q", rec.Body.String())
+ }
+ assertLifecycleAPITokenIDs(t, cfg.APITokens, "newest", "oldest")
+ if cfg.APIToken != "hash-newest" {
+ t.Fatalf("legacy primary token = %q, want rollback to %q", cfg.APIToken, "hash-newest")
+ }
+}
+
func TestSecurityTokensDeletePersistsOnlyRequestedRemoval(t *testing.T) {
now := time.Now().UTC()
tokens := []config.APITokenRecord{