>From 01cc11aa33505f6d22a0f3647863f89934704e7f Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Fri, 23 May 2014 10:30:18 -0400
Subject: [PATCH 217/218] latest changes for code review

---
 base/common/python/pki/key.py    | 73 ++++++++++++++++++++-----------------
 base/common/python/pki/system.py | 20 ++++++++---
 base/kra/functional/drmtest.py   | 77 +++++++++++++++++++++-------------------
 3 files changed, 97 insertions(+), 73 deletions(-)

diff --git a/base/common/python/pki/key.py b/base/common/python/pki/key.py
index 42b07933980e80246c354c10601a616c764b3b15..a70c25f205caee5b4d710e7abf86e3bd34fc519e 100644
--- a/base/common/python/pki/key.py
+++ b/base/common/python/pki/key.py
@@ -67,16 +67,18 @@ class KeyData(object):
     def __init__(self):
         """ Constructor """
         self.algorithm = None
-        self.nonceData = None
+        self.nonce_data = None
         self.size = None
-        self.wrappedPrivateData = None
+        self.wrapped_private_data = None
 
     @classmethod
     def from_json(cls, attr_list):
         """ Return a KeyData object from a JSON dict """
         key_data = cls()
-        for key in attr_list:
-            setattr(key_data, key, attr_list[key])
+        key_data.algorithm = attr_list['algorithm']
+        key_data.nonce_data = attr_list['nonceData']
+        key_data.size = attr_list['size']
+        key_data.wrapped_private_data = attr_list['wrappedPrivateData']
         return key_data
 
 
@@ -89,8 +91,8 @@ class Key(object):
 
     def __init__(self, key_data):
         """ Constructor """
-        self.encrypted_data = base64.decodestring(key_data.wrappedPrivateData)
-        self.nonce_data = base64.decodestring(key_data.nonceData)
+        self.encrypted_data = base64.decodestring(key_data.wrapped_private_data)
+        self.nonce_data = base64.decodestring(key_data.nonce_data)
         self.algorithm = key_data.algorithm
         self.size = key_data.size
 
@@ -109,26 +111,30 @@ class KeyInfo(object):
     # pylint: disable-msg=C0103
     def __init__(self):
         """ Constructor """
-        self.clientKeyID = None
-        self.keyURL = None
+        self.client_key_id = None
+        self.key_url = None
         self.algorithm = None
         self.status = None
-        self.ownerName = None
+        self.owner_name = None
         self.size = None
 
     @classmethod
     def from_json(cls, attr_list):
         """ Return KeyInfo from JSON dict """
         key_info = cls()
-        for key in attr_list:
-            setattr(key_info, key, attr_list[key])
+        key_info.client_key_id = attr_list['clientKeyID']
+        key_info.key_url = attr_list['keyURL']
+        key_info.algorithm = attr_list['algorithm']
+        key_info.status = attr_list['status']
+        key_info.owner_name = attr_list['ownerName']
+        key_info.size = attr_list['size']
         return key_info
 
     def get_key_id(self):
         """ Return the key ID as parsed from key URL """
-        if self.keyURL is not None:
-            indx = str(self.keyURL).rfind("/") + 1
-            return str(self.keyURL)[indx:]
+        if self.key_url is not None:
+            indx = str(self.key_url).rfind("/") + 1
+            return str(self.key_url)[indx:]
         return None
 
 
@@ -166,31 +172,33 @@ class KeyRequestInfo(object):
     # pylint: disable-msg=C0103
     def __init__(self):
         """ Constructor """
-        self.requestURL = None
-        self.requestType = None
-        self.keyURL = None
-        self.requestStatus = None
+        self.request_url = None
+        self.request_type = None
+        self.key_url = None
+        self.request_status = None
 
     @classmethod
     def from_json(cls, attr_list):
         """ Return a KeyRequestInfo object from a JSON dict. """
         key_request_info = cls()
