Repository navigation
feat: add an audit trail for custom role changes - #685
carlos-marquez-wgu wants to merge 1 commit into
Conversation
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.
|
Thanks for the pull request, @carlos-marquez-wgu! This repository is currently maintained by 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 approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo 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:
🔘 Get a green buildIf 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 PRYour 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:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
Description
Records every change to a custom role, as decided in ADR 0037.
RoleDefinitionAudittable (migration0016_role_definition_audit). Each record has an operation (created,updatedordeleted), achange_id, the role, the actor, a timestamp and the changes:createdanddeleted: 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.create_custom_role,update_custom_roleandremove_custom_rolesendROLE_DEFINITION_CREATED,ROLE_DEFINITION_UPDATEDorROLE_DEFINITION_DELETEDonce the transaction commits. A new handler writes the record.load_authz_schema --drop-custom-role-permissionswrites oneupdatedrecord, with no actor, for each custom role that loses grants.RoleAssignmentAuditgainsrole_change_id. Each assignment deleted with a removed custom role carries that removal'schange_id.unassign_role_from_subject_in_scopeaccepts it as an optional keyword argument.AUTHZ_AUDIT_RECORDS_ENABLED, on by default. Turning it off stops both audit handlers from writing records. The events are still sent.Supporting information
RoleAssignmentData.role_change_id.Concerns
pyproject.tomlanduv.lockneed the new minimum version, this PR will be updated once openedx-events changes are published.changes = {}. Reviewers: should these be dropped instead? It's a one-line change.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.12In 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-eventsuv run,uv syncandtoxreinstall the released version, so run the tests with.venv/bin/python -m pytestinstead.In Tutor, mount the clone and restart:
tutor mounts add /path/to/openedx-events tutor dev launch # or: tutor dev stop && tutor dev start -d2. Unit tests
.venv/bin/python -m pytest -q # or: uv run --locked pytest -q, with the PyPI releaseThe new tests are in
tests/api/test_custom_roles.py(TestCustomRoleAuditTrail),tests/test_handlers.py,tests/schema/test_apply.pyandtests/test_admin.py.3. Manual test on a Tutor dev stack
Apply the migration:
tutor dev exec lms ./manage.py lms migrate openedx_authzAs 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 newdescriptionand an extra permissionDELETE /api/authz/v1/roles/custom_audit_check/?assignments=revokeOpen Django admin → Open edX AuthZ → Role Definition Audits and check that:
created, anupdatedand adeletedrecord, each with your user ID as the actor;updatedrecord lists only the changed description and the added permission;deletedrecord lists the deleted assignment.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: