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.

Fail closed on unresolvable agent tokens in the connections command-policy view

rcourtman667cb086b00a
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 0b12904daa..1e2b935d7c 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -153,7 +153,18 @@ truth. The desired side is the effective runtime config served to the agent
after token scope and binding checks, not the unsanitized profile desire. If a
profile enables commands but the agent runtime token cannot execute them,
lifecycle surfaces must see desired-disabled/applied-disabled as in sync
-rather than a command-policy rollout failure.
+rather than a command-policy rollout failure. A host whose recorded token
+binding no longer resolves to a live API token is the same case; when API
+tokens exist, desired command policy fails closed to disabled for that host
+instead of reporting drift against a token that cannot execute.
+
+Install tokens for the generic host agent flow are minted server-side through
+the agent install command endpoint. The server decides the token's scopes from
+the operator's command-execution choice at mint time, because scopes are never
+upgraded on an existing token, and stamps the install-type and issuance
+metadata that make the token eligible for one first-use command-channel
+binding. Frontend surfaces may choose whether commands are requested but must
+not compose install-token scope lists themselves.
PVE node setup shared boundaries that render or copy `PulseMonitor`
permissions must treat `VM.GuestAgent.Audit` plus `VM.GuestAgent.FileRead` as
diff --git a/internal/api/connections_aggregator.go b/internal/api/connections_aggregator.go
index d63598df8c..f8b46549f3 100644
--- a/internal/api/connections_aggregator.go
+++ b/internal/api/connections_aggregator.go
@@ -1100,7 +1100,19 @@ func effectiveConnectionAgentConfig(cfg monitoring.HostAgentConfig, host models.
if tokenByID != nil {
record = tokenByID[strings.TrimSpace(host.TokenID)]
}
- cfg = sanitizeHostAgentConfigForToken(cfg, record, host)
+ if record == nil && len(tokenByID) > 0 && cfg.CommandsEnabled != nil && *cfg.CommandsEnabled {
+ // API tokens exist but this host's TokenID resolves to none of them
+ // (stale after a token was revoked and the agent reinstalled, or an
+ // untracked binding). The runtime config served to such an agent can
+ // never enable commands, so the desired side must not either; leaving
+ // it enabled fabricates a command-policy mismatch the operator cannot
+ // fix from the profile. The contract requires desired to be the
+ // sanitized effective config, not the raw profile desire.
+ disabled := false
+ cfg.CommandsEnabled = &disabled
+ } else {
+ cfg = sanitizeHostAgentConfigForToken(cfg, record, host)
+ }
metadata, err := remoteconfig.BuildDesiredConfigMetadata(cfg.CommandsEnabled, cfg.Settings)
if err != nil {
diff --git a/internal/api/connections_command_policy_token_test.go b/internal/api/connections_command_policy_token_test.go
new file mode 100644
index 0000000000..41d1582d01
--- /dev/null
+++ b/internal/api/connections_command_policy_token_test.go
@@ -0,0 +1,60 @@
+package api
+
+import (
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+ "github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
+)
+
+func commandsEnabledConfig() monitoring.HostAgentConfig {
+ enabled := true
+ return monitoring.HostAgentConfig{CommandsEnabled: &enabled}
+}
+
+// The desired side of the /api/connections command-policy comparison must be
+// the effective config after token scope and binding checks. A host whose
+// TokenID resolves to no live token can never be served commands-enabled
+// config, so desired must fail closed instead of fabricating a drift the
+// operator cannot fix from the profile.
+func TestEffectiveConnectionAgentConfigUnresolvableTokenFailsClosed(t *testing.T) {
+ execToken := config.APITokenRecord{ID: "tok-exec", Scopes: []string{config.ScopeAgentExec}}
+ tokenByID := map[string]*config.APITokenRecord{execToken.ID: &execToken}
+
+ staleHost := models.Host{ID: "agent-a", Hostname: "a", TokenID: "tok-revoked"}
+ cfg := effectiveConnectionAgentConfig(commandsEnabledConfig(), staleHost, tokenByID)
+ if cfg.CommandsEnabled == nil || *cfg.CommandsEnabled {
+ t.Fatalf("stale TokenID must force desired commands disabled, got %+v", cfg.CommandsEnabled)
+ }
+
+ untrackedHost := models.Host{ID: "agent-b", Hostname: "b"}
+ cfg = effectiveConnectionAgentConfig(commandsEnabledConfig(), untrackedHost, tokenByID)
+ if cfg.CommandsEnabled == nil || *cfg.CommandsEnabled {
+ t.Fatalf("untracked TokenID must force desired commands disabled, got %+v", cfg.CommandsEnabled)
+ }
+}
+
+func TestEffectiveConnectionAgentConfigResolvableAndAuthOptionalPaths(t *testing.T) {
+ execToken := config.APITokenRecord{
+ ID: "tok-exec",
+ Scopes: []string{config.ScopeAgentExec},
+ Metadata: map[string]string{
+ "bound_hostname": "a",
+ },
+ }
+ tokenByID := map[string]*config.APITokenRecord{execToken.ID: &execToken}
+
+ boundHost := models.Host{ID: "agent-a", Hostname: "a", TokenID: execToken.ID}
+ cfg := effectiveConnectionAgentConfig(commandsEnabledConfig(), boundHost, tokenByID)
+ if cfg.CommandsEnabled == nil || !*cfg.CommandsEnabled {
+ t.Fatalf("exec-scoped bound token must keep desired commands enabled, got %+v", cfg.CommandsEnabled)
+ }
+
+ // Auth-optional installs have no API tokens at all; the config passes
+ // through untouched rather than failing closed.
+ cfg = effectiveConnectionAgentConfig(commandsEnabledConfig(), models.Host{ID: "agent-c", Hostname: "c"}, nil)
+ if cfg.CommandsEnabled == nil || !*cfg.CommandsEnabled {
+ t.Fatalf("auth-optional path must keep desired commands enabled, got %+v", cfg.CommandsEnabled)
+ }
+}