From a0d8c9195e1da8e74ae04cbe661c24911bae118f Mon Sep 17 00:00:00 2001 From: Nathan Reller Date: Fri, 11 Apr 2014 17:38:19 -0400 Subject: [PATCH] Initial commit of KMIP library This contains the basic items for repository. The basic files and directories were created to layout the repository structure. Created a simple KMIP client and server. This was generated using the thrift library. This version only tests the thrift framework. It does not adhere to the spec. --- .gitignore | 3 + LICENSE | 1 + README | 1 + kmip/__init__.py | 0 kmip/kmip_client.py | 29 +++ kmip/kmip_server.py | 32 ++++ kmip/transport/__init__.py | 0 kmip/transport/kmip/KMIP-remote | 95 ++++++++++ kmip/transport/kmip/KMIP.py | 295 +++++++++++++++++++++++++++++++ kmip/transport/kmip/__init__.py | 1 + kmip/transport/kmip/constants.py | 11 ++ kmip/transport/kmip/ttypes.py | 18 ++ requirements.txt | 1 + test-requirements.txt | 1 + thrift/kmip.thrift | 43 +++++ 15 files changed, 531 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README create mode 100644 kmip/__init__.py create mode 100755 kmip/kmip_client.py create mode 100755 kmip/kmip_server.py create mode 100644 kmip/transport/__init__.py create mode 100755 kmip/transport/kmip/KMIP-remote create mode 100644 kmip/transport/kmip/KMIP.py create mode 100644 kmip/transport/kmip/__init__.py create mode 100644 kmip/transport/kmip/constants.py create mode 100644 kmip/transport/kmip/ttypes.py create mode 100644 requirements.txt create mode 100644 test-requirements.txt create mode 100644 thrift/kmip.thrift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dae79de --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.project +.pydevproject +*.pyc diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..439b80a --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +/* FIXME Insert a license here */ diff --git a/README b/README new file mode 100644 index 0000000..2b0b71e --- /dev/null +++ b/README @@ -0,0 +1 @@ +TODO fill this in diff --git a/kmip/__init__.py b/kmip/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kmip/kmip_client.py b/kmip/kmip_client.py new file mode 100755 index 0000000..f84178f --- /dev/null +++ b/kmip/kmip_client.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +# TODO insert license here + +from transport.kmip import KMIP +from transport.kmip.ttypes import * + +from thrift import Thrift +from thrift.transport import TSocket +from thrift.transport import TTransport +from thrift.protocol import TBinaryProtocol + +try: + + transport = TSocket.TSocket('localhost', 9090) + transport = TTransport.TBufferedTransport(transport) + protocol = TBinaryProtocol.TBinaryProtocol(transport) + client = KMIP.Client(protocol) + transport.open() + + print 'ping-client()' + client.create() + print 'register_mo-client()' + client.register_mo() + + transport.close() + +except Thrift.TException, tx: + print '%s' % (tx.message) diff --git a/kmip/kmip_server.py b/kmip/kmip_server.py new file mode 100755 index 0000000..889af51 --- /dev/null +++ b/kmip/kmip_server.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +# TODO insert license here + +from transport.kmip import KMIP +from transport.kmip.ttypes import * + +from thrift.transport import TSocket +from thrift.transport import TTransport +from thrift.protocol import TBinaryProtocol +from thrift.server import TServer + +class KMIPHandler: + def __init__(self): + pass + + def create(self): + print 'create()' + + def register_mo(self): + print 'register_mo()' + +handler = KMIPHandler() +processor = KMIP.Processor(handler) +transport = TSocket.TServerSocket(port=9090) +tfactory = TTransport.TBufferedTransportFactory() +pfactory = TBinaryProtocol.TBinaryProtocolFactory() +server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) + +print 'Starting the KMIP server...' +server.serve() +print 'done.' diff --git a/kmip/transport/__init__.py b/kmip/transport/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kmip/transport/kmip/KMIP-remote b/kmip/transport/kmip/KMIP-remote new file mode 100755 index 0000000..04377d2 --- /dev/null +++ b/kmip/transport/kmip/KMIP-remote @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# +# Autogenerated by Thrift Compiler (0.9.1) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +import sys +import pprint +from urlparse import urlparse +from thrift.transport import TTransport +from thrift.transport import TSocket +from thrift.transport import THttpClient +from thrift.protocol import TBinaryProtocol + +from kmip import KMIP +from kmip.ttypes import * + +if len(sys.argv) <= 1 or sys.argv[1] == '--help': + print '' + print 'Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] function [arg1 [arg2...]]' + print '' + print 'Functions:' + print ' void create()' + print ' void register_mo()' + print '' + sys.exit(0) + +pp = pprint.PrettyPrinter(indent = 2) +host = 'localhost' +port = 9090 +uri = '' +framed = False +http = False +argi = 1 + +if sys.argv[argi] == '-h': + parts = sys.argv[argi+1].split(':') + host = parts[0] + if len(parts) > 1: + port = int(parts[1]) + argi += 2 + +if sys.argv[argi] == '-u': + url = urlparse(sys.argv[argi+1]) + parts = url[1].split(':') + host = parts[0] + if len(parts) > 1: + port = int(parts[1]) + else: + port = 80 + uri = url[2] + if url[4]: + uri += '?%s' % url[4] + http = True + argi += 2 + +if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed': + framed = True + argi += 1 + +cmd = sys.argv[argi] +args = sys.argv[argi+1:] + +if http: + transport = THttpClient.THttpClient(host, port, uri) +else: + socket = TSocket.TSocket(host, port) + if framed: + transport = TTransport.TFramedTransport(socket) + else: + transport = TTransport.TBufferedTransport(socket) +protocol = TBinaryProtocol.TBinaryProtocol(transport) +client = KMIP.Client(protocol) +transport.open() + +if cmd == 'create': + if len(args) != 0: + print 'create requires 0 args' + sys.exit(1) + pp.pprint(client.create()) + +elif cmd == 'register_mo': + if len(args) != 0: + print 'register_mo requires 0 args' + sys.exit(1) + pp.pprint(client.register_mo()) + +else: + print 'Unrecognized method %s' % cmd + sys.exit(1) + +transport.close() diff --git a/kmip/transport/kmip/KMIP.py b/kmip/transport/kmip/KMIP.py new file mode 100644 index 0000000..8d333d7 --- /dev/null +++ b/kmip/transport/kmip/KMIP.py @@ -0,0 +1,295 @@ +# +# Autogenerated by Thrift Compiler (0.9.1) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +from thrift.Thrift import TType, TMessageType, TException, TApplicationException +from ttypes import * +from thrift.Thrift import TProcessor +from thrift.transport import TTransport +from thrift.protocol import TBinaryProtocol, TProtocol +try: + from thrift.protocol import fastbinary +except: + fastbinary = None + + +class Iface: + def create(self): + pass + + def register_mo(self): + pass + + +class Client(Iface): + def __init__(self, iprot, oprot=None): + self._iprot = self._oprot = iprot + if oprot is not None: + self._oprot = oprot + self._seqid = 0 + + def create(self): + self.send_create() + self.recv_create() + + def send_create(self): + self._oprot.writeMessageBegin('create', TMessageType.CALL, self._seqid) + args = create_args() + args.write(self._oprot) + self._oprot.writeMessageEnd() + self._oprot.trans.flush() + + def recv_create(self): + (fname, mtype, rseqid) = self._iprot.readMessageBegin() + if mtype == TMessageType.EXCEPTION: + x = TApplicationException() + x.read(self._iprot) + self._iprot.readMessageEnd() + raise x + result = create_result() + result.read(self._iprot) + self._iprot.readMessageEnd() + return + + def register_mo(self): + self.send_register_mo() + self.recv_register_mo() + + def send_register_mo(self): + self._oprot.writeMessageBegin('register_mo', TMessageType.CALL, self._seqid) + args = register_mo_args() + args.write(self._oprot) + self._oprot.writeMessageEnd() + self._oprot.trans.flush() + + def recv_register_mo(self): + (fname, mtype, rseqid) = self._iprot.readMessageBegin() + if mtype == TMessageType.EXCEPTION: + x = TApplicationException() + x.read(self._iprot) + self._iprot.readMessageEnd() + raise x + result = register_mo_result() + result.read(self._iprot) + self._iprot.readMessageEnd() + return + + +class Processor(Iface, TProcessor): + def __init__(self, handler): + self._handler = handler + self._processMap = {} + self._processMap["create"] = Processor.process_create + self._processMap["register_mo"] = Processor.process_register_mo + + def process(self, iprot, oprot): + (name, type, seqid) = iprot.readMessageBegin() + if name not in self._processMap: + iprot.skip(TType.STRUCT) + iprot.readMessageEnd() + x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name)) + oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid) + x.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + return + else: + self._processMap[name](self, seqid, iprot, oprot) + return True + + def process_create(self, seqid, iprot, oprot): + args = create_args() + args.read(iprot) + iprot.readMessageEnd() + result = create_result() + self._handler.create() + oprot.writeMessageBegin("create", TMessageType.REPLY, seqid) + result.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + + def process_register_mo(self, seqid, iprot, oprot): + args = register_mo_args() + args.read(iprot) + iprot.readMessageEnd() + result = register_mo_result() + self._handler.register_mo() + oprot.writeMessageBegin("register_mo", TMessageType.REPLY, seqid) + result.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + + +# HELPER FUNCTIONS AND STRUCTURES + +class create_args: + + thrift_spec = ( + ) + + def read(self, iprot): + if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None: + fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec)) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None: + oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec))) + return + oprot.writeStructBegin('create_args') + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.iteritems()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + +class create_result: + + thrift_spec = ( + ) + + def read(self, iprot): + if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None: + fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec)) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None: + oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec))) + return + oprot.writeStructBegin('create_result') + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.iteritems()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + +class register_mo_args: + + thrift_spec = ( + ) + + def read(self, iprot): + if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None: + fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec)) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None: + oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec))) + return + oprot.writeStructBegin('register_mo_args') + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.iteritems()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) + +class register_mo_result: + + thrift_spec = ( + ) + + def read(self, iprot): + if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None: + fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec)) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None: + oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec))) + return + oprot.writeStructBegin('register_mo_result') + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.iteritems()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) diff --git a/kmip/transport/kmip/__init__.py b/kmip/transport/kmip/__init__.py new file mode 100644 index 0000000..41fa036 --- /dev/null +++ b/kmip/transport/kmip/__init__.py @@ -0,0 +1 @@ +__all__ = ['ttypes', 'constants', 'KMIP'] diff --git a/kmip/transport/kmip/constants.py b/kmip/transport/kmip/constants.py new file mode 100644 index 0000000..35216c6 --- /dev/null +++ b/kmip/transport/kmip/constants.py @@ -0,0 +1,11 @@ +# +# Autogenerated by Thrift Compiler (0.9.1) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +from thrift.Thrift import TType, TMessageType, TException, TApplicationException +from ttypes import * + diff --git a/kmip/transport/kmip/ttypes.py b/kmip/transport/kmip/ttypes.py new file mode 100644 index 0000000..424ff7e --- /dev/null +++ b/kmip/transport/kmip/ttypes.py @@ -0,0 +1,18 @@ +# +# Autogenerated by Thrift Compiler (0.9.1) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +from thrift.Thrift import TType, TMessageType, TException, TApplicationException + +from thrift.transport import TTransport +from thrift.protocol import TBinaryProtocol, TProtocol +try: + from thrift.protocol import fastbinary +except: + fastbinary = None + + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c334a20 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +# TODO: Add any library dependencies here diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..85cc5c0 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +# TODO: add any test requirements here diff --git a/thrift/kmip.thrift b/thrift/kmip.thrift new file mode 100644 index 0000000..fcb2715 --- /dev/null +++ b/thrift/kmip.thrift @@ -0,0 +1,43 @@ +/** + * This is the thrift definition file. It defines all of the methods and objects + * for the KMIP server. This file is used by thrift to generate the plumbing for + * a KMIP client and server. + * + * This file generated the files under kmip/thrift. + * + * If this file is updated then run the following command to update the + * kmip/thrift directory. + * + * thrift -o kmip -r --gen py thrift/tutorial.thrift + */ + +/** + * The first thing to know about are types. The available types in Thrift are: + * + * bool Boolean, one byte + * byte Signed byte + * i16 Signed 16-bit integer + * i32 Signed 32-bit integer + * i64 Signed 64-bit integer + * double 64-bit floating point value + * string String + * binary Blob (byte array) + * map Map from one type to another + * list Ordered list of one type + * set Set of unique elements of one type + * + */ + +namespace cpp thrift +namespace d thrift +namespace java thrift +namespace php thrift +namespace perl thrift + +service KMIP { + + void create(), + /* Did not use register because it is a reserved word */ + void register_mo() + +} \ No newline at end of file