Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging filter discussion PR #59

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ for line in data:

```


### As a python logging.Filter

```python
import logging

from anonip import AnonipFilter

if __name__ == '__main__':
handler = logging.StreamHandler()
handler.addFilter(AnonipFilter())
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[handler]
)

logging.debug('192.0.2.123 - call from root logger')

logger = logging.getLogger('child')
logger.info('2001:db8:abcd:ef01:2345:6789:abcd:ef01 - call from child logger')
```


### Python 2 or 3?
For compatibility reasons, anonip uses the shebang `#! /usr/bin/env python`.
This will default to python2 on all Linux distributions except for Arch Linux.
Expand Down
67 changes: 67 additions & 0 deletions anonip.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import re
import sys
from io import open
from collections import abc

try:
import ipaddress
Expand Down Expand Up @@ -303,6 +304,72 @@ def truncate_address(self, ip):
return ip.supernet(new_prefix=self._prefixes[ip.version])[0]


class AnonipFilter(logging.Filter):
def __init__(self, name="", args=None, extra=None, anonip=None):
"""
An implementation of Python logging.Filter using anonip.

:param name: str
:param args: list of log message args to filter. Defaults to []
:param extra: list of LogRecord attributes to filter. Defaults to []
:param anonip: dict of parameters for Anonip instance
"""
super(AnonipFilter, self).__init__(name)
self.args = [] if args is None else args
self.extra = [] if extra is None else extra
self.anonip = Anonip(**(anonip or {}))

def _set_args_attr(self, args, key):
value = args[key]
if not isinstance(value, str):
return args

orig_type = type(args)
temp_type = list
if isinstance(args, abc.Mapping):
temp_type = dict
has_setitem = hasattr(args, "__setitem__")
if not has_setitem:
args = temp_type(args)
ip = self.anonip.extract_ip(value)[1]
if ip:
args[key] = str(self.anonip.process_ip(ip))
if not has_setitem:
args = orig_type(args)
return args

def filter(self, record):
"""
Apply anonip IP masking.

:param record: logging.LogRecord
:return: bool
"""
if not super(AnonipFilter, self).filter(record):
return False

for key in self.args:
if isinstance(record.args, abc.Mapping):
if key in record.args:
record.args = self._set_args_attr(record.args, key)
elif isinstance(record.args, abc.Sequence):
if isinstance(key, int) and key < len(record.args):
record.args = self._set_args_attr(record.args, key)

for key in self.extra:
if hasattr(record, key):
value = getattr(record, key)
if isinstance(value, str):
ip = self.anonip.extract_ip(value)[1]
if ip:
setattr(record, key, str(self.anonip.process_ip(ip)))

# IP is not in args or extra, but in msg
record.msg = self.anonip.process_line(record.msg)

return True


def _validate_ipmask(mask, bits=32):
"""
Verify if the supplied ip mask is valid.
Expand Down
9 changes: 9 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import sys

import pytest
Expand All @@ -8,3 +9,11 @@ def backup_and_restore_sys_argv():
old_sys_argv = sys.argv
yield
sys.argv = old_sys_argv


@pytest.fixture()
def enable_logging():
logging.disable(logging.NOTSET)
logging.getLogger("anonip").setLevel(logging.CRITICAL)
yield
logging.disable(logging.CRITICAL)
100 changes: 100 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,103 @@ def test_properties_columns():
assert a.columns == [0]
a.columns = [5, 6]
assert a.columns == [4, 5]


def test_logging_filter_defaults(caplog):
logging.disable(logging.NOTSET)
logging.getLogger("anonip").setLevel(logging.CRITICAL)

logger = logging.getLogger("filter_defaults")
logger.addFilter(anonip.AnonipFilter())
logger.setLevel(logging.INFO)

logger.info("192.168.100.200 string")
logger.info("1.2.3.4 string")
logger.info("2001:0db8:85a3:0000:0000:8a2e:0370:7334 string")
logger.info("2a00:1450:400a:803::200e string")

assert caplog.record_tuples == [
("filter_defaults", logging.INFO, "192.168.96.0 string"),
("filter_defaults", logging.INFO, "1.2.0.0 string"),
("filter_defaults", logging.INFO, "2001:db8:85a0:: string"),
("filter_defaults", logging.INFO, "2a00:1450:4000:: string"),
]

logging.disable(logging.CRITICAL)


@pytest.mark.parametrize(
"string,args,filter_attr,expected",
[
("%(ip)s string", {"ip": "192.168.100.200"}, "ip", "192.168.96.0 string"),
("%s string", "192.168.100.200", 0, "192.168.96.0 string"),
(
"%(ip)s string",
{"ip": "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
"ip",
"2001:db8:85a0:: string",
),
("string", None, "ip", "string"),
("string", {"ip": ["in a list"]}, "ip", "string"),
(
"192.168.100.200 %s string",
"foo",
"also-not-existing-attr",
"192.168.96.0 foo string",
), # make sure to also mask IPs not provided in args
("", "", "", ""), # make base logging filter return False for coverage
],
)
def test_logging_filter_args(
string, args, filter_attr, expected, mocker, caplog, enable_logging
):
logger = logging.getLogger("filter_args")
logger.addFilter(
anonip.AnonipFilter(args=[filter_attr, "non-existing-attr"], extra=[])
)
logger.setLevel(logging.INFO)

log_args = [string]
if args:
log_args.append(args)

if string == args == filter_attr == expected == "":
mocker.patch.object(logging.Filter, "filter", return_value=False)
logger.info(*log_args)
assert len(caplog.record_tuples) == 0
else:
logger.info(*log_args)
assert caplog.record_tuples == [
("filter_args", logging.INFO, expected),
]


def test_logging_filter_extra(caplog):
logging.disable(logging.NOTSET)
logging.getLogger("anonip").setLevel(logging.CRITICAL)

logger = logging.getLogger("filter_args")
logger.addFilter(
anonip.AnonipFilter(
extra=["ip", "non-existing-key"], anonip={"ipv4mask": 16, "ipv6mask": 64}
)
)
logger.setLevel(logging.INFO)

logger.info("string", extra={"ip": "192.168.100.200"})
logger.info("string", extra={"ip": "1.2.3.4"})
logger.info("string", extra={"ip": "2001:0db8:85a3:0000:0000:8a2e:0370:7334"})
logger.info("string", extra={"ip": "2a00:1450:400a:803::200e"})

expected = [
"192.168.0.0",
"1.2.0.0",
"2001:db8:85a3::",
"2a00:1450:400a:803::",
]

actual = [record.ip for record in caplog.records]

assert actual == expected

logging.disable(logging.CRITICAL)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ envlist = py{27,36,37,38,39,310}, pypy3, flake8, black
deps=
pytest
pytest-cov
pytest-mock
commands=pytest -r a -vv tests.py anonip.py

[testenv:flake8]
Expand Down