>From b7f7221ae464614c3c1ae513b2449cae8850460d Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Tue, 5 Aug 2014 14:18:38 -0400
Subject: [PATCH] Adds a new CLI command pki ca-kraconnector-show.

The new command allows users to view the information about
kra connectors registered with the CA.

Ticket #479
---
 .../server/ca/rest/KRAConnectorService.java        | 13 +++++
 .../certsrv/system/KRAConnectorClient.java         |  7 +++
 .../certsrv/system/KRAConnectorResource.java       |  9 ++++
 .../netscape/cmstools/system/KRAConnectorCLI.java  |  1 +
 .../cmstools/system/KRAConnectorShowCLI.java       | 57 ++++++++++++++++++++++
 .../cms/servlet/admin/KRAConnectorProcessor.java   | 21 ++++++++
 6 files changed, 108 insertions(+)
 create mode 100644 base/java-tools/src/com/netscape/cmstools/system/KRAConnectorShowCLI.java

diff --git a/base/ca/src/org/dogtagpki/server/ca/rest/KRAConnectorService.java b/base/ca/src/org/dogtagpki/server/ca/rest/KRAConnectorService.java
index b488e5381bbe4e5ad42a71b193ebeb065a8182c8..93e571aa2934e54403d95292069f216fd905bfd4 100644
--- a/base/ca/src/org/dogtagpki/server/ca/rest/KRAConnectorService.java
+++ b/base/ca/src/org/dogtagpki/server/ca/rest/KRAConnectorService.java
@@ -22,6 +22,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Request;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
 import com.netscape.certsrv.base.BadRequestException;
@@ -84,4 +85,16 @@ public class KRAConnectorService extends PKIService implements KRAConnectorResou
         addConnector(info);
     }
 
+    @Override
+    public Response getConnectorInfo() {
+
+        try {
+            KRAConnectorProcessor processor = new KRAConnectorProcessor(getLocale(headers));
+            return createOKResponse(processor.getConnectorInfo());
+        } catch (EBaseException e) {
+            e.printStackTrace();
+            throw new PKIException(e.getMessage());
+        }
+    }
+
 }
diff --git a/base/common/src/com/netscape/certsrv/system/KRAConnectorClient.java b/base/common/src/com/netscape/certsrv/system/KRAConnectorClient.java
index 4a29ca154207fba5f3a40ccea562c3ee97ddbabb..91deb7b62a52e328f6932e930a3f533cd61bd0df 100644
--- a/base/common/src/com/netscape/certsrv/system/KRAConnectorClient.java
+++ b/base/common/src/com/netscape/certsrv/system/KRAConnectorClient.java
@@ -19,6 +19,8 @@
 package com.netscape.certsrv.system;
 import java.net.URISyntaxException;
 
+import javax.ws.rs.core.Response;
+
 import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.PKIClient;
 
@@ -46,4 +48,9 @@ public class KRAConnectorClient extends Client {
         kraConnectorClient.removeConnector(host, port);
     }
 
+    public KRAConnectorInfo getConnectorInfo(){
+        Response response = kraConnectorClient.getConnectorInfo();
+        return client.getEntity(response, KRAConnectorInfo.class);
+    }
+
 }
diff --git a/base/common/src/com/netscape/certsrv/system/KRAConnectorResource.java b/base/common/src/com/netscape/certsrv/system/KRAConnectorResource.java
index 970224b9a83d6ef119bf954b08ba0210cd62ea6e..2d43bab08c4f58cfbaf30a0a359742435255562c 100644
--- a/base/common/src/com/netscape/certsrv/system/KRAConnectorResource.java
+++ b/base/common/src/com/netscape/certsrv/system/KRAConnectorResource.java
@@ -19,10 +19,14 @@ package com.netscape.certsrv.system;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.annotations.ClientResponseType;
 
 import com.netscape.certsrv.acls.ACLMapping;
 import com.netscape.certsrv.authentication.AuthMethodMapping;
