Only log text strings in requests

As was exposed by object_store.create_object, we were attempting to mix
text and bytes in log messages, which was causing _logger.debug to choke
when combining the parts of the message, specifically the --data piece.

This is an issue for two reasons:
1. It's binary data in general
2. It's a very large amount of binary data

The fix is to not log byte strings that come in as `data` unless we can
decode them to ascii. This will allow us to continue logging some data
such as auth credentials, but we won't be logging megabytes or gigabytes
worth of binary data - such as objects being uploaded to Swift - into
the log file.

Change-Id: Iba49552b8ebc58a46cedf826751193a1fa7feb11
Partial-Bug: 1485054
This commit is contained in:
Brian Curtin 2015-08-14 11:50:24 -05:00
parent b5056b3cdc
commit 9524fdf7a6

View File

@ -364,8 +364,26 @@ class Transport(requests.Session):
if 'data' in kwargs and kwargs['data'] is not None:
string_parts.append("--data '")
string_parts.append(kwargs['data'])
data = kwargs['data']
# Only log text strings and byte strings that can be decoded
# in ascii. Raw byte strings both mess up the actual
# writing of the data to any log stream because we'd be mixing
# text and bytes, and they are generally overly long strings
# that would make the logs unreadable anyway.
if isinstance(data, six.binary_type):
# Some data, such as auth creds, is generally decodable
# to ascii. If it works, log it, otherwise put in a
# placeholder to specify that it's a blob of binary data.
try:
string_parts.append(data.decode("ascii"))
except UnicodeDecodeError:
string_parts.append("<binary data>")
else:
string_parts.append(data)
string_parts.append("'")
_logger.debug("REQ: %s" % " ".join(string_parts))
def _log_response(self, response):