-        for key in attr_list:
-            setattr(key_request_info, key, attr_list[key])
+        key_request_info.request_url = attr_list['requestURL']
+        key_request_info.request_type = attr_list['requestType']
+        key_request_info.key_url = attr_list['keyURL']
+        key_request_info.request_status = attr_list['requestStatus']
         return key_request_info
 
     def get_request_id(self):
         """ Return the request ID by parsing the request URL. """
-        if self.requestURL is not None:
-            indx = str(self.requestURL).rfind("/") + 1
-            return str(self.requestURL)[indx:]
+        if self.request_url is not None:
+            indx = str(self.request_url).rfind("/") + 1
+            return str(self.request_url)[indx:]
         return None
 
     def get_key_id(self):
         """ Return the ID of the secret referred to by this request. """
-        if self.keyURL is not None:
-            indx = str(self.keyURL).rfind("/") + 1
-            return str(self.keyURL)[indx:]
+        if self.key_url is not None:
+            indx = str(self.key_url).rfind("/") + 1
+            return str(self.key_url)[indx:]
         return None
 
 
@@ -231,8 +239,8 @@ class KeyRequestResponse(object):
     # pylint: disable-msg=C0103
     def __init__(self):
         """ Constructor """
-        self.requestInfo = None
-        self.keyData = None
+        self.request_info = None
+        self.key_data = None
 
     @classmethod
     def from_json(cls, json_value):
@@ -240,19 +248,19 @@ class KeyRequestResponse(object):
         ret = cls()
 
         if 'RequestInfo' in json_value:
-            ret.requestInfo = KeyRequestInfo.from_json(json_value['RequestInfo'])
+            ret.request_info = KeyRequestInfo.from_json(json_value['RequestInfo'])
 
         if 'KeyData' in json_value:
-            ret.keyData = KeyData.from_json(json_value['KeyData'])
+            ret.key_data = KeyData.from_json(json_value['KeyData'])
         return ret
 
     def get_key_id(self):
         """ Return the id for the key archived, recovered or generated """
-        return self.requestInfo.get_key_id()
+        return self.request_info.get_key_id()
 
     def get_request_id(self):
         """ Return the id for the created request """
-        return self.requestInfo.get_request_id()
+        return self.request_info.get_request_id()
 
 
 class KeyArchivalRequest(pki.ResourceMessage):
@@ -511,6 +519,7 @@ class KeyClient(object):
 
         if trans_wrapped_session_key is not None:
             twsk = base64.encodestring(trans_wrapped_session_key)
