Skip to content

fix: load AuthzEnforcer policy before publishing the singleton - #680

Open
wgu-taylor-payne wants to merge 1 commit into
openedx:mainfrom
WGU-Open-edX:tpayne/issue-671-enforcer-init-lock
Open

wgu-taylor-payne wants to merge 1 commit into
openedx:mainfrom
WGU-Open-edX:tpayne/issue-671-enforcer-init-lock

Conversation

@wgu-taylor-payne

Copy link
Copy Markdown
Contributor

Closes #671.

Problem

AuthzEnforcer built 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 replaced cls._enforcer with 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 called invalidate_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

  • Locked, load-before-publish initialization: a new _ensure_enforcer() builds the enforcer under a class-level threading.Lock with a second None check inside the lock, loads its policy, records the version, and only then assigns cls._enforcer. After the first call, the check outside the lock returns without taking it.
  • Version read before load: the policy version is read before load_policy(), so a write that lands during the first load bumps the version and the next check reloads. This is the same order load_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__ and get_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 call get_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.
  • Both tests fail against main's enforcer.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 400 Role ... does not exist in scope ....

Rollback

Revert this commit. There are no migrations or data changes.

Merge checklist:

  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

AI Usage

Kiro was used to aid development and testing.

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
@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Dec 3, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @wgu-taylor-payne!

This repository is currently maintained by @openedx/committers-openedx-authz.

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 approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To 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:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where 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:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

AuthzEnforcer singleton is built without a lock: a process can keep an empty policy and refuse every role

2 participants