Repository navigation
refactor!: remove get_enforcer() - #477
Conversation
Since the enforcer cache, get_enforcer() returned the enforcer shared by the threads of an app instance without holding its lock, so another thread could update it while in use. FOCA 0.18.0 is not released yet, so remove it instead of deprecating it; callers use locked_enforcer(). Tests now use locked_enforcer() too. BREAKING CHANGE: get_enforcer() is removed from foca.security.access_control.enforcement; use locked_enforcer(). Closes #468 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis breaking refactor removes the obsolete public Sequence diagram for lock-safe shared enforcer accesssequenceDiagram
participant Caller
participant locked_enforcer
participant SharedEnforcer
Caller->>locked_enforcer: locked_enforcer()
locked_enforcer->>SharedEnforcer: acquire lock
locked_enforcer-->>Caller: enforcer
Caller->>SharedEnforcer: enforce(subject, path, method)
Caller->>locked_enforcer: exit context
locked_enforcer->>SharedEnforcer: release lock
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #477 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 3447 3444 -3
=========================================
- Hits 3447 3444 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="docs/migration-0.18.md" line_range="19" />
<code_context>
-date with the stored rules, instead of a new enforcer with all rules loaded
-for each call.
+`get_enforcer()` (in `foca.security.access_control.enforcement`), which
+returned a new enforcer with all rules loaded for each call, is removed.
+The enforcer is now shared by the threads of the app instance and kept up
+to date with the stored rules; use it via the new context manager
</code_context>
<issue_to_address>
**Migration guide misstates prior behavior**
When a user relies on the migration guide to understand the previous API’s behavior, readers are told the removed function created a fresh enforcer on each call, although it returned the shared cached enforcer without holding its lock; they may misunderstand that this migration changes the locking requirement, not enforcer sharing.
Describe `get_enforcer()` as returning the shared enforcer without holding its lock.
</issue_to_address>Sourcery assessment
Approved.
Summary
Maintainer decision of 2026-10-24 for the unreleased 0.18.0: "Remove in 0.18. We're still moving fast with small apps. No need to deprecate and then remove 3 days later."
get_enforcer()fromfoca.security.access_control.enforcement. Since the enforcer cache (perf(access-control): cache the policy enforcer #450/PR perf(access-control): cache the policy enforcer #454), it returned the shared enforcer without holding its lock; callers uselocked_enforcer(). FOCA's own code no longer called it.EnforcerCache.get_enforcer()(internal) stays.docs/reference.md) anddocs/access-control.md; the "Shared enforcer" section ofdocs/migration-0.18.mdnow says it is removed and showslocked_enforcer().locked_enforcer()(converted with an AST-based script, then reviewed by hand where identity checks were involved).test_remove_resource_does_not_load_policiesnow patches_get_enforcer_cache, so any route to the enforcer fails it (the old target,get_enforcer, was no longer called by anything).Verification
pytestwith MongoDB 7 (coverage), after mergingdev(incl. feat!: tighten group checks and error details #475): 2326 passed, 1 skipped;coverage report: 100%mypy: no issues;ruff check: clean;pre-commit run --all-files: all hooks passedmkdocs build --strict: no warningstest_shared).Declined review findings
Helper for the repeated one-line
with locked_enforcer() as enforcer: assert enforcer.get_named_grouping_policy("g2") == ...blocks intest_enforcement.py(optional readability): declined, the explicit blocks show the intended usage, and a helper returning rules would be a second way to read them.Sourcery: describe the removed
get_enforcer()as returning the shared enforcer without its lock: declined, the guide describes 0.17.0 (a new enforcer per call); the shared variant was never released.Open questions
None.
Closes #468
🤖 Generated with Claude Code