From 8c159a2eb4d61f2b9691fbfa1288bcda2156f3f1 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Fri, 17 Oct 2014 07:53:05 -0700 Subject: [PATCH] Don't set X-Auth-Token key in http session header if no token provided Commit f980fc549247fa2deb87dfacebc6d8d13ccd45d1 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 --- glanceclient/common/http.py | 4 +++- tests/test_http.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index ad9da20d..8609ef39 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -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)) diff --git a/tests/test_http.py b/tests/test_http.py index 114481ad..31cd6d53 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -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.