Repository navigation
feat: let service clients own their tasks - #521
Conversation
Opt-in, off by default: clients listed in security.auth.service_clients (with their issuer) and mapped to the role user may create tasks with their own client-credentials tokens and own them as client:<id>; they list, read and cancel only their own tasks, nothing beyond. Tasks are relayed with proTES' own credentials, as before. The startup refusal of FOCA's require_no_service_clients() is replaced by checks of the clients: no acting for users, an explicit issuer, the role user only, not proTES' own client. The ID of a service client is accepted as audience for its own service tokens only, before each request on all endpoints, so that users' tokens obtained by the client (login, token exchange) are not accepted for it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
The fake identity provider issues client-credentials tokens of a service client for its own audience, and user tokens of other clients and audiences; the smoke test creates, lists and cancels a task as the client on the backend that requires proTES' token, checks that users and the client do not see each other's tasks, and that a user token for the client's audience is rejected. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Access control (new section Service clients), configuration, operations (owners of client tasks) and upgrading. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
check_service_clients() only checks; app.py registers the audience hook if service clients are configured. Wording: "not an accepted audience", no identity provider named in the fake identity provider. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
The audience check of service clients split the Authorization header at a space only, while Connexion and FOCA split it at any whitespace, so a tab or a no-break space after the scheme let tokens for the audience of a service client past the check. The mock user info endpoints of the tests accept any audience now, as real ones do, so that the tests see the rejections of the check; the smoke test has a positive control. Upgrade notes: users whose subject starts with client: are refused. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Tokens of the parametrized audience tests were made at collection and expired in long runs of the whole suite (5 minutes); they are made in the tests now. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Add positive controls for the audience hook and note how usage counts legacy client: users together with a service client of the same ID. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Reviewer's GuideSchema and migrationsRepresent task owners unambiguously as either issuer-namespaced users or
Uses of the APIRun service-client validation at startup and register the audience-check hook before requests when service clients are configured.
ImplementationValidate service-client configuration and reject service tokens with unsafe audiences or groups while preserving FOCA's subsequent token verification.
ConfigurationExplain the opt-in service-client settings in the shipped configuration example.
Enable a service client and its own audience in the smoke-test deployment configuration.
DocumentationDocument service-client ownership, audience and group restrictions, deployment settings, API access, operations, and upgrade implications.
TestsExercise service-client startup rules, ownership isolation, task operations, relaying, audience checks, and group rejection.
Update the task-owner test helper for FOCA's explicit owner subject type.
Allow mocked user-info endpoints to validate tokens regardless of audience, so audience rejections are exercised by proTES.
Add smoke coverage for service-client task ownership, isolation, and rejection of user tokens for the client's audience.
Extend the fake identity provider to issue service-client credentials and audience-specific tokens, and accept any audience at user info.
Update the startup-refusal test to match the revised service-client validation.
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 #521 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 38 +1
Lines 3537 3635 +98
=========================================
+ Hits 3537 3635 +98 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit da5ea2a #517 feat: opt-in service clients that own their tasks · 9 of 9 met
|
There was a problem hiding this comment.
Hey - I've found 6 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pro_tes/service_clients.py" line_range="86" />
<code_context>
+ " (`security.auth.service_clients`), as client IDs are unique"
+ " per issuer only"
+ )
+ if mapping.get(client.client_id) != ROLE:
+ raise RuntimeError(
+ f"Service client {client.client_id}: map it to the role"
</code_context>
<issue_to_address>
**Service clients gain admin access**
When a listed service client's token or user-info response includes a group mapped to `admin`, `check_service_clients()` checks only `client_mapping`, while FOCA also grants roles through `group_mapping`. The service client therefore receives `admin` access, including to the permissions API, despite the documented restriction to the `user` role.
Ensure service-client group mappings cannot grant roles beyond `user`, and reject this configuration at startup.
</issue_to_address>
### Comment 2
<location path="pro_tes/ga4gh/tes/ownership.py" line_range="57" />
<code_context>
"""
+ client = get_current_client()
+ if client is not None:
+ return f"{CLIENT_PREFIX}{client}"
owner = get_current_subject()
- if owner is None:
</code_context>
<issue_to_address>
**Different clients share task access**
When two trusted issuers have service clients with the same ID, or a legacy user's `client:` subject matches a service client ID, `get_owner()` stores only the client ID, and `owner_subject()` derives the client role subject from that value, so clients share task ownership, access, and usage accounting; matching legacy user rows also share the `user_id` and task-limit counts. The startup check also refuses a service client at another issuer when its ID matches proTES' ID.
Include the issuer in stored client ownership and role subjects, keep client ownership distinct from legacy user subjects, and compare issuer-qualified identities in the startup check.
Also at `pro_tes/service_clients.py:79-86`, `pro_tes/service_clients.py:94`, `pro_tes/ga4gh/tes/ownership.py:75`.
</issue_to_address>
### Comment 3
<location path="pro_tes/service_clients.py" line_range="76" />
<code_context>
+ " `security.auth.on_behalf_of_header` and `on_behalf_of` of"
+ " `security.auth.service_clients`"
+ )
+ if not auth.service_clients:
+ return
+ mapping = security.access_control.client_mapping or {}
</code_context>
<issue_to_address>
**Unlisted clients receive mapped roles**
When `security.access_control.client_mapping` is nonempty while `security.auth.service_clients` is empty, `check_service_clients()` returns before checking `client_mapping`, so FOCA starts with mapped clients despite there being no service-client allow-list. A client matched by the mapping can receive a role and create tasks without being listed as an allowed service client.
Check and reject nonempty client mappings when the service-client allow-list is empty instead of returning early.
</issue_to_address>
### Comment 4
<location path="pro_tes/service_clients.py" line_range="199-200" />
<code_context>
+ if issuer not in auth.issuer_urls:
+ return
+ matched = _audiences(claims) & set(auth.get_audience(issuer) or [])
+ clients = {client.client_id for client in auth.service_clients}
+ if not matched or matched - clients:
+ return
+ client = get_service_client(claims=claims, conf=auth)
</code_context>
<issue_to_address>
**Valid tokens are rejected**
When a trusted issuer accepts an audience equal to a service-client ID configured for a different issuer, and a token from that issuer names only that audience, `check_service_audience()` compares the token's audiences with service-client IDs from every issuer, treats the audience as service-client-only, and then `get_service_client()` finds no client for that issuer. The valid request receives `401`.
Match reserved service-client audiences only against clients configured for the token's issuer.
</issue_to_address>
### Comment 5
<location path="pro_tes/ga4gh/tes/ownership.py" line_range="57" />
<code_context>
"""
+ client = get_current_client()
+ if client is not None:
+ return f"{CLIENT_PREFIX}{client}"
owner = get_current_subject()
- if owner is None:
</code_context>
<issue_to_address>
**API guide omits client owners**
The API guide still says the user who created a task is its only owner and describes usage as totals for users. Service clients now own tasks, and their `client:<client ID>` values appear in `user_id` totals, so API consumers get inaccurate authorization and usage guidance.
Update the API guide to describe service clients as task owners and clarify how their `client:<client ID>` identities appear in usage totals.
</issue_to_address>
### Comment 6
<location path="tests/ga4gh/tes/test_service_clients.py" line_range="206" />
<code_context>
+ {"client_id": CLIENT, "issuer": ISSUER, "on_behalf_of": True},
+ {"client_id": OTHER_CLIENT, "issuer": ISSUER},
+ ],
+ on_behalf_of_header="X-On-Behalf-Of",
+ ),
+ "act for users",
</code_context>
<issue_to_address>
**Isolate on-behalf-of test**
The `on_behalf_of` startup case also sets `on_behalf_of_header`, which independently causes startup refusal. Add a case with `on_behalf_of` set and the header unset to verify that check on its own.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 4 findings to address first, and a flaw in the service-token audience or owner checks could let a caller act as the wrong owner or access another owner’s tasks. Reverting removes the new checks, but any task records or owner roles created—and any data already exposed—would remain.
Blocking findings: pro_tes/service_clients.py:86, pro_tes/ga4gh/tes/ownership.py:57, pro_tes/service_clients.py:76, pro_tes/service_clients.py:200
FOCA adds the roles of a token's groups (group_mapping) to the role of a service client, so a service token with an admin group got admin. FOCA requests no user info for service tokens, so the request hook now rejects service tokens of listed clients whose group claim has groups, read as FOCA reads it, and logs a warning naming client and claim. Also: the API guide names service clients as task owners, and a startup test checks on_behalf_of without the header. From the Sourcery review. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
# Conflicts: # docs/operations.md
With FOCA 0.23, users are namespaced by issuer, clients are not: the owner test uses the stored form, and a new test checks both owners. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Release notes: the FOCA 0.23 move (#522, #523: breaking changes, security, maintenance) and the service clients (#517, #521, D-451); citation date 2026-11-26. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Closes #517
Summary
Opt-in service clients: apps with client-credentials tokens of their own (e.g., a workflow engine) may create tasks and own them. Off by default; nothing changes without them.
security.auth.service_clients, each with an explicitissuerandclient_id, mapped to the roleuser(only) insecurity.access_control.client_mapping.client:<client ID>(user_idand FOCAg2owner roles with subject typeclient). The client lists, reads and cancels its own tasks only. It sees no tasks of others, others (users, other clients, admins) don't see its tasks, and it has no admin rights.require_no_service_clients()): refuseson_behalf_of_headerand clients withon_behalf_of, clients withoutissuer, clients without the roleuseror with another role, and proTES' own client (PROTES_CLIENT_ID). Warns if a client's ID is not an accepted audience. Logs the allowed clients.aud, so deployments add it tosecurity.auth.audience. A before-request hook (pro_tes/service_clients.py) accepts that audience for that client's own service tokens only, on all endpoints, the permissions API included. Other tokens whose only accepted audiences are service-client IDs get401with aninvalid_tokenchallenge, e.g., users' tokens obtained by the client by login, or by token exchange, after which the current identity provider keeps the exchanging client inaud(feat: exchange user tokens per TES backend #501).before_request) lives inapp.py(ast-grep ruleframework-free-tasks); the hook reads the request through FOCA'scurrent_request().substarts withclient:can no longer create tasks (401), also without service clients.Relation to other work:
docs/upgrading.md, but in another hunk.How it was verified
tests/ga4gh/tes/test_service_clients.py(with the mock identity provider and fake backends):subequals a client ID;dev. The whitespace cases failed before fd2ed92.tests/conftest.py,tests/smoke/fake_tes.py) now accept any audience, as real ones do, so that they don't mask the app's rejections.tests/smoke/): the fake identity provider issues a service client's tokens for its own audience. A task of the client goes to the fake backend that requires proTES' token, and is listed, cancelled and invisible to a user. A user token for the client's audience gets401, with a positive control.coverage run -m pytest: 2286 passed, 11 skipped, 100% coverage (before the last review fixes;tests/ga4gh/tes/test_service_clients.pyafter them: 108 passed, 100% coverage ofpro_tes/service_clients.py)pre-commit runon the changed files (ruff, mypy, semgrep, ast-grep, gitleaks): passed.mkdocs build --strict: passed.test_service_token_with_groups); a third adversarial round on that fix found two low findings, both fixed.Scan image, and so the gateCI passed, fails on CVE-2026-124851 (HIGH, fixed) inlibzstd1of the FOCA base image: unrelated to this PR, it fails ondevand on every open PR as well. CodeQL (default setup) failed while uploading its results during a GitHub outage; it can't be rerun by hand and runs again with the next push.Declined review findings
Copy of FOCA's
invalid_tokenchallenge text inpro_tes/service_clients.py(quality, low): FOCA exposes no public constant or helper; the tests assert the challenge.Tests that patch FOCA's getters for
get_owner()(quality, low): kept for theclient:-subject refusal, which no identity provider of the tests can issue; the behaviour is also covered end to end.Helper for the client token in
tests/smoke/check.sh(quality, low): used once; it follows the existing inline snippet for proTES' token.Changelog entry (quality): release notes are written per release in
docs/releases/by the release-prep PR (build: prepare the 0.7.0 release #511).Sourcery: service clients of two issuers with the same ID share an owner: FOCA rejects duplicate client IDs of service clients, for all issuers.
Sourcery:
client_mappingwithout service clients: FOCA rejects mapped clients that aren't service clients.Sourcery: the audience rule compares with the IDs of service clients of all issuers: kept, as it fails closed.
Sourcery: users whose subject starts with
client:are refused also without service clients: kept, so that owners are unambiguous whenever service clients are enabled; in the upgrade notes.Open questions
None.
🤖 Generated with Claude Code
https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
Summary by Sourcery
Enable opt-in service clients to create and manage tasks under their own isolated ownership while preserving user ownership and preventing delegated access.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: