>From b855b7c5056903650c572ef1d862eedcae6d231a Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 01:02:16 +0100
Subject: [PATCH] Refactored KRA KeyCLI.

The KRA KeyCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/key/KeyArchiveCLI.java |  8 +++++---
 .../src/com/netscape/cmstools/key/KeyCLI.java        | 12 ++++++++----
 .../src/com/netscape/cmstools/key/KeyFindCLI.java    |  4 +++-
 .../com/netscape/cmstools/key/KeyGenerateCLI.java    |  7 +++++--
 .../src/com/netscape/cmstools/key/KeyModifyCLI.java  |  6 ++++--
 .../src/com/netscape/cmstools/key/KeyRecoverCLI.java |  6 ++++--
 .../com/netscape/cmstools/key/KeyRequestFindCLI.java |  4 +++-
 .../netscape/cmstools/key/KeyRequestReviewCLI.java   | 10 ++++++----
 .../com/netscape/cmstools/key/KeyRequestShowCLI.java |  4 +++-
 .../com/netscape/cmstools/key/KeyRetrieveCLI.java    | 20 +++++++++++---------
 .../src/com/netscape/cmstools/key/KeyShowCLI.java    |  6 ++++--
 11 files changed, 56 insertions(+), 31 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java
index c3116a673bba7c57ec9a04881b3a9fb5fc48899f..6789957c333edeab5d7bd071e9a36c23ed3678cb 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java
@@ -12,6 +12,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.key.KeyArchivalRequest;
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRequestResponse;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -68,6 +69,7 @@ public class KeyArchiveCLI extends CLI {
         String requestFile = cmd.getOptionValue("input");
 
         KeyRequestResponse response = null;
+        KeyClient keyClient = keyCLI.getKeyClient();
 
         if (requestFile != null) {
             // Case where the request template file is used. For pre-encrypted data.
@@ -78,11 +80,11 @@ public class KeyArchiveCLI extends CLI {
                 KeyArchivalRequest req = (KeyArchivalRequest) unmarshaller.unmarshal(fis);
 
                 if (req.getPKIArchiveOptions() != null) {
-                    response = keyCLI.keyClient.archivePKIOptions(req.getClientKeyId(), req.getDataType(),
+                    response = keyClient.archivePKIOptions(req.getClientKeyId(), req.getDataType(),
                             req.getKeyAlgorithm(), req.getKeySize(), Utils.base64decode(req.getPKIArchiveOptions()),
                             req.getRealm());
                 } else {
-                    response = keyCLI.keyClient.archiveEncryptedData(req.getClientKeyId(), req.getDataType(),
+                    response = keyClient.archiveEncryptedData(req.getClientKeyId(), req.getDataType(),
                             req.getKeyAlgorithm(), req.getKeySize(), req.getAlgorithmOID(),
                             Utils.base64decode(req.getSymmetricAlgorithmParams()),
                             Utils.base64decode(req.getWrappedPrivateData()),
@@ -109,7 +111,7 @@ public class KeyArchiveCLI extends CLI {
             }
             String realm = cmd.getOptionValue("realm");
 
-            response = keyCLI.keyClient.archivePassphrase(clientKeyId, passphrase, realm);
+            response = keyClient.archivePassphrase(clientKeyId, passphrase, realm);
         }
 
         MainCLI.printMessage("Archival request details");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
index d2b425b9fbe0e9616b579dc2f8490ce13d55b82c..b9b27d1132fbff0ed50c22a147ee09513ab66db9 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
@@ -19,6 +19,7 @@
 package com.netscape.cmstools.key;
 
 import com.netscape.certsrv.cert.CertData;
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyInfo;
 import com.netscape.certsrv.key.KeyRequestInfo;
@@ -70,9 +71,11 @@ public class KeyCLI extends CLI {
         return "pki-key";
     }
 
-    public void execute(String[] args) throws Exception {
+    public KeyClient getKeyClient() throws Exception {
 
-        client = parent.getClient();
+        if (keyClient != null) return keyClient;
+
+        PKIClient client = getClient();
 
         // determine the subsystem
         String subsystem = client.getSubsystem();
@@ -81,15 +84,16 @@ public class KeyCLI extends CLI {
 
         // create new key client
         keyClient = new KeyClient(client, subsystem);
+        systemCertClient = new SystemCertClient(client, subsystem);
 
         // if security database password is specified,
         // prepare key client for archival/retrieval
         if (client.getConfig().getCertPassword() != null) {
+
             // create crypto provider for key client
             keyClient.setCrypto(new NSSCryptoProvider(client.getConfig()));
 
             // download transport cert
-            systemCertClient = new SystemCertClient(client, subsystem);
             String transportCert = systemCertClient.getTransportCert().getEncoded();
             transportCert = transportCert.substring(CertData.HEADER.length(),
                     transportCert.indexOf(CertData.FOOTER));
@@ -98,7 +102,7 @@ public class KeyCLI extends CLI {
             keyClient.setTransportCert(transportCert);
         }
 
-        super.execute(args);
+        return keyClient;
     }
 
     public static void printKeyInfo(KeyInfo info) {
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java
index 87d6b2f98f658634f204eb14fd230b235908df8b..2e41d98f8dd983d0a3ce7a532b2afd8a3c42400c 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyInfo;
 import com.netscape.certsrv.key.KeyInfoCollection;
 import com.netscape.cmstools.cli.CLI;
@@ -108,7 +109,8 @@ public class KeyFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        KeyInfoCollection keys = keyCLI.keyClient.listKeys(clientKeyID, status, maxResults, maxTime, start, size, realm);
+        KeyClient keyClient = keyCLI.getKeyClient();
+        KeyInfoCollection keys = keyClient.listKeys(clientKeyID, status, maxResults, maxTime, start, size, realm);
 
         Collection<KeyInfo> entries = keys.getEntries();
 
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java
index 312fbf0780df56787fe4f79a8de732c65448cb2d..fb7b919a3c576e175553860df05d657fe7c86e64 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java
@@ -6,6 +6,7 @@ import java.util.List;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRequestResource;
 import com.netscape.certsrv.key.KeyRequestResponse;
 import com.netscape.cmstools.cli.CLI;
@@ -109,6 +110,8 @@ public class KeyGenerateCLI extends CLI {
             usages = Arrays.asList(givenUsages.split(","));
         }
 
+        KeyClient keyClient = keyCLI.getKeyClient();
+
         KeyRequestResponse response = null;
         switch (keyAlgorithm) {
         case KeyRequestResource.DES3_ALGORITHM:
@@ -117,12 +120,12 @@ public class KeyGenerateCLI extends CLI {
         case KeyRequestResource.RC4_ALGORITHM:
         case KeyRequestResource.AES_ALGORITHM:
         case KeyRequestResource.RC2_ALGORITHM:
-            response = keyCLI.keyClient.generateSymmetricKey(
+            response = keyClient.generateSymmetricKey(
                     clientKeyId, keyAlgorithm, size, usages, null, realm);
             break;
         case KeyRequestResource.RSA_ALGORITHM:
         case KeyRequestResource.DSA_ALGORITHM:
-            response = keyCLI.keyClient.generateAsymmetricKey(
+            response = keyClient.generateAsymmetricKey(
                     clientKeyId, keyAlgorithm, size, usages, null, realm);
             break;
         default:
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java
index 1778fef479b251d156fb0c33ff3c6c70ea5f2daf..52bf173d5f4866bb83bcbf79f3c6cdeff01b8959 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java
@@ -24,6 +24,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyInfo;
 import com.netscape.cmstools.cli.CLI;
 
@@ -67,9 +68,10 @@ public class KeyModifyCLI extends CLI {
 
         KeyId keyId = new KeyId(cmdArgs[0]);
 
-        keyCLI.keyClient.modifyKeyStatus(keyId, status);
+        KeyClient keyClient = keyCLI.getKeyClient();
+        keyClient.modifyKeyStatus(keyId, status);
 
-        KeyInfo keyInfo = keyCLI.keyClient.getKeyInfo(keyId);
+        KeyInfo keyInfo = keyClient.getKeyInfo(keyId);
         KeyCLI.printKeyInfo(keyInfo);
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java
index 0bb2a2d3e50c354b11d9c9534ea4d457ab1649a0..4f4929a581e660075295abcb92ff2ac4c7339e23 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java
@@ -12,6 +12,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRecoveryRequest;
 import com.netscape.certsrv.key.KeyRequestResponse;
 import com.netscape.cmstools.cli.CLI;
@@ -61,6 +62,7 @@ public class KeyRecoverCLI extends CLI {
         String keyID = cmd.getOptionValue("keyID");
 
         KeyRequestResponse response = null;
+        KeyClient keyClient = keyCLI.getKeyClient();
 
         if (requestFile != null) {
             try {
@@ -68,7 +70,7 @@ public class KeyRecoverCLI extends CLI {
                 Unmarshaller unmarshaller = context.createUnmarshaller();
                 FileInputStream fis = new FileInputStream(requestFile);
                 KeyRecoveryRequest req = (KeyRecoveryRequest) unmarshaller.unmarshal(fis);
-                response = keyCLI.keyClient.recoverKey(req.getKeyId(),
+                response = keyClient.recoverKey(req.getKeyId(),
                         Utils.base64decode(req.getSessionWrappedPassphrase()),
                         Utils.base64decode(req.getTransWrappedSessionKey()), Utils.base64decode(req.getNonceData()),
                         req.getCertificate());
@@ -80,7 +82,7 @@ public class KeyRecoverCLI extends CLI {
 
         } else if (keyID != null) {
             String keyId = cmd.getOptionValue("keyID");
-            response = keyCLI.keyClient.recoverKey(new KeyId(keyId), null, null, null, null);
+            response = keyClient.recoverKey(new KeyId(keyId), null, null, null, null);
         } else {
             throw new Exception("Neither a key ID nor a request file's path is specified.");
         }
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
index 69730f9b30e3c2131a1ac29f2b852fd4a0c2e1f6..75f08f161940eaebdadf0b224e53ff191ae1836c 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRequestInfo;
 import com.netscape.certsrv.key.KeyRequestInfoCollection;
 import com.netscape.certsrv.request.RequestId;
@@ -114,7 +115,8 @@ public class KeyRequestFindCLI extends CLI {
         s = cmd.getOptionValue("maxTime");
         Integer maxTime = s == null ? null : Integer.valueOf(s);
 
-        KeyRequestInfoCollection keys = keyCLI.keyClient.listRequests(
+        KeyClient keyClient = keyCLI.getKeyClient();
+        KeyRequestInfoCollection keys = keyClient.listRequests(
                 status, type, clientKeyID, start, pageSize, maxResults, maxTime, realm);
 
         MainCLI.printMessage(keys.getTotal() + " entries matched");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java
index d3ab01b70299d0da8015077907ebde8922517232..09cb0a1859911ab0067132b1e65c35dc11828e72 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java
@@ -5,6 +5,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRequestInfo;
 import com.netscape.certsrv.request.RequestId;
 import com.netscape.cmstools.cli.CLI;
@@ -48,23 +49,24 @@ public class KeyRequestReviewCLI extends CLI {
         }
 
         RequestId reqId = new RequestId(cmdArgs[0]);
+        KeyClient keyClient = keyCLI.getKeyClient();
 
         String action = cmd.getOptionValue("action");
         switch (action.toLowerCase()) {
         case "approve":
-            keyCLI.keyClient.approveRequest(reqId);
+            keyClient.approveRequest(reqId);
             break;
         case "reject":
-            keyCLI.keyClient.rejectRequest(reqId);
+            keyClient.rejectRequest(reqId);
             break;
         case "cancel":
-            keyCLI.keyClient.cancelRequest(reqId);
+            keyClient.cancelRequest(reqId);
             break;
         default:
             throw new Exception("Invalid action.");
         }
 
-        KeyRequestInfo keyRequestInfo = keyCLI.keyClient.getRequestInfo(reqId);
+        KeyRequestInfo keyRequestInfo = keyClient.getRequestInfo(reqId);
 
         MainCLI.printMessage("Result");
         KeyCLI.printKeyRequestInfo(keyRequestInfo);
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java
index 82aabfc42f962f33e7d68f925a7d7a9f1d9f6c19..9ca6ff9bc4941f5f71b068c80d81e90ef02f4b00 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRequestInfo;
 import com.netscape.certsrv.request.RequestId;
 import com.netscape.cmstools.cli.CLI;
@@ -57,7 +58,8 @@ public class KeyRequestShowCLI extends CLI {
 
         RequestId requestId = new RequestId(args[0].trim());
 
-        KeyRequestInfo keyRequestInfo = keyCLI.keyClient.getRequestInfo(requestId);
+        KeyClient keyClient = keyCLI.getKeyClient();
+        KeyRequestInfo keyRequestInfo = keyClient.getRequestInfo(requestId);
 
         KeyCLI.printKeyRequestInfo(keyRequestInfo);
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java
index 23e220a9a9e1f45d2e71ed02a171f25ed8a3e008..740baf3b42fd223501485a583feb80b3443ded49 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java
@@ -13,6 +13,7 @@ import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
 import com.netscape.certsrv.key.Key;
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyRecoveryRequest;
 import com.netscape.certsrv.request.RequestId;
 import com.netscape.cmstools.cli.CLI;
@@ -77,6 +78,7 @@ public class KeyRetrieveCLI extends CLI {
 
         String requestFile = cmd.getOptionValue("input");
 
+        KeyClient keyClient = keyCLI.getKeyClient();
         Key keyData = null;
 
         if (requestFile != null) {
@@ -90,24 +92,24 @@ public class KeyRetrieveCLI extends CLI {
             }
 
             if (req.getCertificate() != null) {
-                keyData = keyCLI.keyClient.retrieveKeyByPKCS12(req.getKeyId(), req.getCertificate(),
+                keyData = keyClient.retrieveKeyByPKCS12(req.getKeyId(), req.getCertificate(),
                         req.getPassphrase());
 
             } else if (req.getPassphrase() != null) {
-                keyData = keyCLI.keyClient.retrieveKeyByPassphrase(req.getKeyId(), req.getPassphrase());
+                keyData = keyClient.retrieveKeyByPassphrase(req.getKeyId(), req.getPassphrase());
 
             } else if (req.getSessionWrappedPassphrase() != null) {
-                keyData = keyCLI.keyClient.retrieveKeyUsingWrappedPassphrase(req.getKeyId(),
+                keyData = keyClient.retrieveKeyUsingWrappedPassphrase(req.getKeyId(),
                         Utils.base64decode(req.getTransWrappedSessionKey()),
                         Utils.base64decode(req.getSessionWrappedPassphrase()),
                         Utils.base64decode(req.getNonceData()));
 
             } else if (req.getTransWrappedSessionKey() != null) {
-                keyData = keyCLI.keyClient.retrieveKey(req.getKeyId(),
+                keyData = keyClient.retrieveKey(req.getKeyId(),
                         Utils.base64decode(req.getTransWrappedSessionKey()));
 
             } else {
-                keyData = keyCLI.keyClient.retrieveKey(req.getKeyId());
+                keyData = keyClient.retrieveKey(req.getKeyId());
             }
 
         } else {
@@ -122,16 +124,16 @@ public class KeyRetrieveCLI extends CLI {
 
             if (passphrase != null) {
                 if (requestId != null) {
-                    keyData = keyCLI.keyClient.retrieveKeyByRequestWithPassphrase(
+                    keyData = keyClient.retrieveKeyByRequestWithPassphrase(
                             new RequestId(requestId), passphrase);
                 } else {
-                    keyData = keyCLI.keyClient.retrieveKeyByPassphrase(new KeyId(keyId), passphrase);
+                    keyData = keyClient.retrieveKeyByPassphrase(new KeyId(keyId), passphrase);
                 }
             } else {
                 if (requestId != null) {
-                    keyData = keyCLI.keyClient.retrieveKeyByRequest(new RequestId(requestId));
+                    keyData = keyClient.retrieveKeyByRequest(new RequestId(requestId));
                 } else {
-                    keyData = keyCLI.keyClient.retrieveKey(new KeyId(keyId));
+                    keyData = keyClient.retrieveKey(new KeyId(keyId));
                 }
 
                 clientEncryption = false;
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java
index d877222dc3850ad24b30071e8f59295324c22bec..c579fe5f80b23c6daec6d182fd416e4936b8cb92 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java
@@ -24,6 +24,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
+import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyInfo;
 import com.netscape.cmstools.cli.CLI;
 
@@ -59,14 +60,15 @@ public class KeyShowCLI extends CLI {
 
         String[] cmdArgs = cmd.getArgs();
         String clientKeyId = cmd.getOptionValue("clientKeyID");
+        KeyClient keyClient = keyCLI.getKeyClient();
         KeyInfo keyInfo;
 
         if (cmdArgs.length == 1) {
             KeyId keyId = new KeyId(cmdArgs[0]);
-            keyInfo = keyCLI.keyClient.getKeyInfo(keyId);
+            keyInfo = keyClient.getKeyInfo(keyId);
 
         } else if (clientKeyId != null) {
-            keyInfo = keyCLI.keyClient.getActiveKeyInfo(clientKeyId);
+            keyInfo = keyClient.getActiveKeyInfo(clientKeyId);
 
         } else {
             throw new Exception("Missing Key ID or Client Key ID.");
-- 
2.9.3

