Repository navigation
fix(access-control): let newest policies win - #680
Conversation
Add `policies_revision`: instances with an older revision than the stored one leave configured rules alone, with a warning, so that old pods restarting in a rolling upgrade don't bring back old rules. The check is part of the write that activates the rules. `policies_force` is the deliberate way to overwrite, for rollbacks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Force also without rules, for the revision, and limit revisions to MongoDB's integers. Describe what old pods enforce, rollbacks, versions before revisions and shared policy collections. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Revision options without access control have no effect, so refuse them like the other options. Say what an unset `policies` stores. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s-win # Conflicts: # tests/models/test_config.py
Reviewer's GuideIntroduces revision-stamped access-control policies with conditional generation activation, preventing stale pods from overwriting newer rules during rolling upgrades while supporting explicit forced rollbacks; configuration, startup behavior, documentation, and concurrency-related tests are updated accordingly. Sequence diagram for revisioned policy activationsequenceDiagram
participant Pod as Application pod
participant Adapter
participant MongoDB
Pod->>Adapter: set_configured_policies(rules, revision, force)
Adapter->>MongoDB: find_one(_id=configured_policies)
alt stored revision is newer and force is false
Adapter-->>Pod: false
Adapter-->>Pod: Log warning
else eligible or forced update
Adapter->>MongoDB: Store new policy generation
Adapter->>MongoDB: find_one_and_update(marker, revision condition)
alt activation succeeds
MongoDB-->>Adapter: Previous generation
Adapter-->>Pod: true
else concurrent newer revision wins
MongoDB-->>Adapter: No activation
Adapter-->>Pod: false
end
end
File-Level Changes
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 #680 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 60 60
Lines 7807 7828 +21
=========================================
+ Hits 7807 7828 +21
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="tests/security/access_control/test_register_casbin_enforcer.py" line_range="484-486" />
<code_context>
+ assert self._start(collection, new, revision=2) == {"new"}
+ assert self._start(collection, None, revision=1) == {"new"}
+ assert "Removed stored" not in caplog.text
+ assert self._start(collection, None, revision=1, force=True) == set()
+ assert "Removed stored" in caplog.text
+
+ def test_forced_without_rules(self) -> None:
+ """Forcing also stores the revision if there are no rules to remove."""
+ collection: Collection[dict[str, Any]] = (
+ mongomock.MongoClient().db.revisions_empty
+ )
+ self._start(collection, [], revision=3)
+ assert self._start(collection, None, revision=1, force=True) == set()
+ rules = [["p", "new", "/a", "GET"]]
+ assert self._start(collection, rules, revision=2) == {"new"}
+
+ def test_rollback(self) -> None:
</code_context>
<issue_to_address>
**Cleared revision passes test**
When forced removal clears the stored revision instead of storing revision 1, `test_forced_without_rules` accepts a cleared revision just as it accepts revision 1: the later revision-2 write succeeds in either case, so the test passes even though older pods would no longer be blocked by the forced revision.
Assert that the stored marker revision is 1 after the forced removal, or verify that an older revision is rejected afterward.
</issue_to_address>Linked issues
#612 fix(access-control): let newest policies win · 7 of 7 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Stamp configured policies with an ordered revision; older pods must leave newer rules alone and log a warning. | ✅ Met | policies_revision is stored on the generation marker; older or unversioned updates are skipped with a warning. |
| 2 | Make activation conditional on the stored revision so concurrent starts cannot undo newer rules. | ✅ Met | _activate_generation includes the revision condition in the marker update, and handles a conflicting newer marker. |
| 3 | Document a deliberate way to overwrite rules for rollbacks. | ✅ Met | The deployment guide documents policies_force: true for rollback and advises removing it afterward. |
| 4 | Test old-after-new, new-after-old, rollback, and first start without a stamp. | ✅ Met | The revision adapter tests cover each scenario, including skipped older and unversioned updates. |
| 5 | Maintain 100% test coverage. | ✅ Met | The PR reports 100% coverage; the added tests exercise the revision and forced-update paths. |
| 6 | Document the behavior in the deployment guide. | ✅ Met | docs/deployment.md adds a section on revisions, rolling upgrades, rollbacks, older versions, and shared collections. |
| 7 | Let the foca CLI issue's sync-policies use the same code. |
✅ Met | Adapter.set_configured_policies provides the shared revision-aware operation and returns whether the update took effect. |
Sourcery assessment
Needs a human reviewer. 1 finding to address first, and the stored revision controls which configured policy generation becomes active, so a faulty revision check or forced rollback could leave rules active that grant unintended access. Reverting the code would not undo requests already authorized under those rules or restore the previous stored policies.
Blocking findings: tests/security/access_control/test_register_casbin_enforcer.py:486
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Linked issues
#612 fix(access-control): let newest policies win · 6 of 7 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Older pods leave newer configured rules alone and log a warning, including when starts race. | ✅ Met | Revision checks and conditional activation are implemented; tests cover older and unrevisioned pods, plus a newer revision stored between the initial check and activation. |
| 2 | A new pod replaces older rules. | ✅ Met | Tests cover replacement by the same or a newer revision, and replacement of unstamped rules. |
| 3 | Provide a deliberate rollback path. | ✅ Met | policies_force bypasses the stored-revision check, and the docs explain how to use it for rollbacks. |
| 4 | Handle the first start without a stamp. | ✅ Met | Tests verify that a revisioned update takes over from previously unstamped rules. |
| 5 | Keep 100% coverage. | ✅ Met | The PR reports 100% coverage, and adds tests for revision ordering, rollback, startup removal, and concurrent activation. |
| 6 | Document the behavior in the deployment guide. | ✅ Met | docs/deployment.md describes revisions, rollbacks, old-version pods, and shared policy collections. |
| 7 | Have the CLI sync-policies use the same code. |
🟡 Partly met | The adapter exposes the revision-aware update method and its applied-status result, but this diff adds no CLI sync-policies caller. |
…s-win # Conflicts: # docs/configuration/access-control.md # foca/security/access_control/register_access_control.py
Summary
Closes #612.
Keeps older pods from overwriting or removing the configured policy rules of newer ones during rolling upgrades.
security.access_control.policies_revision: optional integer stamp for the configured rules, stored on the marker document. A pod with an older revision than the stored one leaves the stored rules alone and logs a warning. Activation of a generation is conditional on the stored revision, so concurrent starts can't undo this either.security.access_control.policies_force: a deliberate rollback, for example, writes the rules and revision whatever is stored.Adapter.set_configured_policies(rules, revision=None, force=False)returns whether the rules are now in effect; feat(cli): add foca CLI and startup switches #621'ssync-policiescan use it.docs/deployment.md(rolling upgrades),docs/access-control.md, configuration reference.Verification
ruff check,ruff format --check,mypy foca, strict docs build, fullpytestagainst a local MongoDB: 3570 passed, 1 skipped, 100% coverage, before mergingdev; after the merge, the affected test files again (337 passed).toYaml, values and--set) was checked by the reviewer on a minimal chart, not onfoca-common.Declined review findings
generation. Documented rather than solved: an optionalrevision_generationfield would add state for one upgrade only. Protection needs all pods on this version.policieson an empty collection stores no revision: an idle restart should stay a no-op. Documented; it's not the normal upgrade path.Open questions
None. Design choices for the maintainer to look at when reading this: an integer revision (rather than a version string), and rollbacks staying sticky until a newer revision.
🤖 Generated with Claude Code
Summary by Sourcery
Protect configured access-control policies from stale instances during rolling upgrades while supporting deliberate revisioned rollbacks.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: