Skip to content

feat: add an audit trail for custom role changes - #685

Draft
carlos-marquez-wgu wants to merge 1 commit into
openedx:mainfrom
WGU-Open-edX:carlos-marquez-wgu/issue-588-custom-roles-audit-trail
Draft

carlos-marquez-wgu wants to merge 1 commit into
openedx:mainfrom
WGU-Open-edX:carlos-marquez-wgu/issue-588-custom-roles-audit-trail

Conversation

@carlos-marquez-wgu

@carlos-marquez-wgu carlos-marquez-wgu commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Records every change to a custom role, as decided in ADR 0037.

  • New RoleDefinitionAudit table (migration 0016_role_definition_audit). Each record has an operation (created, updated or deleted), a change_id, the role, the actor, a timestamp and the changes:
    • created and deleted: the full role (display_name, description, scope_types, permissions).
    • updated: only what changed, for example {"description": {"old": "...", "new": "..."}, "permissions_added": [...], "permissions_removed": [...]}. Empty permission lists are left out.
  • Events. create_custom_role, update_custom_role and remove_custom_role send ROLE_DEFINITION_CREATED, ROLE_DEFINITION_UPDATED or ROLE_DEFINITION_DELETED once the transaction commits. A new handler writes the record.
  • Deployments. load_authz_schema --drop-custom-role-permissions writes one updated record, with no actor, for each custom role that loses grants.
  • Linking assignments to role changes. RoleAssignmentAudit gains role_change_id. Each assignment deleted with a removed custom role carries that removal's change_id. unassign_role_from_subject_in_scope accepts it as an optional keyword argument.
  • Django admin. A read-only list of the records, filterable by operation, role, actor and date. It shows the role's display name, also for removed roles, and links each record to the assignment changes it caused.
  • New setting AUTHZ_AUDIT_RECORDS_ENABLED, on by default. Turning it off stops both audit handlers from writing records. The events are still sent.

Supporting information

Concerns

  • Depends on an unreleased openedx-events version. CI fails on the new imports until it's released. Then pyproject.toml and uv.lock need the new minimum version, this PR will be updated once openedx-events changes are published.
  • Edits that change nothing are recorded, with changes = {}. Reviewers: should these be dropped instead? It's a one-line change.
  • Records are written after the commit, not in the same transaction. Issue Implementation: Custom roles audit trail #588 says "in the same transaction", but ADR 0037 §4 sends the events with transaction.on_commit, as ADR 0012 does. I followed the ADR. A rolled-back change records nothing. If the audit write fails, the role change still applies, and the error is logged.

Testing instructions

1. Install openedx-events

If the openedx-events release is on PyPI:

# in pyproject.toml: "openedx-events>=<new version>"
uv lock --upgrade-package openedx-events
uv sync --locked --python 3.12

In Tutor, the new version is installed when the image is rebuilt with these requirements.

If the openedx-events PR is still open:

# Using my branch on WGU's public github repo, I'll remove this step once openedx-events is released
git clone -b carlos-marquez-wgu/issue-588-custom-roles-audit-trail https://github.com/WGU-Open-edX/openedx-events.git ../openedx-events
uv sync --locked --python 3.12
uv pip install --python .venv/bin/python -e ../openedx-events

uv run, uv sync and tox reinstall the released version, so run the tests with .venv/bin/python -m pytest instead.

In Tutor, mount the clone and restart:

tutor mounts add /path/to/openedx-events
tutor dev launch    # or: tutor dev stop && tutor dev start -d

2. Unit tests

.venv/bin/python -m pytest -q          # or: uv run --locked pytest -q, with the PyPI release

The new tests are in tests/api/test_custom_roles.py (TestCustomRoleAuditTrail), tests/test_handlers.py, tests/schema/test_apply.py and tests/test_admin.py.

3. Manual test on a Tutor dev stack

  1. Apply the migration:

    tutor dev exec lms ./manage.py lms migrate openedx_authz
  2. As a staff user, create, edit and remove a custom role through the API:

    • POST /api/authz/v1/roles/ with {"display_name": "Audit Check", "scope_types": ["course-v1"], "permissions": ["courses.create_files"]}
    • PATCH /api/authz/v1/roles/custom_audit_check/ with a new description and an extra permission
    • Assign the role to a user in a course, then DELETE /api/authz/v1/roles/custom_audit_check/?assignments=revoke
  3. Open Django admin → Open edX AuthZ → Role Definition Audits and check that:

    • there's a created, an updated and a deleted record, each with your user ID as the actor;
    • the updated record lists only the changed description and the added permission;
    • the list shows "Audit Check" as the display name, even though the role is removed;
    • "View assignment changes" on the deleted record lists the deleted assignment.
  4. Optional: set AUTHZ_AUDIT_RECORDS_ENABLED = False, repeat step 2 with another name, and check that no new records are written.


Merge checklist:
Check off if complete or not applicable:

  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

Record each create, edit and removal of a custom role in a new
RoleDefinitionAudit table (ADR 0037). The custom role API sends
ROLE_DEFINITION_* events on commit and a handler writes the record:
the full role for created and deleted, only the changed fields for
updated.

A deployment with --drop-custom-role-permissions writes one updated
record per affected role. RoleAssignmentAudit gains role_change_id,
which links the assignments deleted with a role to that removal.

Add a read-only Django admin for the records and the
AUTHZ_AUDIT_RECORDS_ENABLED setting to turn off both audit handlers.
@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 Dec 4, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @carlos-marquez-wgu!

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.

🔘 Update the status of your PR

Your PR is currently marked as a draft. After completing the steps above, update its status by clicking "Ready for Review", or removing "WIP" from the title, as appropriate.


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.

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). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

Implementation: Custom roles audit trail

2 participants