Skip to content

refactor!: remove get_enforcer() - #477

Merged
uniqueg merged 3 commits into
devfrom
refactor/remove-get-enforcer
Oct 24, 2026
Merged

uniqueg merged 3 commits into
devfrom
refactor/remove-get-enforcer

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

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."

  • Removes get_enforcer() from foca.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 use locked_enforcer(). FOCA's own code no longer called it. EnforcerCache.get_enforcer() (internal) stays.
  • Removed from the API reference (docs/reference.md) and docs/access-control.md; the "Shared enforcer" section of docs/migration-0.18.md now says it is removed and shows locked_enforcer().
  • Tests use locked_enforcer() (converted with an AST-based script, then reviewed by hand where identity checks were involved). test_remove_resource_does_not_load_policies now patches _get_enforcer_cache, so any route to the enforcer fails it (the old target, get_enforcer, was no longer called by anything).

Verification

  • pytest with MongoDB 7 (coverage), after merging dev (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 passed
  • mkdocs build --strict: no warnings
  • CI: all 29 checks passed (2 skipped: image push/publish); Sourcery approved.
  • Review: adversarial + quality reviewers, one round (no high or medium findings; lows fixed: migration wording, stronger monkeypatch target, simpler test_shared).

Declined review findings

  • Helper for the repeated one-line with locked_enforcer() as enforcer: assert enforcer.get_named_grouping_policy("g2") == ... blocks in test_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

uniqueg and others added 3 commits October 24, 2026 16:22
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]>
@sourcery-ai

sourcery-ai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This breaking refactor removes the obsolete public get_enforcer() API, requiring callers to access the shared enforcer through locked_enforcer() so reads and mutations occur while its synchronization lock is held. Tests are migrated accordingly, including cache-sharing and resource-removal coverage, while documentation and API reference entries explain the removal and safe replacement.

Sequence diagram for lock-safe shared enforcer access

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Remove the public unlocked enforcer accessor and migrate all usage to the lock-safe context manager.
  • Delete get_enforcer() from the enforcement module.
  • Update access-control tests to perform reads and mutations inside locked_enforcer() scopes.
  • Retain the internal cache accessor and strengthen the resource-removal test to fail on any enforcer-cache access.
foca/security/access_control/enforcement.py
tests/security/access_control/test_enforcement.py
tests/security/access_control/test_enforcer_cache.py
tests/security/access_control/test_ownership.py
tests/security/access_control/test_service_clients.py
Update documentation and API metadata for the breaking API removal.
  • Remove get_enforcer() from the generated API reference.
  • Document that the accessor is removed and provide a lock-held locked_enforcer() example.
  • Clarify that the lock should be held briefly and that callers must not retain or directly mutate the enforcer.
docs/access-control.md
docs/migration-0.18.md
docs/reference.md

Assessment against linked issues

Issue Objective Addressed Explanation
#468 Remove the public get_enforcer() API from the enforcement module and API reference, while retaining the internal cache method if needed. ✅
#468 Update tests and callers to use locked_enforcer() and preserve full test coverage. ✅
#468 Update access-control and 0.18 migration documentation to direct callers to locked_enforcer(). ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b2cc3f3) to head (99b73e0).

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     
Flag Coverage Δ
test_unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review October 24, 2026 14:51

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread docs/migration-0.18.md Resolved
@uniqueg
uniqueg merged commit f6a2467 into dev Oct 24, 2026
32 checks passed
@uniqueg
uniqueg deleted the refactor/remove-get-enforcer branch October 24, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: remove get_enforcer()

1 participant