Repository navigation
fix: let admins manage ownerless resources - #666
Conversation
Resources stored before an app adopted ownership have no owners, so the role controllers didn't find them and nobody could assign the first owner via the API. If the operation passes an existence check, admins can now list the roles of such resources and assign owners. Everyone else still gets the same responses, as do admins if the resource doesn't exist. DELETE doesn't change. Closes #608 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis PR enables admins to adopt legacy ownerless resources through existence-aware role controllers: admins can list roles and assign the first owner when the application confirms the resource exists, while existing behavior for other callers, owned resources, missing resources, and DELETE operations remains unchanged. Sequence diagram for admin ownership adoptionsequenceDiagram
actor Admin
participant API
participant Controller
participant Exists as ResourceExistenceCheck
participant Roles as RoleStore
Admin->>API: GET permissions or PUT owner role
API->>Controller: getResourceRoles or putUserRole
Controller->>Roles: _get_owners
Roles-->>Controller: No owners
Controller->>Exists: exists()
Exists-->>Controller: Resource exists
Controller->>Roles: list_roles or grant_role
Roles-->>Controller: Roles or owner assignment
Controller-->>API: Success
File-Level Changes
Possibly 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 #666 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 56 56
Lines 7228 7230 +2
=========================================
+ Hits 7228 7230 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/security/access_control/ownership_server.py" line_range="192-193" />
<code_context>
+ Whether the resource has owners (whose role applies) or, if not, the
+ caller is an admin and `exists` finds the resource.
+ """
+ return bool(_get_owners(resource, RESOURCE_ROLE_PTYPE)) or (
+ exists is not None and is_admin() and exists()
+ )
+
</code_context>
<issue_to_address>
**Interrupted grant leaves phantom owner**
When the resource is deleted and the worker stops after the first owner role is persisted but before the post-grant existence check and cleanup, `grant_role` persists the owner role before its post-grant existence check, so process termination in that gap leaves the role behind. `_is_manageable` then treats the resource as manageable from that owner role without calling `exists`; a later resource reusing the same path inherits the stale ownership and permissions.
Make resource deletion and role cleanup crash-safe, for example with a durable cleanup/reconciliation mechanism or a transaction covering both stores.
</issue_to_address>
### Comment 2
<location path="foca/security/access_control/ownership_server.py" line_range="271" />
<code_context>
"""
role = _get_role(body)
- resource = _get_owned_resource(with_subject=True)
+ resource = _get_owned_resource(with_subject=True, exists=exists)
try:
grant_role(resource, subject, role, subject_type=subject_type, exists=exists)
</code_context>
<issue_to_address>
**Failed grant removes concurrent owner**
When two admin requests grant the same first owner and the resource is deleted and recreated between their existence checks, `_grant` removes the exact role whenever its existence check returns `False`, even if another request has since reused that same role and its own check succeeds. If two admins grant the same first owner while the resource is deleted and recreated between checks, one request returns success but its owner rule is removed, leaving the existing resource ownerless.
Make rollback conditional on the grant instance that failed, or coordinate concurrent grants and their existence checks so a failed request cannot remove a successful request's role.
</issue_to_address>
### Comment 3
<location path="foca/security/access_control/ownership_server.py" line_range="321" />
<code_context>
@_require_enforcement
-def getResourceRoles(**kwargs: Any) -> list[dict[str, str]]:
+def getResourceRoles(
+ exists: Callable[[], bool] | None = None, **kwargs: Any
+) -> list[dict[str, str]]:
"""List the roles granted on the resource of the route.
</code_context>
<issue_to_address>
**Ownerless role lists return 500**
When an admin lists an ownerless resource on a route with a path parameter named `exists`, connexion binds the path value to `getResourceRoles`’s `exists` parameter, so `_is_manageable` calls that string as a callback and raises `TypeError` instead of returning the role list.
Rename the callback parameter and update its callers so a route parameter named `exists` cannot bind to it.
</issue_to_address>
### Comment 4
<location path="examples/petstore-access-control/controllers.py" line_range="202" />
<code_context>
+ Returns:
+ Subject type, subject and role of each role granted on the pet.
+ """
+ return ownership_server.getResourceRoles(exists=_pet_exists(id), id=id, **kwargs)
</code_context>
<issue_to_address>
**Example spec excludes admin access**
The example’s OpenAPI descriptions still say the role endpoints are for “owners only” (`examples/petstore-access-control/petstore-access-control.yaml:169` and `:214-216`), while these wrappers now let admins list roles and assign the first owner on existing ownerless pets. Users of the served specification are told that supported admin operations are unavailable.
Update the example OpenAPI descriptions to mention admins when the existence check confirms the pet exists.
Also at `foca/security/access_control/ownership_server.py:271`.
</issue_to_address>Linked issues
#608 fix(access-control): let admins manage ownerless · 6 of 6 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Admins can list roles and grant the first owner on an existing ownerless resource. | ✅ Met | The existence-aware controllers support listing and granting user or group ownership; tests cover listing, both owner types, and refusal of a non-owner role as the first grant. |
| 2 | Non-admins get 404 for ownerless resources. | ✅ Met | A test confirms a non-admin gets 404 when policies allow the request through; requests denied by policy still receive 403. |
| 3 | Missing resources get 404. | ✅ Met | Tests cover admin listing and granting against a resource the existence check does not find. |
| 4 | Existing behavior for resources with roles is unchanged. | ✅ Met | A test verifies existing roles remain accessible and the existence check is not called when the resource already has an owner. |
| 5 | 100% coverage. | ✅ Met | Coverage is configured to fail below 100%, and the PR verification reports a passing test run with 100% coverage. |
| 6 | Document adopting ownership in an existing app on the access-control page. | ✅ Met | The new section explains how to wrap the role controllers with existence checks and describes the resulting behavior. |
Sourcery assessment
Needs a human reviewer. 3 findings to address first, and an admin can now inspect roles on an existing ownerless resource and assign its first owner, which can enable that owner to access the resource. Reverting stops the new behavior, but it cannot undo any access or data exposure that occurred before the grant is removed.
Blocking findings: foca/security/access_control/ownership_server.py:193, foca/security/access_control/ownership_server.py:271, foca/security/access_control/ownership_server.py:321
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Resources stored before an app adopted ownership have no owners, so the role controllers respond with
404and nobody can assign the first owner via the API.getResourceRoles,putUserRoleandputGroupRole: if the caller is an admin and the existence check passed asexistsfinds the resource, the operation works on a resource without owners.getResourceRolestakesexistsas a new optional argument.DELETEoperations don't change.getPetRolesto a wrapper with the check. Docs: new section "Adopting ownership in an existing app".Closes #608
Verification
ruff check,ruff format --check,mypy,ast-grep scan,mkdocs build --strict: pass.coverage run -m pytestwith MongoDB 7.0: 3387 passed, 1 skipped, 100% coverage.🤖 Generated with Claude Code
Summary by Sourcery
Enable administrators to recover and assign ownership of existing ownerless resources through the permissions API.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: