Skip to content

[FC-0099] refactor: manage enforcer state and casbin models only when app is ready#93

Merged
mariajgrimaldi merged 21 commits intomainfrom
MJG/manage-enforcer-safely
Oct 16, 2025
Merged

[FC-0099] refactor: manage enforcer state and casbin models only when app is ready#93
mariajgrimaldi merged 21 commits intomainfrom
MJG/manage-enforcer-safely

Conversation

@mariajgrimaldi
Copy link
Copy Markdown
Member

@mariajgrimaldi mariajgrimaldi commented Oct 10, 2025

Description

This PR fixes an issue with including the library in non-testing environments since the Casbin adapter was trying to access the database when the apps were not fully loaded, raising errors like:


 > [production 19/37] RUN ./manage.py lms --settings=tutor.i18n pull_plugin_translations --verbose --repository='openedx/openedx-translations' --revision='release/teak.2':
4.409 Traceback (most recent call last):
4.409   File "/openedx/edx-platform/./manage.py", line 99, in <module>
4.409     execute_from_command_line([sys.argv[0]] + django_args)
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
4.409     utility.execute()
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 416, in execute
4.409     django.setup()
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
4.409     apps.populate(settings.INSTALLED_APPS)
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/apps/registry.py", line 124, in populate
4.409     app_config.ready()
4.409   File "/openedx/venv/lib/python3.11/site-packages/casbin_adapter/apps.py", line 12, in ready
4.409     initialize_enforcer(db_alias)
4.409   File "/openedx/venv/lib/python3.11/site-packages/casbin_adapter/enforcer.py", line 67, in initialize_enforcer
4.409     with connect.cursor() as cursor:
4.409          ^^^^^^^^^^^^^^^^
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
4.409     return func(*args, **kwargs)
4.409            ^^^^^^^^^^^^^^^^^^^^^
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 330, in cursor
4.409     return self._cursor()
4.409            ^^^^^^^^^^^^^^
4.409   File "/openedx/venv/lib/python3.11/site-packages/django/db/backends/dummy/base.py", line 20, in complain
4.409     raise ImproperlyConfigured(
4.409 django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
------
Dockerfile:194
--------------------
 192 |     # Pull latest translations via atlas
 193 |     RUN make clean_translations
 194 | >>> RUN ./manage.py lms --settings=tutor.i18n pull_plugin_translations --verbose --repository='openedx/openedx-translations' --revision='release/teak.2' 
 195 |     RUN ./manage.py lms --settings=tutor.i18n pull_xblock_translations --repository='openedx/openedx-translations' --revision='release/teak.2' 
 196 |     RUN atlas pull --repository='openedx/openedx-translations' --revision='release/teak.2'  \
--------------------
ERROR: failed to solve: process "/bin/sh -c ./manage.py lms --settings=tutor.i18n pull_plugin_translations --verbose --repository='openedx/openedx-translations' --revision='release/teak.2'" did not complete successfully: exit code: 1

Now the initialization of the enforcer happens once when it's first used when the application is fully loaded. See https://github.com/officialpycasbin/django-orm-adapter/blob/5196741aadf6060023e933abedaac93e50008cc6/casbin_adapter/enforcer.py#L63-L79 for more details on what the initialization entails.

@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 Oct 10, 2025
@openedx-webhooks
Copy link
Copy Markdown

openedx-webhooks commented Oct 10, 2025

Thanks for the pull request, @mariajgrimaldi!

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.

Comment on lines +57 to +58
if cls._enforcer is None:
cls._enforcer = cls._initialize_enforcer()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this would be needed.

Comment thread openedx_authz/apps.py Outdated
@mariajgrimaldi mariajgrimaldi changed the title refactor: manage enforcer state within class when app is ready refactor: manage enforcer state and casbin models only when app is ready Oct 13, 2025
@mariajgrimaldi mariajgrimaldi force-pushed the MJG/manage-enforcer-safely branch from 6fedcea to bcae860 Compare October 13, 2025 12:21
@mariajgrimaldi mariajgrimaldi changed the title refactor: manage enforcer state and casbin models only when app is ready [FC-0099] refactor: manage enforcer state and casbin models only when app is ready Oct 14, 2025
@mariajgrimaldi mariajgrimaldi force-pushed the MJG/manage-enforcer-safely branch from d875701 to 5996ef9 Compare October 14, 2025 11:06
@mariajgrimaldi mariajgrimaldi marked this pull request as ready for review October 14, 2025 11:18
Copy link
Copy Markdown
Contributor

@BryanttV BryanttV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mariajgrimaldi, thanks for this! I was testing the REST API with these changes, and all works fine except for the permission validation.

I also built the openedx image, and it completed successfully 🚀

Comment thread openedx_authz/api/permissions.py Outdated
Copy link
Copy Markdown
Contributor

@BryanttV BryanttV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mphilbrick211 mphilbrick211 added the FC Relates to an Axim Funded Contribution project label Oct 15, 2025
@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Oct 15, 2025
@mariajgrimaldi mariajgrimaldi merged commit a3b5cdd into main Oct 16, 2025
13 of 14 checks passed
@mariajgrimaldi mariajgrimaldi deleted the MJG/manage-enforcer-safely branch October 16, 2025 13:49
@github-project-automation github-project-automation Bot moved this from In Eng Review to Done in Contributions Oct 16, 2025
BryanttV pushed a commit to eduNEXT/openedx-authz that referenced this pull request Oct 16, 2025
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). FC Relates to an Axim Funded Contribution project open-source-contribution PR author is not from Axim or 2U

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

5 participants