Files
Pulse/internal/api/attention_actions.go
rcourtman 9bd857636b Require action permission for host work outside Assistant reads
Four ways remained for a signed-in user who cannot run actions, such as
a viewer, to start work on a monitored host or plan an action:

- POST /api/infra-updates/check needed only monitoring:write, which
  session callers skip. It queues the Docker update scan on an agent,
  whose registry lookups can run the host's credential helper. It now
  needs the action-execute capability, as the Actions execute route does.
- The Assistant's pulse_control plans an action on the shared lifecycle,
  but every session caller could use it once the control level allowed
  control tools. POST /api/actions/plan needs the action-plan
  capability; the chat route now binds the same capability and the
  Assistant offers and runs pulse_control only with it.
- POST /api/ai/findings/{id}/reinvestigate starts a Patrol investigation
  that runs commands on hosts and can plan a fix. It now needs both the
  plan and execute capabilities.
- The attention restart offer and its plan route checked the actor
  against the authorizer, which under the default authorizer admits
  every org member. A viewer was offered the restart and could plan it.
  They now also apply the request rule the Actions routes use.

Under the default authorizer these refuse non-admin sessions and
non-admin proxy roles; an RBAC grant decides otherwise. API tokens keep
their existing scope rules, including relay-mobile planning.
2026-11-14 09:07:00 +00:00

268 lines
9.0 KiB
Go

