Add complete functionality to the command line tool

Make the __str__() method for secrets and orders more
descriptive
This commit is contained in:
Arash Ghoreyshi
2013-06-17 16:38:35 -05:00
parent a5d9ca5ec6
commit 22ac560432
4 changed files with 157 additions and 53 deletions

View File

@@ -16,6 +16,7 @@ from urlparse import urljoin
LOG = log.getLogger(__name__)
log.setup('barbicanclient')
class Connection(object):
@@ -103,7 +104,7 @@ class Connection(object):
self._token = value
self._session.headers['X-Auth-Token'] = value
def list_secrets(self, limit=20, offset=0):
def list_secrets(self, limit=10, offset=0):
"""
Returns a tuple containing three items: a list of secrets pertaining
to the given offset and limit, a reference to the previous set of
@@ -206,7 +207,7 @@ class Connection(object):
LOG.debug(_("Response - headers: {0}\nbody: {1}").format(hdrs, body))
return body
def list_orders(self, limit=20, offset=0):
def list_orders(self, limit=10, offset=0):
"""
Returns a tuple containing three items: a list of orders pertaining
to the given offset and limit, a reference to the previous set of
@@ -334,4 +335,3 @@ class Connection(object):
if __name__ == '__main__':
config.parse_args()
log.setup('barbicanclient')

View File

@@ -36,4 +36,11 @@ class Order(object):
self.connection.delete_order(self)
def __str__(self):
return "<Order %s>" % self.id
return ("Order - ID: {0}\n"
" order reference: {1}\n"
" secret reference: {2}\n"
" created: {3}\n"
" status: {4}\n"
.format(self.id, self.order_ref, self.secret_ref, self.created,
self.status)
)

View File

@@ -39,4 +39,16 @@ class Secret(object):
return self._id
def __str__(self):
return "<Secret %s>" % self.id
return ("Secret - ID: {0}\n"
" reference: {1}\n"
" name: {2}\n"
" created: {3}\n"
" MIME type: {4}\n"
" status: {5}\n"
" bit length: {6}\n"
" algorithm: {7}\n"
" cypher type: {8}\n"
.format(self.id, self.secret_ref, self.name, self.created,
self.mime_type, self.status, self.bit_length,
self.algorithm, self.cypher_type)
)

181
keep
View File

