Set credentials headers if there is no auth token.

Credentials will be set instead of a token for all invocations where
--os-no-client-auth is specified.

Change-Id: I35a81a9518833ce9758228266deb36c8073f9fb8
This commit is contained in:
Steve Baker 2013-07-19 14:09:34 +12:00
parent 0254ad50a2
commit 78c98ee91b
2 changed files with 58 additions and 4 deletions

View File

@ -131,6 +131,8 @@ class HTTPClient(object):
kwargs['headers'].setdefault('User-Agent', USER_AGENT)
if self.auth_token:
kwargs['headers'].setdefault('X-Auth-Token', self.auth_token)
else:
kwargs['headers'].update(self.credentials_headers())
if self.auth_url:
kwargs['headers'].setdefault('X-Auth-Url', self.auth_url)
@ -175,10 +177,12 @@ class HTTPClient(object):
return resp, body_str
def credentials_headers(self):
return {
'X-Auth-User': self.username,
'X-Auth-Key': self.password
}
creds = {}
if self.username:
creds['X-Auth-User'] = self.username
if self.password:
creds['X-Auth-Key'] = self.password
return creds
def json_request(self, method, url, **kwargs):
kwargs.setdefault('headers', {})

View File

@ -37,6 +37,56 @@ class HttpClientTest(testtools.TestCase):
self.assertEqual(''.join([x for x in body]), '')
self.m.VerifyAll()
def test_token_or_credentials(self):
# Record a 200
fake200 = fakes.FakeHTTPResponse(
200, 'OK',
{'content-type': 'application/octet-stream'},
'')
# no token or credentials
mock_conn = http.httplib.HTTPConnection('example.com', 8004,
timeout=600.0)
mock_conn.request('GET', '/',
headers={'Content-Type': 'application/octet-stream',
'User-Agent': 'python-heatclient'})
mock_conn.getresponse().AndReturn(fake200)
# credentials
mock_conn = http.httplib.HTTPConnection('example.com', 8004,
timeout=600.0)
mock_conn.request('GET', '/',
headers={'Content-Type': 'application/octet-stream',
'User-Agent': 'python-heatclient',
'X-Auth-Key': 'pass',
'X-Auth-User': 'user'})
mock_conn.getresponse().AndReturn(fake200)
# token suppresses credentials
mock_conn = http.httplib.HTTPConnection('example.com', 8004,
timeout=600.0)
mock_conn.request('GET', '/',
headers={'Content-Type': 'application/octet-stream',
'User-Agent': 'python-heatclient',
'X-Auth-Token': 'abcd1234'})
mock_conn.getresponse().AndReturn(fake200)
# Replay, create client, assert
self.m.ReplayAll()
client = http.HTTPClient('http://example.com:8004')
resp, body = client.raw_request('GET', '')
self.assertEqual(resp.status, 200)
client.username = 'user'
client.password = 'pass'
resp, body = client.raw_request('GET', '')
self.assertEqual(resp.status, 200)
client.auth_token = 'abcd1234'
resp, body = client.raw_request('GET', '')
self.assertEqual(resp.status, 200)
self.m.VerifyAll()
def test_http_json_request(self):
# Record a 200
mock_conn = http.httplib.HTTPConnection('example.com', 8004,