Repository navigation
fix: load AuthzEnforcer policy before publishing the singleton - #680
wgu-taylor-payne wants to merge 1 commit into
Conversation
The singleton was built lazily without a lock. Two concurrent first requests in one process could each build an enforcer; the second overwrote the loaded one, saw the policy version as current, and skipped the load, leaving the process with an empty policy until the next authz write. Build and load the enforcer under a class-level lock and assign it only after the first load. AuthzEnforcer() now goes through the same path, so it also returns a loaded enforcer. Closes openedx#671
|
Thanks for the pull request, @wgu-taylor-payne! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
Closes #671.
Problem
AuthzEnforcerbuilt its singleton lazily and without a lock. When two requests were the first to reach a server process, each could build its own enforcer. The first finished, loaded the policy, and recorded the policy version. The second then replacedcls._enforcerwith its own unloaded enforcer, saw the version as current, and skipped the load. The process kept an empty policy, refusing role assignments and permission checks, until some authz write calledinvalidate_policy_cache(). This matches the end-to-end CI failure described in #671.AuthzEnforcer()(__new__) had the same unguarded check and returned an enforcer without loading its policy at all.Changes
_ensure_enforcer()builds the enforcer under a class-levelthreading.Lockwith a secondNonecheck inside the lock, loads its policy, records the version, and only then assignscls._enforcer. After the first call, the check outside the lock returns without taking it.load_policy(), so a write that lands during the first load bumps the version and the next check reloads. This is the same orderload_policy_if_needed()already used. The "get or initialize the version" code moved into_get_or_init_policy_version()so both paths share it.__new__andget_enforcer()both go through_ensure_enforcer().AuthzEnforcer()now returns a loaded enforcer.load_policy_if_needed()is unchanged. Two concurrent reloads can still record versions out of order, which at worst causes one extra reload later. It cannot leave a process with an empty policy, so I left it out of this fix.Testing
TestEnforcerConcurrentInitialization.test_concurrent_first_access_shares_one_loaded_enforcer: four threads callget_enforcer()together while enforcer construction is slowed down. The test checks that the enforcer was built once, that every thread got the same instance, and that its policy is not empty.test_instantiation_returns_loaded_enforcer:AuthzEnforcer()returns the singleton with its policy loaded and the version recorded.main'senforcer.py(4 builds instead of 1;AuthzEnforcer()returns an empty policy) and pass with this change. The full suite passes locally.Manual check: restart LMS in a Tutor dev environment running several worker processes, then send parallel first requests to
PUT /api/authz/v1/roles/users/for a built-in role. Each should succeed instead of returning 400Role ... does not exist in scope ....Rollback
Revert this commit. There are no migrations or data changes.
Merge checklist:
AI Usage
Kiro was used to aid development and testing.