Added auth param to Client.

This commit is contained in:
Douglas Mendizabal
2013-08-30 11:34:22 -05:00
parent 0718917507
commit c7a32a5053
4 changed files with 65 additions and 37 deletions

View File

@@ -1,6 +1,3 @@
import eventlet
eventlet.monkey_patch(socket=True, select=True)
import json
import os
@@ -23,8 +20,9 @@ class Client(object):
SECRETS_PATH = 'secrets'
ORDERS_PATH = 'orders'
def __init__(self, auth_endpoint=None, user=None, key=None, tenant=None,
token=None, **kwargs):
def __init__(self, auth=True,
auth_endpoint=None, user=None, password=None, tenant=None,
key=None, token=None, **kwargs):
"""
Authenticate and connect to the service endpoint, which can be
received through authentication.
@@ -32,27 +30,44 @@ class Client(object):
Environment variables will be used by default when their corresponding
arguments are not passed in.
:param auth_endpoint: The auth URL to authenticate against
default: env('OS_AUTH_URL')
:param user: The user to authenticate as
default: env('OS_USERNAME')
:param key: The API key or password to auth with
default: env('OS_PASSWORD')
:param auth: Whether the client should use keystone
authentication, defaults to True
:param auth_endpoint: The keystone URL used for authentication
required if auth=True
default: env('OS_AUTH_URL')
:param user: keystone user account, required if auth=True
default: env('OS_USERNAME')
:param password: password associated with the user
required if auth=Tru
default: env('OS_PASSWORD')
:param tenant: The tenant ID
default: env('OS_TENANT_NAME')
:keyword param endpoint: The barbican endpoint to connect to
default: env('BARBICAN_ENDPOINT')
default: env('OS_TENANT_NAME')
If a token is provided, an endpoint should be as well.
:param key: The API key or password to auth with
:keyword param endpoint: The barbican endpoint to connect to
default: env('BARBICAN_ENDPOINT')
"""
LOG.debug(_("Creating Client object"))
self.env = kwargs.get('fake_env') or env
if auth:
LOG.debug(_('Using authentication with keystone'))
self._auth_endpoint = auth_endpoint or self.env('OS_AUTH_URL')
self._user = user or self.env('OS_USERNAME')
self._password = password or self.env('OS_PASSWORD')
self._tenant = tenant or self.env('OS_TENANT_NAME')
if not all([self._auth_endpoint, self._user,
self._password, self._tenant]):
raise ValueError('Authentication requires an endpoint, user, '
'password, and tenant.')
#TODO(dmend): remove these
self._auth_endpoint = auth_endpoint or self.env('OS_AUTH_URL')
self._user = user or self.env('OS_USERNAME')
self._key = key or self.env('OS_PASSWORD')
self._tenant = tenant or self.env('OS_TENANT_NAME')
self._key = key or self._password
if not all([self._auth_endpoint, self._user, self._key, self._tenant]):
raise ClientException("The authorization endpoint, username, key,"
" and tenant name should either be passed i"

View File

@@ -26,8 +26,10 @@ class WhenTestingClient(unittest.TestCase):
def setUp(self):
self.auth_endpoint = 'https://keystone.com/v2'
self.user = 'user'
self.key = 'key'
self.password = 'password'
self.tenant = 'tenant'
self.key = 'key'
self.endpoint = 'http://localhost:9311/v1/'
self.auth_token = 'token'
self.href = 'http://localhost:9311/v1/12345/orders'
@@ -52,21 +54,34 @@ class WhenTestingClient(unittest.TestCase):
'req-6c19d09e-1167-445c-b435-d6b0818b59b9'
}
self.request.return_value.ok = True
self.client = client.Client(self.auth_endpoint, self.user,
self.key, self.tenant,
self.client = client.Client(auth_endpoint=self.auth_endpoint,
user=self.user,
key=self.key, tenant=self.tenant,
token=self.auth_token,
authenticate=self.authenticate,
request=self.request,
endpoint=self.endpoint)
endpoint=self.endpoint,
auth=False)
def test_authenticated_client_requires_endpoint_user_pw_tenant(self):
with self.assertRaises(ValueError):
c = client.Client(auth=True)
with self.assertRaises(ValueError):
c = client.Client() # default auth=True
c=client.Client(auth_endpoint=self.auth_endpoint, user=self.user,
password=self.password, tenant=self.tenant,
#TODO(dmend): remove authenticate below
authenticate=self.authenticate)
def test_should_connect_with_token(self):
self.assertFalse(self.authenticate.called)
def test_should_connect_without_token(self):
self.client = client.Client(self.auth_endpoint,
self.user,
self.key,
self.tenant,
self.client = client.Client(auth=False,
auth_endpoint=self.auth_endpoint,
user=self.user,
key=self.key,
tenant=self.tenant,
authenticate=self.authenticate,
endpoint=self.endpoint)
self.authenticate\
@@ -87,8 +102,11 @@ class WhenTestingClient(unittest.TestCase):
def test_should_raise_for_bad_args(self):
with self.assertRaises(ClientException):
self.client = client.Client(None, self.user,
self.key, self.tenant,
self.client = client.Client(auth=False,
auth_endpoint=None,
user=self.user,
key=self.key,
tenant=self.tenant,
fake_env=self.fake_env,
token=self.auth_token,
authenticate=self.authenticate,

View File

@@ -1,15 +1,10 @@
distribute>=0.6.24
# Install bounded pep8/pyflakes first, then let flake8 install
pep8==1.4.5
pyflakes==0.7.2
flake8==2.0
hacking>=0.5.3,<0.6
pep8>=1.4.5
pyflakes>=0.7.2
flake8>=2.0
hacking>=0.5.3
coverage
discover
mox
mock>=1.0.1
sphinx>=1.1.2

View File

@@ -35,7 +35,7 @@ show-source = True
exclude = .venv,.tox,dist,doc,*egg
[testenv:py26]
commands = nosetests {posargs:--with-xcoverage --all-modules --cover-inclusive --traverse-namespace --with-xunit --cover-package=barbican}
commands = nosetests {posargs:--with-xcoverage --all-modules --cover-inclusive --traverse-namespace --with-xunit --cover-package=barbicanclient}
[testenv:py27]
commands = nosetests {posargs:--with-xcoverage --all-modules --cover-inclusive --traverse-namespace --with-xunit --cover-package=barbican}
commands = nosetests {posargs:--with-xcoverage --all-modules --cover-inclusive --traverse-namespace --with-xunit --cover-package=barbicanclient}