Files
Pulse/internal/api/action_runner_credentials.go
rcourtman c5b6d05107 feat(telemetry): add schema v26 map topology API/CLI/MCP telemetry
I added Schema v26 content-free Infrastructure Map topology read telemetry for external live GET /api/agent/topology requests (total read days, CLI days, MCP days, anonymized days, and an explicit availability flag) alongside hardened opt-out zero-write boundaries, credential-binding checks, and read-only SQLite/config guards.

Key changes:
- Added a dedicated root-scoped daily tally store (infrastructure_map_topology_tally.json) with strict schema validation, fail-closed degraded sidecar accounting, and strict zero-write guarantees when telemetry is disabled by settings or PULSE_TELEMETRY=false.
- Added X-Pulse-Agent-Surface attribution for pulse-cli and MCP clients while filtering out first-party browser traffic, mock-mode snapshots, and unverified monitor states.
- Updated Settings payload preview, localization copy, shipped PRIVACY.md disclosure, integration specs, and schema parity tooling for Schema v26.
- Hardened host/agent token hash binding, self-uninstall live authority and durable hash-scoped tombstones, Patrol investigation replay-floor provenance, bounded non-blocking regular-file config reads, and cold read-only legacy SQLite history queries.
2026-12-02 01:41:25 +00:00

489 lines
19 KiB
Go

