From db00ea3bee6b86ae662cfdb8b50cca4a8be82ef1 Mon Sep 17 00:00:00 2001 From: Ade Lee Date: Wed, 19 Feb 2014 14:37:44 -0500 Subject: Fix minor issues from review. 1. main function in SymKeyGenerationRequest does not use correct usages 2. from_dict -> from_json() for consistency --- base/common/python/pki/__init__.py | 35 +++++++++++++---------------------- base/common/python/pki/cert.py | 8 ++++---- base/common/python/pki/key.py | 27 ++++++++++++++------------- base/common/python/pki/systemcert.py | 2 +- 4 files changed, 32 insertions(+), 40 deletions(-) (limited to 'base/common/python') diff --git a/base/common/python/pki/__init__.py b/base/common/python/pki/__init__.py index e9802a4ba..190d1f10f 100644 --- a/base/common/python/pki/__init__.py +++ b/base/common/python/pki/__init__.py @@ -198,27 +198,18 @@ class UserNotFoundException(ResourceNotFoundException): ''' User Not Found Exception: return code = 404 ''' EXCEPTION_MAPPINGS = { - "com.netscape.certsrv.base.BadRequestException": "pki.BadRequestException", - "com.netscape.certsrv.base.ConflictingOperationException": "pki.ConflictingOperationException", - "com.netscape.certsrv.base.ForbiddenException": "pki.ForbiddenException", - "com.netscape.certsrv.base.HTTPGoneException": "pki.HTTPGoneException", - "com.netscape.certsrv.base.ResourceNotFoundException": "pki.ResourceNotFoundException", - "com.netscape.certsrv.cert.CertNotFoundException": "pki.CertNotFoundException", - "com.netscape.certsrv.group.GroupNotFoundException": "pki.GroupNotFoundException", - "com.netscape.certsrv.key.KeyNotFoundException": "pki.KeyNotFoundException", - "com.netscape.certsrv.profile.ProfileNotFoundException": "pki.ProfileNotFoundException", - "com.netscape.certsrv.request.RequestNotFoundException": "pki.RequestNotFoundException", - "com.netscape.certsrv.base.UserNotFoundException": "pki.UserNotFoundException", - "com.netscape.certsrv.base.PKIException": "pki.PKIException"} - -def get_class( kls ): - ''' Get reference to the class specified by string kls ''' - parts = kls.split('.') - module = ".".join(parts[:-1]) - mod = __import__( module ) - for comp in parts[1:]: - mod = getattr(mod, comp) - return mod + "com.netscape.certsrv.base.BadRequestException": BadRequestException, + "com.netscape.certsrv.base.ConflictingOperationException": ConflictingOperationException, + "com.netscape.certsrv.base.ForbiddenException": ForbiddenException, + "com.netscape.certsrv.base.HTTPGoneException": HTTPGoneException, + "com.netscape.certsrv.base.ResourceNotFoundException": ResourceNotFoundException, + "com.netscape.certsrv.cert.CertNotFoundException": CertNotFoundException, + "com.netscape.certsrv.group.GroupNotFoundException": GroupNotFoundException, + "com.netscape.certsrv.key.KeyNotFoundException": KeyNotFoundException, + "com.netscape.certsrv.profile.ProfileNotFoundException": ProfileNotFoundException, + "com.netscape.certsrv.request.RequestNotFoundException": RequestNotFoundException, + "com.netscape.certsrv.base.UserNotFoundException": UserNotFoundException, + "com.netscape.certsrv.base.PKIException": PKIException} def handle_exceptions(): ''' Decorator handling exceptions from REST methods. ''' @@ -233,7 +224,7 @@ def handle_exceptions(): except requests.exceptions.HTTPError as exc: clazz = exc.response.json()['ClassName'] if clazz in EXCEPTION_MAPPINGS: - exception_class = get_class(EXCEPTION_MAPPINGS[clazz]) + exception_class = EXCEPTION_MAPPINGS[clazz] pki_exception = exception_class.from_json(exc.response.json()) raise pki_exception else: diff --git a/base/common/python/pki/cert.py b/base/common/python/pki/cert.py index 0a720a5e1..3d05df0c3 100644 --- a/base/common/python/pki/cert.py +++ b/base/common/python/pki/cert.py @@ -35,7 +35,7 @@ class CertData(object): self.Encoded = None @classmethod - def from_dict(cls, attr_list): + def from_json(cls, attr_list): ''' Return CertData object from JSON dict ''' cert_data = cls() for key in attr_list: @@ -63,7 +63,7 @@ class CertDataInfo(object): self.issuedBy = None @classmethod - def from_dict(cls, attr_list): + def from_json(cls, attr_list): ''' Return CertDataInfo object from JSON dict ''' cert_data_info = cls() for key in attr_list: @@ -87,10 +87,10 @@ class CertDataInfos(object): ret = cls() cert_infos = json_value['CertDataInfo'] if not isinstance(cert_infos, types.ListType): - ret.certInfoList.append(CertDataInfo.from_dict(cert_infos)) + ret.certInfoList.append(CertDataInfo.from_json(cert_infos)) else: for cert_info in cert_infos: - ret.certInfoList.append(CertDataInfo.from_dict(cert_info)) + ret.certInfoList.append(CertDataInfo.from_json(cert_info)) return ret class CertSearchRequest(object): diff --git a/base/common/python/pki/key.py b/base/common/python/pki/key.py index 1e959785c..235825746 100644 --- a/base/common/python/pki/key.py +++ b/base/common/python/pki/key.py @@ -59,7 +59,7 @@ class KeyData(object): self.wrappedPrivateData = None @classmethod - def from_dict(cls, attr_list): + def from_json(cls, attr_list): ''' Return a KeyData object from a JSON dict ''' key_data = cls() for key in attr_list: @@ -83,7 +83,7 @@ class KeyInfo(object): self.size = None @classmethod - def from_dict(cls, attr_list): + def from_json(cls, attr_list): ''' Return KeyInfo from JSON dict ''' key_info = cls() for key in attr_list: @@ -115,10 +115,10 @@ class KeyInfoCollection(object): ret = cls() infos = json_value['entries'] if not isinstance(infos, types.ListType): - ret.key_infos.append(KeyInfo.from_dict(infos)) + ret.key_infos.append(KeyInfo.from_json(infos)) else: for info in infos: - ret.key_infos.append(KeyInfo.from_dict(info)) + ret.key_infos.append(KeyInfo.from_json(info)) return ret class KeyRequestInfo(object): @@ -135,7 +135,7 @@ class KeyRequestInfo(object): self.requestStatus = None @classmethod - def from_dict(cls, attr_list): + def from_json(cls, attr_list): ''' Return a KeyRequestInfo object from a JSON dict. ''' key_request_info = cls() for key in attr_list: @@ -174,10 +174,10 @@ class KeyRequestInfoCollection(object): ret = cls() infos = json_value['entries'] if not isinstance(infos, types.ListType): - ret.key_requests.append(KeyRequestInfo.from_dict(infos)) + ret.key_requests.append(KeyRequestInfo.from_json(infos)) else: for info in infos: - ret.key_requests.append(KeyRequestInfo.from_dict(info)) + ret.key_requests.append(KeyRequestInfo.from_json(info)) return ret class KeyRequestResponse(object): @@ -199,10 +199,10 @@ class KeyRequestResponse(object): ret = cls() if 'RequestInfo' in json_value: - ret.requestInfo = KeyRequestInfo.from_dict(json_value['RequestInfo']) + ret.requestInfo = KeyRequestInfo.from_json(json_value['RequestInfo']) if 'KeyData' in json_value: - ret.keyData = KeyData.from_dict(json_value['KeyData']) + ret.keyData = KeyData.from_json(json_value['KeyData']) return ret def get_key_id(self): @@ -322,7 +322,7 @@ class KeyClient(object): url = self.keyURL + '/retrieve' keyRequest = json.dumps(data, cls=encoder.CustomTypeEncoder, sort_keys=True) response = self.connection.post(url, keyRequest, self.headers) - return KeyData.from_dict(response.json()) + return KeyData.from_json(response.json()) @pki.handle_exceptions() def request_key_retrieval(self, key_id, request_id, trans_wrapped_session_key=None, @@ -377,7 +377,7 @@ class KeyClient(object): ''' Return a KeyRequestInfo object for a specific request. ''' url = self.keyRequestsURL + '/' + request_id response = self.connection.get(url, self.headers) - return KeyRequestInfo.from_dict(response.json()) + return KeyRequestInfo.from_json(response.json()) @pki.handle_exceptions() def create_request(self, request): @@ -463,7 +463,7 @@ class KeyClient(object): ''' Get the info in the KeyRecord for a specific secret in the DRM. ''' url = self.keyURL + '/' + key_id response = self.connection.get(url, headers=self.headers) - return KeyInfo.from_dict(response.json()) + return KeyInfo.from_json(response.json()) @pki.handle_exceptions() def modify_key_status(self, key_id, status): @@ -483,7 +483,8 @@ def main(): ''' Some unit tests - basically printing different types of requests ''' print "printing symkey generation request" client_id = "vek 123" - gen_request = SymKeyGenerationRequest(client_id, 128, "AES", "encrypt,decrypt") + usages = [SymKeyGenerationRequest.DECRYPT_USAGE, SymKeyGenerationRequest.ENCRYPT_USAGE] + gen_request = SymKeyGenerationRequest(client_id, 128, "AES", usages) print json.dumps(gen_request, cls=encoder.CustomTypeEncoder, sort_keys=True) print "printing key recovery request" diff --git a/base/common/python/pki/systemcert.py b/base/common/python/pki/systemcert.py index 2f51de6f3..f91bbeca0 100644 --- a/base/common/python/pki/systemcert.py +++ b/base/common/python/pki/systemcert.py @@ -43,5 +43,5 @@ class SystemCertClient(object): ''' Return transport certificate ''' url = self.cert_url + '/transport' response = self.connection.get(url, self.headers) - cert_data = CertData.from_dict(response.json()) + cert_data = CertData.from_json(response.json()) return cert_data.Encoded -- cgit