>From 6712f3c9cd1565546e7434f61fd73387b6869b85 Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Tue, 24 Jul 2012 15:35:34 -0400
Subject: [PATCH] Cert CLI - cert-request-review and cert-request-approve
 implementations

---
 .../src/com/netscape/cms/client/cert/CertCLI.java  |    2 +
 .../cms/client/cert/CertRequestApproveCLI.java     |   64 ++++++++++++++++++
 .../cms/client/cert/CertRequestReviewCLI.java      |   68 ++++++++++++++++++++
 .../netscape/cms/client/cert/CertRestClient.java   |   10 +++
 .../cms/profile/common/CAEnrollProfile.java        |   14 ++--
 .../com/netscape/cms/servlet/base/CMSServlet.java  |    4 +-
 .../cms/servlet/cert/RequestProcessor.java         |    1 -
 .../cms/servlet/request/CertRequestResource.java   |    4 +-
 .../request/CertRequestResourceService.java        |    7 +-
 .../cms/servlet/request/model/CertRequestDAO.java  |    1 -
 10 files changed, 160 insertions(+), 15 deletions(-)
 create mode 100644 base/common/src/com/netscape/cms/client/cert/CertRequestApproveCLI.java
 create mode 100644 base/common/src/com/netscape/cms/client/cert/CertRequestReviewCLI.java

