Skip to content

Commit e6aa9e4

Browse files
authored
[FC-0099] refactor: remove casbin redis watcher (openedx#109)
1 parent 1a2207a commit e6aa9e4

13 files changed

Lines changed: 24 additions & 137 deletions

File tree

CHANGELOG.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,20 @@ Change Log
1414
Unreleased
1515
**********
1616

17+
*
18+
19+
0.6.0 - 2025-10-22
20+
******************
21+
22+
Changed
23+
=======
24+
1725
* Use a SyncedEnforcer with default auto load policy.
1826

27+
Removed
28+
=======
29+
30+
* Remove Casbin Redis watcher from engine configuration.
1931

2032
0.5.0 - 2025-10-21
2133
******************

openedx_authz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
import os
66

7-
__version__ = "0.5.0"
7+
__version__ = "0.6.0"
88

99
ROOT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

openedx_authz/engine/enforcer.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"""
22
Core authorization enforcer for Open edX AuthZ system.
33
4-
Provides a Casbin FastEnforcer instance with extended adapter for database policy
5-
storage and Redis watcher for distributed policy synchronization.
4+
Provides a Casbin SyncedEnforcer instance with extended adapter for database policy
5+
storage and automatic policy synchronization.
66
77
Components:
8-
- Enforcer: Main FastEnforcer instance for policy evaluation
8+
- Enforcer: Main SyncedEnforcer instance for policy evaluation
99
- Adapter: ExtendedAdapter for filtered database policy loading
10-
- Watcher: Redis-based watcher for real-time policy updates
1110
1211
Usage:
1312
from openedx_authz.engine.enforcer import AuthzEnforcer
1413
allowed = enforcer.enforce(user, resource, action)
1514
16-
Requires `CASBIN_MODEL` setting and Redis configuration for watcher functionality.
15+
Requires `CASBIN_MODEL` setting.
1716
"""
1817

1918
import logging
@@ -23,7 +22,6 @@
2322
from django.conf import settings
2423

2524
from openedx_authz.engine.adapter import ExtendedAdapter
26-
from openedx_authz.engine.watcher import Watcher
2725

2826
logger = logging.getLogger(__name__)
2927

@@ -32,7 +30,7 @@ class AuthzEnforcer:
3230
"""Singleton class to manage the Casbin SyncedEnforcer instance.
3331
3432
Ensures a single enforcer instance is created safely and configured with the
35-
ExtendedAdapter and Redis watcher for policy management and synchronization.
33+
ExtendedAdapter for policy management and automatic synchronization.
3634
3735
There are two main use cases for this class:
3836
@@ -75,13 +73,12 @@ def _initialize_enforcer() -> SyncedEnforcer:
7573
"""
7674
Create and configure the Casbin SyncedEnforcer instance.
7775
78-
This method initializes the FastEnforcer with the ExtendedAdapter
79-
for database policy storage and sets up the Redis watcher for real-time
80-
policy synchronization if the Watcher is available. It also initializes
81-
the enforcer with the specified database alias from settings.
76+
This method initializes the SyncedEnforcer with the ExtendedAdapter
77+
for database policy storage and automatic policy synchronization.
78+
It also initializes the enforcer with the specified database alias from settings.
8279
8380
Returns:
84-
SyncedEnforcer: Configured Casbin enforcer with adapter and watcher
81+
SyncedEnforcer: Configured Casbin enforcer with adapter and auto-sync
8582
"""
8683
db_alias = getattr(settings, "CASBIN_DB_ALIAS", "default")
8784

@@ -99,14 +96,4 @@ def _initialize_enforcer() -> SyncedEnforcer:
9996
enforcer.start_auto_load_policy(settings.CASBIN_AUTO_LOAD_POLICY_INTERVAL)
10097
enforcer.enable_auto_save(True)
10198

102-
if not Watcher:
103-
logger.warning("Redis configuration not completed successfully. Watcher is disabled.")
104-
return enforcer
105-
106-
try:
107-
enforcer.set_watcher(Watcher)
108-
logger.info("Watcher successfully set on Casbin enforcer")
109-
except Exception as e: # pylint: disable=broad-exception-caught
110-
logger.error(f"Failed to set watcher on Casbin enforcer: {e}")
111-
11299
return enforcer

openedx_authz/engine/watcher.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

openedx_authz/settings/common.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,5 @@ def plugin_settings(settings):
2424
settings.CASBIN_MODEL = os.path.join(
2525
ROOT_DIRECTORY, "engine", "config", "model.conf"
2626
)
27-
settings.CASBIN_WATCHER_ENABLED = False
2827
if not hasattr(settings, "CASBIN_AUTO_LOAD_POLICY_INTERVAL"):
2928
settings.CASBIN_AUTO_LOAD_POLICY_INTERVAL = 5
30-
# TODO: Replace with a more dynamic configuration
31-
# Redis host and port are temporarily loaded here for the MVP
32-
settings.REDIS_HOST = "redis"
33-
settings.REDIS_PORT = 6379

openedx_authz/settings/test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,4 @@ def plugin_settings(settings): # pylint: disable=unused-argument
6969

7070
# Casbin configuration
7171
CASBIN_MODEL = os.path.join(ROOT_DIRECTORY, "engine", "config", "model.conf")
72-
CASBIN_AUTO_LOAD_POLICY_INTERVAL = 1
73-
CASBIN_WATCHER_ENABLED = False
74-
REDIS_HOST = "redis"
75-
REDIS_PORT = 6379
72+
CASBIN_AUTO_LOAD_POLICY_INTERVAL = 0

openedx_authz/tests/api/test_roles.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,9 @@ def setUpClass(cls):
9393
to add their specific role assignments by calling _assign_roles_to_users.
9494
"""
9595
super().setUpClass()
96+
AuthzEnforcer.get_enforcer().stop_auto_load_policy()
9697
cls._seed_database_with_policies()
9798

98-
@classmethod
99-
def tearDownClass(cls):
100-
"""Clean up after all tests in the class.
101-
102-
Stops the auto-load policy thread to prevent database locking issues
103-
with SQLite during concurrent access.
104-
"""
105-
super().tearDownClass()
106-
enforcer = AuthzEnforcer.get_enforcer()
107-
if hasattr(enforcer, 'stop_auto_load_policy'):
108-
enforcer.stop_auto_load_policy()
109-
11099
def setUp(self):
111100
"""Set up test environment."""
112101
super().setUp()

requirements/base.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ openedx-atlas # Open edX Atlas library
77
attrs # Classes without boilerplate
88
pycasbin # Authorization library for implementing access control models
99
casbin-django-orm-adapter # Adapter for Django ORM for Casbin
10-
redis-watcher # Watcher for Redis for Casbin
1110
edx-opaque-keys # Opaque keys for resource identification
1211
edx-api-doc-tools # Tools for API documentation
1312
edx-drf-extensions # Extensions for Django Rest Framework used by Open edX

requirements/base.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ pycasbin==2.2.0
7878
# via
7979
# -r requirements/base.in
8080
# casbin-django-orm-adapter
81-
# redis-watcher
8281
pycparser==2.23
8382
# via cffi
8483
pyjwt[crypto]==2.10.1
@@ -93,10 +92,6 @@ pytz==2025.2
9392
# via drf-yasg
9493
pyyaml==6.0.3
9594
# via drf-yasg
96-
redis==6.4.0
97-
# via redis-watcher
98-
redis-watcher==1.8.0
99-
# via -r requirements/base.in
10095
requests==2.32.5
10196
# via edx-drf-extensions
10297
semantic-version==2.10.0

requirements/dev.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ pycasbin==2.2.0
223223
# via
224224
# -r requirements/quality.txt
225225
# casbin-django-orm-adapter
226-
# redis-watcher
227226
pycodestyle==2.14.0
228227
# via -r requirements/quality.txt
229228
pycparser==2.23
@@ -302,12 +301,6 @@ pyyaml==6.0.3
302301
# code-annotations
303302
# drf-yasg
304303
# edx-i18n-tools
305-
redis==6.4.0
306-
# via
307-
# -r requirements/quality.txt
308-
# redis-watcher
309-
redis-watcher==1.8.0
310-
# via -r requirements/quality.txt
311304
requests==2.32.5
312305
# via
313306
# -r requirements/quality.txt

0 commit comments

Comments
 (0)