django-unique-audit
An app that creates a unique django audit log across multiple instances.
It aims to provide consistent audit logs even on different DBs.
This project was developed based on django-easy-audit.
Quickstart
-
Install Django Unique Audit by running
pip install django-unique-audit.Alternatively, you can download the latest release from GitHub, unzip it, and place the folder 'unique_audit' in the root of your project.
-
Add 'unique_audit' to your
INSTALLED_APPSlike this:INSTALLED_APPS = [ ... 'unique_audit', ]
-
Add Unique Audit's middleware to your
MIDDLEWARE(orMIDDLEWARE_CLASSES) setting like this:MIDDLEWARE = ( ... 'unique_audit.middleware.unique_audit.UniqueAuditMiddleware', )
-
Run
python manage.py migrate unique_auditto create the app's models. -
That's it! Now every CRUD event on your whole project will be registered in the audit models, which you will be able to query from the Django admin app. Additionally, this app will also log all authentication events and all URLs requested.
Settings
For an exhaustive list of available settings, please check our wiki.
Below are some of the settings you may want to use. These should be defined in your project's settings.py file:
-
DJANGO_UNIQUE_AUDIT_WATCH_MODEL_EVENTS -
DJANGO_UNIQUE_AUDIT_WATCH_AUTH_EVENTS -
DJANGO_UNIQUE_AUDIT_WATCH_REQUEST_EVENTSSet these to
Falseto stop logging model, authentication, and/or request events. -
DJANGO_UNIQUE_AUDIT_UNREGISTERED_CLASSES_EXTRAA list of Django models which will be ignored by Django Unique Audit. Use it to prevent logging one or more of your project's models. List items can be classes or strings with
app_name.model_nameformat. -
DJANGO_UNIQUE_AUDIT_UNREGISTERED_URLS_EXTRAA list of URLs which will be ignored by Django Unique Audit. List items are expected to be regular expressions that will be matched against the URL path. Check our wiki for more details on how to use it.
-
DJANGO_Unique_AUDIT_CRUD_DIFFERENCE_CALLBACKSMay point to a list of callables/string-paths-to-functions-classes in which the application code can determine on a per CRUDEvent whether or not the application chooses to create the CRUDEvent or not. This is different from the registered/unregistered settings (e.g.
DJANGO_UNIQUE_AUDIT_UNREGISTERED_CLASSES_EXTRA). This is meant to be for dynamic configurations where the application may inspect the current save/create/delete and choose whether or not to save that into the database or ignore it. -
DJANGO_UNIQUE_AUDIT_USER_DB_CONSTRAINTDefault is
True. This is reserved for future use (does not do anything yet). The functionality provided by the setting (whether enabled or disabled) could be handled more explicitly in certain code paths (or even internally as custom model managers). For projects that separate the unique audit database, such that the tables are not on the same database as the user table, this could help with making certain queries easier. Again, this doesn't do anything yet, and if it ever does, the version will be increased and the README will be updated accordingly. If you keep your database together (the standard usage), you have nothing to worry about. -
DJANGO_UNIQUE_AUDIT_CRUD_EVENT_LIST_FILTER -
DJANGO_UNIQUE_AUDIT_LOGIN_EVENT_LIST_FILTER -
DJANGO_UNIQUE_AUDIT_REQUEST_EVENT_LIST_FILTERChangeview filters configuration. Used to remove filters when the corresponding list of data would be too long. Defaults are:
- ['event_type', 'content_type', 'user', 'datetime', ] for CRUDEventAdmin
- ['login_type', 'user', 'datetime', ] for LoginEventAdmin
- ['method', 'user', 'datetime', ] for RequestEventAdmin
-
DJANGO_UNIQUE_AUDIT_DATABASE_ALIASBy default it is the Django
defaultdatabase alias. But for projects that have split databases, this is necessary in order to keep database atomicity concerns in check during signal handlers.To clarify, this is only truly necessary for the model signals.
-
DJANGO_UNIQUE_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIPBy default this is
False, but this allows the calling project not to saveCRUDEventif the changed fields as determined by thepre_savehandler sees that there are no changed fields. We are keeping it off by default so that projects that wish to use this (potentially lessCRUDEvent) can choose to turn it on! And those that do not want it (yet or ever), or those that do not closely follow the release notes of this project will have one less worry when upgrading. -
DJANGO_UNIQUE_AUDIT_READONLY_EVENTSDefault is
False. The events visible through the admin interface are editable by default by a superuser. Set this toTrueif you wish to make the recorded events read-only through the admin UI. -
DJANGO_UNIQUE_AUDIT_LOGGING_BACKENDA pluggable backend option for logging. Defaults to
unique_audit.backends.ModelBackend. This class expects to have 3 methods:login(self, login_info_dict):crud(self, crud_info_dict):request(self, request_info_dict):
each of these methods accept a dictionary containing the info regarding the event. example overriding:
import logging class PythonLoggerBackend: logging.basicConfig() logger = logging.getLogger('your-kibana-logger') logger.setLevel(logging.DEBUG) def request(self, request_info): return request_info # if you don't need it def login(self, login_info): self.logger.info(msg='your message', extra=login_info) return login_info def crud(self, crud_info): self.logger.info(msg='your message', extra=crud_info) return crud_info
What does it do
Django Unique Audit uses Django signals to listen to the events happening in your project, such as when a user creates, updates or deletes a registry. This applies to every model of every app in your project.
When any of these events takes place, Django Unique Audit will log it in the model CRUDEvent.
You can query this information in the Django Admin app.
Besides logging CRUD events, Django Unique Audit will log all authentication events (such as when a user logs in, out, or fails to log in) and all the URLs requested in the project. This information is stored in models LoginEvent and RequestEvent.
Why you should use it
There are many Django auditing apps out there, but most them require you to change very important parts of your project's code. For example, they require you to add fields to your models, or make them inherit from a certain class. Some of them create a mirror for each of your models, which means duplicate migrations.
It is not that they don't work or that they are not great apps. But in case you need something easier to set up, and you don't want your code to depend so much on a third-party app, Django Unique Audit may be your best choice.
The good thing about this app is that it doesn't get in the way. It is easy and quick to install, and it begins logging everything right away, without you having to inject code anywhere in your project.
More
Thanks to original committer @soynatan.
Metadata
Release files for django-unique-audit 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-unique-audit-0.0.1.tar.gz | 29.3 kB | Without provenance | Details for django-unique-audit-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_unique_audit-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for django_unique_audit-0.0.1-py3-none-any.whl |
Total release size: 63.6 kB
Release files / django-unique-audit-0.0.1.tar.gz
| Download URL | django-unique-audit-0.0.1.tar.gz |
|---|---|
| Size | 29.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
92abef413a02970e66ffef57f2c2152b1930a4446a9d167be7c317cc7931ddbf
|
|
BLAKE2b-256 checksum How to use checksums |
ba3d2c8d65e482fe4eb8f67c8328e5b5780e90bd28f91898b5ae3f71f1b250b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1
|
Release files / django_unique_audit-0.0.1-py3-none-any.whl
| Download URL | django_unique_audit-0.0.1-py3-none-any.whl |
|---|---|
| Size | 34.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43fa16410da57d74f75fdb9bfa435d7c79a7baac5a36b2bca14ab1d53764e874
|
|
BLAKE2b-256 checksum How to use checksums |
4ef91a066a0123fbcf66c35232c3e862273704974fe4fd339602948bae3886eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1
|
Security for django-unique-audit 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.