diff --git a/base/common/src/com/netscape/cms/client/cert/CertCLI.java b/base/common/src/com/netscape/cms/client/cert/CertCLI.java
index ff2383cd1fa11959aecc1f281b82a0c3dbf80526..9340752b9c5bbe384c64fa1d3e36280385be3759 100644
--- a/base/common/src/com/netscape/cms/client/cert/CertCLI.java
+++ b/base/common/src/com/netscape/cms/client/cert/CertCLI.java
@@ -48,6 +48,8 @@ public class CertCLI extends CLI {
         addModule(new CertHoldCLI(this));
         addModule(new CertReleaseHoldCLI(this));
         addModule(new CertRequestSubmitCLI(this));
+        addModule(new CertRequestReviewCLI(this));
+        addModule(new CertRequestApproveCLI(this));
     }
 
     public void printHelp() {
diff --git a/base/common/src/com/netscape/cms/client/cert/CertRequestApproveCLI.java b/base/common/src/com/netscape/cms/client/cert/CertRequestApproveCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..88f1cd573aec57d495c88e5091150723b25f67a0
--- /dev/null
+++ b/base/common/src/com/netscape/cms/client/cert/CertRequestApproveCLI.java
@@ -0,0 +1,64 @@
+package com.netscape.cms.client.cert;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.ParseException;
+
+import com.netscape.certsrv.request.RequestId;
+import com.netscape.cms.client.cli.CLI;
+import com.netscape.cms.client.cli.MainCLI;
+import com.netscape.cms.servlet.base.CMSException;
+import com.netscape.cms.servlet.request.model.AgentEnrollmentRequestData;
+
+public class CertRequestApproveCLI extends CLI {
+    CertCLI parent;
+
+    public CertRequestApproveCLI(CertCLI parent) {
+        super("request-approve", "Approve certificate request");
+        this.parent = parent;
+    }
+
+    @Override
+    public void execute(String[] args) {
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+        } catch (ParseException e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
+        }
+
+        String[] cLineArgs = cmd.getArgs();
+
+        if (cLineArgs.length < 1) {
+            System.err.println("Error: No request id specified.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        RequestId reqId = null;
+        try {
+            reqId = new RequestId(cLineArgs[0]);
+        } catch (NumberFormatException e) {
+            System.err.println("Error: Invalid request id: " + cLineArgs[0]);
+            System.exit(-1);
+        }
+
+        try {
+            AgentEnrollmentRequestData reviewInfo = parent.client.reviewRequest(reqId);
+            parent.client.approveRequest(reqId, reviewInfo);
+        } catch (CMSException e) {
+            System.err.println(e.getMessage());
+            System.exit(-1);
+        }
+        MainCLI.printMessage("Certificate request approved.");
+    }
+
+    @Override
+    public void printHelp() {
+        formatter.printHelp(parent.parent.name
+                + " -U <uri> -d <Certificate database directory> -w <password> -n <cert> " + name + " <request id>",
+                options);
+    }
+}
diff --git a/base/common/src/com/netscape/cms/client/cert/CertRequestReviewCLI.java b/base/common/src/com/netscape/cms/client/cert/CertRequestReviewCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..48661c4750b7e80aa0d9cf4f04512e4291357fe9
--- /dev/null
+++ b/base/common/src/com/netscape/cms/client/cert/CertRequestReviewCLI.java
@@ -0,0 +1,68 @@
+package com.netscape.cms.client.cert;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.ParseException;
+
+import com.netscape.certsrv.request.RequestId;
+import com.netscape.cms.client.cli.CLI;
+import com.netscape.cms.client.cli.MainCLI;
+import com.netscape.cms.servlet.base.CMSException;
+import com.netscape.cms.servlet.request.model.AgentEnrollmentRequestData;
+
+public class CertRequestReviewCLI extends CLI {
+
+    CertCLI parent;
+
+    public CertRequestReviewCLI(CertCLI parent) {
+        super("request-review", "Review certificate request");
+        this.parent = parent;
+    }
+
+    @Override
+    public void execute(String[] args) {
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+        } catch (ParseException e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
+        }
+
+        String[] cLineArgs = cmd.getArgs();
+
+        if (cLineArgs.length < 1) {
+            System.err.println("Error: No request id specified.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        RequestId reqId = null;
+        try {
+            reqId = new RequestId(cLineArgs[0]);
+        } catch (NumberFormatException e) {
+            System.err.println("Error: Invalid RequestID: " + cLineArgs[0]);
+            System.exit(-1);
+        }
+
+        AgentEnrollmentRequestData reviewInfo = null;
+        try {
+            reviewInfo = parent.client.reviewRequest(reqId);
+        } catch (CMSException e) {
+            System.err.println(e.getMessage());
+            System.exit(-1);
+        }
+
+        MainCLI.printMessage("Request: " + cLineArgs[0]);
+
+        System.out.println(reviewInfo.toString());
+    }
+
+    @Override
+    public void printHelp() {
+        formatter.printHelp(parent.parent.name
+                + " -U <uri> -d <Certificate database directory> -w <password> -n <cert> " + name + " <request id>",
+                options);
+    }
+}
diff --git a/base/common/src/com/netscape/cms/client/cert/CertRestClient.java b/base/common/src/com/netscape/cms/client/cert/CertRestClient.java
index ba4c2fb0443eb575a6bc539a65290819be21ac0f..b83912fae9781d621bbd15b2d56d2ae1b1612a11 100644
--- a/base/common/src/com/netscape/cms/client/cert/CertRestClient.java
+++ b/base/common/src/com/netscape/cms/client/cert/CertRestClient.java
@@ -20,6 +20,7 @@ package com.netscape.cms.client.cert;
 import java.net.URISyntaxException;
 
 import com.netscape.certsrv.dbs.certdb.CertId;
+import com.netscape.certsrv.request.RequestId;
 import com.netscape.cms.servlet.cert.CertResource;
 import com.netscape.cms.servlet.cert.model.CertDataInfos;
 import com.netscape.cms.servlet.cert.model.CertRevokeRequest;
@@ -28,6 +29,7 @@ import com.netscape.cms.servlet.cert.model.CertUnrevokeRequest;
 import com.netscape.cms.servlet.cert.model.CertificateData;
 import com.netscape.cms.servlet.csadmin.CMSRestClient;
 import com.netscape.cms.servlet.request.CertRequestResource;
+import com.netscape.cms.servlet.request.model.AgentEnrollmentRequestData;
 import com.netscape.cms.servlet.request.model.CertRequestInfo;
 import com.netscape.cms.servlet.request.model.CertRequestInfos;
 import com.netscape.cms.servlet.request.model.EnrollmentRequestData;
@@ -77,4 +79,12 @@ public class CertRestClient extends CMSRestClient {
     public CertRequestInfos enrollRequest(EnrollmentRequestData data){
         return certRequestResource.enrollCert(data);
     }
+
+    public AgentEnrollmentRequestData reviewRequest(RequestId id){
+        return certRequestResource.reviewRequest(id);
+    }
+
+    public void approveRequest(RequestId id, AgentEnrollmentRequestData data) {
+        certRequestResource.approveRequest(id, data);
+    }
 }
diff --git a/base/common/src/com/netscape/cms/profile/common/CAEnrollProfile.java b/base/common/src/com/netscape/cms/profile/common/CAEnrollProfile.java
index b44a71ee84311bdf818b5b35f6f1e3de3f2f5f72..c80c6f3754132e7455b6d268d27b7eb9af97000e 100644
--- a/base/common/src/com/netscape/cms/profile/common/CAEnrollProfile.java
+++ b/base/common/src/com/netscape/cms/profile/common/CAEnrollProfile.java
@@ -96,6 +96,7 @@ public class CAEnrollProfile extends EnrollProfile {
                 request.getRequestId().toString());
         ICertificateAuthority ca = (ICertificateAuthority) getAuthority();
         ICAService caService = (ICAService) ca.getCAService();
+        CMS.debug("Check Point 1");
 
         if (caService == null) {
             throw new EProfileException("No CA Service");
@@ -104,7 +105,7 @@ public class CAEnrollProfile extends EnrollProfile {
         // if PKI Archive Option present, send this request
         // to DRM
         byte optionsData[] = request.getExtDataInByteArray(REQUEST_ARCHIVE_OPTIONS);
-
+        CMS.debug("Check Point 2");
         // do not archive keys for renewal requests
         if ((optionsData != null) && (!request.getRequestType().equals(IRequest.RENEWAL_REQUEST))) {
             PKIArchiveOptions options = toPKIArchiveOptions(optionsData);
@@ -175,11 +176,11 @@ public class CAEnrollProfile extends EnrollProfile {
                 }
             }
         }
-
+        CMS.debug("Check Point 3");
         // process certificate issuance
         X509CertInfo info = request.getExtDataInCertInfo(REQUEST_CERTINFO);
         X509CertImpl theCert = null;
-
+        CMS.debug("Check Point 4");
         // #615460 - added audit log (transaction)
         SessionContext sc = SessionContext.getExistingContext();
         sc.put("profileId", getId());
@@ -187,7 +188,7 @@ public class CAEnrollProfile extends EnrollProfile {
         if (setId != null) {
             sc.put("profileSetId", setId);
         }
-
+        CMS.debug("Check Point 5");
         try {
             theCert = caService.issueX509Cert(info, getId() /* profileId */,
                     id /* requestId */);
@@ -196,6 +197,7 @@ public class CAEnrollProfile extends EnrollProfile {
 
             throw new EProfileException(e.toString());
         }
+        CMS.debug("Check Point 6");
         request.setExtData(REQUEST_ISSUED_CERT, theCert);
 
         long endTime = CMS.getCurrentDate().getTime();
@@ -223,7 +225,7 @@ public class CAEnrollProfile extends EnrollProfile {
         }
 
         request.setRequestStatus(RequestStatus.COMPLETE);
-
+        CMS.debug("Check Point 7");
         // notifies updater plugins
         Enumeration<String> updaterIds = getProfileUpdaterIds();
         while (updaterIds.hasMoreElements()) {
@@ -237,5 +239,7 @@ public class CAEnrollProfile extends EnrollProfile {
             request.setExtData("isEncryptionCert", "true");
         else
             request.setExtData("isEncryptionCert", "false");
+        CMS.debug("Check Point 8");
     }
+
 }
diff --git a/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java b/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java
index 2273abc21dec15ad01fb9d601135b2b1909e9e48..a4922ab25e38e607b4e0c26582b9899f13103c1c 100644
--- a/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java
+++ b/base/common/src/com/netscape/cms/servlet/base/CMSServlet.java
@@ -830,9 +830,7 @@ public abstract class CMSServlet extends HttpServlet {
     /**
      * get ssl client authenticated certificate
      */
-    protected X509Certificate
-            getSSLClientCertificate(HttpServletRequest httpReq)
-                    throws EBaseException {
+    protected X509Certificate getSSLClientCertificate(HttpServletRequest httpReq) throws EBaseException {
 
         X509Certificate cert = null;
 
diff --git a/base/common/src/com/netscape/cms/servlet/cert/RequestProcessor.java b/base/common/src/com/netscape/cms/servlet/cert/RequestProcessor.java
index 9bb0c4b6483b6a5b71c588744bcba471df48c63b..57e33f3a5aedd3770441706e82cda054e7b2e868 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/RequestProcessor.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/RequestProcessor.java
@@ -81,7 +81,6 @@ public class RequestProcessor extends CertProcessor {
     public void processRequest(HttpServletRequest request, AgentEnrollmentRequestData data, IRequest req, String op)
             throws EBaseException {
         try {
-
             startTiming("approval");
 
             IAuthToken authToken = null;
diff --git a/base/common/src/com/netscape/cms/servlet/request/CertRequestResource.java b/base/common/src/com/netscape/cms/servlet/request/CertRequestResource.java
index f350747388f6b83a7b54560ce9b57a9490b09c62..41f218e6d0fffbf3458e2cb99b5eecccc2da8abe 100644
--- a/base/common/src/com/netscape/cms/servlet/request/CertRequestResource.java
+++ b/base/common/src/com/netscape/cms/servlet/request/CertRequestResource.java
@@ -82,7 +82,7 @@ public interface CertRequestResource {
 
     @POST
     @Path("{id}/approve")
-    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON ,MediaType.TEXT_XML})
     public void approveRequest(@PathParam("id") RequestId id, AgentEnrollmentRequestData data);
 
     @POST
@@ -113,5 +113,5 @@ public interface CertRequestResource {
     @POST
     @Path("{id}/assign")
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
-    public void assignRequest(@PathParam("id") RequestId id, AgentEnrollmentRequestData data);
+    public CertRequestInfos assignRequest(@PathParam("id") RequestId id, AgentEnrollmentRequestData data);
 }
diff --git a/base/common/src/com/netscape/cms/servlet/request/CertRequestResourceService.java b/base/common/src/com/netscape/cms/servlet/request/CertRequestResourceService.java
index 47f63ff6ce3798b29198dcd61b8c43d522eeb235..859b9b69ba37c384dd05ebb90ec1073f474803b0 100644
--- a/base/common/src/com/netscape/cms/servlet/request/CertRequestResourceService.java
+++ b/base/common/src/com/netscape/cms/servlet/request/CertRequestResourceService.java
@@ -127,15 +127,15 @@ public class CertRequestResourceService extends CMSResourceService implements Ce
         changeRequestState(id, data, "unassign");
     }
 
-    public void assignRequest(RequestId id, AgentEnrollmentRequestData data) {
+    public CertRequestInfos assignRequest(RequestId id, AgentEnrollmentRequestData data) {
         changeRequestState(id, data, "assign");
+        return new CertRequestInfos();
     }
 
     public void changeRequestState(RequestId id, AgentEnrollmentRequestData data, String op) {
         if (id == null) {
             throw new BadRequestException("Bad data input in CertRequestResourceService. op:" + op);
         }
-
         CertRequestDAO dao = new CertRequestDAO();
         try {
             dao.changeRequestState(id, servletRequest, data, getLocale(), op);
@@ -160,8 +160,9 @@ public class CertRequestResourceService extends CMSResourceService implements Ce
             throw new CMSException(CMS.getUserMessage(getLocale(), "CMS_INTERNAL_ERROR"));
         } catch (EBaseException e) {
             e.printStackTrace();
-            throw new CMSException("Problem approving request in CertRequestResource.assignRequest!");
+            throw new CMSException("Problem approving request in CertRequestResource.assignRequest!"+ e.toString());
         } catch (RequestNotFoundException e) {
+            CMS.debug("RequestNotFound"+e.toString());
             throw new CMSException(Response.Status.BAD_REQUEST,
                     CMS.getUserMessage(getLocale(), "CMS_REQUEST_NOT_FOUND", id.toString()));
         }
diff --git a/base/common/src/com/netscape/cms/servlet/request/model/CertRequestDAO.java b/base/common/src/com/netscape/cms/servlet/request/model/CertRequestDAO.java
index 193a53c8972dfd2791cfc03eba9536ebbdb80bc2..4ebfc251f533e21610a73584370c8a3abf7f3b6f 100644
--- a/base/common/src/com/netscape/cms/servlet/request/model/CertRequestDAO.java
+++ b/base/common/src/com/netscape/cms/servlet/request/model/CertRequestDAO.java
@@ -146,7 +146,6 @@ public class CertRequestDAO extends CMSRequestDAO {
         }
         String profileId = request.getExtDataInString("profileId");
         IProfile profile = ps.getProfile(profileId);
-
         AgentEnrollmentRequestData info = AgentEnrollmentRequestDataFactory.create(request, profile, uriInfo, locale);
         if (ca.noncesEnabled()) {
             addNonce(info, servletRequest);
-- 
1.7.10.2