@@ -49,4 +53,9 @@ public interface KRAConnectorResource {
     @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
     public void removeConnector(@FormParam("host") String host, @FormParam("port") String port);
 
+    @GET
+    @Path("connectorInfo")
+    @ClientResponseType(entityType = KRAConnectorInfo.class)
+    public Response getConnectorInfo();
+
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java b/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java
index a59809e26ee55b062c3f8b6e9c1ef20d8720e154..872d4b9d646924307e27f360aadc7e31107661f1 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java
@@ -33,6 +33,7 @@ public class KRAConnectorCLI extends CLI {
 
         addModule(new KRAConnectorAddCLI(this));
         addModule(new KRAConnectorRemoveCLI(this));
+        addModule(new KRAConnectorShowCLI(this));
     }
 
     public String getFullName() {
diff --git a/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorShowCLI.java b/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorShowCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..8bf43cbe0088607fb9c24410c6c4415ab8d6521c
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorShowCLI.java
@@ -0,0 +1,57 @@
+package com.netscape.cmstools.system;
+
+import java.util.Arrays;
+
+import com.netscape.certsrv.system.KRAConnectorInfo;
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+public class KRAConnectorShowCLI extends CLI{
+
+    public KRAConnectorCLI kraConnectorCLI;
+
+    public KRAConnectorShowCLI(KRAConnectorCLI kraConnectorCLI) {
+        super("show", "Show KRA Connector", kraConnectorCLI);
+        this.kraConnectorCLI = kraConnectorCLI;
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " [OPTIONS...]", options);
+    }
+
+    public void execute(String[] args){
+     // Always check for "--help" prior to parsing
+        if (Arrays.asList(args).contains("--help")) {
+            // Display usage
+            printHelp();
+            System.exit(0);
+        }
+
+        KRAConnectorInfo info = kraConnectorCLI.kraConnectorClient.getConnectorInfo();
+
+        // Print the KRA Connector Information.
+
+        MainCLI.printMessage("KRA Connector Information");
+        System.out.println();
+        String host = info.getHost().trim();
+        if(host.indexOf(' ') == -1){
+            host += ":"+info.getPort();
+        }else{
+            // Assuming that the configuration file is not corrupted.
+            host.replace(" ", ", ");
+        }
+        System.out.println("Host: " + host);
+        System.out.println("Enabled: " + info.getEnable());
+        System.out.println("Local: " + info.getLocal());
+        System.out.println("Timeout: " +info.getTimeout());
+        System.out.println("URI: " + info.getUri());
+        System.out.println("Transport Cert: \n");
+        String transportCert = info.getTransportCert();
+        int i = 0;
+        for(i=0;i<transportCert.length()/64; i++){
+            System.out.println(transportCert.substring(i*64, (i*64) + 64));
+        }
+        System.out.println(transportCert.substring(i*64));
+        System.out.println();
+    }
+}
diff --git a/base/server/cms/src/com/netscape/cms/servlet/admin/KRAConnectorProcessor.java b/base/server/cms/src/com/netscape/cms/servlet/admin/KRAConnectorProcessor.java
index 69dff05e7eb23f49a5f0df2e9633f4aedc20d8c9..3bb1c9a388ba9ce7149e1aeab64d07160b67eb5b 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/admin/KRAConnectorProcessor.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/admin/KRAConnectorProcessor.java
@@ -220,4 +220,25 @@ public class KRAConnectorProcessor extends CAProcessor {
         replaceConnector();
     }
 
+    public KRAConnectorInfo getConnectorInfo() throws EPropertyNotFound, EBaseException{
+
+        if (! connectorExists) {
+            CMS.debug("getConnectorInfo: no KRA connector exists.");
+            throw new EBaseException("KRAConnector does not exist.");
+        }
+
+        IConfigStore cs = CMS.getConfigStore();
+
+        KRAConnectorInfo info = new KRAConnectorInfo();
+        info.setHost(cs.getString(PREFIX + ".host"));
+        info.setPort(cs.getString(PREFIX + ".port"));
+        info.setEnable(cs.getString(PREFIX + ".enable"));
+        info.setLocal(cs.getString(PREFIX + ".local"));
+        info.setTimeout(cs.getString(PREFIX + ".timeout"));
+        info.setUri(cs.getString(PREFIX + ".uri"));
+        info.setTransportCert(cs.getString(PREFIX + ".transportCert"));
+
+        return info;
+    }
+
 }
-- 
1.8.5.3

