feat(staterpc): one run credential per task, kept in the State Store, authorizes its store calls and its push

This commit is contained in:
sam committed 2026-10-04 18:32:45 +11:00
1 parent ad13393e6b
commit 319e8f4945
18 files changed
+1809 -1470

No files matched your search

+5 -11
View File
@@ -56,7 +56,6 @@ import (
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/webui"
"github.com/samcharles93/archie-core/internal/worktree"
"github.com/samcharles93/archie-core/internal/worktreerpc"
)
// boot is the mutable wiring state assembled by the run() composition
@@ -99,11 +98,9 @@ type boot struct {
// surface reports. Both are nil when no policy store is wired.
accessChain access.Authorizer
accessLive *infraaccess.Live
// stateStoreGrants issues per-task, scoped State Store credentials for
// agent containers (daemon.StateStoreGrantIssuer), wrapping the same
// *staterpc.Client as stateStore. Nil when the State Store adapter isn't
// the remote gRPC client (never true in production; state_store.go's
// standalone-only compose leaves no other case).
// stateStoreGrants issues each task's run credential
// (daemon.RunCredentialIssuer), wrapping the same *staterpc.Client as
// stateStore, which also verifies a credential for a worktree push.
stateStoreGrants *staterpc.GrantIssuer
stateStoreToken string
controlPlane *controlplane.Client
@@ -173,7 +170,6 @@ type boot struct {
startGateways []func()
trees *worktree.Manager
worktreeGrants *worktreerpc.Grants
identityRunners []*daemon.IdentityRunner
d *daemon.Daemon
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
@@ -565,8 +561,7 @@ func (b *boot) registerNATSRPC() error {
log.Error("nats connection unavailable for task RPC", "err", err)
return err
}
b.worktreeGrants = worktreerpc.NewGrants()
unsubscribe, err := registerTaskRPCServers(coreConn, b.forgeClient, b.trees, b.identityRunners, b.worktreeGrants, log)
unsubscribe, err := registerTaskRPCServers(coreConn, b.forgeClient, b.trees, b.identityRunners, b.stateStoreGrants.Client, log)
if err != nil {
log.Error("task RPC server registration failed", "err", err)
return err
@@ -610,8 +605,7 @@ func (b *boot) buildDaemon() {
Log: log,
Tasks: b.natsClient,
Reactions: b.reactionClient,
WorktreeGrants: b.worktreeGrants,
StateStoreGrants: b.stateStoreGrants,
RunCredentials: b.stateStoreGrants,
ContainerPool: b.containerPool,
KitLauncher: b.kitLauncher,
Identities: b.identityRunners,
+2 -2
View File
@@ -343,7 +343,7 @@ func subscribeAgentEvents(nc *natsio.Conn, bus *events.Bus, log *slog.Logger) (u
// tasks, and one server pair per identity answers that identity's scoped
// subjects so a container-mode task owned by a non-root identity has its RPC
// calls served by its own forge client and worktree manager.
func registerTaskRPCServers(nc *natsio.Conn, forgeClient forge.Forge, trees *worktree.Manager, identities []*daemon.IdentityRunner, grants *worktreerpc.Grants, log *slog.Logger) (unsubscribe func(), err error) {
func registerTaskRPCServers(nc *natsio.Conn, forgeClient forge.Forge, trees *worktree.Manager, identities []*daemon.IdentityRunner, runs worktreerpc.Runs, log *slog.Logger) (unsubscribe func(), err error) {
unsubs := make([]func(), 0, 2+2*len(identities))
unsubAll := func() {
for _, u := range unsubs {
@@ -361,7 +361,7 @@ func registerTaskRPCServers(nc *natsio.Conn, forgeClient forge.Forge, trees *wor
return nil
}
registerTrees := func(mgr *worktree.Manager, identity string) error {
srv := &worktreerpc.Server{Trees: mgr, Grants: grants, Log: log.With("rpc_identity", identity)}
srv := &worktreerpc.Server{Trees: mgr, Runs: runs, Log: log.With("rpc_identity", identity)}
u, err := srv.RegisterFor(nc, identity)
if err != nil {
return fmt.Errorf("register worktreerpc%s: %w", identitySuffix(identity), err)
+27 -4
View File
@@ -111,11 +111,11 @@ func Run(ctx context.Context, options Options) error { //nolint:cyclop // the co
}
b.reportUnseededResources(skipped)
token := options.Token
if token == "" {
token = b.cfg.Services.ResolvedToken(config.ServiceNameState, b.secrets.Getenv)
token := b.instanceToken(options)
grants, err := b.runCredentials(ctx)
if err != nil {
return err
}
grants := &staterpc.TaskGrants{}
//nolint:contextcheck // grpc.StreamServerInterceptor has no context.Context parameter; TaskGrants.StreamInterceptor derives its context from stream.Context() instead
opts, loopback, err := stateStoreServerOpts(listen, token, grants, b.callers())
if err != nil {
@@ -373,6 +373,29 @@ func (b *server) executionDeps(deps *staterpc.Deps) {
}
}
// instanceToken is the bearer token a non-loopback listener requires: the
// flag, else [services.state].target_token.
func (b *server) instanceToken(options Options) string {
if options.Token != "" {
return options.Token
}
return b.cfg.Services.ResolvedToken(config.ServiceNameState, b.secrets.Getenv)
}
// runCredentials builds the run-credential check over this store and drops
// the credentials that expired while nothing revoked them.
func (b *server) runCredentials(ctx context.Context) (*staterpc.TaskGrants, error) {
store, ok := b.st.(storecontract.RunCredentialStore)
if !ok {
return nil, fmt.Errorf("state store does not support run credentials")
}
grants := &staterpc.TaskGrants{Store: store}
if err := grants.Sweep(ctx); err != nil {
return nil, fmt.Errorf("sweep expired run credentials: %w", err)
}
return grants, nil
}
// callers resolves the service and principal of each call against this
// store's principals.
func (b *server) callers() staterpc.Callers {
File diff suppressed because it is too large. Load diff
@@ -26,6 +26,7 @@ const (
StateStoreService_RemoveInstalledPackage_FullMethodName = "/state.v1.StateStoreService/RemoveInstalledPackage"
StateStoreService_RegisterTaskGrant_FullMethodName = "/state.v1.StateStoreService/RegisterTaskGrant"
StateStoreService_RevokeTaskGrant_FullMethodName = "/state.v1.StateStoreService/RevokeTaskGrant"
StateStoreService_ResolveTaskGrant_FullMethodName = "/state.v1.StateStoreService/ResolveTaskGrant"
StateStoreService_ListIdentities_FullMethodName = "/state.v1.StateStoreService/ListIdentities"
StateStoreService_GetIdentity_FullMethodName = "/state.v1.StateStoreService/GetIdentity"
StateStoreService_CreateIdentity_FullMethodName = "/state.v1.StateStoreService/CreateIdentity"
@@ -148,6 +149,7 @@ type StateStoreServiceClient interface {
// Administrative task credential lifecycle. Worker credentials cannot call these.
RegisterTaskGrant(ctx context.Context, in *RegisterTaskGrantRequest, opts ...grpc.CallOption) (*RegisterTaskGrantResponse, error)
RevokeTaskGrant(ctx context.Context, in *RevokeTaskGrantRequest, opts ...grpc.CallOption) (*RevokeTaskGrantResponse, error)
ResolveTaskGrant(ctx context.Context, in *ResolveTaskGrantRequest, opts ...grpc.CallOption) (*ResolveTaskGrantResponse, error)
// Identity domain. Task-scoped grants cannot call these administrative RPCs.
ListIdentities(ctx context.Context, in *ListIdentitiesRequest, opts ...grpc.CallOption) (*ListIdentitiesResponse, error)
GetIdentity(ctx context.Context, in *GetIdentityRequest, opts ...grpc.CallOption) (*GetIdentityResponse, error)
@@ -409,6 +411,16 @@ func (c *stateStoreServiceClient) RevokeTaskGrant(ctx context.Context, in *Revok
return out, nil
}
func (c *stateStoreServiceClient) ResolveTaskGrant(ctx context.Context, in *ResolveTaskGrantRequest, opts ...grpc.CallOption) (*ResolveTaskGrantResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ResolveTaskGrantResponse)
err := c.cc.Invoke(ctx, StateStoreService_ResolveTaskGrant_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *stateStoreServiceClient) ListIdentities(ctx context.Context, in *ListIdentitiesRequest, opts ...grpc.CallOption) (*ListIdentitiesResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ListIdentitiesResponse)
@@ -1420,6 +1432,7 @@ type StateStoreServiceServer interface {
// Administrative task credential lifecycle. Worker credentials cannot call these.
RegisterTaskGrant(context.Context, *RegisterTaskGrantRequest) (*RegisterTaskGrantResponse, error)
RevokeTaskGrant(context.Context, *RevokeTaskGrantRequest) (*RevokeTaskGrantResponse, error)
ResolveTaskGrant(context.Context, *ResolveTaskGrantRequest) (*ResolveTaskGrantResponse, error)
// Identity domain. Task-scoped grants cannot call these administrative RPCs.
ListIdentities(context.Context, *ListIdentitiesRequest) (*ListIdentitiesResponse, error)
GetIdentity(context.Context, *GetIdentityRequest) (*GetIdentityResponse, error)
@@ -1632,6 +1645,9 @@ func (UnimplementedStateStoreServiceServer) RegisterTaskGrant(context.Context, *
func (UnimplementedStateStoreServiceServer) RevokeTaskGrant(context.Context, *RevokeTaskGrantRequest) (*RevokeTaskGrantResponse, error) {
return nil, status.Error(codes.Unimplemented, "method RevokeTaskGrant not implemented")
}
func (UnimplementedStateStoreServiceServer) ResolveTaskGrant(context.Context, *ResolveTaskGrantRequest) (*ResolveTaskGrantResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ResolveTaskGrant not implemented")
}
func (UnimplementedStateStoreServiceServer) ListIdentities(context.Context, *ListIdentitiesRequest) (*ListIdentitiesResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ListIdentities not implemented")
}
@@ -2067,6 +2083,24 @@ func _StateStoreService_RevokeTaskGrant_Handler(srv interface{}, ctx context.Con
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_ResolveTaskGrant_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ResolveTaskGrantRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StateStoreServiceServer).ResolveTaskGrant(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: StateStoreService_ResolveTaskGrant_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StateStoreServiceServer).ResolveTaskGrant(ctx, req.(*ResolveTaskGrantRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_ListIdentities_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ListIdentitiesRequest)
if err := dec(in); err != nil {
@@ -3809,6 +3843,10 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
MethodName: "RevokeTaskGrant",
Handler: _StateStoreService_RevokeTaskGrant_Handler,
},
{
MethodName: "ResolveTaskGrant",
Handler: _StateStoreService_ResolveTaskGrant_Handler,
},
{
MethodName: "ListIdentities",
Handler: _StateStoreService_ListIdentities_Handler,
+37 -68
View File
@@ -50,17 +50,11 @@ type TaskBus interface {
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
}
// WorktreeGrantIssuer gives one container dispatch the narrow authority to
// publish its already-prepared branch. The daemon owns grant lifetime; the
// transport implementation owns token mechanics.
type WorktreeGrantIssuer interface {
Issue(task *workflow.Task) (token string, revoke func(), err error)
}
// StateStoreGrantIssuer issues a State Store credential scoped to one task's
// own Update, Transition and InsertEvent calls.
type StateStoreGrantIssuer interface {
Issue(task *workflow.Task) (token string, revoke func(), err error)
// RunCredentialIssuer issues the one credential a task's container holds: it
// authorizes the task's own State Store calls and the push of its branch.
// It lapses at the task's time limit plus a margin if never revoked.
type RunCredentialIssuer interface {
Issue(task *workflow.Task, limit time.Duration) (token string, revoke func(), err error)
}
// NATSEndpoint is the broker address and credential archied connected with at
@@ -132,13 +126,11 @@ type Daemon struct {
reactionPublisher ReactionPublisher
// Reactions is the review-reaction stream, drained into remediate runs each
// cycle. Nil disables it.
Reactions ReactionSource
WorktreeGrants WorktreeGrantIssuer
// StateStoreGrants issues per-task State Store credentials for container
// env (see containerEnv). Required whenever ConnectedStateStore.URL is
// set -- acquireTaskContainer parks the task rather than fall back to
// forwarding the daemon's own administrative token.
StateStoreGrants StateStoreGrantIssuer
Reactions ReactionSource
// RunCredentials issues each task's run credential (see runCredential).
// Required whenever ConnectedStateStore.URL is set: the task is parked
// rather than handed the daemon's own administrative token.
RunCredentials RunCredentialIssuer
// KitLauncher starts tasks whose agent profile is a Kit. Nil parks them.
KitLauncher KitLauncher
// TaskRunReadyTimeout bounds how long runViaAgent retries a taskrun request
@@ -1331,18 +1323,18 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task) {
d.cleanupTerminalTaskWorktree(ctx, task, trees)
return
}
ctr, revokeStateStoreGrant, ok := d.acquireTaskContainer(ctx, task, repo, workDir, profile.Image)
ctr, credential, revokeCredential, ok := d.acquireTaskContainer(ctx, task, repo, workDir, profile.Image)
if !ok {
return
}
defer d.ContainerPool.Release(ctx, ctr)
defer revokeStateStoreGrant()
defer revokeCredential()
// Run the whole task in archie-agent, which calls back to the daemon for
// store, forge and push operations.
limitCtx, stopLimit := withTaskTimeLimit(ctx, d.configFor(task).Budgets.TaskWallClock.Std())
runCtx, stopWatch := withContainerExit(limitCtx, ctr.Exited())
d.runViaAgent(runCtx, task, repo, profile, nil)
d.runViaAgent(runCtx, task, repo, profile, nil, credential)
stopWatch()
stopLimit()
@@ -1488,7 +1480,7 @@ func (d *Daemon) acquireTaskContainer(
repo config.Repo,
workDir string,
image string,
) (*container.Container, func(), bool) {
) (*container.Container, string, func(), bool) {
park := func(reason string, err error) {
d.Log.Error(reason, "err", err)
d.parkRunningTask(ctx, task.ID, reason+": "+err.Error(), taskstate.ParkTransient)
@@ -1501,7 +1493,7 @@ func (d *Daemon) acquireTaskContainer(
if err := writeTaskBrief(workDir, task); err != nil {
park("task.json write failed", err)
return nil, nil, false
return nil, "", nil, false
}
// Fetch the pull request with the daemon's forge credential into workDir,
@@ -1509,7 +1501,7 @@ func (d *Daemon) acquireTaskContainer(
if task.Workflow == "pr-review" {
if err := prefetchPRReview(ctx, d.forgeFor(task), task.Owner, task.Repo, task.PRNumber, workDir); err != nil {
park("pr-review prefetch failed", err)
return nil, nil, false
return nil, "", nil, false
}
}
@@ -1518,7 +1510,7 @@ func (d *Daemon) acquireTaskContainer(
if d.Storage == nil {
d.Log.Error("storage backend is nil -- cannot acquire container")
d.parkRunningTask(ctx, task.ID, "storage backend not configured", taskstate.ParkTransient)
return nil, nil, false
return nil, "", nil, false
}
mounts, err := d.Storage.Setup(ctx, storage.TaskRef{
@@ -1530,10 +1522,10 @@ func (d *Daemon) acquireTaskContainer(
})
if err != nil {
park("storage setup failed", err)
return nil, nil, false
return nil, "", nil, false
}
stateStoreToken, revokeStateStoreGrant, err := d.stateStoreGrantToken(task)
credential, revokeCredential, err := d.runCredential(task)
if err != nil {
if terr := d.Storage.Teardown(ctx, storage.TaskRef{
WorktreeDir: workDir,
@@ -1542,15 +1534,15 @@ func (d *Daemon) acquireTaskContainer(
Owner: task.Owner,
Repo: task.Repo,
}); terr != nil {
d.Log.Warn("storage teardown after state store grant failure failed", "err", terr)
d.Log.Warn("storage teardown after run credential failure failed", "err", terr)
}
park("state store grant failed", err)
return nil, nil, false
park("run credential failed", err)
return nil, "", nil, false
}
ctr, err := d.ContainerPool.Acquire(ctx, image, mounts, d.containerEnv(task, stateStoreToken))
ctr, err := d.ContainerPool.Acquire(ctx, image, mounts, d.containerEnv(task, credential))
if err != nil {
revokeStateStoreGrant()
revokeCredential()
// Roll back the storage we just set up: the mounts were created
// for this task but no container will use them. Leaking them until
// a later TTL sweep is not acceptable on a backend that allocates
@@ -1565,9 +1557,9 @@ func (d *Daemon) acquireTaskContainer(
d.Log.Warn("storage teardown after acquire failure failed", "err", terr)
}
park("container acquire failed", err)
return nil, nil, false
return nil, "", nil, false
}
return ctr, revokeStateStoreGrant, true
return ctr, credential, revokeCredential, true
}
// writeTaskBrief writes task.json, the container's boot-time brief.
@@ -1643,12 +1635,10 @@ func (d *Daemon) recordPark(ctx context.Context, taskID int64, reason string) {
// runViaAgent hands a task to archie-agent and waits for its result. It parks
// the task only when the agent never answered or failed before recording an
// outcome.
func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo config.Repo, profile config.AgentProfile, harness *agentrun.HarnessSpec) {
grant, revoke, ok := d.publicationGrant(ctx, task)
if !ok {
return
}
defer revoke()
//
// credential is the task's run credential; the agent presents it to push the
// task's branch.
func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo config.Repo, profile config.AgentProfile, harness *agentrun.HarnessSpec, credential string) {
cfg := d.configFor(task)
taskCfg := cfg.ForTask()
d.captureAttemptConfig(ctx, task, taskCfg)
@@ -1658,7 +1648,7 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf
Cfg: taskCfg,
Providers: agentexec.ProvidersFromConfig(cfg.Providers),
MCPServers: cfg.Tools.MCPServers,
WorktreeGrant: grant,
WorktreeGrant: credential,
KindWorkflows: d.KindWorkflows,
LabelWorkflows: d.LabelWorkflows,
WorkflowDefinition: task.WorkflowDefinitionYAML,
@@ -1746,27 +1736,6 @@ func validateProfileMeetsNeeds(profile config.AgentProfile, needs workflowtask.W
return nil
}
// publicationGrant issues the capability to publish the task's branch. A task
// with no repository has nothing to publish and gets none.
func (d *Daemon) publicationGrant(ctx context.Context, task *workflow.Task) (string, func(), bool) {
if !task.HasRepository() {
return "", func() {}, true
}
if d.WorktreeGrants == nil {
const reason = "worktree publication grants are unavailable"
d.Log.Error(reason, "task", task.ID)
d.parkRunningTask(ctx, task.ID, reason, taskstate.ParkTransient)
return "", nil, false
}
grant, revoke, err := d.WorktreeGrants.Issue(task)
if err != nil {
d.Log.Error("worktree publication grant failed", "task", task.ID, "err", err)
d.parkRunningTask(ctx, task.ID, "worktree publication grant failed: "+err.Error(), taskstate.ParkTransient)
return "", nil, false
}
return grant, revoke, true
}
// pinWorkflowDefinition ensures the task carries a valid definition pin for
// the workflow it names. A valid pin is kept; a missing or mismatched one is
// resolved again from the active definitions.
@@ -1989,16 +1958,16 @@ func (d *Daemon) containerEnv(task *workflow.Task, stateStoreToken string) []str
return env
}
// stateStoreGrantToken issues the task's scoped State Store token, or "" when
// no State Store is configured. It fails when no issuer is wired.
func (d *Daemon) stateStoreGrantToken(task *workflow.Task) (string, func(), error) {
// runCredential issues the task's run credential, or "" when no State Store
// is configured. It fails when no issuer is wired.
func (d *Daemon) runCredential(task *workflow.Task) (string, func(), error) {
if d.ConnectedStateStore.URL == "" {
return "", func() {}, nil
}
if d.StateStoreGrants == nil {
return "", nil, fmt.Errorf("state store is configured but no task-scoped grant issuer is wired")
if d.RunCredentials == nil {
return "", nil, fmt.Errorf("state store is configured but no run credential issuer is wired")
}
return d.StateStoreGrants.Issue(task)
return d.RunCredentials.Issue(task, d.configFor(task).Budgets.TaskWallClock.Std())
}
func (d *Daemon) configFor(task *workflow.Task) config.Config {
+5 -5
View File
@@ -38,15 +38,15 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi
park("task.json write failed", err)
return
}
stateStoreToken, revokeStateStoreGrant, err := d.stateStoreGrantToken(task)
credential, revokeCredential, err := d.runCredential(task)
if err != nil {
park("state store grant failed", err)
park("run credential failed", err)
return
}
defer revokeStateStoreGrant()
defer revokeCredential()
env, err := kitrun.WorkerEnv(kitrun.Endpoints{
NATS: d.ConnectedNATS.URL, NATSToken: d.ConnectedNATS.Token,
StateStore: d.ConnectedStateStore.URL, StateStoreToken: stateStoreToken,
StateStore: d.ConnectedStateStore.URL, StateStoreToken: credential,
}, os.Getuid(), os.Getgid())
if err != nil {
park("kit worker environment", err)
@@ -83,7 +83,7 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi
limitCtx, stopLimit := withTaskTimeLimit(ctx, d.configFor(task).Budgets.TaskWallClock.Std())
runCtx, stopWatch := withContainerExit(limitCtx, run.Container.Exited())
d.runViaAgent(runCtx, task, repo, profile, &run.Harness)
d.runViaAgent(runCtx, task, repo, profile, &run.Harness, credential)
stopWatch()
stopLimit()
@@ -518,3 +518,17 @@ type ResourceWrite struct {
ExpectedVersion int64
At time.Time
}
// ErrRunCredentialUnknown is a run credential that was never issued, was
// revoked, or has expired.
var ErrRunCredentialUnknown = errors.New("run credential unknown or expired")
// RunCredentialStore keeps run credentials by digest: the SHA-256 of the
// token a task's container presents. A lookup of an unknown or expired
// digest reports ErrRunCredentialUnknown.
type RunCredentialStore interface {
PutRunCredential(ctx context.Context, digest [32]byte, taskID int64, expires time.Time) error
DeleteRunCredential(ctx context.Context, digest [32]byte) error
RunCredentialTask(ctx context.Context, digest [32]byte, now time.Time) (int64, error)
DeleteExpiredRunCredentials(ctx context.Context, now time.Time) error
}
@@ -0,0 +1,15 @@
-- A run credential authorizes one task's container: its State Store calls
-- and its branch push. Only the SHA-256 of the token is kept, so the table
-- cannot be replayed, and a row outlives a restart of either service.
-- +goose Up
CREATE TABLE run_credentials (
digest bytea PRIMARY KEY,
task_id bigint NOT NULL,
expires_at timestamptz NOT NULL
);
-- +goose Down
DROP TABLE run_credentials;
@@ -316,6 +316,12 @@ type ResourceHistory struct {
OrgID string
}
type RunCredential struct {
Digest []byte
TaskID int64
ExpiresAt time.Time
}
type Session struct {
SessionID string
Platform string
@@ -0,0 +1,60 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: run_credentials.sql
package postgresdb
import (
"context"
"time"
)
const deleteExpiredRunCredentials = `-- name: DeleteExpiredRunCredentials :exec
DELETE FROM run_credentials WHERE expires_at <= $1
`
func (q *Queries) DeleteExpiredRunCredentials(ctx context.Context, expiresAt time.Time) error {
_, err := q.db.Exec(ctx, deleteExpiredRunCredentials, expiresAt)
return err
}
const deleteRunCredential = `-- name: DeleteRunCredential :exec
DELETE FROM run_credentials WHERE digest = $1
`
func (q *Queries) DeleteRunCredential(ctx context.Context, digest []byte) error {
_, err := q.db.Exec(ctx, deleteRunCredential, digest)
return err
}
const insertRunCredential = `-- name: InsertRunCredential :exec
INSERT INTO run_credentials (digest, task_id, expires_at) VALUES ($1, $2, $3)
`
type InsertRunCredentialParams struct {
Digest []byte
TaskID int64
ExpiresAt time.Time
}
func (q *Queries) InsertRunCredential(ctx context.Context, arg InsertRunCredentialParams) error {
_, err := q.db.Exec(ctx, insertRunCredential, arg.Digest, arg.TaskID, arg.ExpiresAt)
return err
}
const runCredentialTask = `-- name: RunCredentialTask :one
SELECT task_id FROM run_credentials WHERE digest = $1 AND expires_at > $2
`
type RunCredentialTaskParams struct {
Digest []byte
ExpiresAt time.Time
}
func (q *Queries) RunCredentialTask(ctx context.Context, arg RunCredentialTaskParams) (int64, error) {
row := q.db.QueryRow(ctx, runCredentialTask, arg.Digest, arg.ExpiresAt)
var task_id int64
err := row.Scan(&task_id)
return task_id, err
}
@@ -0,0 +1,11 @@
-- name: InsertRunCredential :exec
INSERT INTO run_credentials (digest, task_id, expires_at) VALUES ($1, $2, $3);
-- name: DeleteRunCredential :exec
DELETE FROM run_credentials WHERE digest = $1;
-- name: RunCredentialTask :one
SELECT task_id FROM run_credentials WHERE digest = $1 AND expires_at > $2;
-- name: DeleteExpiredRunCredentials :exec
DELETE FROM run_credentials WHERE expires_at <= $1;
@@ -0,0 +1,34 @@
package postgres
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
var _ storecontract.RunCredentialStore = (*Store)(nil)
func (s *Store) PutRunCredential(ctx context.Context, digest [32]byte, taskID int64, expires time.Time) error {
return s.queries().InsertRunCredential(ctx, postgresdb.InsertRunCredentialParams{Digest: digest[:], TaskID: taskID, ExpiresAt: expires})
}
func (s *Store) DeleteRunCredential(ctx context.Context, digest [32]byte) error {
return s.queries().DeleteRunCredential(ctx, digest[:])
}
func (s *Store) RunCredentialTask(ctx context.Context, digest [32]byte, now time.Time) (int64, error) {
id, err := s.queries().RunCredentialTask(ctx, postgresdb.RunCredentialTaskParams{Digest: digest[:], ExpiresAt: now})
if errors.Is(err, pgx.ErrNoRows) {
return 0, storecontract.ErrRunCredentialUnknown
}
return id, err
}
func (s *Store) DeleteExpiredRunCredentials(ctx context.Context, now time.Time) error {
return s.queries().DeleteExpiredRunCredentials(ctx, now)
}
@@ -4,6 +4,7 @@ import (
"context"
"strings"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
@@ -12,8 +13,31 @@ import (
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// credentials is an in-memory RunCredentialStore.
type credentials map[[32]byte]int64
func (c credentials) PutRunCredential(_ context.Context, d [32]byte, id int64, _ time.Time) error {
c[d] = id
return nil
}
func (c credentials) DeleteRunCredential(_ context.Context, d [32]byte) error {
delete(c, d)
return nil
}
func (c credentials) RunCredentialTask(_ context.Context, d [32]byte, _ time.Time) (int64, error) {
if id, ok := c[d]; ok {
return id, nil
}
return 0, storecontract.ErrRunCredentialUnknown
}
func (credentials) DeleteExpiredRunCredentials(context.Context, time.Time) error { return nil }
type principalsByID map[identity.IdentityID]org.OrgID
func (p principalsByID) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
@@ -25,8 +49,8 @@ func (p principalsByID) PrincipalFor(_ context.Context, id identity.IdentityID)
func TestCallerAttribution(t *testing.T) {
const admin = "admin-token"
taskToken := strings.Repeat("ab", 32)
grants := &TaskGrants{}
if err := grants.register(7, taskToken, 3600e9); err != nil {
grants := &TaskGrants{Store: credentials{}}
if err := grants.register(context.Background(), 7, taskToken, time.Hour); err != nil {
t.Fatal(err)
}
callers := Callers{Principals: principalsByID{"alice": "acme"}}
+92 -63
View File
@@ -6,8 +6,8 @@ import (
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"errors"
"fmt"
"sync"
"time"
"google.golang.org/grpc"
@@ -17,52 +17,40 @@ 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/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
// TaskGrants lives in the State Store process. Restart invalidates all worker
// grants; administrative credentials are never sent to workers.
// 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 {
mu sync.Mutex
active map[int64]taskGrant
Store storecontract.RunCredentialStore
}
type taskGrant struct {
digest [32]byte
expires time.Time
}
// maxGrantLifetime caps any requested lifetime.
const maxGrantLifetime = 7 * 24 * time.Hour
func (g *TaskGrants) register(taskID int64, token string, lifetime time.Duration) error {
if taskID <= 0 || len(token) != 64 || lifetime <= 0 || lifetime > 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")
}
g.mu.Lock()
defer g.mu.Unlock()
if g.active == nil {
g.active = make(map[int64]taskGrant)
}
now := time.Now()
for id, grant := range g.active {
if !now.Before(grant.expires) {
delete(g.active, id)
}
}
g.active[taskID] = taskGrant{digest: sha256.Sum256([]byte(token)), expires: now.Add(lifetime)}
return nil
return g.Store.PutRunCredential(ctx, sha256.Sum256([]byte(token)), taskID, time.Now().Add(lifetime))
}
func (g *TaskGrants) revoke(token string) {
digest := sha256.Sum256([]byte(token))
g.mu.Lock()
defer g.mu.Unlock()
for id, grant := range g.active {
if grant.digest == digest {
delete(g.active, id)
}
}
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.
@@ -76,20 +64,15 @@ func requestToken(ctx context.Context) string {
// 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(token string) int64 {
func (g *TaskGrants) taskFor(ctx context.Context, token string) (int64, error) {
if token == "" {
return 0
return 0, nil
}
digest := sha256.Sum256([]byte(token))
g.mu.Lock()
defer g.mu.Unlock()
now := time.Now()
for id, grant := range g.active {
if grant.digest == digest && now.Before(grant.expires) {
return id
}
id, err := g.Store.RunCredentialTask(ctx, sha256.Sum256([]byte(token)), time.Now())
if errors.Is(err, storecontract.ErrRunCredentialUnknown) {
return 0, nil
}
return 0
return id, err
}
// authorizesTaskScopedCall reports whether a task-scoped token may make this
@@ -165,7 +148,10 @@ func (g *TaskGrants) UnaryInterceptor(adminToken string) grpc.UnaryServerInterce
if token == "" {
return nil, status.Error(codes.Unauthenticated, "missing state store token")
}
taskID := g.taskFor(token)
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 state store token")
}
@@ -194,27 +180,46 @@ func (g *TaskGrants) StreamInterceptor(adminToken string) grpc.StreamServerInter
}
}
func (s *server) RegisterTaskGrant(_ context.Context, r *pb.RegisterTaskGrantRequest) (*pb.RegisterTaskGrantResponse, error) {
func (s *server) 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 r.LifetimeSeconds <= 0 || r.LifetimeSeconds > 7*24*60*60 {
return nil, status.Error(codes.InvalidArgument, "invalid task grant lifetime")
}
if err := s.deps.Grants.register(r.TaskId, r.Token, time.Duration(r.LifetimeSeconds)*time.Second); err != nil {
return nil, err
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")
}
return &pb.RegisterTaskGrantResponse{}, nil
}
func (s *server) RevokeTaskGrant(_ context.Context, r *pb.RevokeTaskGrantRequest) (*pb.RevokeTaskGrantResponse, error) {
func (s *server) RevokeTaskGrant(ctx context.Context, r *pb.RevokeTaskGrantRequest) (*pb.RevokeTaskGrantResponse, error) {
if s.deps.Grants == nil {
return nil, status.Error(codes.Unavailable, "task grants unavailable")
}
s.deps.Grants.revoke(r.Token)
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 *server) 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")
}
return &pb.ResolveTaskGrantResponse{TaskId: id}, 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) {
@@ -235,23 +240,38 @@ func (c *Client) RevokeTaskGrant(ctx context.Context, token string) error {
return unmapError(err)
}
// defaultGrantLifetime bounds how long an unrevoked grant stays valid if the
// daemon crashes before its deferred revoke runs. It is generous relative to
// any realistic task run, not a task timeout -- the server's own register
// additionally caps any requested lifetime at 7 days.
const defaultGrantLifetime = 24 * time.Hour
// ResolveTaskGrant returns the task a run credential was issued for.
func (c *Client) ResolveTaskGrant(ctx context.Context, token string) (int64, error) {
r, err := c.client.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
}
// GrantIssuer implements daemon.StateStoreGrantIssuer over a *Client,
// registering a fresh per-task credential at the remote State Store before a
// container starts and revoking it when the daemon releases the container.
// 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) (string, func(), error) {
func (g *GrantIssuer) Issue(task *task.Task, limit time.Duration) (string, func(), error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
token, err := g.Client.RegisterTaskGrant(ctx, task.ID, defaultGrantLifetime)
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
}
@@ -261,3 +281,12 @@ func (g *GrantIssuer) Issue(task *task.Task) (string, func(), error) {
_ = 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) {
id, err := c.ResolveTaskGrant(ctx, token)
if err != nil {
return nil, err
}
return c.TaskByID(ctx, id)
}
+23 -52
View File
@@ -4,13 +4,11 @@ package worktreerpc
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"log/slog"
"strings"
"sync"
"time"
"github.com/nats-io/nats.go"
@@ -41,54 +39,27 @@ type Response struct {
natsrpc.Envelope
}
type grant struct {
identity string
owner, repo string
issue int
branch string
// Runs resolves a run credential to the task it was issued for. The State
// Store answers it, so a push survives a restart of archied mid-task.
type Runs interface {
TaskForCredential(ctx context.Context, token string) (*workflow.Task, error)
}
// Grants owns short-lived publication capabilities issued for one dispatched
// task. A grant is useful only on the identity-scoped server that issued it.
type Grants struct {
mu sync.RWMutex
grants map[string]grant
}
func NewGrants() *Grants { return &Grants{grants: make(map[string]grant)} }
// Issue creates a per-dispatch grant and a revocation function.
func (g *Grants) Issue(task *workflow.Task) (string, func(), error) {
if task == nil || task.ID <= 0 || task.Branch == "" ||
// publishable returns the task a credential authorizes this identity's
// server to push for.
func publishable(ctx context.Context, runs Runs, token, identity string) (*workflow.Task, error) {
if runs == nil || token == "" {
return nil, errors.New("worktree publication grant is required")
}
task, err := runs.TaskForCredential(ctx, token)
if err != nil {
return nil, fmt.Errorf("worktree publication grant is invalid: %w", err)
}
if task.Identity != identity || task.Branch == "" ||
!worktree.ValidCoordinates(task.Owner, task.Repo, task.IssueNumber) {
return "", nil, errors.New("task is not ready for worktree publication")
return nil, errors.New("worktree publication grant is invalid")
}
raw := make([]byte, 32)
if _, err := rand.Read(raw); err != nil {
return "", nil, fmt.Errorf("generate worktree grant: %w", err)
}
token := fmt.Sprintf("%x", raw)
g.mu.Lock()
g.grants[token] = grant{identity: task.Identity, owner: task.Owner, repo: task.Repo, issue: task.IssueNumber, branch: task.Branch}
g.mu.Unlock()
return token, func() {
g.mu.Lock()
delete(g.grants, token)
g.mu.Unlock()
}, nil
}
func (g *Grants) resolve(token, identity string) (grant, error) {
if g == nil || token == "" {
return grant{}, errors.New("worktree publication grant is required")
}
g.mu.RLock()
item, ok := g.grants[token]
g.mu.RUnlock()
if !ok || item.identity != identity {
return grant{}, errors.New("worktree publication grant is invalid")
}
return item, nil
return task, nil
}
// defaultHandlerTimeout bounds one publication request when the Server has no
@@ -98,9 +69,9 @@ const defaultHandlerTimeout = 15 * time.Minute
// Server proxies push requests to a real worktree.Manager holding the
// forge push token.
type Server struct {
Trees *worktree.Manager
Grants *Grants
Log *slog.Logger
Trees *worktree.Manager
Runs Runs
Log *slog.Logger
// Timeout bounds a single publication. Zero uses
// defaultHandlerTimeout.
Timeout time.Duration
@@ -136,13 +107,13 @@ func (s *Server) handlePush(identity string, msg *nats.Msg) {
}
ctx, cancel := s.handlerContext()
defer cancel()
item, err := s.Grants.resolve(req.Grant, identity)
task, err := publishable(ctx, s.Runs, req.Grant, identity)
if err != nil {
s.respond(msg, err)
return
}
dir := s.Trees.Dir(item.owner, item.repo, item.issue)
s.respond(msg, s.Trees.Push(ctx, dir, item.branch))
dir := s.Trees.Dir(task.Owner, task.Repo, task.IssueNumber)
s.respond(msg, s.Trees.Push(ctx, dir, task.Branch))
}
func (s *Server) respond(msg *nats.Msg, err error) {
+47
View File
@@ -0,0 +1,47 @@
package worktreerpc
import (
"context"
"errors"
"testing"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
type runsByToken map[string]*workflow.Task
func (r runsByToken) TaskForCredential(_ context.Context, token string) (*workflow.Task, error) {
if task, ok := r[token]; ok {
return task, nil
}
return nil, errors.New("unknown")
}
// TestPushNeedsTheRunsOwnCredential pins that a push is authorized only by a
// live run credential, only on the server of the run's identity, and only for
// that run's branch.
func TestPushNeedsTheRunsOwnCredential(t *testing.T) {
runs := runsByToken{
"live": {ID: 1, Identity: "bot", Owner: "o", Repo: "r", IssueNumber: 3, Branch: "archie/3"},
}
tests := []struct {
name, token, identity string
allowed bool
}{
{"own run on own server", "live", "bot", true},
{"unknown credential", "gone", "bot", false},
{"another identity's server", "live", "other", false},
{"no credential", "", "bot", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
task, err := publishable(context.Background(), runs, tt.token, tt.identity)
if (err == nil) != tt.allowed {
t.Fatalf("err %v, want allowed %v", err, tt.allowed)
}
if tt.allowed && task.Branch != "archie/3" {
t.Fatalf("pushed branch %q", task.Branch)
}
})
}
}
+5
View File
@@ -24,6 +24,7 @@ service StateStoreService {
// Administrative task credential lifecycle. Worker credentials cannot call these.
rpc RegisterTaskGrant(RegisterTaskGrantRequest) returns (RegisterTaskGrantResponse);
rpc RevokeTaskGrant(RevokeTaskGrantRequest) returns (RevokeTaskGrantResponse);
rpc ResolveTaskGrant(ResolveTaskGrantRequest) returns (ResolveTaskGrantResponse);
// Identity domain. Task-scoped grants cannot call these administrative RPCs.
rpc ListIdentities(ListIdentitiesRequest) returns (ListIdentitiesResponse);
rpc GetIdentity(GetIdentityRequest) returns (GetIdentityResponse);
@@ -234,6 +235,10 @@ message RevokeTaskGrantRequest {
string token = 1;
}
message RevokeTaskGrantResponse {}
// ResolveTaskGrantRequest names a run credential; the response is the task
// it was issued for.
message ResolveTaskGrantRequest { string token = 1; }
message ResolveTaskGrantResponse { int64 task_id = 1; }
message Identity {
string id = 1;