From f25605d3dd0d72cd28600e137d4a67b6731b4c4e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 4 Oct 2026 18:57:19 +1100 Subject: [PATCH] feat(staterpc): every run start is audited by the State Store, or the run credential is withdrawn --- internal/app/statestore/server.go | 6 +- internal/events/events.go | 5 ++ .../staterpc/identity_start_test.go | 70 +++++++++++++++++++ internal/infrastructure/staterpc/server.go | 2 + .../infrastructure/staterpc/task_grants.go | 35 ++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 internal/infrastructure/staterpc/identity_start_test.go diff --git a/internal/app/statestore/server.go b/internal/app/statestore/server.go index bd307f6de..454b2892e 100644 --- a/internal/app/statestore/server.go +++ b/internal/app/statestore/server.go @@ -264,7 +264,11 @@ func (b *server) openStateStore(ctx context.Context) error { // pattern the daemon's wireWebStoreSurfaces uses) and a store that lacks one // degrades that group rather than aborting boot. func (b *server) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { - deps := staterpc.Deps{Tasks: b.st, Log: b.log, Grants: grants} + deps := staterpc.Deps{ + Tasks: b.st, Log: b.log, Grants: grants, + // The same root archied dispatches as (servicekit.IdentityNames). + RootIdentity: identity.StableID(servicekit.IdentityNames(b.cfg)[0]), + } if b.pg != nil { packages := storepkg.Service{ Registry: registry.OCIRegistry{}, diff --git a/internal/events/events.go b/internal/events/events.go index 6ce53f5ec..e7b59a9ce 100644 --- a/internal/events/events.go +++ b/internal/events/events.go @@ -96,6 +96,11 @@ const ( // started its callee run. data: // callee_task_id, workflow, wait. KindWorkflowCallStarted = "workflow_call_started" + // KindIdentityStarted marks a run starting as its identity, stamped by + // the State Store when the run credential is registered: ActorID is the + // service that started it, PrincipalID the identity it acts as. + // data: org. + KindIdentityStarted = "identity_started" // KindWorkflowCallFinished carries the terminal state of a waited-on // callee back onto the caller's timeline. data: callee_task_id, // workflow, status, detail. Only a call the caller waited on records diff --git a/internal/infrastructure/staterpc/identity_start_test.go b/internal/infrastructure/staterpc/identity_start_test.go new file mode 100644 index 000000000..47c769f50 --- /dev/null +++ b/internal/infrastructure/staterpc/identity_start_test.go @@ -0,0 +1,70 @@ +package staterpc + +import ( + "context" + "errors" + "strings" + "testing" + + 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/storecontract" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" +) + +type auditTasks struct { + storecontract.TaskStore + failInsert bool + recorded []events.Event +} + +func (*auditTasks) TaskByID(_ context.Context, id int64) (*task.Task, error) { + return &task.Task{ID: id, Identity: "bot", Workflow: "deploy"}, nil +} + +func (a *auditTasks) InsertEvent(_ context.Context, e events.Event) (int64, error) { + if a.failInsert { + return 0, errors.New("store down") + } + a.recorded = append(a.recorded, e) + return 1, nil +} + +// TestIdentityStartIsAudited pins that a run credential is only issued with +// its identity-start audit written, stamped by the server, and that a start +// whose audit fails leaves no usable credential. +func TestIdentityStartIsAudited(t *testing.T) { + token := strings.Repeat("cd", 32) + tests := []struct { + name string + failInsert bool + }{ + {"audited start issues the credential", false}, + {"unaudited start is undone", true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tasks := &auditTasks{failInsert: tt.failInsert} + grants := &TaskGrants{Store: credentials{}} + s := &server{deps: Deps{Tasks: tasks, Grants: grants, RootIdentity: "root"}} + ctx := access.WithActor(context.Background(), "archied") + _, err := s.RegisterTaskGrant(ctx, &pb.RegisterTaskGrantRequest{TaskId: 7, Token: token, LifetimeSeconds: 60}) + live, _ := grants.taskFor(ctx, token) + if tt.failInsert { + if err == nil || live != 0 { + t.Fatalf("err %v, credential live for task %d", err, live) + } + return + } + if err != nil || live != 7 || len(tasks.recorded) != 1 { + t.Fatalf("err %v live %d events %d", err, live, len(tasks.recorded)) + } + e := tasks.recorded[0] + if e.Kind != events.KindIdentityStarted || e.ActorID != "archied" || e.PrincipalID != string(identity.StableID("bot")) { + t.Fatalf("event %+v", e) + } + }) + } +} diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index 5b00f10ad..6d2305102 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -123,6 +123,8 @@ type Deps struct { // serves and its memberships. Optional: nil answers GetPrincipal with // codes.Unavailable. Principals access.PrincipalSource + // RootIdentity is the identity a task with no identity name runs as. + RootIdentity identity.IdentityID // Policies is the stored policy chain. // Optional: nil answers the access RPCs with codes.Unavailable. The reset // surface is deliberately not an RPC: `archie-state-store access reset` runs on the diff --git a/internal/infrastructure/staterpc/task_grants.go b/internal/infrastructure/staterpc/task_grants.go index acde1f594..dc8163059 100644 --- a/internal/infrastructure/staterpc/task_grants.go +++ b/internal/infrastructure/staterpc/task_grants.go @@ -17,8 +17,10 @@ import ( 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/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" ) // TaskGrants verifies run credentials: the one token a task's container @@ -190,6 +192,12 @@ func (s *server) RegisterTaskGrant(ctx context.Context, r *pb.RegisterTaskGrantR } 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 } @@ -290,3 +298,30 @@ func (c *Client) TaskForCredential(ctx context.Context, token string) (*task.Tas } return c.TaskByID(ctx, id) } + +// 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 := s.deps.RootIdentity + if t.Identity != "" { + principal = identity.StableID(t.Identity) + } + data := map[string]any{} + 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 +}