mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-24 12:42:49 +00:00
9615e46d25
## Problem An agent's command channel could be refused for good after a restart whenever the ID it presented differed from the host ID Pulse resolved for its reports. This applied to every install path. - The first command-channel registration bound the install token to whatever ID the agent presented, and a binding never moves. - The agent registered before its first report, under a configured `--agent-id`, or under an ID Pulse had forked for a cloned machine ID or kept from an earlier enrollment. - Pulse resolved its reports to another host ID and acknowledged that as `agentId`, and the agent persisted it. - The config gate judges the resolved ID, so it switched the operator's commands off. - The next restart registered under the persisted ID: "Agent token mismatch: token is not bound to the registering agent", indefinitely. This was the remaining gap called out in #2542. ## Why not move the binding at report ingest The obvious fix is to rebind the token to the resolved ID when a report arrives. It isn't safe. Identity resolution matches a report onto a host's continuity record by machine ID without checking the token, and a token-machine binding then keeps that resolution stable. A holder of one host's token could send one report that resolves onto another host's retained record, then a second claiming the bound ID, and move the token's command identity across hosts. A forged report and a legitimately stale one look identical, so this change moves no binding from report contents. ## Fix **Server (`internal/api/agent_ingest.go`)** - Report acknowledgements name `commandAgentId`, the resolved host ID, only when the token's binding already holds it, or when a registration presenting it would take the first bind, backfill, legacy migration, hostname rebind or one-time deploy repair it would get anyway. - The credential must be one command admission takes (`agent:exec`, at most one organization, legacy or no runtime role), and the host must be held under the reporting token, so mock acks name nothing. - The ID is judged for the hostname the report presents. - Naming grants nothing that registration wouldn't. The config gate takes the same decision. **Agent (`internal/hostagent`)** - The command channel stays staged until Pulse acknowledges a report, then registers under the named identity. - When Pulse names none (an older Pulse, or a token already bound to the presented ID such as an existing `--agent-id` install), it registers under the agent's own ID as before. - Only a real ack negotiates the identity: `success` set, a host named, and a report that presents this process's agent ID and hostname. An unreadable or non-ack body, a replayed buffered report from an earlier run, or an observer ack neither names nor releases the channel. - A 403 (the token lacks report scope, so Pulse can never answer) releases the channel under the agent's own ID, keeping today's behaviour for exec-only tokens. - A client Pulse has admitted is never rebuilt, because that would reopen its receipt store and mark in-flight operations interrupted. A client Pulse never admitted is retired and rebuilt under the named identity, behind a compare-and-swap so it can't be admitted afterwards. - A client a concurrent disable has already cleared can no longer take the run slot. ## Tests - **Agent (`internal/hostagent/command_client_test.go`):** - registers under the acknowledged identity, and waits for an accepted report; - keeps its own ID when none is named, and keeps the acknowledged ID through a later ack that names none and through a disable/enable; - ignores unreadable and non-ack bodies; - releases on 403; - leaves an admitted running client in place, and moves a never-admitted one; - ignores observer acks and replayed buffered reports; - lets no superseded client take the run slot; - CAS exclusivity, plus a real websocket handshake test. - **Server:** - `TestHostAgentCommandChannelBindsTheIdentityPulseAcknowledges`: end to end through the router. A configured-ID install goes through report, registration, restart, and the gate keeping commands on. - `TestContract_UnifiedAgentReportAckNamesCommandIdentityOnlyWhenTheBindingAdmitsIt`: the field is present only when admitted, and absent for bound-elsewhere, no-exec, collector and two-org tokens. - Security cases in `security_regression_test.go`: - an established binding never moves; - a hostname-bound deploy token backfills the resolved ID; - the deploy placeholder repair lands on the resolved ID; - an agent reconfigured after #2547 recorded its ID is named that ID; - mock acks name nothing. - **Fail without the fix:** the behavioural agent and server tests were run against the previous `agent.go` and `agent_ingest.go` and fail there, apart from the compatibility and invariant checks (own ID kept, established binding never moves), which pass either way by design. The tests for the newest guards use new seams, so the mutation checks below cover them instead. - **Mutation checks:** each guard was dropped in turn (real-ack check, current-identity check, never-admitted retirement, handshake admission, admitted-client protection, binding decision, role check, org check, token-held check), and each mutation fails at least one test. - **Locally:** the full `internal/hostagent` and `cmd/pulse-agent` suites pass; the new agent tests pass under `-race`; the governance guards and audits pass. ## Not in this change - Tokens already bound to a diverged ID are not recovered automatically, for the reason above. Re-running the install command issues a fresh token, which binds the resolved identity. - If Pulse later changes a host's resolved ID (a rename heal), it is named only where the existing binding rules already allow that ID.