From 9796490f1c9537cf6ca9d4f5a4cd4134eaf9fe55 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 3 Oct 2013 18:07:05 -0400
Subject: [PATCH] Added CA certificate CLI.

The ca-cert-* commands have been added to eventually replace cert-*.
The CATest has been updated to use the CertClient directly.
---
 .../src/com/netscape/cms/servlet/test/CATest.java  | 35 ++++----
 .../src/com/netscape/certsrv/ca/CAClient.java      | 93 +---------------------
 .../src/com/netscape/certsrv/cert/CertClient.java  |  4 +
 .../src/com/netscape/cmstools/cert/CertCLI.java    | 14 ++--
 .../src/com/netscape/cmstools/cli/CACLI.java       |  2 +
 5 files changed, 36 insertions(+), 112 deletions(-)

diff --git a/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java b/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java
index 7e12b8675d8b56480daff55cccd5455b06261cd0..904e60c627cf6ab488a193ceada1b50e10704d3d 100644
--- a/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java
+++ b/base/ca/functional/src/com/netscape/cms/servlet/test/CATest.java
@@ -32,6 +32,7 @@ import org.mozilla.jss.crypto.CryptoToken;
 import org.mozilla.jss.util.Password;
 
 import com.netscape.certsrv.ca.CAClient;
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.cert.CertDataInfo;
 import com.netscape.certsrv.cert.CertDataInfos;
@@ -151,12 +152,14 @@ public class CATest {
         }
 
         CAClient client;
