diff --git a/barbicanclient/client.py b/barbicanclient/client.py index be0294a6..b95a4d2e 100644 --- a/barbicanclient/client.py +++ b/barbicanclient/client.py @@ -6,14 +6,14 @@ eventlet.monkey_patch(socket=True, select=True) import json import requests +from barbicanclient.secrets import Secret from barbicanclient.common.auth import authenticate -from barbicanclient.common.utils import proc_template from barbicanclient.common.exceptions import ClientException from urlparse import urljoin class Connection(object): - def __init__(self, auth_endpoint, user, key, **kwargs): + def __init__(self, auth_endpoint, user, key, tenant, **kwargs): """ :param auth_endpoint: The auth URL to authenticate against :param user: The user to authenticate as @@ -22,13 +22,15 @@ class Connection(object): self._auth_endpoint = auth_endpoint self._user = user self._key = key + self._tenant = tenant self._endpoint = kwargs.get('endpoint') or 'https://barbican.api.rackspacecloud.com/v1/' self._cacert = kwargs.get('cacert') + self.connect() + # Hardcoded uri's right now self.secrets_href = 'secrets/' - @property def _conn(self): """ @@ -63,11 +65,8 @@ class Connection(object): self.auth_token = token else: (self._endpoint, - self.auth_token) = authenticate(self._auth_endpoint, - self._user, self._key, - endpoint=self._endpoint, - cacert=self._cacert) - #self._load_homedoc_hrefs() + self.auth_token) = authenticate(self._auth_endpoint, self._user, self._key, self._tenant, + endpoint=self._endpoint, cacert=self._cacert) @property def auth_token(self): @@ -85,51 +84,17 @@ class Connection(object): """ Returns the list of secrets for the auth'd tenant """ - href = proc_template(self.secrets_href) + #href = proc_template(self.secrets_href) + href = "%s/%s" % (self._tenant, self.secrets_href) hdrs, body = self._perform_http(href=href, method='GET') - #return Queue(self, href=href, name=queue_name, metadata=body) + secrets_dict = body['secrets'] + secrets = [] + for s in secrets_dict: + secrets.append(Secret(self._conn, s)) + return secrets - - - - # - # def _load_homedoc_hrefs(self): - # """ - # Loads the home document hrefs for each endpoint - # Note: at the present time homedocs have not been - # implemented so these hrefs are simply hard-coded. When - # they are implemented we should update this function to - # actually parse the home document. - # """ - # - # # Queues endpoint{" + name + "}", quote(str(value))) - # self.queues_href = self._endpoint + "/queues" - # - # # Specific queue endpoint - # self.queue_href = self.queues_href + "/{queue_name}" - # - # # Messages endpoint - # self.messages_href = self.queue_href + "/messages" - # - # # Specific message endpoint - # self.message_href = self.messages_href + "/{message_id}" - # - # # Claims endpoint - # self._claims_href = self.queues_href + "/claims" - # - # # Specific claim endpoint - # self._claim_href = self.queues_href + "/claims/{claim_id}" - # - # # Actions endpoint - # self.actions_href = self._endpoint + "/actions" - # - # # Specific action endpoint - # self.action_href = self.actions_href + "/{action_id}" - # - # # Statistics endpoint - # self.stats_href = self.queue_href + "/stats" # # def create_queue(self, queue_name): # """ @@ -192,7 +157,6 @@ class Connection(object): Perform an HTTP operation, checking for appropriate errors, etc. and returns the response - :param conn: The HTTPConnection or HTTPSConnection to use :param method: The http method to use (GET, PUT, etc) :param body: The optional body to submit :param headers: Any additional headers to submit diff --git a/barbicanclient/common/auth.py b/barbicanclient/common/auth.py index 226c34fc..65248134 100644 --- a/barbicanclient/common/auth.py +++ b/barbicanclient/common/auth.py @@ -5,7 +5,7 @@ from keystoneclient.v2_0 import client as ksclient from keystoneclient import exceptions -def authenticate(auth_url, user, key, **kwargs): +def authenticate(auth_url, user, key, tenant, **kwargs): """Authenticates against the endpoint to use. The correct endpoint to use is looked up in the service catalog. The caller can override this lookup by passing the endpoint @@ -32,25 +32,22 @@ def authenticate(auth_url, user, key, **kwargs): """ insecure = kwargs.get('insecure', False) endpoint = kwargs.get('endpoint') - tenant_name = kwargs.get('tenant_name') - tenant_id = kwargs.get('tenant_id') cacert = kwargs.get('cacert') try: _ksclient = ksclient.Client(username=user, password=key, - tenant_name=tenant_name, - tenant_id=tenant_id, + tenant_name=tenant, cacert=cacert, auth_url=auth_url, insecure=insecure) - except exceptions.Unauthorized as ex: + except exceptions.Unauthorized: raise ClientException('Unauthorized. Check username, password' ' and tenant name/id') - except exceptions.AuthorizationFailure as err: - raise ClientException('Authorization Failure. %s' % err) + except exceptions.AuthorizationFailure: + raise ClientException('Authorization Failure. %s') if not endpoint: # The user did not pass in an endpoint, so we need to @@ -70,4 +67,4 @@ def authenticate(auth_url, user, key, **kwargs): except exceptions.EndpointNotFound as ex: raise ClientException('Endpoint not found in service catalog') - return (endpoint, _ksclient.auth_token) \ No newline at end of file + return endpoint, _ksclient.auth_token \ No newline at end of file diff --git a/barbicanclient/secrets.py b/barbicanclient/secrets.py index 7392aeaa..25201492 100644 --- a/barbicanclient/secrets.py +++ b/barbicanclient/secrets.py @@ -1,14 +1,37 @@ +from urlparse import urlparse +from openstack.common.timeutils import parse_isotime + + class Secret(object): """ A secret is any data the user has stored in the key management system. """ - def __init__(self, connection, json): + def __init__(self, connection, dict): """ Builds a secret object from a json representation. Includes the connection object for subtasks. """ + self._connection = connection + self._href = dict['secret_ref'] + self._created = parse_isotime(dict['created']) + self._status = dict['status'] + self._algorithm = dict.get('algorithm') + self._bit_length = dict.get('bit_length') + self._mime_type = dict.get('mime_type') + self._name = dict.get('name') + self._cypher_type = dict.get('cypher_type') + if dict.get('expiration') is not None: + self._expiration = parse_isotime(dict['expiration']) + if dict.get('updated') is not None: + self._updated = parse_isotime(dict['updated']) + + self._id = urlparse(self._href).path.split('/').pop() + + @property + def id(self): + return self._id def __repr__(self): - return "" % self.name + return "" % self.id diff --git a/examples/secrets.py b/examples/secrets.py index e1b387e0..1b30e67d 100644 --- a/examples/secrets.py +++ b/examples/secrets.py @@ -6,17 +6,20 @@ IDENTITY = 'https://identity.api.rackspacecloud.com/v2.0' ENDPOINT = 'https://barbican.api.rackspacecloud.com/v1/' -def list_secrets(username, password): - connection = client.Connection(IDENTITY, username, password) +def list_secrets(username, password, tenant, endpoint): + connection = client.Connection(IDENTITY, username, password, tenant, endpoint=endpoint) secrets = connection.list_secrets() - print secrets.list() + print 'Current Secrets (%d):' % (len(secrets)) + for secret in secrets: + print '- %s' % secret def parse_args(): parser = argparse.ArgumentParser(description='Testing code for barbican secrets api resource.') parser.add_argument('--username', help='The keystone username used for for authentication') parser.add_argument('--password', help='The keystone password used for for authentication') + parser.add_argument('--tenant', help='The keystone tenant used for for authentication') parser.add_argument('--keystone', default=IDENTITY, help='The keystone endpoint used for for authentication') parser.add_argument('--endpoint', default=ENDPOINT, @@ -28,4 +31,4 @@ def parse_args(): if __name__ == '__main__': args = parse_args() - list_secrets(args.username, args.password) \ No newline at end of file + list_secrets(args.username, args.password, args.tenant, args.endpoint) \ No newline at end of file diff --git a/tools/pip-requires b/tools/pip-requires index 7cf33e89..b296544e 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -1,4 +1,5 @@ httplib2>=0.7.7 argparse>=1.2.1 python-keystoneclient>=0.2.3 -eventlet>=0.12.1 \ No newline at end of file +eventlet>=0.12.1 +iso8601>=0.1.4 \ No newline at end of file