Don't set X-Auth-Token key in http session header if no token provided

Commit f980fc5492 changed how the
X-Auth-Token header was scrubbed when logging the request, but
effectively made the value required which can lead to an AttributeError
if the value for the header is None.

The value can be None if you're using Nova but don't have Nova
configured with auth_strategy='keystone' (see
nova.image.glance._create_glance_client for details).

This patch simply checks if the auth_token is set in the http client
object and if not, it doesn't set the X-Auth-Token key in the session
header.

Closes-Bug: #1381295

Change-Id: Ie285d5253df28a9f0f964147a53c99ceaa919c5c
This commit is contained in:
Matt Riedemann
2014-10-17 07:53:05 -07:00
committed by Qin Zhao
parent cfe0623520
commit 8c159a2eb4
2 changed files with 19 additions and 1 deletions

View File

@@ -62,7 +62,9 @@ class HTTPClient(object):
self.session = requests.Session()
self.session.headers["User-Agent"] = USER_AGENT
self.session.headers["X-Auth-Token"] = self.auth_token
if self.auth_token:
self.session.headers["X-Auth-Token"] = self.auth_token
self.timeout = float(kwargs.get('timeout', 600))

View File

@@ -75,6 +75,22 @@ class TestClient(testtools.TestCase):
self.assertTrue(http_client_object.identity_headers.
get('X-Auth-Token') is None)
def test_identity_headers_and_no_token_in_session_header(self):
# Tests that if token or X-Auth-Token are not provided in the kwargs
# when creating the http client, the session headers don't contain
# the X-Auth-Token key.
identity_headers = {
'X-User-Id': 'user',
'X-Tenant-Id': 'tenant',
'X-Roles': 'roles',
'X-Identity-Status': 'Confirmed',
'X-Service-Catalog': 'service_catalog',
}
kwargs = {'identity_headers': identity_headers}
http_client_object = http.HTTPClient(self.endpoint, **kwargs)
self.assertIsNone(http_client_object.auth_token)
self.assertNotIn('X-Auth-Token', http_client_object.session.headers)
def test_connection_refused(self):
"""
Should receive a CommunicationError if connection refused.