Ensure that identity token in header is not an unicode string

We need all the headers to be safe strings so they can be joined
together and not become an unicode string in doing so.

This fixes a bug when creating an image with non-ascii characters in the
name.

This is required for python 2.6 compatibility.

Change-Id: I66ebc27edf4ccd8f903399da58705711c372536d
Closes-Bug: 1448080
This commit is contained in:
Vincent Untz 2015-04-24 13:29:12 +02:00
parent d91210c806
commit f65ba82268
4 changed files with 14 additions and 4 deletions

View File

@ -135,7 +135,8 @@ class HTTPClient(_BaseHTTPClient):
self.session.headers["User-Agent"] = USER_AGENT
if self.auth_token:
self.session.headers["X-Auth-Token"] = self.auth_token
self.session.headers["X-Auth-Token"] = encodeutils.safe_encode(
self.auth_token)
if self.language_header:
self.session.headers["Accept-Language"] = self.language_header

View File

@ -449,8 +449,7 @@ def memoized_property(fn):
def safe_header(name, value):
if value is not None and name in SENSITIVE_HEADERS:
v = value.encode('utf-8')
h = hashlib.sha1(v)
h = hashlib.sha1(value)
d = h.hexdigest()
return name, "{SHA1}%s" % d
else:

View File

@ -210,6 +210,14 @@ class TestClient(testtools.TestCase):
self.assertEqual(b"ni\xc3\xb1o", encoded[b"test"])
self.assertNotIn("none-val", encoded)
def test_auth_token_header_encoding(self):
# Tests that X-Auth-Token header is converted to ascii string, as
# httplib in python 2.6 won't do the conversion
value = u'ni\xf1o'
http_client_object = http.HTTPClient(self.endpoint, token=value)
self.assertEqual(b'ni\xc3\xb1o',
http_client_object.session.headers['X-Auth-Token'])
def test_raw_request(self):
"""Verify the path being used for HTTP requests reflects accurately."""
headers = {"Content-Type": "text/plain"}

View File

@ -168,7 +168,9 @@ class TestUtils(testtools.TestCase):
utils.safe_header('somekey', None))
for sensitive_header in utils.SENSITIVE_HEADERS:
(name, value) = utils.safe_header(sensitive_header, 'somestring')
(name, value) = utils.safe_header(
sensitive_header,
encodeutils.safe_encode('somestring'))
self.assertEqual(sensitive_header, name)
self.assertTrue(value.startswith("{SHA1}"))