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 <mamulsow@us.ibm.com>
This commit is contained in:
Matt Mulsow 2016-08-08 21:26:01 -05:00
parent c9ae7067bd
commit 4a0ac4583b
2 changed files with 8 additions and 3 deletions

View File

@ -155,6 +155,9 @@ def _determine_user_agent():
# sys.argv is empty, usually the Python interpreter prevents this. # sys.argv is empty, usually the Python interpreter prevents this.
return None return None
if not name:
return None
name = os.path.basename(name) name = os.path.basename(name)
if name in ignored: if name in ignored:
name = _determine_calling_package() name = _determine_calling_package()

View File

@ -146,9 +146,11 @@ class SessionTests(utils.TestCase):
# If sys.argv[0] is an empty string, then doesn't fail. # If sys.argv[0] is an empty string, then doesn't fail.
with mock.patch.object(sys, 'argv', ['']): with mock.patch.object(sys, 'argv', ['']):
session = client_session.Session() session = client_session.Session()
# NOTE(blk-u): This isn't working right, see bug 1611426 resp = session.get(self.TEST_URL)
self.assertRaises(exceptions.UnknownConnectionError, session.get, self.assertTrue(resp.ok)
self.TEST_URL) self.assertRequestHeaderEqual(
'User-Agent',
client_session.DEFAULT_USER_AGENT)
def test_http_session_opts(self): def test_http_session_opts(self):
session = client_session.Session(cert='cert.pem', timeout=5, session = client_session.Session(cert='cert.pem', timeout=5,