package api
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/agentexec"
"github.com/rcourtman/pulse-go-rewrite/internal/api/agentbinding"
"github.com/rcourtman/pulse-go-rewrite/internal/api/agenttokens"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
"github.com/rcourtman/pulse-go-rewrite/pkg/auth"
)
const maxActionRunnerCredentialRequestBytes int64 = 16 << 10
type actionRunnerCredentialRequest struct {
AgentID string `json:"agentId"`
Hostname string `json:"hostname"`
Name string `json:"name,omitempty"`
}
type actionRunnerCredentialResponse struct {
Token string `json:"token"`
TokenID string `json:"tokenId"`
OrganizationID string `json:"organizationId"`
AgentID string `json:"agentId"`
Hostname string `json:"hostname"`
RuntimeRole string `json:"runtimeRole"`
ActionCapability string `json:"actionCapability"`
ActivationPending bool `json:"activationPending"`
ActivationDeadline *time.Time `json:"activationDeadline,omitempty"`
}
type actionRunnerCredentialSelfRevokeRequest struct {
AgentID string `json:"agentId"`
Hostname string `json:"hostname"`
}
func actionRunnerCredentialRoute(cfg *config.Config, issue, activate, selfRevoke http.HandlerFunc) http.HandlerFunc {
issue = RequireAdmin(cfg, RequireScope(config.ScopeSettingsWrite, RequireScope(config.ScopeActionsExecute, issue)))
activate = RequireAuth(cfg, RequireScope(config.ScopeAgentExec, activate))
selfRevoke = RequireAuth(cfg, RequireScope(config.ScopeAgentExec, selfRevoke))
return func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
issue(w, req)
case http.MethodPatch:
activate(w, req)
case http.MethodDelete:
selfRevoke(w, req)
default:
w.Header().Set("Allow", http.MethodPost+", "+http.MethodPatch+", "+http.MethodDelete)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
}
// handleIssueActionRunnerCredential creates the separately scoped credential
// consumed by pulse-agent-runner. The route is operator-only; a monitoring
// collector credential cannot mint or upgrade itself into remediation
// authority.
func (r *Router) handleIssueActionRunnerCredential(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if r == nil || r.config == nil || r.persistence == nil {
http.Error(w, "Action runner credential service unavailable", http.StatusServiceUnavailable)
return
}
decoder := json.NewDecoder(io.LimitReader(req.Body, maxActionRunnerCredentialRequestBytes+1))
decoder.DisallowUnknownFields()
var payload actionRunnerCredentialRequest
if err := decoder.Decode(&payload); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
organizationID := strings.TrimSpace(GetOrgID(req.Context()))
canonicalAgentID, canonicalHostname, found := r.resolveActionRunnerHostIdentity(req, payload.AgentID, payload.Hostname)
if !found {
http.Error(w, "Canonical monitored host identity not found", http.StatusNotFound)
return
}
preResolvedTenantCfg := tenantTokenConfigForRequest(req)
issued, err := agenttokens.IssueActionRunnerAndPersistDetailed(r.config, r.persistence, agenttokens.ActionRunnerIssueOptions{
TokenName: payload.Name,
OrgID: organizationID,
OwnerUserID: apiTokenOwnerUserIDForRequest(r.config, req),
AgentID: canonicalAgentID,
Hostname: canonicalHostname,
AuthorizeLocked: func() error {
callerToken, failure := r.currentTokenMutationCallerWithPreResolvedTenantLocked(req, preResolvedTenantCfg)
if failure != nil {
return failure
}
if callerToken != nil {
if !tokenActsForInstance(callerToken) {
return &tokenMutationFailure{
status: http.StatusForbidden,
message: "Admin privileges required",
}
}
if !callerToken.HasScope(config.ScopeActionsExecute) {
return &tokenMutationFailure{
status: http.StatusForbidden,
message: "Missing actions:execute scope",
missingScope: true,
requiredScope: config.ScopeActionsExecute,
}
}
}
return nil
},
})
if err != nil {
var failure *tokenMutationFailure
if errors.As(err, &failure) {
failure.write(w)
return
}
status := http.StatusInternalServerError
if errors.Is(err, agenttokens.ErrRecord) {
status = http.StatusBadRequest
} else if errors.Is(err, agenttokens.ErrActionRunnerAmbiguousCredential) {
status = http.StatusConflict
}
http.Error(w, "Failed to issue action runner credential", status)
return
}
record := issued.Record
for _, replaced := range issued.Replaced {
r.invalidateActionRunnerRecord(replaced)
}
LogAuditEventForTenant(organizationID, "action_runner_credential_issued", auth.GetUser(req.Context()), GetClientIP(req), req.URL.Path, true, "Issued host-bound typed action runner credential")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(actionRunnerCredentialResponse{
Token: issued.Token,
TokenID: record.ID,
OrganizationID: record.OrgID,
AgentID: record.Metadata["bound_agent_id"],
Hostname: record.Metadata["bound_hostname"],
RuntimeRole: record.Metadata[agenttokens.RuntimeRoleMetadataKey],
ActionCapability: record.Metadata[agenttokens.ActionCapabilityMetadataKey],
ActivationPending: strings.TrimSpace(record.Metadata[agenttokens.ActionRunnerActivationPendingMetadataKey]) == "true",
ActivationDeadline: record.ExpiresAt,
})
}
// handleActivateActionRunnerCredential commits a prepared rotation only after
// the exact replacement runner has registered and durably written its local
// pending proof. The runner calls this endpoint itself; no plaintext token or
// caller-selected predecessor identity crosses the boundary.
func (r *Router) handleActivateActionRunnerCredential(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPatch {
w.Header().Set("Allow", http.MethodPatch)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if r == nil || r.config == nil || r.persistence == nil || r.agentExecServer == nil {
http.Error(w, "Action runner activation service unavailable", http.StatusServiceUnavailable)
return
}
caller := getAPITokenRecordFromRequest(req)
if caller == nil {
http.Error(w, "Action runner bearer credential required", http.StatusForbidden)
return
}
decoder := json.NewDecoder(io.LimitReader(req.Body, maxActionRunnerCredentialRequestBytes+1))
decoder.DisallowUnknownFields()
var payload actionRunnerCredentialSelfRevokeRequest
if err := decoder.Decode(&payload); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
organizationID := strings.TrimSpace(GetOrgID(req.Context()))
if len(caller.GetBoundOrgs()) != 1 || strings.TrimSpace(caller.GetBoundOrgs()[0]) != organizationID ||
!agentbinding.EvaluateActionRunner(caller, payload.AgentID, payload.Hostname).Admit {
http.Error(w, "Action runner credential binding mismatch", http.StatusForbidden)
return
}
admission := agentexec.AgentAdmission{
OrganizationID: organizationID,
TokenID: strings.TrimSpace(caller.ID),
TokenHash: strings.TrimSpace(caller.Hash),
AgentID: strings.TrimSpace(caller.Metadata["bound_agent_id"]),
Hostname: strings.TrimSpace(caller.Metadata["bound_hostname"]),
RuntimeRole: agentexec.RuntimeRoleActionRunner,
ActionCapability: agentexec.ActionCapabilityTypedV1,
ActivationPending: true,
}
var promotion *agentexec.ActionRunnerSessionPromotion
_, revoked, changed, err := agenttokens.ActivateActionRunnerAndPersistWithPromotionForAdmission(
r.config, r.persistence, organizationID, caller.ID, caller.Hash, admission.AgentID, admission.Hostname,
func() (agenttokens.ActionRunnerPromotionTransaction, bool) {
var begun bool
promotion, begun = r.agentExecServer.BeginActionRunnerSessionPromotion(admission)
return promotion, begun
},
)
if promotion != nil {
promotion.Cleanup()
}
if err != nil {
status := http.StatusInternalServerError
if errors.Is(err, agenttokens.ErrRecord) {
status = http.StatusForbidden
} else if errors.Is(err, agenttokens.ErrActionRunnerSessionUnavailable) || errors.Is(err, agenttokens.ErrActionRunnerAmbiguousCredential) {
status = http.StatusConflict
}
http.Error(w, "Failed to activate action runner credential", status)
return
}
if changed {
for _, previous := range revoked {
r.invalidateActionRunnerRecord(previous)
}
LogAuditEventForTenant(organizationID, "action_runner_credential_activated", auth.GetUser(req.Context()), GetClientIP(req), req.URL.Path, true, "Activated host-bound typed action runner credential")
}
w.WriteHeader(http.StatusNoContent)
}
// handleCancelPendingActionRunnerCredentialActivation is the sole authority
// for installer rollback. It durably removes the exact still-pending
// replacement under the same token-inventory lock as activation; a committed
// credential returns conflict and can never authorize predecessor restore.
func (r *Router) handleCancelPendingActionRunnerCredentialActivation(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
w.Header().Set("Allow", http.MethodDelete)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if r == nil || r.config == nil || r.agentExecServer == nil {
http.Error(w, "Action runner activation service unavailable", http.StatusServiceUnavailable)
return
}
if req.Body != nil {
bodyPrefix, err := io.ReadAll(io.LimitReader(req.Body, 1))
if err != nil || len(bodyPrefix) != 0 {
http.Error(w, "Action runner activation cancellation accepts no request body", http.StatusBadRequest)
return
}
}
caller := getAPITokenRecordFromRequest(req)
if caller == nil {
http.Error(w, "Action runner bearer credential required", http.StatusForbidden)
return
}
organizationID := strings.TrimSpace(GetOrgID(req.Context()))
agentID := strings.TrimSpace(caller.Metadata["bound_agent_id"])
hostname := strings.TrimSpace(caller.Metadata["bound_hostname"])
if len(caller.GetBoundOrgs()) != 1 || strings.TrimSpace(caller.GetBoundOrgs()[0]) != organizationID ||
!agentbinding.EvaluateActionRunner(caller, agentID, hostname).Admit {
http.Error(w, "Action runner credential binding mismatch", http.StatusForbidden)
return
}
removed, err := agenttokens.CancelPendingActionRunnerAndPersistForHash(r.config, r.persistence, caller.ID, caller.Hash, organizationID, agentID, hostname)
tombstonePending := func(rec *config.APITokenRecord) bool {
if rec == nil {
return false
}
admission := agentexec.AgentAdmission{
OrganizationID: organizationID,
TokenID: strings.TrimSpace(rec.ID),
TokenHash: strings.TrimSpace(rec.Hash),
AgentID: strings.TrimSpace(rec.Metadata["bound_agent_id"]),
Hostname: strings.TrimSpace(rec.Metadata["bound_hostname"]),
RuntimeRole: strings.TrimSpace(rec.Metadata[agenttokens.RuntimeRoleMetadataKey]),
ActionCapability: strings.TrimSpace(rec.Metadata[agenttokens.ActionCapabilityMetadataKey]),
}
until := time.Now().UTC().Add(agenttokens.ActionRunnerActivationWindow)
if rec.ExpiresAt != nil && rec.ExpiresAt.After(time.Now()) {
until = *rec.ExpiresAt
}
return r.agentExecServer.TombstoneActionRunnerAdmission(admission, until)
}
if err != nil {
if removed != nil {
// Removal was published on disk before a dir sync error; still
// tombstone the pending socket admission before reporting 500.
_ = tombstonePending(removed)
}
if errors.Is(err, agenttokens.ErrActionRunnerAlreadyActivated) {
http.Error(w, "Action runner credential activation already committed", http.StatusConflict)
return
}
if errors.Is(err, agenttokens.ErrActionRunnerAmbiguousCredential) {
http.Error(w, "Ambiguous pending action runner credential hash", http.StatusConflict)
return
}
http.Error(w, "Could not establish rollback-safe action runner credential state", http.StatusInternalServerError)
return
}
if !tombstonePending(removed) {
http.Error(w, "Could not establish rollback-safe action runner admission state", http.StatusInternalServerError)
return
}
LogAuditEventForTenant(organizationID, "action_runner_credential_activation_cancelled", auth.GetUser(req.Context()), GetClientIP(req), req.URL.Path, true, "Cancelled pending host-bound typed action runner credential")
w.WriteHeader(http.StatusNoContent)
}
// handleSelfRevokeActionRunnerCredential lets the separately credentialed
// runner revoke only its own exact tenant/host binding. It cannot select a
// token ID or another host, and browser/session authentication is rejected.
func (r *Router) handleSelfRevokeActionRunnerCredential(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
w.Header().Set("Allow", http.MethodDelete)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if r == nil || r.config == nil || r.persistence == nil {
http.Error(w, "Action runner credential service unavailable", http.StatusServiceUnavailable)
return
}
caller := getAPITokenRecordFromRequest(req)
if caller == nil {
http.Error(w, "Action runner bearer credential required", http.StatusForbidden)
return
}
decoder := json.NewDecoder(io.LimitReader(req.Body, maxActionRunnerCredentialRequestBytes+1))
decoder.DisallowUnknownFields()
var payload actionRunnerCredentialSelfRevokeRequest
if err := decoder.Decode(&payload); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
organizationID := strings.TrimSpace(GetOrgID(req.Context()))
config.Mu.Lock()
var (
removed config.APITokenRecord
revokeAfter func()
revokeDone bool
published bool
)
_ = r.persistence.WithAPITokenWriteLock(func() error {
if r.persistence != nil {
if err := r.persistence.SyncPendingRootImportTokensAlreadyLocked(r.config); err != nil {
revokeAfter = func() {
http.Error(w, "Failed to verify action runner credential inventory", http.StatusInternalServerError)
}
return nil
}
}
r.config.SortAPITokens()
previousTokens := append([]config.APITokenRecord(nil), r.config.APITokens...)
index := -1
hashMatches := 0
for candidateIndex := range r.config.APITokens {
candidate := &r.config.APITokens[candidateIndex]
if !constantTimeStringEqual(candidate.Hash, caller.Hash) {
continue
}
hashMatches++
if hashMatches > 1 {
// Persisted inventories permit duplicate hashes, and authentication
// selects the first remaining match. Fail closed rather than deleting
// one record while a shadowed record keeps the same bearer valid.
revokeAfter = func() {
http.Error(w, "Ambiguous action runner credential hash", http.StatusConflict)
}
return nil
}
// Mirror authentication's first-matching-hash rule: reject if the
// authoritative first record for this hash does not match the caller.
if candidate.ID != caller.ID ||
candidate.IsExpired() || apiTokenAuthenticatedUser(candidate) != apiTokenAuthenticatedUser(caller) {
index = -1
break
}
orgs := candidate.GetBoundOrgs()
if len(orgs) != 1 || strings.TrimSpace(orgs[0]) != organizationID ||
!agentbinding.EvaluateActionRunner(candidate, payload.AgentID, payload.Hostname).Admit {
revokeAfter = func() {
http.Error(w, "Action runner credential binding mismatch", http.StatusForbidden)
}
return nil
}
index = candidateIndex
removed = candidate.Clone()
}
if index < 0 {
revokeAfter = func() {
http.Error(w, "Action runner credential not found", http.StatusUnauthorized)
}
return nil
}
r.config.APITokens = append(r.config.APITokens[:index], r.config.APITokens[index+1:]...)
r.config.SortAPITokens()
if err := r.persistence.SaveAPITokensAlreadyLocked(r.config.APITokens); err != nil {
if config.IsDurablePublishSyncError(err) {
published = true
} else {
r.config.APITokens = previousTokens
r.config.SortAPITokens()
}
revokeAfter = func() {
http.Error(w, "Failed to revoke action runner credential", http.StatusInternalServerError)
}
return nil
}
published = true
revokeDone = true
return nil
})
config.Mu.Unlock()
if published {
r.invalidateActionRunnerRecord(removed)
}
if !revokeDone {
if revokeAfter != nil {
revokeAfter()
}
return
}
LogAuditEventForTenant(organizationID, "action_runner_credential_revoked", auth.GetUser(req.Context()), GetClientIP(req), req.URL.Path, true, "Revoked exact host-bound typed action runner credential")
w.WriteHeader(http.StatusNoContent)
}
func (r *Router) invalidateActionRunnerRecord(record config.APITokenRecord) bool {
if r == nil || r.agentExecServer == nil {
return false
}
orgs := record.GetBoundOrgs()
if len(orgs) != 1 {
return false
}
return r.agentExecServer.InvalidateActionRunnerSession(agentexec.AgentAdmission{
OrganizationID: strings.TrimSpace(orgs[0]),
TokenID: strings.TrimSpace(record.ID),
TokenHash: strings.TrimSpace(record.Hash),
AgentID: strings.TrimSpace(record.Metadata["bound_agent_id"]),
Hostname: strings.TrimSpace(record.Metadata["bound_hostname"]),
RuntimeRole: strings.TrimSpace(record.Metadata[agenttokens.RuntimeRoleMetadataKey]),
ActionCapability: strings.TrimSpace(record.Metadata[agenttokens.ActionCapabilityMetadataKey]),
})
}
func (r *Router) resolveActionRunnerHostIdentity(req *http.Request, requestedID, requestedHostname string) (string, string, bool) {
requestedID = strings.TrimSpace(requestedID)
requestedHostname = strings.TrimSpace(requestedHostname)
if r == nil || r.unifiedAgentHandlers == nil || req == nil || requestedID == "" || requestedHostname == "" {
return "", "", false
}
monitor := r.unifiedAgentHandlers.getMonitor(req.Context())
if monitor == nil {
return "", "", false
}
var matchedID, matchedHostname string
matches := 0
for _, host := range monitor.GetLiveHostsSnapshot() {
if strings.TrimSpace(host.ID) != requestedID ||
!unifiedresources.HostnamesEquivalent(host.Hostname, requestedHostname) ||
strings.TrimSpace(host.IntegrationSource) != "" || host.IdentityConflict != nil {
continue
}
matchedID = strings.TrimSpace(host.ID)
matchedHostname = unifiedresources.NormalizeFullHostname(host.Hostname)
matches++
}
if matches != 1 || matchedID == "" || matchedHostname == "" {
return "", "", false
}
return matchedID, matchedHostname, true
}