summaryrefslogtreecommitdiffstats
path: root/base/common/python/pki
diff options
context:
space:
mode:
authorAde Lee <alee@redhat.com>2017-04-03 12:43:05 -0400
committerAde Lee <alee@redhat.com>2017-04-04 09:49:13 -0400
commit14c84eb0851a9b4bb5c976e138854b7d8e875aa8 (patch)
tree8ab681d04fbb11bc868c5754a56c7512bc8a2177 /base/common/python/pki
parentfceca0ccfc8341d9255540e9a1c3c406373a9483 (diff)
Added python info client
Add python client code to read from the InfoResource class and get the server version. As the PKIConnection in the python client currently requires a subsystem, it is difficult to add an infoclient to an existing KRAClient (or any other client). To get around this, I modified the PKIConnection to allow using the rootURI. Change-Id: Ided75f45f741e2ba3fc86acec715d24b829c8a97
Diffstat (limited to 'base/common/python/pki')
-rw-r--r--base/common/python/pki/client.py51
-rw-r--r--base/common/python/pki/info.py98
2 files changed, 138 insertions, 11 deletions
diff --git a/base/common/python/pki/client.py b/base/common/python/pki/client.py
index 90ca4fefb..805d0fadb 100644
--- a/base/common/python/pki/client.py
+++ b/base/common/python/pki/client.py
@@ -78,9 +78,8 @@ class PKIConnection:
self.port = port
self.subsystem = subsystem
- self.serverURI = self.protocol + '://' + \
- self.hostname + ':' + self.port + '/' + \
- self.subsystem
+ self.rootURI = self.protocol + '://' + self.hostname + ':' + self.port
+ self.serverURI = self.rootURI + '/' + self.subsystem
self.session = requests.Session()
self.session.trust_env = trust_env
@@ -125,7 +124,8 @@ class PKIConnection:
self.session.cert = pem_cert_path
@catch_insecure_warning
- def get(self, path, headers=None, params=None, payload=None):
+ def get(self, path, headers=None, params=None, payload=None,
+ use_root_uri=False):
"""
Uses python-requests to issue a GET request to the server.
@@ -137,12 +137,19 @@ class PKIConnection:
:type params: dict or bytes
:param payload: data to be sent in the body of the request
:type payload: dict, bytes, file-like object
+ :param use_root_uri: use root URI instead of subsystem URI as base
+ :type use_root_uri: boolean
:returns: request.response -- response from the server
:raises: Exception from python-requests in case the GET was not
successful, or returns an error code.
"""
+ if use_root_uri:
+ target_path = self.rootURI + path
+ else:
+ target_path = self.serverURI + path
+
r = self.session.get(
- self.serverURI + path,
+ target_path,
verify=False,
headers=headers,
params=params,
@@ -151,7 +158,8 @@ class PKIConnection:
return r
@catch_insecure_warning
- def post(self, path, payload, headers=None, params=None):
+ def post(self, path, payload, headers=None, params=None,
+ use_root_uri=False):
"""
Uses python-requests to issue a POST request to the server.
@@ -163,12 +171,19 @@ class PKIConnection:
:type headers: dict
:param params: Query parameters for the POST request
:type params: dict or bytes
+ :param use_root_uri: use root URI instead of subsystem URI as base
+ :type use_root_uri: boolean
:returns: request.response -- response from the server
:raises: Exception from python-requests in case the POST was not
successful, or returns an error code.
"""
+ if use_root_uri:
+ target_path = self.rootURI + path
+ else:
+ target_path = self.serverURI + path
+
r = self.session.post(
- self.serverURI + path,
+ target_path,
verify=False,
data=payload,
headers=headers,
@@ -177,7 +192,7 @@ class PKIConnection:
return r
@catch_insecure_warning
- def put(self, path, payload, headers=None):
+ def put(self, path, payload, headers=None, use_root_uri=False):
"""
Uses python-requests to issue a PUT request to the server.
@@ -187,16 +202,23 @@ class PKIConnection:
:type payload: dict, bytes, file-like object
:param headers: headers for the PUT request
:type headers: dict
+ :param use_root_uri: use root URI instead of subsystem URI as base
+ :type use_root_uri: boolean
:returns: request.response -- response from the server
:raises: Exception from python-requests in case the PUT was not
successful, or returns an error code.
"""
- r = self.session.put(self.serverURI + path, payload, headers=headers)
+ if use_root_uri:
+ target_path = self.rootURI + path
+ else:
+ target_path = self.serverURI + path
+
+ r = self.session.put(target_path, payload, headers=headers)
r.raise_for_status()
return r
@catch_insecure_warning
- def delete(self, path, headers=None):
+ def delete(self, path, headers=None, use_root_uri=False):
"""
Uses python-requests to issue a DEL request to the server.
@@ -204,11 +226,18 @@ class PKIConnection:
:type path: str
:param headers: headers for the DEL request
:type headers: dict
+ :param use_root_uri: use root URI instead of subsystem URI as base
+ :type use_root_uri: boolean
:returns: request.response -- response from the server
:raises: Exception from python-requests in case the DEL was not
successful, or returns an error code.
"""
- r = self.session.delete(self.serverURI + path, headers=headers)
+ if use_root_uri:
+ target_path = self.rootURI + path
+ else:
+ target_path = self.serverURI + path
+
+ r = self.session.delete(target_path, headers=headers)
r.raise_for_status()
return r
diff --git a/base/common/python/pki/info.py b/base/common/python/pki/info.py
new file mode 100644
index 000000000..65d482568
--- /dev/null
+++ b/base/common/python/pki/info.py
@@ -0,0 +1,98 @@
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the Lesser GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2013 Red Hat, Inc.
+# All rights reserved.
+#
+# Author:
+# Ade Lee <alee@redhat.com>
+#
+"""
+Module containing the Python client classes for the InfoClient
+"""
+from six import iteritems
+
+import pki
+
+
+class Info(object):
+ """
+ This class encapsulates the parameters returned by the server's
+ InfoService.
+ """
+
+ json_attribute_names = {
+ 'Version': 'version',
+ 'Banner': 'banner'
+ }
+
+ def __init__(self, version=None, banner=None):
+ """ Constructor """
+ self.version = version
+ self.banner = banner
+
+ @classmethod
+ def from_json(cls, attr_list):
+ """ Return Info from JSON dict """
+ info = cls()
+ for k, v in iteritems(attr_list):
+ if k in Info.json_attribute_names:
+ setattr(info, Info.json_attribute_names[k], v)
+ else:
+ setattr(info, k, v)
+ return info
+
+
+class Version(object):
+ """
+ This class encapsulates a version object as returned from
+ a Dogtag server and decomposes it into major, minor, etc.
+ """
+
+ def __init__(self, version_string):
+ for idx, val in enumerate(version_string.split('.')):
+ if idx == 0:
+ self.major = val
+ if idx == 1:
+ self.minor = val
+ if idx == 2:
+ self.patch = val
+
+
+class InfoClient(object):
+ """
+ Class encapsulating and mirroring the functionality in the
+ InfoResource Java interface class defining the REST API for
+ server Info resources.
+ """
+
+ def __init__(self, connection):
+ """ Constructor """
+ self.connection = connection
+
+ @pki.handle_exceptions()
+ def get_info(self):
+ """ Return an Info object form a PKI server """
+
+ url = '/pki/rest/info'
+ headers = {'Content-type': 'application/json',
+ 'Accept': 'application/json'}
+ r = self.connection.get(url, headers, use_root_uri=True)
+ return Info.from_json(r.json())
+
+ @pki.handle_exceptions()
+ def get_version(self):
+ """ return Version object from server """
+ version_string = self.get_info().version
+ return Version(version_string)