Repository navigation
fix(plugins): align Principal with access control - #527
Conversation
Principal.groups held only the roles from group claims, and Principal.subject the identity claim, so plugins saw neither the role of a service client (client_mapping) nor roles from global role rules, and could not tell clients from users or see the user a client acts for. - Add get_current_roles(): client role, group roles and roles granted via g rules, transitively, as access control applies them. - Principal: subject is the client ID for service tokens, with the new subject_type (user or client) and acting_for (on-behalf-of user); groups come from get_current_roles(). - Test the principal against a real access control setup. Closes #519 Co-Authored-By: Claude Opus 5.5 <[email protected]>
- get_current_roles() follows only the g role manager and keeps roles within Casbin's maximum hierarchy level, as enforcement does; object groups (default model g2) and resource roles no longer count. - Principal defaults subject_type to user and rejects a type without a subject; the adapter no longer derives it. - Move shared callers of the access control tests into a module, test get_current_roles() and the builder arguments, and clarify the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- get_current_roles() returns no roles for tokens without a user or service client, as is_admin() does (is_permitted() refuses them). - current_principal() resolves acting_for for service clients only, so the on-behalf-of header of other callers no longer makes the plugin listing fail; the ownership helpers still reject it. - Simplify the adapter and rewrap the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideAligns plugin Sequence diagram for access-control-aligned plugin PrincipalsequenceDiagram
participant Request
participant Adapter as current_principal()
participant Access as AccessControl
participant Plugin as Plugin
Request->>Adapter: current_principal()
Adapter->>Access: get_current_client()
Access-->>Adapter: client or none
alt service client
Adapter->>Access: get_on_behalf_of_user()
Access-->>Adapter: acting_for user
end
Adapter->>Access: get_current_roles()
Access-->>Adapter: client, group, and global roles
Adapter-->>Plugin: Principal(subject, subject_type, acting_for, groups)
Flow diagram for current role resolutionflowchart TD
Identity["Resolve service client or user identity"] --> BaseRoles["Collect client_mapping and group-claim roles"]
BaseRoles --> Enabled{"Access control enabled?"}
Enabled -->|No| Result["Return roles"]
Enabled -->|Yes| Global{"Model defines global g roles?"}
Global -->|No| Result
Global -->|Yes| Traverse["Traverse g role links transitively"]
Traverse --> Filter["Keep roles linked within hierarchy limit"]
Filter --> Result
Identity --> Unidentified{"Identity found?"}
Unidentified -->|No| Empty["Return no roles"]
File-Level Changes
Assessment against linked issues
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 #527 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 55 55
Lines 6636 6667 +31
=========================================
+ Hits 6636 6667 +31
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 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="docs/plugins.md" line_range="513-517" />
<code_context>
+The caller (`Principal`) is identified as by access control and its
+ownership helpers:
+
+- `subject` and `subject_type`: the client ID of a service client
+ (`client`), or else the user's subject (`user`); key quotas or records
+ on both, as a client and a user may have the same identifier;
+- `acting_for`: the user that a service client acts for (header
+ `auth.on_behalf_of_header`), who owns the resources it creates (e.g.,
+ tasks); service clients that may not act for users get `403`, invalid
+ names `400` (the header of other callers is ignored here, but rejected
</code_context>
<issue_to_address>
**Identity values include undocumented namespaces**
When the app uses issuer namespaces and a plugin interprets these fields as raw input values, when issuer namespaces are configured, `get_current_subject()` and `get_on_behalf_of_user()` return namespaced identifiers, so a plugin treating `subject` or `acting_for` as the raw subject/header value gets a different identity from access control and ownership helpers.
Clarify in the plugin contract that user `subject` and `acting_for` use the access-control namespace format when applicable.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and plugins now receive service-client identities, acting-for users, and the roles access control grants, including roles from global rules; a wrong identity or role could affect plugin access or who owns resources it creates. Reverting stops future requests from receiving these values, but cannot undo access or resource changes that already occurred.
Blocking findings: docs/plugins.md:517
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
The
Principalthat plugins see now identifies the caller the same way access control does. This fixes finding 4 of the v0.18.0 pre-release review.get_current_roles()(foca.security.access_control): returns the roles that access control applies to the request.client_mapping), the roles from group claims, and the roles granted via global role rules (g), also transitively.grole manager, within Casbin's maximum hierarchy level, asenforce()andis_admin()do. Default-modelg2object groups and resource-scoped roles don't count.is_admin().Principal.groupsnow comes fromget_current_roles(). Pluginrolesandclient_pipelines.rolestherefore match access control. For example,client_mapping: {portal: admin}now satisfiesroles: [admin].Principal.subjectis the client ID for service tokens. The newsubject_typeisuserorclientand defaults touserwhen there is a subject. Users and clients with the same identifier no longer share one namespace.Principal.acting_for: the user that a service client acts for (auth.on_behalf_of_header), i.e. the owner of the resources it creates.make_principal()takessubject_typeandacting_for. The shared callers of the access control tests moved totests/security/access_control/callers.py.docs/plugins.md(the caller),docs/access-control.md(get_current_groups()vs.get_current_roles()) anddocs/reference.md.Plugins and
Principalare new in 0.18 (they are in no released tag), so this needs no migration note. proTES (planned consumer) should key quotas and provenance on (subject_type,subject), and ownership onacting_foror else the subject.Closes #519
Verification
pytest tests/plugins tests/security -m "not mongodb": 1487 passed. Coverage offoca/plugins,enforcement.pyandownership.pyis 100%.tests/plugins/test_plugin_adapter.py::TestPrincipalIdentity: user,client_mapping,grules, on-behalf-of, access control disabled.tests/security/access_control/test_current_roles.py: hierarchy limit, other role definitions, opt-in roles, unidentified callers.ruff check,ruff format --check,mypy,ast-grep scan, semgrep (.semgrep) andmkdocs build --strictare all clean.mongodb-marked tests locally because no MongoDB server was available; CI runs them.Declined review findings
gtargets that look like subjects (e.g.,g, group:devs, client:protes) appear as roles (low). Enforcement applies them the same way. The point of this change is consistency with access control, and filtering by prefix would make the two diverge again.test_current_principaloverlaps the unmocked tests (low). I kept it for the bearer-scheme and non-string-issuer cases.🤖 Generated with Claude Code
Summary by Sourcery
Align plugin principals with access-control identity, roles, and ownership semantics.
New Features:
get_current_roles()to expose the complete set of roles applied by access control, including mapped client, group, and global-rule roles.subject_type, and service-clientacting_forinformation.Bug Fixes:
Enhancements:
Documentation:
Tests: