summaryrefslogtreecommitdiff
path: root/keystoneclient/session.py
diff options
context:
space:
mode:
authorTobias Diaz <tobias.deb@gmail.com>2016-08-23 17:13:24 +0200
committerSamuel de Medeiros Queiroz <samueldmq@gmail.com>2017-01-05 10:19:25 -0300
commit3e56e0d7e5e1a76d806a3bc1f6d5ef9070f95771 (patch)
treecf73b3ba11fab18e8d64d220366282e969f3a062 /keystoneclient/session.py
parent39373aa40b085c3277fe393de0f638f12ac16d53 (diff)
downloadpython-keystoneclient-3e56e0d7e5e1a76d806a3bc1f6d5ef9070f95771.tar.gz
Prevent MemoryError when logging response bodies
Response bodies are loaded into memory prior to being logged. Loading huge response bodies may result in a MemoryError. This patch proposes that only JSON and TEXT responses be logged, i.e when the Content-Type header is application/json or application/text. Responses that do not include or have a different Content-Type header will have their body omitted. This is a sort of backport of the fix for keystoneauth sessions, see I93b6fff73368c4f58bdebf8566c4948b50980cee Co-Authored-By: Samuel de Medeiros Queiroz <samueldmq@gmail.com> Closes-bug: 1616105 Change-Id: I8f43eee3a0b35041c6cf672e476f8151cf2f8d14
Diffstat (limited to 'keystoneclient/session.py')
-rw-r--r--keystoneclient/session.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/keystoneclient/session.py b/keystoneclient/session.py
index 522a533..98fe42a 100644
--- a/keystoneclient/session.py
+++ b/keystoneclient/session.py
@@ -37,6 +37,8 @@ osprofiler_web = importutils.try_import("osprofiler.web")
USER_AGENT = 'python-keystoneclient'
+_LOG_CONTENT_TYPES = set(['application/json', 'application/text'])
+
_logger = logging.getLogger(__name__)
@@ -216,7 +218,18 @@ class Session(object):
if not logger.isEnabledFor(logging.DEBUG):
return
- text = _remove_service_catalog(response.text)
+ # NOTE(samueldmq): If the response does not provide enough info about
+ # the content type to decide whether it is useful and safe to log it
+ # or not, just do not log the body. Trying to# read the response body
+ # anyways may result on reading a long stream of bytes and getting an
+ # unexpected MemoryError. See bug 1616105 for further details.
+ content_type = response.headers.get('content-type', None)
+ if content_type in _LOG_CONTENT_TYPES:
+ text = _remove_service_catalog(response.text)
+ else:
+ text = ('Omitted, Content-Type is set to %s. Only '
+ 'application/json and application/text responses '
+ 'have their bodies logged.') % content_type
string_parts = [
'RESP:',
@@ -224,9 +237,7 @@ class Session(object):
]
for header in six.iteritems(response.headers):
string_parts.append('%s: %s' % self._process_header(header))
- if text:
- string_parts.append('\nRESP BODY: %s\n' %
- strutils.mask_password(text))
+ string_parts.append('\nRESP BODY: %s\n' % strutils.mask_password(text))
logger.debug(' '.join(string_parts))