diff --git a/barbicanclient/client.py b/barbicanclient/client.py index 58699551..226da500 100644 --- a/barbicanclient/client.py +++ b/barbicanclient/client.py @@ -9,6 +9,7 @@ from barbicanclient.secrets import Secret from barbicanclient.orders import Order from barbicanclient.common.auth import authenticate from barbicanclient.common.exceptions import ClientException +from openstack.common.timeutils import parse_isotime from urlparse import urljoin @@ -100,6 +101,42 @@ class Connection(object): return secrets + def create_secret(self, + name, + mime_type, + algorithm, + bit_length, + cypher_type, + plain_text, + expiration): + href = "%s/%s" % (self._tenant, self.SECRETS_PATH) + secret_dict = {} + secret_dict['name'] = name + secret_dict['mime_type'] = mime_type + secret_dict['algorithm'] = algorithm + secret_dict['bit_length'] = int(bit_length) + secret_dict['cypher_type'] = cypher_type + secret_dict['plain_text'] = plain_text + if expiration is not None: + secret_dict['expiration'] = parse_isotime(expiration) + hdrs, body = self._perform_http(href=href, + method='POST', + request_body=json.dumps(secret_dict)) + return body['secret_ref'] + + def delete_secret(self, secret_id): + href = "%s/%s/%s" % (self._tenant, self.SECRETS_PATH, secret_id) + hdrs, body = self._perform_http(href=href, method='DELETE') + # TODO: should this return something + + def get_secret(self, secret_id, mime_type): + href = "%s/%s/%s" % (self._tenant, self.SECRETS_PATH, secret_id) + hdrs = {"Accept": mime_type} + hdrs, body = self._perform_http(href=href, method='GET', headers=hdrs, + parse_json=False) + + return body + def list_orders(self): """ Returns the list of orders @@ -137,7 +174,8 @@ class Connection(object): hdrs, body = self._perform_http(href=href, method='DELETE') # TODO: should this return something - def _perform_http(self, method, href, request_body='', headers={}): + def _perform_http(self, method, href, request_body='', headers={}, + parse_json=True): """ Perform an HTTP operation, checking for appropriate errors, etc. and returns the response @@ -145,6 +183,7 @@ class Connection(object): :param method: The http method to use (GET, PUT, etc) :param body: The optional body to submit :param headers: Any additional headers to submit + :param parse_json: Whether the response body should be parsed as json :return: (headers, body) """ if not isinstance(request_body, str): @@ -152,10 +191,8 @@ class Connection(object): url = urljoin(self._endpoint, href) - response = requests.request(method=method, url=url, data=request_body) - - #response = self._session.request(method=method, url=url, - # data=request_body, headers=headers) + response = requests.request(method=method, url=url, data=request_body, + headers=headers) # Check if the status code is 2xx class if not response.ok: @@ -163,6 +200,11 @@ class Connection(object): http_status=response.status_code, http_response_content=response.content) - resp_body = json.loads(response.content) if response.content else '' + if response.content and parse_json is True: + resp_body = json.loads(response.content) + elif response.content and parse_json is False: + resp_body = response.content + else: + resp_body = '' return response.headers, resp_body diff --git a/examples/create_secret.py b/examples/create_secret.py new file mode 100644 index 00000000..a6d826c0 --- /dev/null +++ b/examples/create_secret.py @@ -0,0 +1,88 @@ +import argparse + +from barbicanclient import client + +IDENTITY = 'https://identity.api.rackspacecloud.com/v2.0' +ENDPOINT = 'https://barbican.api.rackspacecloud.com/v1/' + + +def connect(username, password, tenant, endpoint): + connection = client.Connection(IDENTITY, + username, + password, + tenant, + endpoint=endpoint) + return connection + + +def parse_args(): + parser = argparse.ArgumentParser( + description='Testing code for creating barbican secret.' + ) + 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, + help='The barbican endpoint to test against' + ) + parser.add_argument( + '--name', + help='Name of secret' + ) + parser.add_argument( + '--mime-type', + help='MIME type of secret to create' + ) + parser.add_argument( + '--algorithm', + help='Algorithm of secret to create' + ) + parser.add_argument( + '--bit-length', + help='Bit length of secret to create' + ) + parser.add_argument( + '--cypher-type', + help='Cypher type of secret to create' + ) + parser.add_argument( + '--plain-text', + help='Plain text of the secret' + ) + parser.add_argument( + '--expiration', + default=None, + help='Plain text of the secret' + ) + + args = parser.parse_args() + return args + + +if __name__ == '__main__': + args = parse_args() + conn = connect(args.username, args.password, args.tenant, args.endpoint) + secret_ref = conn.create_secret(args.name, + args.mime_type, + args.algorithm, + args.bit_length, + args.cypher_type, + args.plain_text, + args.expiration) + print secret_ref diff --git a/examples/delete_order.py b/examples/delete_order.py index f0690548..6f2ec61d 100644 --- a/examples/delete_order.py +++ b/examples/delete_order.py @@ -17,7 +17,7 @@ def connect(username, password, tenant, endpoint): def parse_args(): parser = argparse.ArgumentParser( - description='Testing code for creating barbican order.' + description='Testing code for deleting barbican order.' ) parser.add_argument( '--username', diff --git a/examples/delete_secret.py b/examples/delete_secret.py new file mode 100644 index 00000000..a3b671fd --- /dev/null +++ b/examples/delete_secret.py @@ -0,0 +1,56 @@ +import argparse + +from barbicanclient import client + +IDENTITY = 'https://identity.api.rackspacecloud.com/v2.0' +ENDPOINT = 'https://barbican.api.rackspacecloud.com/v1/' + + +def connect(username, password, tenant, endpoint): + connection = client.Connection(IDENTITY, + username, + password, + tenant, + endpoint=endpoint) + return connection + + +def parse_args(): + parser = argparse.ArgumentParser( + description='Testing code for deleting barbican secret.' + ) + 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, + help='The barbican endpoint to test against' + ) + parser.add_argument( + '--secret-id', + help='ID of secret' + ) + + args = parser.parse_args() + return args + + +if __name__ == '__main__': + args = parse_args() + conn = connect(args.username, args.password, args.tenant, args.endpoint) + conn.delete_secret(args.secret_id) diff --git a/examples/get_secret.py b/examples/get_secret.py new file mode 100644 index 00000000..8aa18453 --- /dev/null +++ b/examples/get_secret.py @@ -0,0 +1,61 @@ +import argparse + +from barbicanclient import client + +IDENTITY = 'https://identity.api.rackspacecloud.com/v2.0' +ENDPOINT = 'https://barbican.api.rackspacecloud.com/v1/' + + +def connect(username, password, tenant, endpoint): + connection = client.Connection(IDENTITY, + username, + password, + tenant, + endpoint=endpoint) + return connection + + +def parse_args(): + parser = argparse.ArgumentParser( + description='Testing code for getting a barbican secret.' + ) + 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, + help='The barbican endpoint to test against' + ) + parser.add_argument( + '--secret-id', + help='ID of secret' + ) + parser.add_argument( + '--mime-type', + help='MIME of secret' + ) + + args = parser.parse_args() + return args + + +if __name__ == '__main__': + args = parse_args() + conn = connect(args.username, args.password, args.tenant, args.endpoint) + s = conn.get_secret(args.secret_id, args.mime_type) + print s