package api
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/actionlifecycle"
"github.com/rcourtman/pulse-go-rewrite/internal/ai"
"github.com/rcourtman/pulse-go-rewrite/internal/mock"
"github.com/rcourtman/pulse-go-rewrite/internal/operationaltrust"
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
"github.com/rcourtman/pulse-go-rewrite/pkg/auth"
)
const operationalTrustActionOriginSurface = "operational_trust_attention"
func isAttentionActionPlanPath(path string) bool {
_, capability, ok := parseAttentionActionPlanPath(path)
return ok && capability != ""
}
func parseAttentionActionPlanPath(path string) (string, string, bool) {
path = strings.Trim(path, "/")
actionBoundary := strings.LastIndex(path, "/actions/")
if actionBoundary <= 0 {
return "", "", false
}
itemPart := path[:actionBoundary]
actionPart := path[actionBoundary+len("/actions/"):]
actionSegments := strings.Split(actionPart, "/")
if len(actionSegments) != 2 || actionSegments[1] != "plan" {
return "", "", false
}
itemID, err := url.PathUnescape(itemPart)
if err != nil {
return "", "", false
}
capability, err := url.PathUnescape(actionSegments[0])
if err != nil {
return "", "", false
}
itemID = strings.TrimSpace(itemID)
capability = strings.TrimSpace(capability)
return itemID, capability, itemID != "" && capability != ""
}
func (h *AttentionHandlers) projectAttentionActions(
r *http.Request,
details []ai.AttentionItemDetail,
) {
if h == nil || h.resources == nil || len(details) == 0 {
return
}
orgID := GetOrgID(r.Context())
registry, registryErr := h.resources.buildRegistry(orgID)
store, storeErr := h.resources.getStore(orgID)
actor, actorErr := actionActorForRequest(h.resources.cfg, r, orgID)
authorized := actorErr == nil &&
h.hasAutoFixLicense != nil &&
h.hasAutoFixLicense(r.Context()) &&
h.callerMayReviewAndRunAction(r, orgID, actor)
actions := make(map[string]unified.ActionAuditRecord, len(details))
if storeErr == nil {
if reader, ok := store.(unified.OperationalActionAuditOriginBatchReader); ok {
recordIDs := make([]string, 0, len(details))
for index := range details {
recordIDs = append(recordIDs, details[index].Item.OperationalRecordID)
}
if records, err := reader.GetLatestActionAuditsByOperationalRecords(
operationalTrustActionOriginSurface,
recordIDs,
); err == nil {
actions = records
}
}
}
now := time.Now().UTC()
for index := range details {
detail := &details[index]
candidate := ai.AttentionActionCandidate{Authorized: authorized}
if record, found := actions[detail.Item.OperationalRecordID]; found &&
ai.AttentionActionMatchesItem(detail.Item, &record) {
candidate.Action = &record
detail.Item.VerificationState = ai.AttentionActionVerificationState(&record)
}
if registryErr == nil {
if resource, found := registry.Get(detail.Item.SubjectResourceID); found {
candidate.Resource = resource
candidate.Readiness = unified.ResourceActionReadiness{
Name: ai.AttentionDockerRestartCapability,
Available: false,
ReasonCode: "executor_readiness_unavailable",
Reason: "Current action readiness could not be verified.",
}
if checker, ok := h.resources.actionExecutor.(ActionAvailabilityChecker); ok {
candidate.Readiness = checker.CheckActionAvailable(
r.Context(),
unified.ActionRequest{
RequestID: attentionActionRequestID(detail.Item.OperationalRecordID),
ResourceID: detail.Item.SubjectResourceID,
CapabilityName: ai.AttentionDockerRestartCapability,
Params: map[string]any{},
Reason: attentionActionReason(detail.Item),
},
*resource,
)
}
}
}
offer, reason := ai.ProjectAttentionAction(detail, candidate, now)
if reason == ai.AttentionActionEligible {
operationaltrust.GetMetrics().ObserveActionOffer("eligible")
} else {
operationaltrust.GetMetrics().ObserveActionOffer("ineligible")
}
if reason == ai.AttentionActionEligible {
detail.Item.AvailableActions = []ai.AttentionActionOffer{offer}
}
}
}
// callerMayReviewAndRunAction reports whether this caller may plan, approve
// and run an attention action. authorizeActor checks the actor, its org access
// and the authorizer, but under the default authorizer the authorizer allows
// every signed-in user. The request-level check adds what the Actions routes
// require there: an administrator session or admin proxy role, or a token
// scope for each capability.
func (h *AttentionHandlers) callerMayReviewAndRunAction(r *http.Request, orgID string, actor unified.ActionActor) bool {
for _, capability := range []string{auth.ActionPlan, auth.ActionApprove, auth.ActionExecute} {
if h.actionAuthority.authorizeActor(r.Context(), orgID, actor, capability) != nil ||
!requestHasActionCapability(h.resources.cfg, h.actionAuthority.authorizer, r, capability) {
return false
}
}
return true
}
func (h *AttentionHandlers) handleAttentionActionPlan(
w http.ResponseWriter,
r *http.Request,
path string,
) {
if mock.IsMockEnabled() {
writeJSONError(w, http.StatusForbidden, "attention_action_mock_mode", "Cannot plan actions in mock mode")
return
}
itemID, capability, ok := parseAttentionActionPlanPath(path)
if !ok {
writeJSONError(w, http.StatusNotFound, "attention_action_not_found", "Attention action was not found")
return
}
if capability != ai.AttentionDockerRestartCapability {
writeJSONError(w, http.StatusConflict, "attention_action_ineligible", "This action is not eligible for the selected attention item")
return
}
if h == nil || h.resources == nil {
writeJSONError(w, http.StatusServiceUnavailable, "attention_actions_unavailable", "Governed actions are unavailable")
return
}
if h.hasAutoFixLicense == nil || !h.hasAutoFixLicense(r.Context()) {
WriteLicenseRequired(
w,
featureAIAutoFixKey,
"Patrol fix actions require Pulse Pro",
)
return
}
projection, err := h.project(r, true)
if err != nil {
writeAttentionUnavailable(w, err)
return
}
var detail *ai.AttentionItemDetail
for index := range projection.Details {
if projection.Details[index].Item.ID == itemID {
detail = &projection.Details[index]
break
}
}
if detail == nil {
writeJSONError(w, http.StatusNotFound, "attention_item_not_found", "Attention item was not found")
return
}
enriched := []ai.AttentionItemDetail{*detail}
h.projectAttentionActions(r, enriched)
detail = &enriched[0]
var offer *ai.AttentionActionOffer
for index := range detail.Item.AvailableActions {
if detail.Item.AvailableActions[index].Capability == capability {
offer = &detail.Item.AvailableActions[index]
break
}
}
if offer == nil {
writeJSONError(w, http.StatusConflict, "attention_action_ineligible", "Current evidence, capability state, or operator authority does not permit this action")
return
}
orgID := GetOrgID(r.Context())
actor, err := actionActorForRequest(h.resources.cfg, r, orgID)
if err != nil || !h.callerMayReviewAndRunAction(r, orgID, actor) {
writeJSONError(w, http.StatusForbidden, "attention_action_denied", "You do not have permission to review and run this action")
return
}
if offer.ActionID != "" {
record, found, getErr := h.resources.ActionLifecycle().Get(orgID, offer.ActionID)
if getErr != nil || !found {
writeJSONError(w, http.StatusServiceUnavailable, "attention_action_query_failed", "The governed action record is unavailable")
return
}
writeAttentionActionPlan(w, record.Plan)
return
}
plan, err := h.resources.ActionLifecycle().PlanWithOptions(
r.Context(),
orgID,
unified.ActionRequest{
RequestID: attentionActionRequestID(detail.Item.OperationalRecordID),
ResourceID: detail.Item.SubjectResourceID,
CapabilityName: capability,
Params: map[string]any{},
Reason: attentionActionReason(detail.Item),
},
actionlifecycle.PlanOptions{
Actor: actor,
Origin: &unified.ActionOrigin{
Surface: operationalTrustActionOriginSurface,
OperationalRecordID: detail.Item.OperationalRecordID,
EvidenceIDs: append([]string(nil), offer.EvidenceIDs...),
},
},
)
if err != nil {
writeActionPlanError(w, err)
return
}
writeAttentionActionPlan(w, plan)
}
func writeAttentionActionPlan(w http.ResponseWriter, plan unified.ActionPlan) {
if err := utils.WriteJSONResponse(w, plan); err != nil {
writeJSONError(w, http.StatusInternalServerError, "attention_action_encode_failed", "Failed to encode action plan")
}
}
func attentionActionRequestID(operationalRecordID string) string {
return "operational-trust:" + strings.TrimSpace(operationalRecordID) + ":" +
ai.AttentionDockerRestartCapability
}
func attentionActionReason(item ai.AttentionItem) string {
name := strings.TrimSpace(item.SubjectResourceName)
if name == "" {
name = strings.TrimSpace(item.SubjectResourceID)
}
return fmt.Sprintf(
"Restart %s after fresh confirmed evidence reported an unhealthy container.",
name,
)
}