fix(staterpc): a task's own credential cannot replace its workflow pin
The pinned definition carries the package a run's authority is read from. A task-scoped Update now reaches the store without a definition, and UpdateTask keeps the stored pin when none is sent; the workflow name still moves for handoffs and the daemon re-pins it.
This commit is contained in:
4 files changed
+100
-20
No files matched your search
@@ -1177,18 +1177,19 @@ func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updateTask = `-- name: UpdateTask :exec
|
const updateTask = `-- name: UpdateTask :exec
|
||||||
UPDATE tasks SET workflow = $2, branch = $3, plan = $4, notes = $5,
|
UPDATE tasks SET workflow = $1, branch = $2, plan = $3, notes = $4,
|
||||||
pr_number = $6, tokens_used = $7, iterations = $8, park_reason = $9,
|
pr_number = $5, tokens_used = $6, iterations = $7, park_reason = $8,
|
||||||
watch_comment_id = $10, retry_count = $11, remediation_rounds = $12,
|
watch_comment_id = $9, retry_count = $10, remediation_rounds = $11,
|
||||||
review_payload = $13, workflow_definition_version = $14,
|
review_payload = $12,
|
||||||
workflow_definition_digest = $15, workflow_definition_yaml = $16,
|
workflow_definition_version = CASE WHEN $13::text = '' THEN workflow_definition_version ELSE $14 END,
|
||||||
outputs = $17, review_gate = $18, rereview_rounds = $19,
|
workflow_definition_digest = CASE WHEN $13::text = '' THEN workflow_definition_digest ELSE $15 END,
|
||||||
retry_mode = $20, updated_at = now()
|
workflow_definition_yaml = CASE WHEN $13::text = '' THEN workflow_definition_yaml ELSE $13::text END,
|
||||||
WHERE id = $1
|
outputs = $16, review_gate = $17, rereview_rounds = $18,
|
||||||
|
retry_mode = $19, updated_at = now()
|
||||||
|
WHERE id = $20
|
||||||
`
|
`
|
||||||
|
|
||||||
type UpdateTaskParams struct {
|
type UpdateTaskParams struct {
|
||||||
ID int64
|
|
||||||
Workflow string
|
Workflow string
|
||||||
Branch string
|
Branch string
|
||||||
Plan string
|
Plan string
|
||||||
@@ -1201,18 +1202,20 @@ type UpdateTaskParams struct {
|
|||||||
RetryCount int64
|
RetryCount int64
|
||||||
RemediationRounds int64
|
RemediationRounds int64
|
||||||
ReviewPayload string
|
ReviewPayload string
|
||||||
|
WorkflowDefinitionYaml string
|
||||||
WorkflowDefinitionVersion int64
|
WorkflowDefinitionVersion int64
|
||||||
WorkflowDefinitionDigest string
|
WorkflowDefinitionDigest string
|
||||||
WorkflowDefinitionYaml string
|
|
||||||
Outputs string
|
Outputs string
|
||||||
ReviewGate string
|
ReviewGate string
|
||||||
RereviewRounds int64
|
RereviewRounds int64
|
||||||
RetryMode string
|
RetryMode string
|
||||||
|
ID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// An update without a definition keeps the stored pin: a task's own
|
||||||
|
// credential sends none, so it cannot replace the definition it runs.
|
||||||
func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
|
func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
|
||||||
_, err := q.db.Exec(ctx, updateTask,
|
_, err := q.db.Exec(ctx, updateTask,
|
||||||
arg.ID,
|
|
||||||
arg.Workflow,
|
arg.Workflow,
|
||||||
arg.Branch,
|
arg.Branch,
|
||||||
arg.Plan,
|
arg.Plan,
|
||||||
@@ -1225,13 +1228,14 @@ func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
|
|||||||
arg.RetryCount,
|
arg.RetryCount,
|
||||||
arg.RemediationRounds,
|
arg.RemediationRounds,
|
||||||
arg.ReviewPayload,
|
arg.ReviewPayload,
|
||||||
|
arg.WorkflowDefinitionYaml,
|
||||||
arg.WorkflowDefinitionVersion,
|
arg.WorkflowDefinitionVersion,
|
||||||
arg.WorkflowDefinitionDigest,
|
arg.WorkflowDefinitionDigest,
|
||||||
arg.WorkflowDefinitionYaml,
|
|
||||||
arg.Outputs,
|
arg.Outputs,
|
||||||
arg.ReviewGate,
|
arg.ReviewGate,
|
||||||
arg.RereviewRounds,
|
arg.RereviewRounds,
|
||||||
arg.RetryMode,
|
arg.RetryMode,
|
||||||
|
arg.ID,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -97,14 +97,18 @@ ORDER BY id;
|
|||||||
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1;
|
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1;
|
||||||
|
|
||||||
-- name: UpdateTask :exec
|
-- name: UpdateTask :exec
|
||||||
UPDATE tasks SET workflow = $2, branch = $3, plan = $4, notes = $5,
|
-- An update without a definition keeps the stored pin: a task's own
|
||||||
pr_number = $6, tokens_used = $7, iterations = $8, park_reason = $9,
|
-- credential sends none, so it cannot replace the definition it runs.
|
||||||
watch_comment_id = $10, retry_count = $11, remediation_rounds = $12,
|
UPDATE tasks SET workflow = @workflow, branch = @branch, plan = @plan, notes = @notes,
|
||||||
review_payload = $13, workflow_definition_version = $14,
|
pr_number = @pr_number, tokens_used = @tokens_used, iterations = @iterations, park_reason = @park_reason,
|
||||||
workflow_definition_digest = $15, workflow_definition_yaml = $16,
|
watch_comment_id = @watch_comment_id, retry_count = @retry_count, remediation_rounds = @remediation_rounds,
|
||||||
outputs = $17, review_gate = $18, rereview_rounds = $19,
|
review_payload = @review_payload,
|
||||||
retry_mode = $20, updated_at = now()
|
workflow_definition_version = CASE WHEN @workflow_definition_yaml::text = '' THEN workflow_definition_version ELSE @workflow_definition_version END,
|
||||||
WHERE id = $1;
|
workflow_definition_digest = CASE WHEN @workflow_definition_yaml::text = '' THEN workflow_definition_digest ELSE @workflow_definition_digest END,
|
||||||
|
workflow_definition_yaml = CASE WHEN @workflow_definition_yaml::text = '' THEN workflow_definition_yaml ELSE @workflow_definition_yaml::text END,
|
||||||
|
outputs = @outputs, review_gate = @review_gate, rereview_rounds = @rereview_rounds,
|
||||||
|
retry_mode = @retry_mode, updated_at = now()
|
||||||
|
WHERE id = @id;
|
||||||
|
|
||||||
-- name: RereviewRounds :one
|
-- name: RereviewRounds :one
|
||||||
-- Read under the lock LockTaskStatus takes in the same transaction, so the
|
-- Read under the lock LockTaskStatus takes in the same transaction, so the
|
||||||
|
|||||||
@@ -183,10 +183,25 @@ func (g *TaskGrants) UnaryInterceptor(adminToken string) grpc.UnaryServerInterce
|
|||||||
// A container's metadata is its own claim: drop it so no later
|
// A container's metadata is its own claim: drop it so no later
|
||||||
// interceptor attributes the call to a service or principal.
|
// interceptor attributes the call to a service or principal.
|
||||||
ctx = metadata.NewIncomingContext(ctx, metadata.MD{})
|
ctx = metadata.NewIncomingContext(ctx, metadata.MD{})
|
||||||
|
dropDefinitionPin(req)
|
||||||
return handler(access.WithActor(ctx, fmt.Sprintf("task/%d", taskID)), 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 administrators to store read streams.
|
// StreamInterceptor admits only administrators to store read streams.
|
||||||
func (g *TaskGrants) StreamInterceptor(adminToken string) grpc.StreamServerInterceptor {
|
func (g *TaskGrants) StreamInterceptor(adminToken string) grpc.StreamServerInterceptor {
|
||||||
return func(srv any, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
return func(srv any, stream grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package staterpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
|
||||||
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestTaskCredentialCannotReplaceItsPin pins that an Update made with a
|
||||||
|
// task's own credential reaches the store without a workflow definition, so
|
||||||
|
// the stored pin (where the run's package authority is read from) stays. The
|
||||||
|
// workflow name still moves for a handoff; the daemon's credential re-pins.
|
||||||
|
func TestTaskCredentialCannotReplaceItsPin(t *testing.T) {
|
||||||
|
taskToken := strings.Repeat("ab", 32)
|
||||||
|
grants := &TaskGrants{Store: credentials{sha256.Sum256([]byte(taskToken)): 7}}
|
||||||
|
intercept := grants.UnaryInterceptor("admin")
|
||||||
|
tests := []struct {
|
||||||
|
name, token, wantYAML string
|
||||||
|
}{
|
||||||
|
{"task credential", taskToken, ""},
|
||||||
|
{"daemon credential", "admin", "yaml-b"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
ctx := metadata.NewIncomingContext(context.Background(), metadata.Pairs(tokenMetadataKey, tt.token))
|
||||||
|
req := &pb.UpdateRequest{Task: &pb.Task{
|
||||||
|
Id: 7, Workflow: "handoff-target",
|
||||||
|
WorkflowDefinitionVersion: 4, WorkflowDefinitionDigest: "digest-b", WorkflowDefinitionYaml: "yaml-b",
|
||||||
|
}}
|
||||||
|
var got *pb.Task
|
||||||
|
_, err := intercept(ctx, req, &grpc.UnaryServerInfo{FullMethod: pb.StateStoreService_Update_FullMethodName},
|
||||||
|
func(_ context.Context, r any) (any, error) {
|
||||||
|
update, ok := r.(*pb.UpdateRequest)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("handler got %T", r)
|
||||||
|
}
|
||||||
|
got = update.Task
|
||||||
|
return &pb.UpdateResponse{}, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got.WorkflowDefinitionYaml != tt.wantYAML || (tt.wantYAML == "" && got.WorkflowDefinitionDigest != "") {
|
||||||
|
t.Fatalf("definition reaching the store = %q/%q, want %q", got.WorkflowDefinitionYaml, got.WorkflowDefinitionDigest, tt.wantYAML)
|
||||||
|
}
|
||||||
|
if got.Workflow != "handoff-target" {
|
||||||
|
t.Fatalf("workflow = %q, want handoff-target", got.Workflow)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user