Merge pull request #57 from piston/make_keystone_auth_work

Make it possible to authenticate against keystone
This commit is contained in:
Sandy Walsh 2011-08-08 06:47:42 -07:00
commit 7e5a474d0d
3 changed files with 85 additions and 9 deletions

View File

@ -1,4 +1,6 @@
# Copyright 2010 Jacob Kaplan-Moss
# Copyright 2011 Piston Cloud Computing
"""
OpenStack Client interface. Handles the REST calls and responses.
"""
@ -132,14 +134,32 @@ class HTTPClient(httplib2.Http):
self.version = part
break
headers = {'X-Auth-User': self.user,
'X-Auth-Key': self.apikey}
if self.projectid:
headers['X-Auth-Project-Id'] = self.projectid
resp, body = self.request(self.auth_url, 'GET', headers=headers)
self.management_url = resp['x-server-management-url']
if not self.version == "v2.0": #FIXME(chris): This should be better.
headers = {'X-Auth-User': self.user,
'X-Auth-Key': self.apikey}
if self.projectid:
headers['X-Auth-Project-Id'] = self.projectid
self.auth_token = resp['x-auth-token']
resp, body = self.request(self.auth_url, 'GET', headers=headers)
self.management_url = resp['x-server-management-url']
self.auth_token = resp['x-auth-token']
else:
body = {"passwordCredentials": {"username": self.user,
"password": self.apikey}}
if self.projectid:
body['passwordCredentials']['tenantId'] = self.projectid
token_url = urlparse.urljoin(self.auth_url, "tokens")
resp, body = self.request(token_url, "POST", body=body)
self.management_url = body["auth"]["serviceCatalog"] \
["nova"][0]["publicURL"]
self.auth_token = body["auth"]["token"]["id"]
#TODO(chris): Implement service_catalog
self.service_catalog = None
def _munge_get_url(self, url):
"""

View File

@ -25,6 +25,7 @@ class Client(object):
"""
# FIXME(jesse): project_id isn't required to autenticate
def __init__(self, username, api_key, project_id, auth_url, timeout=None):
self.flavors = flavors.FlavorManager(self)
self.images = images.ImageManager(self)

View File

@ -1,14 +1,69 @@
import httplib2
import json
import mock
import urlparse
from novaclient.v1_1 import client
from novaclient import exceptions
from tests import utils
class AuthenticationTests(utils.TestCase):
class AuthenticateAgainstKeystoneTests(utils.TestCase):
def test_authenticate_success(self):
cs = client.Client("username", "apikey", "project_id", "auth_url/v2.0")
resp = {"auth": {"token": {"expires": "12345", "id": "FAKE_ID"},
"serviceCatalog": {
"nova": [{"adminURL": "http://localhost:8774/v1.1",
"region": "RegionOne",
"internalURL": "http://localhost:8774/v1.1",
"publicURL": "http://localhost:8774/v1.1/"}]}}}
auth_response = httplib2.Response({
"status": 204,
"body": json.dumps(resp),
})
mock_request = mock.Mock(return_value=(auth_response, json.dumps(resp)))
@mock.patch.object(httplib2.Http, "request", mock_request)
def test_auth_call():
cs.client.authenticate()
headers = {'User-Agent': cs.client.USER_AGENT,
'Content-Type': 'application/json',}
body = {'passwordCredentials': {'username': cs.client.user,
'password': cs.client.apikey,
'tenantId': cs.client.projectid,}}
token_url = urlparse.urljoin(cs.client.auth_url, "tokens")
mock_request.assert_called_with(token_url, "POST",
headers=headers,
body=json.dumps(body))
self.assertEqual(cs.client.management_url,
resp["auth"]["serviceCatalog"]["nova"][0]["publicURL"])
self.assertEqual(cs.client.auth_token, resp["auth"]["token"]["id"])
test_auth_call()
def test_authenticate_failure(self):
cs = client.Client("username", "apikey", "project_id", "auth_url/v2.0")
resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}}
auth_response = httplib2.Response({
"status": 401,
"body": json.dumps(resp),
})
mock_request = mock.Mock(return_value=(auth_response, json.dumps(resp)))
@mock.patch.object(httplib2.Http, "request", mock_request)
def test_auth_call():
self.assertRaises(exceptions.Unauthorized, cs.client.authenticate)
test_auth_call()
class AuthenticationTests(utils.TestCase):
def test_authenticate_success(self):
cs = client.Client("username", "apikey", "project_id", "auth_url")
management_url = 'https://servers.api.rackspacecloud.com/v1.1/443470'