Repository navigation
feat: record run owners - #2
Conversation
Add an owner and an auth context to runs (migration 000009) and scope repository reads to the owner carried in the request context (new package internal/ownership). New runs are stamped with that owner; Get, GetStatus and List on runs and Get, GetByTESID, List and ListByRunID on tasks return only that owner's runs and their tasks. Runs without an owner never match an owner. Nothing sets an owner yet, so behaviour is unchanged: an unscoped context sees everything, as internal callers keep doing. Token validation (next slice) will scope API requests. Add an integration CI job that runs the PostgreSQL repository tests and rolls the newest migration down and up again. Part of elixir-cloud-aai/generic-issues#18. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
- TES CreateTask answers 404 when the run in conquest_run_id can't be read (missing, or hidden by the owner scope) instead of attaching the task to it; other store errors give 500. - Owner filters append to the query's arguments and number their placeholder from them, instead of hand-counted numbers. - Run.Owner and Run.AuthContext are never serialised. - Integration tests cancel their tasks, so that they stay out of the dispatch queue; cover paged ListByRunID and an owner without runs. - README: scoping covers reads only; runnable integration test snippet. Part of elixir-cloud-aai/generic-issues#18. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Part of elixir-cloud-aai/generic-issues#18. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Reviewer's GuideSchema and migrationsAdd nullable run owner and authentication metadata columns, plus an owner-oriented index, so runs can be persisted and queried by ownership.
Data structuresAdd non-serialized owner and authentication context fields to the run model, and define the context value used to carry caller ownership.
APIMake TES task creation reject a tagged run that is missing, unreadable under the caller scope, or unavailable due to a store error.
ImplementationAdd shared owner predicates for run and task queries, including argument-safe placeholder numbering and owner-based task-to-run matching.
Stamp newly created runs only from scoped context, persist and scan ownership metadata, and apply ownership filters to run reads and paginated listing while decoding cursors as timestamps.
Apply ownership predicates to task lookups and list queries, including run-specific task pagination.
Define the repository error returned when a scoped run creation has an empty owner ID.
ConfigurationRun PostgreSQL integration tests in CI, verify the latest migration can roll back and reapply, and require the integration job for the CI success gate.
DocumentationDocument owner-scoped run behavior and explain how to run the PostgreSQL integration tests locally.
TestsTest owner context behavior, PostgreSQL owner filters and empty-owner rejection, and TES handling of readable, unreadable, and failed run lookups.
Exercise owner stamping, scoped and unscoped repository reads, and owner-specific pagination against PostgreSQL; also verify cursor parsing and malformed tokens.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
List and ListByRunID compared created_at (timestamptz) with the token's Unix nanoseconds as an integer, which pgx can't encode: every second page of runs or tasks failed with 500. parseCursor now returns a time.Time. Found by the new integration tests; unrelated to run owners. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="internal/repository/pg/runs.go" line_range="36-41" />
<code_context>
+// and run.Owner and run.AuthContext are set to what was stored. An empty
+// owner ID is stored as NULL: such a run belongs to nobody.
func (s *RunStore) Create(ctx context.Context, run *domain.Run) error {
+ if owner, ok := ownership.FromContext(ctx); ok {
+ run.Owner, run.AuthContext = owner.ID, owner.Context
+ }
+ var owner *string
+ if run.Owner != "" {
+ owner = &run.Owner
+ }
wsKind, wsClaim, wsAccess, wsPV := flattenWorkspaceBinding(run.Workspace)
</code_context>
<issue_to_address>
**Unscoped runs can be assigned owners**
When an unscoped repository caller supplies a run whose `Owner` is non-empty, an unscoped caller that passes a run with a non-empty `run.Owner` leaves that value intact, and `Create` persists it. The run then appears to belong to that owner despite having been created without an owner context.
Clear owner and auth context for unscoped creates, or reject pre-populated ownership unless it came from the scoped context.
</issue_to_address>
### Comment 2
<location path="internal/repository/pg/runs.go" line_range="40" />
<code_context>
+ run.Owner, run.AuthContext = owner.ID, owner.Context
+ }
+ var owner *string
+ if run.Owner != "" {
+ owner = &run.Owner
+ }
</code_context>
<issue_to_address>
**Empty owner creates inaccessible runs**
When a scoped context is created with an empty owner ID, such as when the authentication layer receives a missing identity claim, `RunStore.Create` stores an empty scoped owner as SQL `NULL`, while `runOwnerFilter` later compares `owner = ''`; the insert succeeds but the creating caller cannot read the run, so the run becomes inaccessible to that caller.
Reject creation with a scoped empty owner ID before inserting the run.
Also at `internal/repository/pg/ownership.go:28-29`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and when a request context carries an owner, the new repository filters determine which runs and tasks that caller can read, and the TES run-tag check relies on that result; a missed or incorrect filter could expose another owner’s records. Reverting removes the behavior but cannot undo data already exposed, though the README notes that no application path currently sets an owner.
Blocking findings: internal/repository/pg/runs.go:41, internal/repository/pg/runs.go:40
Create ignores an Owner the caller set on the run and refuses a context scoped to an empty owner ID (ErrEmptyOwner), whose caller could never read the run. Addresses Sourcery review findings. Part of elixir-cloud-aai/generic-issues#18. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Keep both integration jobs (PostgreSQL from #2, storage) and both README paragraphs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Summary
Slice S2 of the Conquest auth design (generic-issues#18): runs record their owner, and repository reads are scoped to the owner carried in the request context. Nothing sets an owner yet, so behaviour is unchanged until token validation (next slice, S3) is switched on.
migrations/000009_run_owner:runs.owner TEXT NULL,runs.auth_context JSONB NULL(identifiers only, never tokens), partial index(owner, created_at DESC, run_id DESC). Tasks get no column: they belong to their run.internal/ownership: anOwnerincontext.Context(NewContext,FromContext). No change to the repository interfaces, the controller or the handlers' signatures, to keep the edit to upstream code small (D-406).internal/repository/pg: in a scoped context,Createstamps the owner;Get,GetStatus,Liston runs andGet,GetByTESID,List,ListByRunIDon tasks add an owner predicate (ownership.go). NULL owners never match, so runs from before auth belong to nobody (D-414). Unscoped contexts (auth off, reconcilers, dispatcher, runner callback routes) see everything, as today. Writes stay unscoped: callers read through a scoped query first.internal/api/tes: publicCreateTaskwith tagconquest_run_idnow answers 404 when that run can't be read (before: the read error was ignored, and the task was attached to the run anyway, or failed later on the foreign key with 500). With owners in place this closes F3 of the design (task injection into someone else's run).integration("Integration tests") with a PostgreSQL 16 service:migrate up,down 1,up, thengo test -tags integration ./internal/repository/pg/(the existing dispatch integration test now runs in CI too). Added to theCI passedgate'sneeds, so the ruleset needs no change.Closes elixir-cloud-aai/generic-issues#145. Part of elixir-cloud-aai/generic-issues#18 (closed by the last slice).
Verification
go build ./...,go vet ./...,go vet -tags integration ./internal/repository/pg/,go test -race -count=1 ./...: pass locally (Go 1.25.5).internal/ownership,internal/repository/pg/ownership_test.go(predicates and placeholder numbering),internal/api/tes/run_tag_test.go(fails without the handler fix: 200 instead of 404/500).ownership_integration_test.go: stamping, scoped Get/GetStatus/GetByTESID/ListByRunID for owner, other owner, empty owner, NULL-owner run, unscoped; keyset pages per owner for runs, tasks and ListByRunID) did not run locally (no PostgreSQL available to the agent); they run in the new CI job.gofmt -s -lreports onlyinternal/backend/k8s/workspace.go, which is untouched (local gofmt version differs from CI's).Unrelated fixes
fix: decode page tokens to timestamps(separate commit):List/ListByRunIDpassed the page token's Unix nanoseconds as an integer wherecreated_at(timestamptz) is compared; pgx can't encode that, so every second page of runs or tasks answered 500. Found by the new integration tests;parseCursornow returns atime.Time(unit test added).Declined review findings
TaskStore.List(low): the owner predicate on public TES ListTasks is anEXISTSper task without a supporting index. Fine at current volumes; revisit (join viaidx_runs_owner_createdor an owner column on tasks) if task tables grow large.go installof migrate (low): kept;name:follows the org's job naming rule, andgo install …@v4.17.0is checksum-verified via the Go module proxy (same version asdocker/server/Dockerfile).Open questions
None.
🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5