mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
Planning an action, refreshing it, reviewing it (the readiness in
GET /api/actions/{id}) and approving it each asked the agent to check
the action first, for example a simulated `apt-get -s upgrade` for OS
updates, a package cache scan or a container inspect. Under an RBAC
authorizer a role with only the plan or approve permission could
therefore make the agent run commands. The default authorizer limits
those routes to administrators, so viewers were not exposed.
The check is agent work, so the action lifecycle now runs it before
dispatch only for callers allowed to execute actions, the permission
every other route that reaches an agent requires. Callers without it
still get every Pulse-side check, and dispatch always asks the agent,
so an action that is no longer feasible still cannot run. Patrol plans
keep the check. The Assistant plans for the chat caller under its own
identity and passes the caller's execute permission; a refresh uses the
refreshing caller's permission even when Patrol owns the action.
Keeping the check behind a separate permission was the alternative. It
would add a role permission without making dispatch any safer.
173 lines
6.4 KiB
Go
173 lines
6.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"sync/atomic"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/actionlifecycle"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/api/apicontext"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/api/resourceapi"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/auth"
|
|
)
|
|
|
|
// ResourceHandlers preserves the established router and extension surface
|
|
// while composing the resource-query service with API-owned action mutations.
|
|
// QueryService owns resource registry construction, durable resource storage,
|
|
// filtering, projections, and read handlers. The fields below remain here
|
|
// because action lifecycle and operator-state mutations are separate domains.
|
|
type ResourceHandlers struct {
|
|
resourceapi.QueryService
|
|
|
|
cfg *config.Config
|
|
tenantStateProvider TenantStateProvider
|
|
actionExecutor ActionExecutor
|
|
broadcastAvailability atomic.Pointer[broadcastAvailabilityChecker]
|
|
actionCompleted func(unified.ActionAuditRecord)
|
|
actionTransition func(orgID string, record unified.ActionAuditRecord)
|
|
policyAdmission *actionlifecycle.PolicyAdmissionCoordinator
|
|
actionEmergencyStop func(orgID string) (bool, error)
|
|
actionDecisionAuthorizer actionlifecycle.DecisionAuthorizer
|
|
actionExecutionAuthorizer actionlifecycle.ExecutionAuthorizer
|
|
actionLiveCheckAuthorizer actionlifecycle.LiveCheckAuthorizer
|
|
actionRefreshPlanner actionlifecycle.RefreshPlanner
|
|
operatorStateChanged func(orgID, resourceID string)
|
|
}
|
|
|
|
type SupplementalRecordsProvider = resourceapi.SupplementalRecordsProvider
|
|
type TenantSupplementalRecordsProvider = resourceapi.TenantSupplementalRecordsProvider
|
|
type SupplementalSnapshotSourceOwner = resourceapi.SupplementalSnapshotSourceOwner
|
|
type TenantSupplementalSnapshotSourceOwner = resourceapi.TenantSupplementalSnapshotSourceOwner
|
|
type ResourcesResponse = resourceapi.ResourcesResponse
|
|
type ResourcesMeta = resourceapi.ResourcesMeta
|
|
type StorageSummaryResponse = resourceapi.StorageSummaryResponse
|
|
type StorageSummaryIncident = resourceapi.StorageSummaryIncident
|
|
type StorageIncidentsResponse = resourceapi.StorageIncidentsResponse
|
|
type StorageIncidentSection = resourceapi.StorageIncidentSection
|
|
|
|
func EmptyResourcesResponse() ResourcesResponse { return resourceapi.EmptyResourcesResponse() }
|
|
|
|
func EmptyStorageSummaryResponse() StorageSummaryResponse {
|
|
return resourceapi.EmptyStorageSummaryResponse()
|
|
}
|
|
|
|
func EmptyStorageIncidentsResponse() StorageIncidentsResponse {
|
|
return resourceapi.EmptyStorageIncidentsResponse()
|
|
}
|
|
|
|
// NewResourceHandlers builds the compatibility composition used by Router.
|
|
func NewResourceHandlers(cfg *config.Config) *ResourceHandlers {
|
|
return &ResourceHandlers{
|
|
QueryService: *resourceapi.NewQueryService(cfg),
|
|
cfg: cfg,
|
|
policyAdmission: &actionlifecycle.PolicyAdmissionCoordinator{},
|
|
}
|
|
}
|
|
|
|
func (h *ResourceHandlers) ensureQueryService() *resourceapi.QueryService {
|
|
return &h.QueryService
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetStateProvider(provider SnapshotProvider) {
|
|
h.ensureQueryService().SetStateProvider(provider)
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetTenantStateProvider(provider TenantStateProvider) {
|
|
h.tenantStateProvider = provider
|
|
h.ensureQueryService().SetTenantStateProvider(provider)
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetActionExecutor(executor ActionExecutor) {
|
|
h.actionExecutor = executor
|
|
checker, _ := executor.(actionlifecycle.AvailabilityChecker)
|
|
h.ensureQueryService().SetActionAvailabilityChecker(checker)
|
|
h.broadcastAvailability.Store(&broadcastAvailabilityChecker{checker: checker})
|
|
}
|
|
|
|
// broadcastAvailabilityChecker lets monitor goroutines, which can tick before
|
|
// the router finishes wiring its executors, read the checker without a race.
|
|
type broadcastAvailabilityChecker struct {
|
|
checker actionlifecycle.AvailabilityChecker
|
|
}
|
|
|
|
// AttachBroadcastActionRefusals records the server's action refusals on one
|
|
// organization's broadcast rows, so a browser learns of a refusal, or of its
|
|
// end, from the next websocket frame and not from its next REST read.
|
|
func (h *ResourceHandlers) AttachBroadcastActionRefusals(orgID string, resources []unified.Resource) {
|
|
holder := h.broadcastAvailability.Load()
|
|
if holder == nil || holder.checker == nil {
|
|
return
|
|
}
|
|
ctx := context.WithValue(context.Background(), apicontext.OrgIDContextKey, orgID)
|
|
actionlifecycle.AttachActionRefusals(ctx, holder.checker, resources)
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetActionEmergencyStopChecker(checker func(orgID string) (bool, error)) {
|
|
h.actionEmergencyStop = checker
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetActionAuthorizers(decision actionlifecycle.DecisionAuthorizer, execution actionlifecycle.ExecutionAuthorizer, liveCheck actionlifecycle.LiveCheckAuthorizer) {
|
|
h.actionDecisionAuthorizer = decision
|
|
h.actionExecutionAuthorizer = execution
|
|
h.actionLiveCheckAuthorizer = liveCheck
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetActionRefreshPlanner(planner actionlifecycle.RefreshPlanner) {
|
|
h.actionRefreshPlanner = planner
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetActionCompletedPublisher(publisher func(unified.ActionAuditRecord)) {
|
|
h.actionCompleted = publisher
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetActionTransitionPublisher(publisher func(orgID string, record unified.ActionAuditRecord)) {
|
|
h.actionTransition = publisher
|
|
}
|
|
|
|
func (h *ResourceHandlers) SetOperatorStateChanged(callback func(orgID, resourceID string)) {
|
|
h.operatorStateChanged = callback
|
|
}
|
|
|
|
func (h *ResourceHandlers) buildRegistry(orgID string) (*unified.ResourceRegistry, error) {
|
|
return h.ensureQueryService().BuildRegistry(orgID)
|
|
}
|
|
|
|
func (h *ResourceHandlers) getStore(orgID string) (unified.ResourceStore, error) {
|
|
return h.ensureQueryService().Store(orgID)
|
|
}
|
|
|
|
func (h *ResourceHandlers) invalidateCache(orgID string) {
|
|
h.ensureQueryService().InvalidateCache(orgID)
|
|
}
|
|
|
|
func (h *ResourceHandlers) CloseTenantStore(orgID string) error {
|
|
if h == nil {
|
|
return nil
|
|
}
|
|
return h.QueryService.CloseTenantStore(orgID)
|
|
}
|
|
|
|
func (h *ResourceHandlers) CloseStores() error {
|
|
if h == nil {
|
|
return nil
|
|
}
|
|
return h.QueryService.CloseStores()
|
|
}
|
|
|
|
// firstNonEmptyTrimmed is retained for non-resource connection projections.
|
|
func firstNonEmptyTrimmed(values ...string) string {
|
|
for _, value := range values {
|
|
if trimmed := strings.TrimSpace(value); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func getUserID(r *http.Request) string {
|
|
return auth.GetUser(r.Context())
|
|
}
|