From 908aece78a84872d547d9c5da703597e67dfc594 Mon Sep 17 00:00:00 2001 From: Peter Hamilton Date: Fri, 27 Feb 2015 10:48:07 -0500 Subject: [PATCH] Adding custom configuration support This change updates the KMIP client to support the custom selection of client configuration options. This makes it easy to dynamically create clients that connect to different backends. All unit demos have been updated to support this feature. --- kmip/core/config_helper.py | 6 ++---- kmip/demos/units/create.py | 3 ++- kmip/demos/units/create_key_pair.py | 3 ++- kmip/demos/units/destroy.py | 3 ++- kmip/demos/units/discover_versions.py | 5 +++-- kmip/demos/units/get.py | 3 ++- kmip/demos/units/locate.py | 3 ++- kmip/demos/units/query.py | 3 ++- kmip/demos/units/register.py | 3 ++- kmip/demos/utils.py | 8 ++++++++ kmip/services/kmip_client.py | 27 +++++++++++++-------------- 11 files changed, 40 insertions(+), 27 deletions(-) diff --git a/kmip/core/config_helper.py b/kmip/core/config_helper.py index 7fbb2e8..bdfb93a 100644 --- a/kmip/core/config_helper.py +++ b/kmip/core/config_helper.py @@ -13,13 +13,11 @@ # License for the specific language governing permissions and limitations # under the License. -try: - from configparser import SafeConfigParser -except ImportError: - from ConfigParser import SafeConfigParser import logging import os +from six.moves.configparser import SafeConfigParser + FILE_PATH = os.path.dirname(os.path.abspath(__file__)) CONFIG_FILE = os.path.normpath(os.path.join(FILE_PATH, '../kmipconfig.ini')) diff --git a/kmip/demos/units/create.py b/kmip/demos/units/create.py index c0b5341..57a83a3 100644 --- a/kmip/demos/units/create.py +++ b/kmip/demos/units/create.py @@ -46,6 +46,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config algorithm = opts.algorithm length = opts.length @@ -77,7 +78,7 @@ if __name__ == '__main__': credential = credential_factory.create_credential(credential_type, credential_value) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() # Build the different object attributes diff --git a/kmip/demos/units/create_key_pair.py b/kmip/demos/units/create_key_pair.py index cbbe612..62e95aa 100644 --- a/kmip/demos/units/create_key_pair.py +++ b/kmip/demos/units/create_key_pair.py @@ -46,6 +46,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config algorithm = opts.algorithm length = opts.length @@ -85,7 +86,7 @@ if __name__ == '__main__': credential = credential_factory.create_credential(credential_type, credential_value) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() algorithm_obj = attribute_factory.create_attribute(attribute_type, diff --git a/kmip/demos/units/destroy.py b/kmip/demos/units/destroy.py index d85e022..a88b7f6 100644 --- a/kmip/demos/units/destroy.py +++ b/kmip/demos/units/destroy.py @@ -36,6 +36,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config uuid = opts.uuid # Exit early if the UUID is not specified @@ -63,7 +64,7 @@ if __name__ == '__main__': credential = credential_factory.create_credential(credential_type, credential_value) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() # Destroy the SYMMETRIC_KEY object diff --git a/kmip/demos/units/discover_versions.py b/kmip/demos/units/discover_versions.py index 0d3a1e9..402d268 100644 --- a/kmip/demos/units/discover_versions.py +++ b/kmip/demos/units/discover_versions.py @@ -32,15 +32,16 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config - # Build and setup logging and needed factories + # Build and setup logging f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'logconfig.ini') logging.config.fileConfig(f_log) logger = logging.getLogger(__name__) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() result = client.discover_versions() diff --git a/kmip/demos/units/get.py b/kmip/demos/units/get.py index 03643f0..f325ea5 100644 --- a/kmip/demos/units/get.py +++ b/kmip/demos/units/get.py @@ -36,6 +36,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config uuid = opts.uuid # Exit early if the UUID is not specified @@ -63,7 +64,7 @@ if __name__ == '__main__': credential = credential_factory.create_credential(credential_type, credential_value) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() # Retrieve the SYMMETRIC_KEY object diff --git a/kmip/demos/units/locate.py b/kmip/demos/units/locate.py index 9c3c4e1..2aa1b17 100644 --- a/kmip/demos/units/locate.py +++ b/kmip/demos/units/locate.py @@ -41,6 +41,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config name = opts.name # Exit early if the UUID is not specified @@ -68,7 +69,7 @@ if __name__ == '__main__': credential = credential_factory.create_credential(credential_type, credential_value) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() # Build name attribute diff --git a/kmip/demos/units/query.py b/kmip/demos/units/query.py index 8065385..254241d 100644 --- a/kmip/demos/units/query.py +++ b/kmip/demos/units/query.py @@ -37,6 +37,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config # Build and setup logging and needed factories f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, @@ -60,7 +61,7 @@ if __name__ == '__main__': QueryFunction(QueryFunctionEnum.QUERY_EXTENSION_MAP)) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() result = client.query(query_functions=query_functions) diff --git a/kmip/demos/units/register.py b/kmip/demos/units/register.py index c773324..8b83c83 100644 --- a/kmip/demos/units/register.py +++ b/kmip/demos/units/register.py @@ -44,6 +44,7 @@ if __name__ == '__main__': username = opts.username password = opts.password + config = opts.config algorithm = opts.algorithm length = opts.length @@ -76,7 +77,7 @@ if __name__ == '__main__': credential = credential_factory.create_credential(credential_type, credential_value) # Build the client and connect to the server - client = KMIPProxy() + client = KMIPProxy(config=config) client.open() # Build the different object attributes diff --git a/kmip/demos/utils.py b/kmip/demos/utils.py index 77f45cb..04a12b9 100644 --- a/kmip/demos/utils.py +++ b/kmip/demos/utils.py @@ -40,6 +40,14 @@ def build_cli_parser(operation): default=None, dest="password", help="Password for KMIP server account") + parser.add_option( + "-c", + "--config", + action="store", + type="str", + default="client", + dest="config", + help="Client configuration group to load from configuration file") if operation is Operation.CREATE: parser.add_option( diff --git a/kmip/services/kmip_client.py b/kmip/services/kmip_client.py index 5ed6aa1..6eed114 100644 --- a/kmip/services/kmip_client.py +++ b/kmip/services/kmip_client.py @@ -72,17 +72,16 @@ class KMIPProxy(KMIP): cert_reqs=None, ssl_version=None, ca_certs=None, do_handshake_on_connect=None, suppress_ragged_eofs=None, - username=None, - password=None): + username=None, password=None, config='client'): super(self.__class__, self).__init__() self.logger = logging.getLogger(__name__) self.credential_factory = CredentialFactory() + self.config = config self._set_variables(host, port, keyfile, certfile, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, suppress_ragged_eofs, username, password) - self.batch_items = [] def open(self): @@ -624,42 +623,42 @@ class KMIPProxy(KMIP): conf = ConfigHelper() self.host = conf.get_valid_value( - host, 'client', 'host', conf.DEFAULT_HOST) + host, self.config, 'host', conf.DEFAULT_HOST) self.port = int(conf.get_valid_value( - port, 'client', 'port', conf.DEFAULT_PORT)) + port, self.config, 'port', conf.DEFAULT_PORT)) self.keyfile = conf.get_valid_value( - keyfile, 'client', 'keyfile', None) + keyfile, self.config, 'keyfile', None) self.certfile = conf.get_valid_value( - certfile, 'client', 'certfile', None) + certfile, self.config, 'certfile', None) self.cert_reqs = getattr(ssl, conf.get_valid_value( - cert_reqs, 'client', 'cert_reqs', 'CERT_REQUIRED')) + cert_reqs, self.config, 'cert_reqs', 'CERT_REQUIRED')) self.ssl_version = getattr(ssl, conf.get_valid_value( - ssl_version, 'client', 'ssl_version', conf.DEFAULT_SSL_VERSION)) + ssl_version, self.config, 'ssl_version', conf.DEFAULT_SSL_VERSION)) self.ca_certs = conf.get_valid_value( - ca_certs, 'client', 'ca_certs', conf.DEFAULT_CA_CERTS) + ca_certs, self.config, 'ca_certs', conf.DEFAULT_CA_CERTS) if conf.get_valid_value( - do_handshake_on_connect, 'client', + do_handshake_on_connect, self.config, 'do_handshake_on_connect', 'True') == 'True': self.do_handshake_on_connect = True else: self.do_handshake_on_connect = False if conf.get_valid_value( - suppress_ragged_eofs, 'client', + suppress_ragged_eofs, self.config, 'suppress_ragged_eofs', 'True') == 'True': self.suppress_ragged_eofs = True else: self.suppress_ragged_eofs = False self.username = conf.get_valid_value( - username, 'client', 'username', conf.DEFAULT_USERNAME) + username, self.config, 'username', conf.DEFAULT_USERNAME) self.password = conf.get_valid_value( - password, 'client', 'password', conf.DEFAULT_PASSWORD) + password, self.config, 'password', conf.DEFAULT_PASSWORD)