-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving logging filters from edx-platform (#65)
* Moving logging filters from edx-platform Moving logging filters for user and remote IP to this repo, so they can be re-used by newly created IDAs through the cookie-cutter template. SEG-34 Co-authored-by: Robert Raposa <[email protected]>
- Loading branch information
1 parent
a6a4704
commit 13faebe
Showing
16 changed files
with
156 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Logging filters for user and IP | ||
================================================ | ||
|
||
Status | ||
------ | ||
|
||
Accepted | ||
|
||
Context | ||
------- | ||
|
||
As part of the Security Working Group's work on SEG-34, we recognized that the `logging filters`_ for | ||
LMS users and remote IP addresses were not reusable by other IDAs from inside LMS. | ||
|
||
.. _logging filters: https://github.com/edx/edx-platform/blob/11e4cab6220c8c503787142f48a352410191de0a/openedx/core/djangoapps/util/log_utils.py#L16 | ||
|
||
Decision | ||
-------- | ||
|
||
We decided to move the LMS users and remote IP addresses to this library, so these filters may be re-used by any edx component. Of particular use, we can update the logging settings in the IDA cookie-cutter repo to reference these filters in the standard logging context that is created from the repo for new IDAs. | ||
|
||
Consequences | ||
------------ | ||
|
||
We will need to: | ||
* Update the IDA cookie-cutter once these are available. | ||
* Remove these classes from edx-platform. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
Logging utilities public api | ||
See README.rst for details. | ||
""" | ||
from .internal.filters import RemoteIpFilter, UserIdFilter |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Django-based logging filters | ||
""" | ||
|
||
from logging import Filter | ||
|
||
from crum import get_current_request, get_current_user | ||
|
||
|
||
class RemoteIpFilter(Filter): | ||
""" | ||
A logging filter that adds the remote IP to the logging context | ||
""" | ||
def filter(self, record): | ||
request = get_current_request() | ||
if request and 'REMOTE_ADDR' in request.META: | ||
record.remoteip = request.META['REMOTE_ADDR'] | ||
else: | ||
record.remoteip = None | ||
return True | ||
|
||
|
||
class UserIdFilter(Filter): | ||
""" | ||
A logging filter that adds userid to the logging context | ||
""" | ||
def filter(self, record): | ||
user = get_current_user() | ||
if user and user.pk: | ||
record.userid = user.pk | ||
else: | ||
record.userid = None | ||
return True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Tests for logging. | ||
""" | ||
|
||
from django.test import TestCase | ||
from mock import MagicMock, patch | ||
|
||
from edx_django_utils.logging import RemoteIpFilter, UserIdFilter | ||
|
||
|
||
class MockRecord: | ||
""" | ||
Mocks a logging construct to receive data to be interpolated. | ||
""" | ||
def __init__(self): | ||
self.userid = None | ||
self.remoteip = None | ||
|
||
|
||
class TestLoggingFilters(TestCase): | ||
""" | ||
Test the logging filters for users and IP addresses | ||
""" | ||
|
||
@patch('edx_django_utils.logging.internal.filters.get_current_user') | ||
def test_userid_filter(self, mock_get_user): | ||
mock_user = MagicMock() | ||
mock_user.pk = '1234' | ||
mock_get_user.return_value = mock_user | ||
|
||
user_filter = UserIdFilter() | ||
test_record = MockRecord() | ||
user_filter.filter(test_record) | ||
|
||
self.assertEqual(test_record.userid, '1234') | ||
|
||
def test_userid_filter_no_user(self): | ||
user_filter = UserIdFilter() | ||
test_record = MockRecord() | ||
user_filter.filter(test_record) | ||
|
||
self.assertEqual(test_record.userid, None) | ||
|
||
@patch('edx_django_utils.logging.internal.filters.get_current_request') | ||
def test_remoteip_filter(self, mock_get_request): | ||
mock_request = MagicMock() | ||
mock_request.META = {'REMOTE_ADDR': '192.168.1.1'} | ||
mock_get_request.return_value = mock_request | ||
|
||
ip_filter = RemoteIpFilter() | ||
test_record = MockRecord() | ||
ip_filter.filter(test_record) | ||
|
||
self.assertEqual(test_record.remoteip, '192.168.1.1') | ||
|
||
def test_remoteip_filter_no_request(self): | ||
ip_filter = RemoteIpFilter() | ||
test_record = MockRecord() | ||
ip_filter.filter(test_record) | ||
|
||
self.assertEqual(test_record.remoteip, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters