Improve logentry shredder function

This commit is contained in:
Raphael Michel
2023-12-21 22:46:19 +01:00
parent 12804ff1a0
commit 8c8650090d
2 changed files with 101 additions and 6 deletions

View File

@@ -159,9 +159,16 @@ def shred_log_fields(logentry, banlist=None, whitelist=None):
for k, v in d.items():
if k not in whitelist:
if isinstance(d[k], list):
d[k] = [''] * len(d[k])
if isinstance(d[k], dict):
_shred(d[k], None, ['__'])
newlist = []
for i in d[k]:
if isinstance(i, dict):
_shred(i, None, [None])
else:
i = ''
newlist.append(i)
d[k] = newlist
elif isinstance(d[k], dict):
_shred(d[k], None, [None])
elif d[k]:
d[k] = ''
shredded = True
@@ -169,9 +176,16 @@ def shred_log_fields(logentry, banlist=None, whitelist=None):
for k in banlist:
if k in d:
if isinstance(d[k], list):
d[k] = [''] * len(d[k])
if isinstance(d[k], dict):
_shred(d, None, ['__'])
newlist = []
for i in d[k]:
if isinstance(i, dict):
_shred(i, None, [None])
else:
i = ''
newlist.append(i)
d[k] = newlist
elif isinstance(d[k], dict):
_shred(d[k], None, [None])
elif d[k]:
d[k] = ''
shredded = True

View File

@@ -39,6 +39,7 @@ from pretix.base.shredder import (
AttendeeInfoShredder, CachedTicketShredder, EmailAddressShredder,
InvoiceAddressShredder, InvoiceShredder, PaymentInfoShredder,
QuestionAnswerShredder, WaitingListShredder, shred_constraints,
shred_log_fields,
)
@@ -412,3 +413,83 @@ def test_shred_constraint_after_event_subevents(event):
date_to=now_dt + timedelta(hours=1)
)
assert shred_constraints(event)
@pytest.mark.django_db
def test_shred_log_fields_banlist(event):
le = event.log_action("foo.bar", data={
"dict": {
"subdict": {
"foo": "bar",
"empty": None,
}
},
"list": [
{
"foo": "bar",
},
"baz",
],
"string": "foo",
"bool": True,
"int": 0,
})
shred_log_fields(le, banlist=["dict", "list", "int", "bool"])
assert le.shredded
assert le.parsed_data == {
"dict": {
"subdict": {
"foo": "",
"empty": None,
}
},
"list": [
{
"foo": "",
},
"",
],
"string": "foo",
"bool": "",
"int": 0,
}
@pytest.mark.django_db
def test_shred_log_fields_whitelist(event):
le = event.log_action("foo.bar", data={
"dict": {
"subdict": {
"foo": "bar",
"empty": None,
}
},
"list": [
{
"foo": "bar",
},
"baz",
],
"string": "foo",
"bool": True,
"int": 0,
})
shred_log_fields(le, whitelist=["string"])
assert le.shredded
assert le.parsed_data == {
"dict": {
"subdict": {
"foo": "",
"empty": None,
}
},
"list": [
{
"foo": "",
},
"",
],
"string": "foo",
"bool": "",
"int": 0,
}