Only log application/json in session to start
When whitelisting content types to debug print from session we chose application/json and application/text. application/text is not a real mime type, text is typically text/plain. Rather than guess at mime types only print application/json to start with, but make it easy for additional types to be added later. Adapted from keystoneauth: Ica5fee076cdab8b1d5167161d28af7313fad9477 Related-Bug: 1616105 Change-Id: Ieaa8fb3ea8d25e09b89498f23b70b18c0f6153f1
This commit is contained in:
@@ -37,7 +37,10 @@ osprofiler_web = importutils.try_import("osprofiler.web")
|
|||||||
|
|
||||||
USER_AGENT = 'python-keystoneclient'
|
USER_AGENT = 'python-keystoneclient'
|
||||||
|
|
||||||
_LOG_CONTENT_TYPES = set(['application/json', 'application/text'])
|
# NOTE(jamielennox): Clients will likely want to print more than json. Please
|
||||||
|
# propose a patch if you have a content type you think is reasonable to print
|
||||||
|
# here and we'll add it to the list as required.
|
||||||
|
_LOG_CONTENT_TYPES = set(['application/json'])
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -233,8 +236,8 @@ class Session(object):
|
|||||||
text = _remove_service_catalog(response.text)
|
text = _remove_service_catalog(response.text)
|
||||||
else:
|
else:
|
||||||
text = ('Omitted, Content-Type is set to %s. Only '
|
text = ('Omitted, Content-Type is set to %s. Only '
|
||||||
'application/json and application/text responses '
|
'%s responses have their bodies logged.')
|
||||||
'have their bodies logged.') % content_type
|
text = text % (content_type, ', '.join(_LOG_CONTENT_TYPES))
|
||||||
|
|
||||||
string_parts = [
|
string_parts = [
|
||||||
'RESP:',
|
'RESP:',
|
||||||
|
@@ -157,8 +157,8 @@ class SessionTests(utils.TestCase):
|
|||||||
'X-Auth-Token': uuid.uuid4().hex,
|
'X-Auth-Token': uuid.uuid4().hex,
|
||||||
'X-Subject-Token': uuid.uuid4().hex,
|
'X-Subject-Token': uuid.uuid4().hex,
|
||||||
'X-Service-Token': uuid.uuid4().hex}
|
'X-Service-Token': uuid.uuid4().hex}
|
||||||
body = 'BODYRESPONSE'
|
body = '{"a": "b"}'
|
||||||
data = 'BODYDATA'
|
data = '{"c": "d"}'
|
||||||
all_headers = dict(
|
all_headers = dict(
|
||||||
itertools.chain(headers.items(), security_headers.items()))
|
itertools.chain(headers.items(), security_headers.items()))
|
||||||
self.stub_url('POST', text=body, headers=all_headers)
|
self.stub_url('POST', text=body, headers=all_headers)
|
||||||
@@ -185,26 +185,25 @@ class SessionTests(utils.TestCase):
|
|||||||
def test_logs_failed_output(self):
|
def test_logs_failed_output(self):
|
||||||
"""Test that output is logged even for failed requests."""
|
"""Test that output is logged even for failed requests."""
|
||||||
session = client_session.Session()
|
session = client_session.Session()
|
||||||
body = uuid.uuid4().hex
|
body = {uuid.uuid4().hex: uuid.uuid4().hex}
|
||||||
|
|
||||||
self.stub_url('GET', text=body, status_code=400,
|
self.stub_url('GET', json=body, status_code=400,
|
||||||
headers={'Content-Type': 'application/text'})
|
headers={'Content-Type': 'application/json'})
|
||||||
resp = session.get(self.TEST_URL, raise_exc=False)
|
resp = session.get(self.TEST_URL, raise_exc=False)
|
||||||
|
|
||||||
self.assertEqual(resp.status_code, 400)
|
self.assertEqual(resp.status_code, 400)
|
||||||
self.assertIn(body, self.logger.output)
|
self.assertIn(list(body.keys())[0], self.logger.output)
|
||||||
|
self.assertIn(list(body.values())[0], self.logger.output)
|
||||||
|
|
||||||
def test_logging_body_only_for_text_and_json_content_types(self):
|
def test_logging_body_only_for_specified_content_types(self):
|
||||||
"""Verify response body is only logged in specific content types.
|
"""Verify response body is only logged in specific content types.
|
||||||
|
|
||||||
Response bodies are logged only when the response's Content-Type header
|
Response bodies are logged only when the response's Content-Type header
|
||||||
is set to application/json or application/text. This prevents us to get
|
is set to application/json. This prevents us to get an unexpected
|
||||||
an unexpected MemoryError when reading arbitrary responses, such as
|
MemoryError when reading arbitrary responses, such as streams.
|
||||||
streams.
|
|
||||||
"""
|
"""
|
||||||
OMITTED_BODY = ('Omitted, Content-Type is set to %s. Only '
|
OMITTED_BODY = ('Omitted, Content-Type is set to %s. Only '
|
||||||
'application/json and application/text responses '
|
'application/json responses have their bodies logged.')
|
||||||
'have their bodies logged.')
|
|
||||||
session = client_session.Session(verify=False)
|
session = client_session.Session(verify=False)
|
||||||
|
|
||||||
# Content-Type is not set
|
# Content-Type is not set
|
||||||
@@ -229,14 +228,6 @@ class SessionTests(utils.TestCase):
|
|||||||
self.assertIn(body, self.logger.output)
|
self.assertIn(body, self.logger.output)
|
||||||
self.assertNotIn(OMITTED_BODY % 'application/json', self.logger.output)
|
self.assertNotIn(OMITTED_BODY % 'application/json', self.logger.output)
|
||||||
|
|
||||||
# Content-Type is set to application/text
|
|
||||||
body = uuid.uuid4().hex
|
|
||||||
self.stub_url('POST', text=body,
|
|
||||||
headers={'Content-Type': 'application/text'})
|
|
||||||
session.post(self.TEST_URL)
|
|
||||||
self.assertIn(body, self.logger.output)
|
|
||||||
self.assertNotIn(OMITTED_BODY % 'application/text', self.logger.output)
|
|
||||||
|
|
||||||
def test_unicode_data_in_debug_output(self):
|
def test_unicode_data_in_debug_output(self):
|
||||||
"""Verify that ascii-encodable data is logged without modification."""
|
"""Verify that ascii-encodable data is logged without modification."""
|
||||||
session = client_session.Session(verify=False)
|
session = client_session.Session(verify=False)
|
||||||
@@ -812,22 +803,24 @@ class SessionAuthTests(utils.TestCase):
|
|||||||
|
|
||||||
auth = AuthPlugin()
|
auth = AuthPlugin()
|
||||||
sess = client_session.Session(auth=auth)
|
sess = client_session.Session(auth=auth)
|
||||||
response = uuid.uuid4().hex
|
response = {uuid.uuid4().hex: uuid.uuid4().hex}
|
||||||
|
|
||||||
self.stub_url('GET',
|
self.stub_url('GET',
|
||||||
text=response,
|
json=response,
|
||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
|
|
||||||
resp = sess.get(self.TEST_URL, logger=logger)
|
resp = sess.get(self.TEST_URL, logger=logger)
|
||||||
|
|
||||||
self.assertEqual(response, resp.text)
|
self.assertEqual(response, resp.json())
|
||||||
output = io.getvalue()
|
output = io.getvalue()
|
||||||
|
|
||||||
self.assertIn(self.TEST_URL, output)
|
self.assertIn(self.TEST_URL, output)
|
||||||
self.assertIn(response, output)
|
self.assertIn(list(response.keys())[0], output)
|
||||||
|
self.assertIn(list(response.values())[0], output)
|
||||||
|
|
||||||
self.assertNotIn(self.TEST_URL, self.logger.output)
|
self.assertNotIn(self.TEST_URL, self.logger.output)
|
||||||
self.assertNotIn(response, self.logger.output)
|
self.assertNotIn(list(response.keys())[0], self.logger.output)
|
||||||
|
self.assertNotIn(list(response.values())[0], self.logger.output)
|
||||||
|
|
||||||
|
|
||||||
class AdapterTest(utils.TestCase):
|
class AdapterTest(utils.TestCase):
|
||||||
@@ -1009,21 +1002,23 @@ class AdapterTest(utils.TestCase):
|
|||||||
sess = client_session.Session(auth=auth)
|
sess = client_session.Session(auth=auth)
|
||||||
adpt = adapter.Adapter(sess, auth=auth, logger=logger)
|
adpt = adapter.Adapter(sess, auth=auth, logger=logger)
|
||||||
|
|
||||||
response = uuid.uuid4().hex
|
response = {uuid.uuid4().hex: uuid.uuid4().hex}
|
||||||
|
|
||||||
self.stub_url('GET', text=response,
|
self.stub_url('GET', json=response,
|
||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
|
|
||||||
resp = adpt.get(self.TEST_URL, logger=logger)
|
resp = adpt.get(self.TEST_URL, logger=logger)
|
||||||
|
|
||||||
self.assertEqual(response, resp.text)
|
self.assertEqual(response, resp.json())
|
||||||
output = io.getvalue()
|
output = io.getvalue()
|
||||||
|
|
||||||
self.assertIn(self.TEST_URL, output)
|
self.assertIn(self.TEST_URL, output)
|
||||||
self.assertIn(response, output)
|
self.assertIn(list(response.keys())[0], output)
|
||||||
|
self.assertIn(list(response.values())[0], output)
|
||||||
|
|
||||||
self.assertNotIn(self.TEST_URL, self.logger.output)
|
self.assertNotIn(self.TEST_URL, self.logger.output)
|
||||||
self.assertNotIn(response, self.logger.output)
|
self.assertNotIn(list(response.keys())[0], self.logger.output)
|
||||||
|
self.assertNotIn(list(response.values())[0], self.logger.output)
|
||||||
|
|
||||||
|
|
||||||
class ConfLoadingTests(utils.TestCase):
|
class ConfLoadingTests(utils.TestCase):
|
||||||
|
@@ -3,6 +3,6 @@ fixes:
|
|||||||
- >
|
- >
|
||||||
[`bug 1616105 <https://bugs.launchpad.net/keystoneauth/+bug/1616105>`_]
|
[`bug 1616105 <https://bugs.launchpad.net/keystoneauth/+bug/1616105>`_]
|
||||||
Only log the response body when the ``Content-Type`` header is set to
|
Only log the response body when the ``Content-Type`` header is set to
|
||||||
``application/json`` or ``application/text``. This avoids logging large
|
``application/json``. This avoids logging large binary objects (such as
|
||||||
binary objects (such as images). Other ``Content-Type`` will not be
|
images). Other ``Content-Type`` will not be logged. Additional
|
||||||
logged.
|
``Content-Type`` strings can be added as required.
|
||||||
|
Reference in New Issue
Block a user