Skip to content

feat: add authz schema applier - #480

Open
rodmgwgu wants to merge 2 commits into
rod/authz-schema-rendererfrom
rod/authz-schema-applier
Open

rodmgwgu wants to merge 2 commits into
rod/authz-schema-rendererfrom
rod/authz-schema-applier

Conversation

@rodmgwgu

Copy link
Copy Markdown
Contributor

Problem

Rendered rows have to reach the database without damaging anything the schema loader does not own. The apply step shares tables with dynamic roles, user assignments and the legacy g2 action-inheritance rows, so it needs a change report before any write, precise ownership-scoped removal, and a single transaction (ADR 0018 §2, §3, §6, ADR 0025 §6).

Approach

The rest of openedx_authz/engine/renderer.py, on top of the render half in #479.

  • SchemaApplier.plan() — the change report: p rows to add or remove, plus definition-level diffs including metadata-only edits that change no policy row at all
  • SchemaApplier.apply() — one transaction: add missing rows, remove stale rows it owns, prune definition/source records the compiled schema no longer contains, invalidate the policy cache
  • ChangePlan, DefinitionDiff, ApplyResult
  • openedx_authz/tests/schema/{test_apply,test_source_storage}.py, openedx_authz/tests/integration/test_schema_apply.py

Three properties worth reviewing directly:

  • Ownership is consulted, not assumed. Removal candidates are intersected with the stored role-permission grants, so unmanaged p rows, dynamic roles, user assignments and legacy g2 rows survive.
  • Adoption, not duplication (ADR 0025 §6). A rendered row that already exists gains definition and source records without being rewritten, which is why applying to a database that already ran load_policies adds zero rows.
  • Removal is force-gated (ADR 0018 §6). A role slated for removal that still has assignments aborts before any write; with force its rows and assignments go together and a RoleAssignmentAudit record is written per removed assignment.

Manual testing instructions

pytest openedx_authz/tests/

The integration module is excluded from the default pytest run and needs an edx-platform environment. It exercises the real Casbin enforcer and ORM, including enforce() assertions for permission removal, force-removal, adoption and failure rollback:

tutor dev run cms pytest -p no:randomly --create-db --ds=cms.envs.test \
    /mnt/openedx-authz/openedx_authz/tests/integration/test_schema_apply.py

End-to-end exercise through the management command is in 8/8, which is the only thing that calls the applier.

Rollback plan

Revert this PR. Nothing invokes the applier until the command lands in 8/8, so the revert is inert. If it has already run, the definition tables it populated are additive and the Casbin rows it wrote match what authz.policy already defines.

Retro compatibility

No authorization behavior changes. Loading authz.policy works as before, and no code path reaches the applier until the management command lands.

AI Usage

Kiro was used to assist on feature planning and implementation. Implementation was done step by step with human guidance and validation, based on the ADRs.


Stack (7/8) — #446 split into reviewable pieces. Bases chain bottom-up; merge in order.

  1. feat: add authz schema discovery #474 — schema discovery
  2. feat: add authz schema loading #475 — schema loading
  3. feat: add authz schema compilation #476 — schema compilation
  4. feat: add authz schema validation #477 — schema validation
  5. feat: add authz schema definition models #478 — definition models + migration
  6. feat: add authz schema policy renderer #479 — policy renderer
  7. This PR — schema applier (base: rod/authz-schema-renderer)
  8. feat: add authz schema pipeline and load_authz_schema command #446 — pipeline + load_authz_schema command, version bump and changelog

Largest PR in the stack. It is the one part that cannot be subdivided without landing apply() calling methods added in a later PR.

Merge checklist:

  • Version bumped — deferred to 8/8 so the stack does not conflict on every restack
  • Changelog record added — deferred to 8/8
  • Documentation updated (not only docstrings) — covered by the already-merged ADRs
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets — depends on feat: add authz schema policy renderer #479 and the models in feat: add authz schema definition models #478; no migration of its own

@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 Sep 23, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @rodmgwgu!

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.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Sep 23, 2026
@rodmgwgu
rodmgwgu added this pull request to stack #481 September 23, 2026 16:41
@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-applier branch from 2b7dda1 to 912e202 Compare September 23, 2026 17:03
@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-applier branch 6 times, most recently from 28f621d to 225bbb4 Compare September 24, 2026 23:14
@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-applier branch from afd6ef7 to 7cf22fa Compare September 25, 2026 17:00

@carlos-marquez-wgu carlos-marquez-wgu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks!

@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-applier branch from 7cf22fa to a1496aa Compare September 29, 2026 15:12
@mphilbrick211 mphilbrick211 added the mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). label Sep 29, 2026
@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-applier branch from a1496aa to dd31512 Compare September 30, 2026 23:00

@mariajgrimaldi mariajgrimaldi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left a few comments that I think could apply more broadly across the PR, but I didn’t want to repeat the same thing in multiple places. Let me know what you think!

Comment on lines +227 to +231
enforcer = self._resolve_enforcer()

rendered_set = set(rendered.rows)
stored_set = {PolicyRow.from_policy(row) for row in enforcer.get_policy()}
managed_set = self._managed_rows()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use API methods instead of handling the policy directly?

Comment on lines +283 to +285
from django.db import transaction # pylint: disable=import-outside-toplevel

from openedx_authz.engine.enforcer import AuthzEnforcer # pylint: disable=import-outside-toplevel

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this import in this level necessary?

Comment on lines +308 to +321
with transaction.atomic():
for row in plan.added_rows:
enforcer.add_policy(*row.as_policy())
for row in plan.removed_rows:
enforcer.remove_policy(*row.as_policy())
removed_assignments: list[tuple[str, str, str]] = []
if force and plan.blocking_assignments:
removed_assignments = self._remove_assignments(enforcer, plan.blocking_assignments)
self._store_sources(schema)
# Emit the audit events only if the transaction commits, mirroring
# unassign_role_from_subject_in_scope, so no audit row is written for
# an assignment removal that gets rolled back.
if removed_assignments:
transaction.on_commit(lambda: self._emit_assignment_deleted(removed_assignments))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same question here about not using casbin methods but instead low-level APIs? So we encapsulate getting the enforcer and so on.

Comment on lines +322 to +326
except Exception:
# Runs outside the rolled-back block, so the new version commits.
AuthzEnforcer.invalidate_policy_cache()
logger.exception("Authz schema apply failed; policy cache invalidated to force a reload.")
raise

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also don't think we should do the cache invalidation manually

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). mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). open-source-contribution PR author is not from Axim or 2U

Projects

Status: In Eng Review

Development

Successfully merging this pull request may close these issues.

5 participants