+            # noinspection PyUnusedLocal
             request = SymKeyGenerationRequest(
                 client_key_id=client_key_id,
                 key_size=size,
diff --git a/base/common/python/pki/system.py b/base/common/python/pki/system.py
index 62888335a91f516d93964b336aba7db43c2909b2..821f0dc961df9b3ab1d5a262d6dfe79daca46541 100644
--- a/base/common/python/pki/system.py
+++ b/base/common/python/pki/system.py
@@ -57,6 +57,19 @@ class SecurityDomainHost(object):
         host.unsecure_port = json_value['Port']
         return host
 
+class SecurityDomainSubsystem(object):
+    def __init__(self):
+        self.name = None
+        self.hosts = {}
+
+    @classmethod
+    def from_json(cls, json_value):
+        ret = cls()
+        ret.name = json_value['id']
+        for host in json_value['Host']:
+            ret.hosts[host['id']] = SecurityDomainHost.from_json(host)
+        return ret
+
 
 class SecurityDomainInfo(object):
     def __init__(self):
@@ -68,11 +81,8 @@ class SecurityDomainInfo(object):
         ret = cls()
         ret.name = json_value['id']
         for slist in json_value['Subsystem']:
-            system_type = slist['id']
-            system_list = []
-            for host in slist['Host']:
-                system_list.append(SecurityDomainHost.from_json(host))
-            ret.systems[system_type] = system_list
+            subsystem = SecurityDomainSubsystem.from_json(slist)
+            ret.systems[slist['id']] = subsystem
         return ret
 
 
diff --git a/base/kra/functional/drmtest.py b/base/kra/functional/drmtest.py
index b6fa18dcb2b7f6f8efe1040b1c9be2526762e37f..0fff95c2e3a8d1e7c7414bb0c7c5b568006957fa 100644
--- a/base/kra/functional/drmtest.py
+++ b/base/kra/functional/drmtest.py
@@ -17,9 +17,9 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-'''
+"""
 =========================================================================
-Python test code for interacting with the DRM using the RESTful interface
+Python test code for interacting with the DRM using the REST interface
 =========================================================================
 
 This code is to be viewed as example code on how to interact with the DRM
@@ -27,7 +27,7 @@ for Key and KeyRequest resources using the Python REST client framework.
 
 Some setup is required to run the tests here successfully.
 See drmtest.readme.txt.
-'''
+"""
 
 import base64
 import pki
@@ -38,35 +38,39 @@ import time
 from pki.client import PKIConnection
 from pki.kraclient import KRAClient
 
+
 def print_key_request(request):
-    ''' Prints the relevant fields of a KeyRequestInfo object '''
-    print "RequestURL: " + str(request.requestURL)
-    print "RequestType: " + str(request.requestType)
-    print "RequestStatus: " + str(request.requestStatus)
+    """ Prints the relevant fields of a KeyRequestInfo object """
+    print "RequestURL: " + str(request.request_url)
+    print "RequestType: " + str(request.request_type)
+    print "RequestStatus: " + str(request.request_status)
     print "KeyURL: " + str(request.keyURL)
 
+
 def print_key_info(key_info):
-    ''' Prints the relevant fields of a KeyInfo object '''
-    print "Key URL: " + str(key_info.keyURL)
-    print "Client Key ID: " + str(key_info.clientKeyID)
+    """ Prints the relevant fields of a KeyInfo object """
+    print "Key URL: " + str(key_info.key_url)
+    print "Client Key ID: " + str(key_info.client_key_id)
     print "Algorithm: " + str(key_info.algorithm)
     print "Status: " + str(key_info.status)
-    print "Owner Name: " + str(key_info.ownerName)
+    print "Owner Name: " + str(key_info.owner_name)
     print "Size: " + str(key_info.size)
 
-def print_key_data(key):
-    ''' Prints the relevant fields of a KeyData object '''
-    print "Key Algorithm: " + str(key.algorithm)
-    print "Key Size: " + str(key.size)
-    print "Nonce Data: " + base64.encodestring(key.nonce_data)
-    print "Wrapped Private Data: " + base64.encodestring(key.encrypted_data)
-    if key.data is not None:
-        print "Private Data: " + base64.encodestring(key.data)
+
+def print_key_data(key_data):
+    """ Prints the relevant fields of a KeyData object """
+    print "Key Algorithm: " + str(key_data.algorithm)
+    print "Key Size: " + str(key_data.size)
+    print "Nonce Data: " + base64.encodestring(key_data.nonce_data)
+    print "Wrapped Private Data: " + base64.encodestring(key_data.encrypted_data)
+    if key_data.data is not None:
+        print "Private Data: " + base64.encodestring(key_data.data)
+
 
 def main():
-    ''' test code execution '''
+    """ test code execution """
 
-    # set up the connectoon to the DRM, including authentication credentials
+    # set up the connection to the DRM, including authentication credentials
     connection = PKIConnection('https', 'localhost', '8443', 'kra')
     connection.set_authentication_cert('/tmp/temp4.pem')
 
@@ -83,7 +87,7 @@ def main():
     # Get transport cert and insert in the certdb
     transport_nick = "kra transport cert"
     transport_cert = kraclient.system_certs.get_transport_cert()
-    tcert = transport_cert[len(pki.CERT_HEADER):len(transport_cert) -len(pki.CERT_FOOTER)]
+    tcert = transport_cert[len(pki.CERT_HEADER):len(transport_cert) - len(pki.CERT_FOOTER)]
     crypto.import_cert(transport_nick, base64.decodestring(tcert), "u,u,u")
 
     # initialize the certdb for crypto operations
@@ -96,8 +100,8 @@ def main():
     # Test 2: Get key request info
     print "Now getting key request"
     try:
-        keyrequest = keyclient.get_request_info('2')
-        print_key_request(keyrequest)
+        key_request = keyclient.get_request_info('2')
+        print_key_request(key_request)
     except pki.RequestNotFoundException:
         pass
 
@@ -125,6 +129,7 @@ def main():
     # Test 5: Confirm the key_id matches
     print "Now getting key ID for clientKeyID=\"" + client_key_id + "\""
     key_infos = keyclient.list_keys(client_key_id=client_key_id, status=keyclient.KEY_STATUS_ACTIVE)
+    key_id2 = None
     for key_info in key_infos.key_infos:
         print_key_info(key_info)
         key_id2 = key_info.get_key_id()
@@ -160,24 +165,24 @@ def main():
     # Test 10 = test BadRequestException on create()
     print "Trying to generate a new symkey with the same client ID"
     try:
-        response = keyclient.generate_symmetric_key(client_key_id,
-                                                    algorithm=algorithm,
-                                                    size=key_size,
-                                                    usages=usages)
+        keyclient.generate_symmetric_key(client_key_id,
+                                         algorithm=algorithm,
+                                         size=key_size,
+                                         usages=usages)
     except pki.BadRequestException as exc:
         print "BadRequestException thrown - Code:" + exc.code + " Message: " + exc.message
 
     # Test 11 - Test RequestNotFoundException on get_request_info
     print "Try to list a nonexistent request"
     try:
-        keyrequest = keyclient.get_request_info('200000034')
+        keyclient.get_request_info('200000034')
     except pki.RequestNotFoundException as exc:
         print "RequestNotFoundException thrown - Code:" + exc.code + " Message: " + exc.message
 
     # Test 12 - Test exception on retrieve_key.
     print "Try to retrieve an invalid key"
     try:
-        key_data = keyclient.retrieve_key('2000003434')
+        keyclient.retrieve_key('2000003434')
     except pki.KeyNotFoundException as exc:
         print "KeyNotFoundException thrown - Code:" + exc.code + " Message: " + exc.message
 
@@ -199,7 +204,7 @@ def main():
     # Test 16: Get key info for non-existent key
     print "Get key info for non-existent key"
     try:
-        key_info = keyclient.get_key_info('200004556')
+        keyclient.get_key_info('200004556')
     except pki.KeyNotFoundException as exc:
         print "KeyNotFoundException thrown - Code:" + exc.code + " Message: " + exc.message
 
@@ -214,7 +219,7 @@ def main():
     #Test 18: Generate a symmetric key with default parameters
     client_key_id = "Vek #3" + time.strftime('%c')
     response = keyclient.generate_symmetric_key(client_key_id)
-    print_key_request(response.requestInfo)
+    print_key_request(response.request_info)
 
     # Test 19: Try to archive key
     print "try to archive key"
@@ -222,10 +227,10 @@ def main():
     client_key_id = "Vek #4" + time.strftime('%c')
 
     response = keyclient.archive_key(client_key_id, keyclient.SYMMETRIC_KEY_TYPE,
-                                    base64.decodestring(key1),
-                                    key_algorithm=keyclient.AES_ALGORITHM,
-                                    key_size=128)
-    print_key_request(response.requestInfo)
+                                     base64.decodestring(key1),
+                                     key_algorithm=keyclient.AES_ALGORITHM,
+                                     key_size=128)
+    print_key_request(response.request_info)
 
     # Test 20: Lets get it back
     key_info = keyclient.get_active_key_info(client_key_id)
-- 
1.9.0

