Skip to content

Commit

Permalink
Merge branch 'feature/scrub-keen-headers' into next
Browse files Browse the repository at this point in the history
 [SVCS-576]
 Closes: #299
  • Loading branch information
felliott committed Dec 5, 2017
2 parents c87ed06 + 44d372f commit 6249a7a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/core/test_remote_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest

from waterbutler.core import remote_logging


class TestScrubPayloadForKeen:

def test_flat_dict(self):
payload = {
'key': 'value',
'key2': 'value2'
}

result = remote_logging._scrub_headers_for_keen(payload)

assert result == {
'key': 'value',
'key2': 'value2'
}

def test_flat_dict_needs_scrubbing(self):
payload = {
'key.test': 'value',
'key2': 'value2'
}

result = remote_logging._scrub_headers_for_keen(payload)

assert result == {
'key-test': 'value',
'key2': 'value2'
}

def test_scrub_and_rename(self):
payload = {
'key.test': 'unique value',
'key-test': 'value2'
}

result = remote_logging._scrub_headers_for_keen(payload)

# "key.test" sorts after "key-test" and will therefore be renamed
assert result == {
'key-test': 'value2',
'key-test-1': 'unique value'
}

def test_scrub_and_loop_rename(self):
payload = {
'key.test': 'value1',
'key-test': 'value2',
'key-test-1': 'value3'
}

result = remote_logging._scrub_headers_for_keen(payload)

assert result == {
'key-test': 'value2',
'key-test-2': 'value1',
'key-test-1': 'value3'

}

def test_max_iteration(self):
payload = {
'key.test': 'value1',
'key-test': 'value2',
'key-test-1': 'value3'
}

result = remote_logging._scrub_headers_for_keen(payload, MAX_ITERATIONS=1)

assert result == {
'key-test': 'value2',
'key-test-1': 'value3'
}
21 changes: 21 additions & 0 deletions waterbutler/core/remote_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,26 @@ def _build_public_file_payload(action, request, file_metadata):
return public_payload


def _scrub_headers_for_keen(payload, MAX_ITERATIONS=10):
""" Scrub unwanted characters like \\.\\ from the keys in the keen payload """

scrubbed_payload = {}
for key in sorted(payload):
scrubbed_key = key.replace('.', '-')

# if our new scrubbed key is already in the payload, we need to increment it
if scrubbed_key in scrubbed_payload:
for i in range(1, MAX_ITERATIONS + 1): # try MAX_ITERATION times, then give up & drop it
incremented_key = '{}-{}'.format(scrubbed_key, i)
if incremented_key not in scrubbed_payload: # we found an unused key!
scrubbed_payload[incremented_key] = payload[key]
break
else:
scrubbed_payload[scrubbed_key] = payload[key]

return scrubbed_payload


def _serialize_request(request):
"""Serialize the original request so we can log it across celery."""
if request is None:
Expand All @@ -312,6 +332,7 @@ def _serialize_request(request):
if k not in ('Authorization', 'Cookie', 'User-Agent',):
headers_dict[k] = v

headers_dict = _scrub_headers_for_keen(headers_dict)
serialized = {
'tech': {
'ip': request.remote_ip,
Expand Down

0 comments on commit 6249a7a

Please sign in to comment.