Files
archie/internal/infrastructure/staterpc/task_grants.go
T
sam a2f4b8ef1b refactor(state)!: split StateStoreService into per-domain services
One gRPC service per domain in state.v1 (tasks, executions, accounts, identities, packages, captures, bindings, access, orgs, alerts and so on), each with its own server type in staterpc. RPC and message names and numbers are unchanged.
2026-10-25 19:54:27 +11:00

422 lines
14 KiB
Go

package staterpc
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strconv"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
)
// TaskGrants verifies run credentials: the one token a task's container
// presents to the State Store and, through ResolveTaskGrant, to archied's
// worktree push. Only digests are stored, in the State Store's database, so
// a credential survives a restart of either service until it is revoked or
// expires.
type TaskGrants struct {
Store storecontract.RunCredentialStore
}
// maxGrantLifetime caps any requested lifetime.
const maxGrantLifetime = 7 * 24 * time.Hour
func (g *TaskGrants) register(ctx context.Context, taskID int64, token string, lifetime time.Duration) error {
if taskID <= 0 || len(token) != 64 || lifetime <= 0 || lifetime > maxGrantLifetime {
return status.Error(codes.InvalidArgument, "invalid task grant")
}
if _, err := hex.DecodeString(token); err != nil {
return status.Error(codes.InvalidArgument, "invalid task grant")
}
return g.Store.PutRunCredential(ctx, sha256.Sum256([]byte(token)), taskID, time.Now().Add(lifetime))
}
func (g *TaskGrants) revoke(ctx context.Context, token string) error {
return g.Store.DeleteRunCredential(ctx, sha256.Sum256([]byte(token)))
}
// Sweep deletes every expired credential: the ones a crashed daemon never
// revoked.
func (g *TaskGrants) Sweep(ctx context.Context) error {
return g.Store.DeleteExpiredRunCredentials(ctx, time.Now())
}
// requestToken extracts the bearer token from ctx's metadata, if any.
func requestToken(ctx context.Context) string {
md, _ := metadata.FromIncomingContext(ctx)
if tokens := md.Get(tokenMetadataKey); len(tokens) > 0 {
return tokens[0]
}
return ""
}
// taskFor resolves token to the task ID it was issued for, or 0 if the token
// is unknown, expired, or empty.
func (g *TaskGrants) taskFor(ctx context.Context, token string) (int64, error) {
if token == "" {
return 0, nil
}
id, err := g.Store.RunCredentialTask(ctx, sha256.Sum256([]byte(token)), time.Now())
if errors.Is(err, storecontract.ErrRunCredentialUnknown) {
return 0, nil
}
return id, err
}
// authorizesTaskScopedCall reports whether a task-scoped token may make this
// call: only RPCs in taskScopedTargets that target taskID.
func authorizesTaskScopedCall(fullMethod string, req any, taskID int64) bool {
target := taskScopedTargets[fullMethod]
return target != nil && target(req) == taskID
}
// taskScopedTargets maps each RPC a task grant may call to the task ID its
// request targets.
var taskScopedTargets = map[string]func(any) int64{
pb.StatusService_PutPresence_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.PutPresenceRequest)
if !ok || r.Presence == nil || r.Presence.Service != presence.Agent {
return 0
}
id, err := strconv.ParseInt(r.Presence.InstanceId, 10, 64)
if err != nil || id <= 0 || strconv.FormatInt(id, 10) != r.Presence.InstanceId {
return 0
}
return id
},
pb.TaskService_Update_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.UpdateRequest)
if !ok || r.Task == nil {
return 0
}
return r.Task.Id
},
pb.TaskService_Transition_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.TransitionRequest)
if !ok {
return 0
}
return r.TaskId
},
pb.TaskEventService_InsertEvent_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.InsertEventRequest)
if !ok || r.Event == nil {
return 0
}
return r.Event.TaskId
},
pb.ExecutionService_StartStep_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.StartStepRequest)
if !ok {
return 0
}
return r.ExecutionId
},
pb.ExecutionService_RecordUsage_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.RecordUsageRequest)
if !ok || r.Record == nil {
return 0
}
return r.Record.TaskId
},
pb.ExecutionService_FinishStep_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.FinishStepRequest)
if !ok {
return 0
}
return r.ExecutionId
},
pb.TaskService_EnqueueCallTask_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.EnqueueCallTaskRequest)
if !ok {
return 0
}
return r.CallerTaskId
},
pb.TaskService_WorkflowCallStatus_FullMethodName: func(req any) int64 {
r, ok := req.(*pb.WorkflowCallStatusRequest)
if !ok {
return 0
}
return r.CallerTaskId
},
}
// UnaryInterceptor separates services from task-scoped workers. A trusted
// caller (a verified service certificate, or a plaintext peer, which only a
// loopback listener admits) has full access; any other call needs a task
// grant and is confined to its task.
func (g *TaskGrants) UnaryInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if rpctls.Trusted(ctx) {
return handler(ctx, req)
}
token := requestToken(ctx)
if token == "" {
return nil, status.Error(codes.Unauthenticated, "missing client certificate or task grant")
}
taskID, err := g.taskFor(ctx, token)
if err != nil {
return nil, status.Error(codes.Unavailable, "run credentials unavailable")
}
if taskID == 0 {
return nil, status.Error(codes.Unauthenticated, "invalid task grant")
}
if !authorizesTaskScopedCall(info.FullMethod, req, taskID) {
return nil, status.Error(codes.PermissionDenied, "task grant does not authorize this operation")
}
// A container's metadata is its own claim: drop it so no later
// interceptor attributes the call to a service or principal.
ctx = metadata.NewIncomingContext(ctx, metadata.MD{})
dropDefinitionPin(req)
return handler(access.WithActor(ctx, fmt.Sprintf("task/%d", taskID)), req)
}
}
// dropDefinitionPin removes the workflow definition from a task-scoped
// Update, so the store keeps the stored pin. The pin is what the run's
// package authority is read from; only the daemon pins, from the active
// definitions.
func dropDefinitionPin(req any) {
r, ok := req.(*pb.UpdateRequest)
if !ok || r.Task == nil {
return
}
r.Task.WorkflowDefinitionVersion = 0
r.Task.WorkflowDefinitionDigest = ""
r.Task.WorkflowDefinitionYaml = ""
}
// StreamInterceptor admits only services to store read streams.
func (g *TaskGrants) StreamInterceptor() grpc.StreamServerInterceptor {
return func(srv any, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if rpctls.Trusted(stream.Context()) {
return handler(srv, stream)
}
return status.Error(codes.PermissionDenied, "task grant does not authorize streaming reads")
}
}
func (s *taskGrantServer) RegisterTaskGrant(ctx context.Context, r *pb.RegisterTaskGrantRequest) (*pb.RegisterTaskGrantResponse, error) {
if s.deps.Grants == nil {
return nil, status.Error(codes.Unavailable, "task grants unavailable")
}
if err := s.deps.Grants.register(ctx, r.TaskId, r.Token, time.Duration(r.LifetimeSeconds)*time.Second); err != nil {
if _, ok := status.FromError(err); ok {
return nil, err
}
return nil, status.Error(codes.Unavailable, "run credentials unavailable")
}
// No run starts unaudited: a start whose audit cannot be written is
// undone.
if err := s.auditIdentityStart(ctx, r.TaskId); err != nil {
_ = s.deps.Grants.revoke(context.WithoutCancel(ctx), r.Token)
return nil, status.Errorf(codes.Unavailable, "audit identity start: %v", err)
}
return &pb.RegisterTaskGrantResponse{}, nil
}
func (s *taskGrantServer) RevokeTaskGrant(ctx context.Context, r *pb.RevokeTaskGrantRequest) (*pb.RevokeTaskGrantResponse, error) {
if s.deps.Grants == nil {
return nil, status.Error(codes.Unavailable, "task grants unavailable")
}
if err := s.deps.Grants.revoke(ctx, r.Token); err != nil {
return nil, status.Error(codes.Unavailable, "run credentials unavailable")
}
return &pb.RevokeTaskGrantResponse{}, nil
}
// ResolveTaskGrant answers which task a run credential belongs to, for the
// services that verify one (archied's worktree push). Task grants cannot call
// it: it is not in taskScopedTargets.
func (s *taskGrantServer) ResolveTaskGrant(ctx context.Context, r *pb.ResolveTaskGrantRequest) (*pb.ResolveTaskGrantResponse, error) {
if s.deps.Grants == nil {
return nil, status.Error(codes.Unavailable, "task grants unavailable")
}
id, err := s.deps.Grants.taskFor(ctx, r.GetToken())
if err != nil {
return nil, status.Error(codes.Unavailable, "run credentials unavailable")
}
if id == 0 {
return nil, status.Error(codes.NotFound, "run credential unknown or expired")
}
t, err := s.deps.Tasks.TaskByID(ctx, id)
if err != nil {
return nil, mapError(err)
}
principal, err := s.executionPrincipal(ctx, t)
if err != nil {
return nil, status.Error(codes.Unavailable, "execution identity unavailable")
}
route := string(principal)
if principal == s.deps.RootIdentity {
route = ""
}
return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: route, PrincipalId: string(principal)}, nil
}
// RegisterTaskGrant creates fresh random material at the daemon and registers
// it at the remote authority before starting the container.
func (c *Client) RegisterTaskGrant(ctx context.Context, taskID int64, lifetime time.Duration) (string, error) {
var bytes [32]byte
if _, err := rand.Read(bytes[:]); err != nil {
return "", err
}
token := hex.EncodeToString(bytes[:])
_, err := c.taskGrant.RegisterTaskGrant(ctx, &pb.RegisterTaskGrantRequest{TaskId: taskID, Token: token, LifetimeSeconds: int64(lifetime / time.Second)})
if err != nil {
return "", unmapError(err)
}
return token, nil
}
func (c *Client) RevokeTaskGrant(ctx context.Context, token string) error {
_, err := c.taskGrant.RevokeTaskGrant(ctx, &pb.RevokeTaskGrantRequest{Token: token})
return unmapError(err)
}
// ResolveTaskGrant returns the task a run credential was issued for.
func (c *Client) ResolveTaskGrant(ctx context.Context, token string) (int64, error) {
r, err := c.taskGrant.ResolveTaskGrant(ctx, &pb.ResolveTaskGrantRequest{Token: token})
if status.Code(err) == codes.NotFound {
return 0, storecontract.ErrRunCredentialUnknown
}
if err != nil {
return 0, unmapError(err)
}
return r.GetTaskId(), nil
}
// grantMargin outlives the task's own time limit, so a credential never
// expires under a run that is still inside its budget.
const grantMargin = time.Hour
// GrantIssuer implements daemon.StateStoreGrantIssuer over a *Client: one run
// credential per task, registered before the container starts and revoked
// when the daemon releases it. A crashed daemon's credential lapses at the
// task's time limit plus grantMargin.
type GrantIssuer struct {
Client *Client
}
func (g *GrantIssuer) Issue(task *task.Task, limit time.Duration) (string, func(), error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
lifetime := min(limit+grantMargin, maxGrantLifetime)
if limit <= 0 {
lifetime = maxGrantLifetime
}
token, err := g.Client.RegisterTaskGrant(ctx, task.ID, lifetime)
if err != nil {
return "", nil, err
}
return token, func() {
revokeCtx, revokeCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer revokeCancel()
_ = g.Client.RevokeTaskGrant(revokeCtx, token)
}, nil
}
// TaskForCredential returns the task a run credential was issued for.
func (c *Client) TaskForCredential(ctx context.Context, token string) (*task.Task, error) {
r, err := c.taskGrant.ResolveTaskGrant(ctx, &pb.ResolveTaskGrantRequest{Token: token})
if err != nil {
return nil, unmapError(err)
}
t, err := c.TaskByID(ctx, r.TaskId)
if err != nil {
return nil, err
}
t.Identity = r.ExecutionIdentity
return t, nil
}
func executionReference(t *task.Task) (string, error) {
iface, err := task.ParseWorkflowInterface(t.WorkflowDefinitionYAML)
if err != nil {
return "", fmt.Errorf("execution identity: %w", err)
}
if iface.Identity == "" {
return "", fmt.Errorf("pinned workflow declares no execution identity")
}
return iface.Identity, nil
}
// auditIdentityStart records the run starting as its identity. Every field is
// the server's: the caller from the authenticated call, the identity from the
// task row, the org from the identity.
func (s *server) auditIdentityStart(ctx context.Context, taskID int64) error {
t, err := s.deps.Tasks.TaskByID(ctx, taskID)
if err != nil {
return err
}
principal, err := s.executionPrincipal(ctx, t)
if err != nil {
return err
}
data := map[string]any{"requested_by": t.Identity}
if s.deps.Principals != nil {
p, err := s.deps.Principals.PrincipalFor(ctx, principal)
if err != nil {
return err
}
data["org"] = string(p.Org)
}
_, err = s.deps.Tasks.InsertEvent(ctx, events.Event{
At: time.Now(), Kind: events.KindIdentityStarted, TaskID: t.ID, Workflow: t.Workflow, Attempt: t.Attempt,
ActorID: access.ActorFromContext(ctx), PrincipalID: string(principal), Data: data,
})
return err
}
func (s *server) executionPrincipal(ctx context.Context, t *task.Task) (identity.IdentityID, error) {
ref, err := executionReference(t)
if err != nil {
return "", err
}
id := identity.IdentityID(ref)
if ref == "" {
id = s.deps.RootIdentity
}
if s.deps.Identities == nil {
return "", fmt.Errorf("identity authority unavailable")
}
value, err := s.deps.Identities.Get(ctx, id)
if errors.Is(err, identity.ErrNotFound) {
value, err = s.deps.Identities.ResolveLegacyName(ctx, ref)
}
if err != nil {
return "", err
}
if !value.Kind.Agent() || value.Lifecycle != identity.LifecycleActive {
return "", fmt.Errorf("execution identity may not act")
}
return value.ID, nil
}
// TaskPrincipalForCredential resolves the task and its canonical execution principal.
func (c *Client) TaskPrincipalForCredential(ctx context.Context, token string) (*task.Task, identity.IdentityID, error) {
r, err := c.taskGrant.ResolveTaskGrant(ctx, &pb.ResolveTaskGrantRequest{Token: token})
if err != nil {
return nil, "", unmapError(err)
}
t, err := c.TaskByID(ctx, r.TaskId)
return t, identity.IdentityID(r.PrincipalId), err
}