Skip to content

feat: add authz schema compilation - #476

Open
rodmgwgu wants to merge 1 commit into
rod/authz-schema-loader-onlyfrom
rod/authz-schema-compiler
Open

rodmgwgu wants to merge 1 commit into
rod/authz-schema-loader-onlyfrom
rod/authz-schema-compiler

Conversation

@rodmgwgu

Copy link
Copy Markdown
Contributor

Problem

Several distributions can contribute to the same authorization schema, and one can extend a role defined by another. The loaded documents need to be resolved into a single set of definitions with per-contribution provenance, and role extensions merged deterministically (ADR 0018 §1, ADR 0023).

Approach

  • openedx_authz/engine/schema/compilation.py — SchemaCompiler
  • openedx_authz/tests/schema/test_compilation.py

Merge rules follow ADR 0023: metadata replace, add/remove permissions, tri-state hidden, and priority to resolve conflicts. An unresolvable equal-priority conflict raises SchemaCompileError rather than picking a winner. Compilation is pure — no database, no Casbin.

Manual testing instructions

pytest openedx_authz/tests/schema/

Rollback plan

Revert this PR. Nothing consumes the compiler yet, so the revert is inert.

Retro compatibility

No authorization behavior changes. No models, no migration, no automatic code path.

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 (3/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. This PR — schema compilation (base: rod/authz-schema-loader-only)
  4. rod/authz-schema-validator — schema validation
  5. rod/authz-schema-models — definition models + migration
  6. rod/authz-schema-renderer — policy renderer
  7. rod/authz-schema-applier — schema applier
  8. feat: add authz schema pipeline and load_authz_schema command #446 — pipeline + load_authz_schema command, version bump and changelog

Compilation lands before validation because the post-compile validation rules — and their tests — operate on a CompiledSchema. The compiler itself does not depend on the validator.

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 loading #475; no migration

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

@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-compiler branch 5 times, most recently from 9c964ac to 2c77167 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
Comment on lines +43 to +47
# Metadata fields an extension may replace on a role.
_METADATA_FIELDS = ("display_name", "description", "icon", "hidden")

# Singular labels for operator-facing messages, keyed by document attribute.
_KIND_LABELS = {"categories": "category", "permissions": "permission", "roles": "role"}

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.

Nit: Could we apply this decorator to a shared build method, with each factory implementing only the part that differs?

role_permission_sources=role_permission_sources,
)

# ---- base collection --------------------------------------------------

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.

nit: do we need these in-line comments?

Comment on lines +104 to +136
if existing.definition == definition:
existing.sources.append(document.source)
elif document.priority > existing.priority:
# The loser is discarded; say so, otherwise the contributing
# file looks like it took effect (ADR 0017 §4).
self._warn_discarded(
kind,
identifier,
loser=existing.sources[0],
loser_priority=existing.priority,
winner=document.source,
winner_priority=document.priority,
)
tracked[identifier] = _Tracked(
definition=definition,
sources=[document.source],
priority=document.priority,
)
elif document.priority == existing.priority:
raise SchemaCompileError(
f"Conflicting {kind} definition for {identifier!r} at equal priority "
f"{document.priority} ({existing.sources[0].source_id} vs {document.source.source_id})."
)
else:
self._warn_discarded(
kind,
identifier,
loser=document.source,
loser_priority=document.priority,
winner=existing.sources[0],
winner_priority=existing.priority,
)
return tracked

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 move this to a different method with a single responsibility of managing priority between documents? Could it be somehow reused to manage conflicts in extensions?


# ---- roles + provenance ----------------------------------------------

def _resolve_roles_and_provenance(

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.

Could we use more descriptive names for md, pc, and rp_sources here? I had to trace them back to understand what they represent, and I think expanding them would make this flow easier to follow.


# ---- roles + provenance ----------------------------------------------

def _resolve_roles_and_provenance(

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.

Also can we move this to a different method with a single responsibility? Resolve _resolve_roles_and_provenance could be
-> apply metadata changes
-> apply permission changes
-> change sources


return rp_sources

def _gather_extension_changes(self, roles: dict[str, _Tracked], documents: list[SchemaDocument]):

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.

Should this be split into two?

pc["remove"].append((perm, document.priority, document.source))
return metadata_changes, perm_changes

def _resolve_metadata(self, role_id: str, md: dict[str, list[tuple[object, int, SourceRecord]]]):

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 reuse the mechanism of managing priority for all cases (base priority, this, etc)?

provenance: dict[str, list[RelationshipSource]] = {
perm: [RelationshipSource(src, SchemaOriginKind.BASE, base_priority) for src in base_sources]
for perm in base
}

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.

Comment on lines +284 to +288
actions: dict[str, list[tuple[str, int, SourceRecord]]] = {}
for perm, priority, src in pc["add"]:
actions.setdefault(perm, []).append(("add", priority, src))
for perm, priority, src in pc["remove"]:
actions.setdefault(perm, []).append(("remove", priority, src))

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 think we could have add/remove methods which could be called directly with a getattr, it could simplify this and the next lines

Comment on lines +299 to +310
winning_sources = [src for act, priority, src in entries if priority == max_priority and act == action]

for act, priority, src in entries:
if priority < max_priority:
self._warn_discarded(
f"role_extension {act} of {perm!r} on role",
role_id,
loser=src,
loser_priority=priority,
winner=winning_sources[0],
winner_priority=max_priority,
)

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 about reusability

@rodmgwgu
rodmgwgu force-pushed the rod/authz-schema-compiler branch from 2c77167 to 3ed5882 Compare September 30, 2026 22:44
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