From 4a0ac4583b5e896d5b1af033ca2347f9fcc9daab Mon Sep 17 00:00:00 2001 From: Matt Mulsow Date: Mon, 8 Aug 2016 21:26:01 -0500 Subject: [PATCH] User-agent header shouldn't start with a space If the application name, sys.argv[0], is an empty string, like when called from the Ansible os_network module, then keystoneauth (with the new requests 2.11.0) will raise an exception saying: Invalid return character or leading space in header: User-Agent This comes from keystoneauth trying to prepend the empty module name to the default user agent, which leads to it sending a User-Agent header that starts with a blank space. This was fine until the new requests 2.11.0 was released which errors if header values start with a leading space. With this change, if sys.argv[0] is an empty string we'll just use the DEFAULT_USER_AGENT and not try to prepend a calling module. Closes-Bug: #1611426 Change-Id: I56d3e352dce7628add0479b3333a880700844ebc Signed-off-by: Matt Mulsow --- keystoneauth1/session.py | 3 +++ keystoneauth1/tests/unit/test_session.py | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/keystoneauth1/session.py b/keystoneauth1/session.py index db01a4de..e4133eda 100644 --- a/keystoneauth1/session.py +++ b/keystoneauth1/session.py @@ -155,6 +155,9 @@ def _determine_user_agent(): # sys.argv is empty, usually the Python interpreter prevents this. return None + if not name: + return None + name = os.path.basename(name) if name in ignored: name = _determine_calling_package() diff --git a/keystoneauth1/tests/unit/test_session.py b/keystoneauth1/tests/unit/test_session.py index ccef46fa..39c87d35 100644 --- a/keystoneauth1/tests/unit/test_session.py +++ b/keystoneauth1/tests/unit/test_session.py @@ -146,9 +146,11 @@ class SessionTests(utils.TestCase): # If sys.argv[0] is an empty string, then doesn't fail. with mock.patch.object(sys, 'argv', ['']): session = client_session.Session() - # NOTE(blk-u): This isn't working right, see bug 1611426 - self.assertRaises(exceptions.UnknownConnectionError, session.get, - self.TEST_URL) + resp = session.get(self.TEST_URL) + self.assertTrue(resp.ok) + self.assertRequestHeaderEqual( + 'User-Agent', + client_session.DEFAULT_USER_AGENT) def test_http_session_opts(self): session = client_session.Session(cert='cert.pem', timeout=5,