@@ -7,89 +7,174 @@ from barbicanclient import client
class Keep:
def __init__(self):
self.parser = argparse.ArgumentParser(description='Access the Barbican'
' key management sevice.')
self.parser = self.get_main_parser()
self.subparsers = self.parser.add_subparsers(title='subcommands',
description=
'Action to perform')
self.parser.add_argument('type',
choices=["order", "secret"],
help="Type to operate on")
self.parser.add_argument('--auth_endpoint', '-A',
help='The URL to authenticate against')
self.parser.add_argument('--user', '-U',
help='The user to authenticate as')
self.parser.add_argument('--password', '-P',
help='The API key or password to '
'authenticate with')
self.parser.add_argument('--tenant', '-T',
help='The tenant ID')
self.parser.add_argument('--endpoint', '-E',
help='The URL of the barbican server')
self.parser.add_argument('--token', '-K',
help='The authentication token')
self.add_create_args()
self.add_delete_args()
self.add_get_args()
self.add_list_args()
self.execute()
def get_main_parser(self):
parser = argparse.ArgumentParser(description='Access the Barbican'
' key management sevice.')
parser.add_argument('type',
choices=["order", "secret"],
help="type to operate on")
parser.add_argument('--auth_endpoint', '-A',
help='the URL to authenticate against')
parser.add_argument('--user', '-U', help='the user to authenticate as')
parser.add_argument('--password', '-P',
help='the API key or password to '
'authenticate with')
parser.add_argument('--tenant', '-T', help='the tenant ID')
parser.add_argument('--endpoint', '-E',
help='the URL of the barbican server')
parser.add_argument('--token', '-K', help='the authentication token')
return parser
def add_create_args(self):
create_parser = self.subparsers.add_parser('create', help='Create a '
'secret or an order')
create_parser.add_argument('--mime_type', '-m', default='text/plain',
help='The MIME type used to fetch the secre'
't (default: %(default)s)')
create_parser.add_argument('--name', '-n', help='A human-friendly name'
help='the MIME type of the raw secret (defa'
'ult: %(default)s)')
create_parser.add_argument('--name', '-n', help='a human-friendly name'
' used only for reference')
create_parser.add_argument('--algorithm', '-a', help='The algorithm us'
create_parser.add_argument('--algorithm', '-a', help='the algorithm us'
'ed only for reference')
create_parser.add_argument('--bit_length', '-b', help='The bit length '
'of the secret used only for reference')
create_parser.add_argument('--cypher_type', '-c', help='The cypher typ'
create_parser.add_argument('--bit_length', '-b', default=256,
help='the bit length of the secret used '
'only for reference (default: %(default)s)',
type=int)
create_parser.add_argument('--cypher_type', '-c', help='the cypher typ'
'e used only for reference')
create_parser.add_argument('--plain_text', '-p', help='The unencrypted'
' secret (only used for creating secrets)')
create_parser.add_argument('--expiration', '-e', help='Expiration time'
' for the secret in ISO 8601 format')
create_parser.add_argument('--plain_text', '-p', help='the unencrypted'
' secret (only used for secrets)')
create_parser.add_argument('--expiration', '-e', help='expiration time'
' for the secret in ISO 8601 format '
'(only used for secrets)')
create_parser.set_defaults(func=self.create)
def add_delete_args(self):
delete_parser = self.subparsers.add_parser('delete', help='Delete a se'
'cret or an order')
'cret or an order either by'
' id or by href')
delete_parser.add_argument('--href', '-r', help='the reference to the '
'secret or order')
delete_parser.add_argument('--id', '-i', help='the id of the secret or'
'order')
delete_parser.set_defaults(func=self.delete)
def add_get_args(self):
get_parser = self.subparsers.add_parser('get', help='Retrieve a secret'
' or an order')
' or an order either by id or'
' by href. The raw secret of '
'the type specified with --mi'
'me_type can be retrieved ins'
'tead for secrets using the -'
'-raw flag.')
get_parser.add_argument('--href', '-r', help='the reference to the '
'secret or order')
get_parser.add_argument('--id', '-i', help='the id of the secret or'
' order')
get_parser.add_argument('--raw', '-w', help='if specified, gets the ra'
'w secret of type specified with --mime_type ('
'only used for secrets)', action='store_true')
get_parser.add_argument('--mime_type', '-m', default='text/plain',
help='the MIME type of the raw secret (defa'
'ult: %(default)s; only used for secrets)')
get_parser.set_defaults(func=self.get)
def add_list_args(self):
list_parser = self.subparsers.add_parser('list',
help='List secrets or orders')
list_parser.add_argument('--href', '-r', help='the reference to what i'
's to be listed; put in quotes to avoid unint'
'entional backgrounding by way of \'&\'')
list_parser.add_argument('--limit', '-l', default=10, help='specify t'
'he number of items to list per page (defaul'
't: %(default)s; maximum: 100)', type=int)
list_parser.add_argument('--offset', '-o', default=0, help='specify t'
'he page offset (default: %(default)s)',
type=int)
list_parser.set_defaults(func=self.lst)
def create(self, args):
if self.args.type == 'secret':
secret = self.conn.create_secret(self.args.mime_type,
self.args.plain_text,
self.args.name,
self.args.algorithm,
self.args.bit_length,
self.args.cypher_type,
self.args.expiration)
if args.type == 'secret':
secret = self.conn.create_secret(args.mime_type,
args.plain_text,
args.name,
args.algorithm,
args.bit_length,
args.cypher_type,
args.expiration)
print secret.secret_ref
else:
order = self.conn.create_order(args.mime_type,
args.name,
args.algorithm,
args.bit_length,
args.cypher_type)
print order.order_ref
def delete(self, args):
if args.type == 'secret':
if args.href:
self.conn.delete_secret(args.href)
elif args.id:
self.conn.delete_secret_by_id(args.id)
else:
if args.href:
self.conn.delete_order(args.href)
elif args.id:
self.conn.delete_order_by_id(args.id)
def get(self, args):
if args.type == 'secret':
if args.href and args.raw:
print self.conn.get_raw_secret(args.href, args.mime_type)
elif args.href:
print self.conn.get_secret(args.href)
elif args.id and args.raw:
print self.conn.get_raw_secret_by_id(args.id, args.mime_type)
elif args.id:
print self.conn.get_secret_by_id(args.id)
else:
if args.href:
print self.conn.get_order(args.href)
elif args.id:
print self.conn.get_order_by_id(args.id)
def lst(self, args):
if args.type == 'secret':
if args.href:
l = self.conn.list_secrets_by_href(args.href)
else:
l = self.conn.list_secrets(args.limit, args.offset)
else:
if args.href:
l = self.conn.list_orders_by_href(args.href)
else:
l = self.conn.list_orders(args.limit, args.offset)
for i in l[0]:
print i
print 'previous reference: ', l[1]
print 'next reference: ', l[2]
def execute(self):
self.args = self.parser.parse_args()
self.conn = client.Connection(self.args.auth_endpoint, self.args.user,
self.args.password, self.args.tenant,
self.args.token,
endpoint=self.args.endpoint)
self.args.func(self.args)
print self.args
args = self.parser.parse_args()
self.conn = client.Connection(args.auth_endpoint, args.user,
args.password, args.tenant,
args.token,
endpoint=args.endpoint)
args.func(args)
def main():
Keep()
k = Keep()
k.execute()
if __name__ == '__main__':