Repository navigation
feat(access-control)!: own tasks per user - #250
Conversation
The user creating a task becomes its owner (the subject of the access token), stored as FOCA owner roles (g2 rules) on the task and on its cancellation path, and as user_id in the task document. Only owners may get and cancel a task; GET /tasks lists only the caller's tasks. Admins no longer may do everything: the model has no admin bypass, and admins only keep the permissions API, via named rules. Tasks are not shared (resource_roles: [owner]). BREAKING CHANGE: admins can no longer list, read or cancel the tasks of others; tasks of others and unknown tasks get 403 instead of 404; the rule users-get-cancel-tasks is replaced by owners-get-cancel-tasks. Tasks of earlier versions have no owner and are visible to nobody. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Document the roles user, owner and admin, task ownership, how operators find, assign and purge tasks (also those without owner), how to stop abusive tasks without API access, and the upgrade notes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Task identifiers were not unique (the unique index covers task_id and worker_id, not task.id), so a new task could get the identifier of a stored task, or of a deleted one whose owner roles are left, and its creator, or the old owner, would get access to the other task. New identifiers now skip stored tasks and identifiers with roles, and the owner is granted before the task is stored. Refuse to start without enabled and enforced access control (owners would not be enforced, and the ownership helpers fail), and with service clients, which are enforced as clients, not as their users. Fix the docstrings, split and extend the access control tests (owners without the role user, admins granting owners via the permissions API, reused identifiers), and update the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Revoke the owner roles of a task again if it can't be stored, and skip identifiers with roles of any subject (also of roles such as user, or only on the cancellation). Run the ownership checks on startup separately from the checks of the role groups. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis breaking change makes task ownership explicit and enforceable: tasks are assigned to their creating user in both MongoDB and FOCA resource-scoped roles, task APIs are owner-only with non-disclosing authorization errors, admins lose task access, unsafe startup configurations are rejected, and migration/operations guidance plus comprehensive tests are added. Sequence diagram for owner-scoped task creationsequenceDiagram
actor User
participant API as proTES API
participant Ownership as ownership helpers
participant FOCA
participant MongoDB
participant TES
User->>API: POST /tasks
API->>Ownership: get_owner()
Ownership-->>API: caller subject
API->>Ownership: task_has_roles()
Ownership->>FOCA: list_resource_roles()
API->>Ownership: add_task_owner()
Ownership->>FOCA: add_owner(task path)
Ownership->>FOCA: add_owner(cancel path)
API->>MongoDB: insert_one(task with user_id)
API->>TES: forward task
API-->>User: task identifier
Sequence diagram for owner-only task accesssequenceDiagram
actor User
participant API as proTES API
participant FOCA
participant MongoDB
participant TES
User->>API: GET /tasks
API->>FOCA: list_subject_roles()
FOCA-->>API: caller owner roles
API->>MongoDB: list_tasks(owned_tasks_filter())
MongoDB-->>API: caller's tasks
API-->>User: task list
User->>API: GET /tasks/{id}
API->>FOCA: authorize owner role
alt owner
FOCA-->>API: allow
API->>MongoDB: get task
API-->>User: task details
else not owner or unknown task
FOCA-->>API: deny
API-->>User: 403 Forbidden
end
User->>API: POST /tasks/{id}:cancel
API->>FOCA: authorize owner role on cancel path
alt owner
FOCA-->>API: allow
API->>TES: cancel task
API-->>User: cancellation result
else not owner or unknown task
FOCA-->>API: deny
API-->>User: 403 Forbidden
end
Flow diagram for task identifier reservation and owner rollbackflowchart TD
A[Generate task identifier] --> B{Stored task or FOCA roles exist?}
B -->|Yes| A
B -->|No| C[add_task_owner]
C --> D[insert_one]
D -->|Success| E[Task stored with user_id]
D -->|DuplicateKeyError or failure| F[remove_task_owner]
F --> A
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #250 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 25 +1
Lines 1180 1216 +36
=========================================
+ Hits 1180 1216 +36 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pro_tes/ga4gh/tes/task_runs.py" line_range="474-476" />
<code_context>
charset=charset,
length=length,
)
+ if self.db_client.count_documents(
+ {"task.id": task_id}, limit=1
+ ) or task_has_roles(collection=collection, task_id=task_id):
+ continue
+ document.task.id = task_id
</code_context>
<issue_to_address>
**Concurrent tasks share an identifier**
When two concurrent creates generate the same task ID and both pass the availability checks, `_write_doc_to_db` checks availability before inserting, but the checks do not reserve `task.id` and the configured unique index does not cover it. Both documents and owner roles can therefore be stored at the same path, letting either owner read or cancel the other’s task.
Reserve task IDs atomically, for example with a unique constraint on `task.id`.
</issue_to_address>
### Comment 2
<location path="pro_tes/ga4gh/tes/task_runs.py" line_range="474-476" />
<code_context>
charset=charset,
length=length,
)
+ if self.db_client.count_documents(
+ {"task.id": task_id}, limit=1
+ ) or task_has_roles(collection=collection, task_id=task_id):
+ continue
+ document.task.id = task_id
</code_context>
<issue_to_address>
**Stale policy rules can expose reused task IDs**
When a task-specific `p` allow rule remains after its task is deleted and a later task gets the same generated ID, `task_has_roles` checks resource-scoped roles, not stored permission (`p`) rules. Because the permissions API can add arbitrary rules, a path-specific allow rule left after a task is deleted is not detected; if that ID is generated again, the stale rule still grants access to the new task at the same path.
Prevent reuse of IDs referenced by any stored resource-specific permission rule, or ensure those rules are removed when the task is deleted.
</issue_to_address>
### Comment 3
<location path="pro_tes/ga4gh/tes/ownership.py" line_range="91-92" />
<code_context>
+ task_id: Identifier of the task.
+ owner: User identifier, without prefix (cf. :py:func:`get_owner`).
+ """
+ for resource in task_resources(collection=collection, task_id=task_id):
+ add_owner(resource=resource, subject=owner)
+
+
</code_context>
<issue_to_address>
**Partial grants leave stale owner rules**
When granting the second resource role fails or execution stops between the two writes, `add_task_owner` writes the roles sequentially, so the first role can remain even though no task is inserted. The stale rule reserves the ID and adds to the rules FOCA loads on requests.
Make the two role grants atomic so a partial grant cannot remain.
Also at `pro_tes/ga4gh/tes/task_runs.py:480-481`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and this changes the authorization boundary for every task: an incorrect ownership rule or grant could let a caller read another user's task data or cancel it. Any data exposed or action taken cannot be undone by reverting the code.
Blocking findings: pro_tes/ga4gh/tes/task_runs.py:476, pro_tes/ga4gh/tes/task_runs.py:476, pro_tes/ga4gh/tes/ownership.py:92
If granting the second owner role of a new task fails, revoke the first one too, so that no rule is left without a task. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adapt the chart to task ownership (#250): as the app now refuses to start unless access control is enabled and enforced, rendering fails otherwise, instead of skipping the checks of groups and admins. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
With this PR, each task belongs to the user who created it, per the maintainer decisions for #215 (Q2, Q4 of the migration plan):
usermay create tasks and become their owners. The owner is thesubof their own access token, stored as FOCA owner roles (g2rules, viaadd_owner()) on the task and on its cancellation path./tasks/{id}:cancelis not a sub-path of/tasks/{id}for FOCA. The owner is also stored asuser_idin the task document.GET /taskslists only the caller's own tasks (via FOCA'slist_subject_roles()).GET /tasks/{id}andPOST /tasks/{id}:cancelare allowed for owners only. Everyone else gets403as problem details, also for unknown tasks, so responses don't reveal which tasks exist.resource_roles: [owner].service_clients,on_behalf_of_header,client_mapping), which are not supported yet (Q4).task.id(fix: make task identifiers unique in the database #248). The owner role is granted before the insert and revoked if the insert fails.Breaking:
404becomes403for unknown tasks;users-get-cancel-tasksis replaced byowners-get-cancel-tasks;Closes #215
Closes #153
How it was verified
pytestwith coverage: 326 passed, 1 skipped, 100% coverage (437 passed after mergingorigin/dev).pytestagainst a real MongoDB 7.0 (MONGODB_TEST_URI): 326 passed, 1 skipped.pre-commit run --all-files(ruff, mypy, semgrep, ast-grep, and the other hooks): passed.mkdocs build --strict: passed.tests/smoke/check.sh) is updated with a second userboband an admin, who both get403. It passed in CI ("Run app with Docker Compose").All are fixed, except those declined below.
Declined review findings
Group mapped to the role
ownergets all tasks (low): FOCA's config validation already rejectsgroup_mappingandclient_mappingvalues that are inresource_roles.Split the third commit, with
!on the startup refusal (medium, commit hygiene): the PR is squash-merged under a breaking title. The squash message and the upgrading notes list the startup refusal.Concurrent creates sharing an identifier (Sourcery): a unique index on
task.idis needed to close this race fully. It is tracked in fix: make task identifiers unique in the database #248, which changes the database configuration. The remaining window is negligible: both creates must draw the same 6-character identifier between the check and the grant.Stale task-specific
prules (Sourcery): only admins can write such rules, and they can already grant themselves ownership.prules are path patterns, so they can't be matched reliably against single identifiers.Open questions
None blocking. Notes for the maintainer:
g2owner rules (documented). FOCA has no way to restrict which rules admins may write.user: owners who lose the roleusercan still read and cancel their own tasks by ID, but can't create or list tasks (documented, tested).🤖 Generated with Claude Code