feat(staterpc): every run start is audited by the State Store, or the run credential is withdrawn

This commit is contained in:
sam committed 2026-10-04 18:57:19 +11:00
1 parent 7f1aeb1927
commit c28b0adbc2
5 files changed
+117 -1

No files matched your search

+5 -1
View File
@@ -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{},
+5
View File
@@ -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
@@ -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)
}
})
}
}
@@ -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
@@ -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
}