Skip to content

Commit

Permalink
Disable logging remote IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan-Cohen committed Mar 25, 2024
1 parent b2aff7e commit 693a509
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 3.0.0-beta.4 (2024-01-02)

#### Improvements
- feat: Excluding ip address when AUDITLOG_DISABLE_REMOTE_ADDR is set to True ([#620](https://github.com/jazzband/django-auditlog/pull/620))
- feat: If any receiver returns False, no logging will be made. This can be useful if logging should be conditionally enabled / disabled ([#590](https://github.com/jazzband/django-auditlog/pull/590))
- Django: Confirm Django 5.0 support ([#598](https://github.com/jazzband/django-auditlog/pull/598))
- Django: Drop Django 4.1 support ([#598](https://github.com/jazzband/django-auditlog/pull/598))
Expand Down
5 changes: 5 additions & 0 deletions auditlog/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@
settings.AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENT = getattr(
settings, "AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENT", False
)

# Disable remote_addr field in database
settings.AUDITLOG_DISABLE_REMOTE_ADDR = getattr(
settings, "AUDITLOG_DISABLE_REMOTE_ADDR", False
)
11 changes: 10 additions & 1 deletion auditlog/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth import get_user_model

from auditlog.cid import set_cid
Expand All @@ -12,6 +13,11 @@ class AuditlogMiddleware:

def __init__(self, get_response=None):
self.get_response = get_response
self.disable_remote_addr = getattr(
settings, "AUDITLOG_DISABLE_REMOTE_ADDR", False
)
if not isinstance(self.disable_remote_addr, bool):
raise ValueError("Setting 'AUDITLOG_DISABLE_REMOTE_ADDR' must be a boolean")

@staticmethod
def _get_remote_addr(request):
Expand All @@ -38,7 +44,10 @@ def _get_actor(request):
return None

def __call__(self, request):
remote_addr = self._get_remote_addr(request)
if self.disable_remote_addr:
remote_addr = None
else:
remote_addr = self._get_remote_addr(request)
user = self._get_actor(request)

set_cid(request)
Expand Down
6 changes: 6 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,12 @@ def test_register_from_settings_invalid_settings(self):
):
self.test_auditlog.register_from_settings()

with override_settings(AUDITLOG_DISABLE_REMOTE_ADDR="str"):
with self.assertRaisesMessage(
TypeError, "Setting 'AUDITLOG_DISABLE_REMOTE_ADDR' must be a boolean"
):
self.test_auditlog.register_from_settings()

@override_settings(
AUDITLOG_INCLUDE_ALL_MODELS=True,
AUDITLOG_EXCLUDE_TRACKING_MODELS=("auditlog_tests.SimpleExcludeModel",),
Expand Down
13 changes: 13 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ It will be considered when ``AUDITLOG_INCLUDE_ALL_MODELS`` is `True`.
.. versionadded:: 3.0.0

**AUDITLOG_EXCLUDE_TRACKING_FIELDS**

When using "AuditlogMiddleware",
the IP address is logged by default, you can use this setting
to exclude the IP address from logging.
It will be considered when ``AUDITLOG_DISABLE_REMOTE_ADDR`` is `True`.

.. code-block:: python
AUDITLOG_DISABLE_REMOTE_ADDR = True
.. versionadded:: 3.0.0

**AUDITLOG_EXCLUDE_TRACKING_MODELS**

You can use this setting to exclude models in registration process.
Expand Down

0 comments on commit 693a509

Please sign in to comment.