-
Notifications
You must be signed in to change notification settings - Fork 9
perf: reduce per-assignment Casbin calls in visible assignments fast path #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mariajgrimaldi
wants to merge
19
commits into
main
Choose a base branch
from
MJG/perf-visible-assignments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5d916d6
perf: reduce per-assignment Casbin calls in visible assignments fast …
mariajgrimaldi 94e8905
perf: replace Casbin batch_enforce with scope-based filter in visible…
mariajgrimaldi 6f78c69
refactor: extract filter_role_assignments_visible_to_subject and is_u…
mariajgrimaldi b7c6b46
refactor: address quality issues
mariajgrimaldi e1a50d7
refactor: drop staff/superuser short-circuit from visible assignments
mariajgrimaldi 6a54ad0
refactor: collapse filter_role_assignments_visible_to_subject to sing…
mariajgrimaldi b77987c
docs: refine ADR 0014 language and structure
mariajgrimaldi d3d971f
docs: add profiler evidence and fix enforce overhead framing in ADR 0014
mariajgrimaldi f99e99b
revert: restore matcher.py and remove unused is_user_staff_or_superuser
mariajgrimaldi 2b7276e
test: add tests for filter_role_assignments_visible_to_subject and ge…
mariajgrimaldi 700ae78
fix: bypass scope-based filter for staff/superusers in _filter_allowe…
mariajgrimaldi 2be73f3
style: fix pydocstyle D213 in is_user_staff_or_superuser
mariajgrimaldi 39f19c2
refactor: use utility in is superuser or admin matcher check for cons…
mariajgrimaldi 6586957
chore: address PR reviews
mariajgrimaldi 402fd59
refactor: clean up after refactor
mariajgrimaldi f19173c
docs: add inline comments explaining why roles[0] works
mariajgrimaldi 6b504e3
fix: update function name after renaming
mariajgrimaldi 477ddfc
fix: restore NotImplementedError guard in filter_role_assignments_vis…
mariajgrimaldi 6af1fda
style: fix ruff I001 import sorting in roles.py and matcher.py
mariajgrimaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
docs/decisions/0014-bulk-assignment-queries-without-casbin-enforce.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| 0014: Visible Role Assignment Queries Without Casbin Enforce Calls | ||
| ################################################################## | ||
|
|
||
| Status | ||
| ****** | ||
|
|
||
| **Accepted** - *2026-05-28* | ||
|
|
||
| Context | ||
| ******* | ||
|
|
||
| ``get_visible_role_assignments_for_user`` and ``get_visible_user_role_assignments_filtered_by_current_user`` | ||
| are the main entry points for listing role assignments in the admin console. Both answer two questions: | ||
|
|
||
| 1. Which assignments match the requested filters (org, scope, role)? | ||
| 2. Which of those assignments is the requesting user allowed to see? | ||
|
|
||
| The original implementation called ``is_user_allowed`` once per candidate assignment. | ||
| ``is_user_allowed`` calls ``enforcer.enforce()``, one policy evaluation per call. With N | ||
| assignments, that is N enforce calls per request. | ||
|
|
||
| Profiling on a realistic dataset (muscat, ``GET /api/authz/v1/assignments/``) showed | ||
| ``_filter_allowed_assignments`` taking 16.74s for a non-admin user (see `pyinstrument report, main non-admin`_). | ||
| Each ``enforce()`` call costs 20 to 80ms due to Casbin evaluating the full policy graph per | ||
| (subject, action, object) triple, including role graph traversal via ``has_link`` and the | ||
| custom matcher function. At scale, N enforce calls dominate the request time. | ||
|
|
||
| The two questions were also answered in a different order: question 2 (authorization) ran first on | ||
| the full assignment list, and question 1 (filtering) ran afterward on the grouped result. | ||
| Assignments that would have been dropped by the filter were still evaluated by Casbin. | ||
|
|
||
| Decision | ||
| ******** | ||
|
|
||
| Avoid Casbin ``enforce()`` in the visible-assignment hot path. Instead, retrieve the viewer's | ||
| accessible scopes from the database and match assignment scopes in Python. | ||
|
|
||
| 1. Replace per-assignment enforce() with scope lookups | ||
| ======================================================= | ||
|
|
||
| A new public function, ``filter_role_assignments_visible_to_subject`` (in | ||
| ``openedx_authz.api.roles``), replaces per-assignment ``enforce()`` calls. It: | ||
|
|
||
| - calls ``get_scopes_for_subject_and_permission`` once per distinct permission type across all | ||
| candidates (one enforcer lookup per type, not one per assignment), | ||
| - uses Casbin's own ``key_match_func`` to check whether each assignment's scope matches any of | ||
| the viewer's accessible scopes. | ||
|
|
||
| ``get_scopes_for_subject_and_permission`` is called once per distinct permission type (typically | ||
| 1-3 per request), not once per assignment. Matching is done in Python via ``key_match_func``. | ||
|
|
||
| 2. Filter by params before the authorization pass | ||
| ================================================== | ||
|
|
||
| A new ``_filter_assignments_by_params`` function applies org, scope, and role filters on the | ||
| flat assignment list before the authorization pass. Assignments that would be dropped by the | ||
| filters are never evaluated for visibility. | ||
|
|
||
| 3. Cache role permission lookups within a call | ||
| =============================================== | ||
|
|
||
| ``get_role_assignments`` now uses a local ``_perm_cache`` dict to avoid calling | ||
| ``get_permissions_for_single_role`` more than once per role key per call. | ||
|
|
||
| Consequences | ||
| ************ | ||
|
|
||
| #. **No Casbin enforce calls in the visible-assignment path** for the common case. | ||
| ``get_scopes_for_subject_and_permission`` is called once per distinct permission type, not | ||
| once per assignment. | ||
|
|
||
| #. **The authorization pass and grouping step operate on a pre-filtered list.** Assignments | ||
| dropped by the filters are never evaluated for visibility. | ||
|
|
||
| #. ``filter_role_assignments_visible_to_subject`` **is a public function** in | ||
| ``openedx_authz.api.roles``, available to callers who need visibility filtering outside of | ||
| the user-assignment endpoints. | ||
|
|
||
| #. ``key_match_func`` **is used directly from** ``casbin.util``. This couples the visibility | ||
| filter to Casbin's matching semantics. If the model's matching behavior changes, this function | ||
| must change too. | ||
|
|
||
| #. ``get_scopes_for_subject_and_permission`` **must return current data.** If the enforcer cache | ||
| is stale, the visibility filter produces wrong results silently. The per-assignment | ||
| ``enforce()`` approach had the same dependency, resolved per call rather than once upfront. | ||
|
|
||
| Patterns for Bulk Authorization Paths | ||
| ************************************** | ||
|
|
||
| While implementing this change, we identified some patterns for bulk authorization paths like this one: | ||
|
|
||
| **Scope lookups for bulk visibility checks.** | ||
| Query the viewer's accessible scopes once rather than calling enforce per item. | ||
| ``get_scopes_for_subject_and_permission`` does this. | ||
|
|
||
| **batch_enforce to reduce per-call overhead** | ||
| If per-item enforce calls are still needed, use Casbin's ``batch_enforce`` to reduce overhead getting the enforcer. | ||
| This was implemented and tested but ultimately not used in this case since scope lookups were sufficient. | ||
|
|
||
| **Use Casbin's own matching utilities.** | ||
| ``casbin.util.key_match_func`` implements the same glob-matching logic as the Casbin model's | ||
| ``keyMatch``. Use it rather than reimplementing the matching logic. | ||
|
|
||
| **Filter before authorizing.** | ||
| Apply cheap filters (field equality, etc.) before authorization. Casbin is not | ||
| involved in the first pass. | ||
|
|
||
| Alternatives Considered | ||
| *********************** | ||
|
|
||
| ``batch_enforce`` to replace the per-assignment loop | ||
| ===================================================== | ||
|
|
||
| Replacing the ``enforce()`` loop with a single ``batch_enforce`` call was implemented first | ||
| (see `528b129`_). It removed per-call overhead but kept N policy evaluations. Dropped in favor | ||
| of the scope-based approach. | ||
|
|
||
| References | ||
| ********** | ||
|
|
||
| - `ADR 0005`_ | ||
| - `ADR 0012`_ | ||
|
|
||
| .. _ADR 0005: https://github.com/openedx/openedx-authz/blob/main/docs/decisions/0005-architecture-and-data-modeling.rst | ||
| .. _ADR 0012: https://github.com/openedx/openedx-authz/blob/main/docs/decisions/0012-auditability.rst | ||
| .. _528b129: https://github.com/openedx/openedx-authz/commit/528b129c829df13588e74965b1f8116d73320627 | ||
| .. _pyinstrument report, main non-admin: ../../../pyinstrument-reports/20260521-131048-main-muscat-nonadmin.html | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.