+        CertClient certClient;
         try {
             ClientConfig config = new ClientConfig();
-            config.setServerURI(protocol + "://" + host + ":" + port + "/ca");
+            config.setServerURI(protocol + "://" + host + ":" + port);
             config.setCertNickname(clientCertNickname);
 
             client = new CAClient(new PKIClient(config));
+            certClient = (CertClient)client.getClient("cert");
         } catch (Exception e) {
             e.printStackTrace();
             return;
@@ -164,7 +167,7 @@ public class CATest {
 
         Collection<CertRequestInfo> list = null;
         try {
-            list = client.listRequests("complete", null);
+            list = certClient.listRequests("complete", null, null, null, null, null).getRequests();
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -176,7 +179,7 @@ public class CATest {
         CertId id = new CertId(certIdToPrint);
         CertData certData = null;
         try {
-            certData = client.getCertData(id);
+            certData = certClient.getCert(id);
         } catch (CertNotFoundException e) {
             e.printStackTrace();
             log("Cert: " + certIdToPrint + " not found. \n" + e.toString());
@@ -190,7 +193,7 @@ public class CATest {
         CertId certIdBad = new CertId(certIdBadToPrint);
         CertData certDataBad = null;
         try {
-            certDataBad = client.getCertData(certIdBad);
+            certDataBad = certClient.getCert(certIdBad);
         } catch (CertNotFoundException e) {
             e.printStackTrace();
             log("Cert: " + certIdBadToPrint + " not found. \n" + e.toString());
@@ -202,7 +205,7 @@ public class CATest {
 
         CertDataInfos infos = null;
         try {
-            infos = client.listCerts("VALID");
+            infos = certClient.listCerts("VALID", 100, 10);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -212,19 +215,19 @@ public class CATest {
         //Initiate a Certificate Enrollment
 
         CertEnrollmentRequest data = createUserCertEnrollment();
-        enrollAndApproveCertRequest(client, data);
+        enrollAndApproveCertRequest(certClient, data);
 
         // submit a RA authenticated user cert request
         CertEnrollmentRequest rdata = createRAUserCertEnrollment();
-        enrollCertRequest(client, rdata);
+        enrollCertRequest(certClient, rdata);
 
         // now try a manually approved server cert
         CertEnrollmentRequest serverData = createServerCertEnrollment();
-        enrollAndApproveCertRequest(client,serverData);
+        enrollAndApproveCertRequest(certClient,serverData);
 
         // submit using an agent approval profile
         serverData.setProfileId("caAgentServerCert");
-        enrollCertRequest(client, serverData);
+        enrollCertRequest(certClient, serverData);
 
         //Perform a sample certificate search with advanced search terms
 
@@ -233,7 +236,7 @@ public class CATest {
         searchData.setSerialFrom("9999");
         searchData.setSerialTo("99990");
 
-        infos = client.searchCerts(searchData);
+        infos = certClient.findCerts(searchData, 100, 10);
 
         printCertInfos(infos, new FilterBuilder(searchData).buildFilter());
 
@@ -244,7 +247,7 @@ public class CATest {
         CertRequestInfo infoBad = null;
 
         try {
-            infoBad = client.getRequest(idBad);
+            infoBad = certClient.getRequest(idBad);
         } catch (RequestNotFoundException e) {
             e.printStackTrace();
             log("Exception getting request #: " + idBad.toString() + "\n" + e.toString());
@@ -259,7 +262,7 @@ public class CATest {
         searchData.setEmail("jmagne@redhat.com");
         searchData.setMatchExactly(true);
 
-        infos = client.searchCerts(searchData);
+        infos = certClient.findCerts(searchData, 100, 10);
 
         printCertInfos(infos, new FilterBuilder(searchData).buildFilter());
 
@@ -277,10 +280,10 @@ public class CATest {
 
     }
 
-    private static void enrollAndApproveCertRequest(CAClient client, CertEnrollmentRequest data) {
+    private static void enrollAndApproveCertRequest(CertClient client, CertEnrollmentRequest data) {
         CertRequestInfos reqInfo = null;
         try {
-            reqInfo = client.enrollCertificate(data);
+            reqInfo = client.enrollRequest(data);
         } catch (Exception e) {
             e.printStackTrace();
             log(e.toString());
@@ -297,10 +300,10 @@ public class CATest {
         }
     }
 
-    private static void enrollCertRequest(CAClient client, CertEnrollmentRequest data) {
+    private static void enrollCertRequest(CertClient client, CertEnrollmentRequest data) {
         CertRequestInfos reqInfo = null;
         try {
-            reqInfo = client.enrollCertificate(data);
+            reqInfo = client.enrollRequest(data);
         } catch (Exception e) {
             e.printStackTrace();
             log(e.toString());
diff --git a/base/common/src/com/netscape/certsrv/ca/CAClient.java b/base/common/src/com/netscape/certsrv/ca/CAClient.java
index 3790c89de9e7ffdba783491378362d88a3a95f4b..24a84a2ead3ae3228e15ea631a03950530409b15 100644
--- a/base/common/src/com/netscape/certsrv/ca/CAClient.java
+++ b/base/common/src/com/netscape/certsrv/ca/CAClient.java
@@ -18,33 +18,20 @@
 package com.netscape.certsrv.ca;
 
 import java.net.URISyntaxException;
-import java.util.Collection;
 
-import com.netscape.certsrv.cert.CertData;
-import com.netscape.certsrv.cert.CertDataInfos;
-import com.netscape.certsrv.cert.CertEnrollmentRequest;
-import com.netscape.certsrv.cert.CertRequestInfo;
-import com.netscape.certsrv.cert.CertRequestInfos;
-import com.netscape.certsrv.cert.CertRequestResource;
-import com.netscape.certsrv.cert.CertResource;
-import com.netscape.certsrv.cert.CertReviewResponse;
-import com.netscape.certsrv.cert.CertSearchRequest;
+import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.client.SubsystemClient;
-import com.netscape.certsrv.dbs.certdb.CertId;
 import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.logging.AuditClient;
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.certsrv.profile.ProfileDataInfos;
 import com.netscape.certsrv.profile.ProfileResource;
-import com.netscape.certsrv.request.RequestId;
 import com.netscape.certsrv.selftests.SelfTestClient;
 import com.netscape.certsrv.user.UserClient;
 
 public class CAClient extends SubsystemClient {
 
-    private CertResource certClient;
-    private CertRequestResource certRequestClient;
     private ProfileResource profileClient;
 
     public CAClient(PKIClient client) throws URISyntaxException {
@@ -55,33 +42,14 @@ public class CAClient extends SubsystemClient {
     public void init() throws URISyntaxException {
 
         addClient(new AuditClient(client, name));
+        addClient(new CertClient(client, name));
         addClient(new GroupClient(client, name));
         addClient(new SelfTestClient(client, name));
         addClient(new UserClient(client, name));
 
-        certRequestClient = createProxy(CertRequestResource.class);
-        certClient = createProxy(CertResource.class);
         profileClient = createProxy(ProfileResource.class);
     }
 
-    public Collection<CertRequestInfo> listRequests(String requestState, String requestType) {
-        CertRequestInfos infos = null;
-        Collection<CertRequestInfo> list = null;
-        infos = certRequestClient.listRequests(
-                requestState, requestType, null, null, null, null);
-        list = infos.getRequests();
-
-        return list;
-    }
-
-    public CertDataInfos listCerts(String status) {
-        return certClient.listCerts(status, 100, 10);
-    }
-
-    public CertDataInfos searchCerts(CertSearchRequest data) {
-        return certClient.searchCerts(data, 100, 10);
-    }
-
     public ProfileDataInfos listProfiles() {
         return profileClient.listProfiles();
     }
@@ -94,61 +62,4 @@ public class CAClient extends SubsystemClient {
 
         return profileClient.retrieveProfile(id);
     }
-
-    public CertData getCertData(CertId id) {
-
-        if (id == null) {
-            return null;
-        }
-
-        return certClient.getCert(id);
-
-    }
-
-    public CertRequestInfos enrollCertificate(CertEnrollmentRequest data) {
-        if (data == null) {
-            return null;
-        }
-
-        return certRequestClient.enrollCert(data);
-    }
-
-    public CertRequestInfo getRequest(RequestId id) {
-        if (id == null) {
-            return null;
-        }
-        return certRequestClient.getRequestInfo(id);
-    }
-
-    public CertReviewResponse reviewRequest(RequestId id) {
-        if (id == null) {
-            return null;
-        }
-        return certRequestClient.reviewRequest(id);
-    }
-
-    public void approveRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.approveRequest(id, data);
-    }
-
-    public void rejectRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.rejectRequest(id, data);
-    }
-
-    public void cancelRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.cancelRequest(id, data);
-    }
-
-    public void updateRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.updateRequest(id, data);
-    }
-
-    public void validateRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.validateRequest(id, data);
-    }
-
-    public void unassignRequest(RequestId id, CertReviewResponse data) {
-        certRequestClient.unassignRequest(id, data);
-    }
-
 }
diff --git a/base/common/src/com/netscape/certsrv/cert/CertClient.java b/base/common/src/com/netscape/certsrv/cert/CertClient.java
index b0ebc04bf0972f25cf8814f357cf354b5e86fe0e..aa94483bdb7e625ed129b1ede4da91991951493e 100644
--- a/base/common/src/com/netscape/certsrv/cert/CertClient.java
+++ b/base/common/src/com/netscape/certsrv/cert/CertClient.java
@@ -51,6 +51,10 @@ public class CertClient extends Client {
         return certClient.reviewCert(id);
     }
 
+    public CertDataInfos listCerts(String status, Integer start, Integer size) {
+        return certClient.listCerts(status, start, size);
+    }
+
     public CertDataInfos findCerts(CertSearchRequest data, Integer start, Integer size) {
         return certClient.searchCerts(data, start, size);
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
index c3f43401c001a4878edfff5ba4509913e605a186..30a808b0675601be57ccc79215c1bc8aef35b36b 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
@@ -69,13 +69,17 @@ public class CertCLI extends CLI {
     public void execute(String[] args) throws Exception {
 
         client = parent.getClient();
+        certClient = (CertClient)parent.getClient("cert");
 
-        // determine the subsystem
-        String subsystem = client.getSubsystem();
-        if (subsystem == null) subsystem = "ca";
+        // if this is a top-level command
+        if (certClient == null) {
+            // determine the subsystem
+            String subsystem = client.getSubsystem();
+            if (subsystem == null) subsystem = "ca";
 
-        // create new cert client
-        certClient = new CertClient(client, subsystem);
+            // create new cert client
+            certClient = new CertClient(client, subsystem);
+        }
 
         super.execute(args);
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
index 0ce1afe8d7f0f76f29a2dd3a563ff99d0145bfa7..481be0330f7c3f0139b34eb446c5ee17062f6424 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
@@ -20,6 +20,7 @@ package com.netscape.cmstools.cli;
 
 import com.netscape.certsrv.ca.CAClient;
 import com.netscape.certsrv.client.Client;
+import com.netscape.cmstools.cert.CertCLI;
 import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.logging.AuditCLI;
 import com.netscape.cmstools.selftests.SelfTestCLI;
@@ -36,6 +37,7 @@ public class CACLI extends SubsystemCLI {
         super("ca", "CA management commands", parent);
 
         addModule(new AuditCLI(this));
+        addModule(new CertCLI(this));
         addModule(new GroupCLI(this));
         addModule(new SelfTestCLI(this));
         addModule(new UserCLI(this));
-- 
1.8.1.4

