From d4e5176702b3a08a67233e069ac211e95e01b228 Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Sat, 3 Jun 2017 01:32:37 +0200 Subject: Refactored CLI.runExternal(). The methods for running external commands in various CLI classes have been merged into CLI.runExternal(). https://pagure.io/dogtagpki/issue/2717 Change-Id: I5b6d136db699d3bb48e4f36f7f187d0240bbbf62 --- .../src/com/netscape/cmstools/cli/CLI.java | 35 ++++++++++++++++++++++ .../src/com/netscape/cmstools/cli/MainCLI.java | 10 +++---- .../cmstools/client/ClientCertImportCLI.java | 23 ++------------ .../cmstools/client/ClientCertModifyCLI.java | 35 +++++----------------- .../cmstools/client/ClientCertRequestCLI.java | 10 +++---- .../cmstools/client/ClientCertShowCLI.java | 27 +++-------------- .../netscape/cmstools/client/ClientInitCLI.java | 25 ++++++---------- 7 files changed, 65 insertions(+), 100 deletions(-) (limited to 'base/java-tools') diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java index 4911b8ae2..60db7a151 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java @@ -18,6 +18,7 @@ package com.netscape.cmstools.cli; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; @@ -351,4 +352,38 @@ public class CLI { public static void setVerbose(boolean verbose) { CLI.verbose = verbose; } + + public void runExternal(List command) throws CLIException, IOException, InterruptedException { + String[] array = command.toArray(new String[command.size()]); + runExternal(array); + } + + public void runExternal(String[] command) throws CLIException, IOException, InterruptedException { + + if (verbose) { + + System.out.print("External command:"); + + for (String c : command) { + + boolean quote = c.contains(" "); + + System.out.print(" "); + + if (quote) System.out.print("\""); + System.out.print(c); + if (quote) System.out.print("\""); + } + + System.out.println(); + } + + Runtime rt = Runtime.getRuntime(); + Process p = rt.exec(command); + int rc = p.waitFor(); + + if (rc != 0) { + throw new CLIException("External command failed. RC: " + rc, rc); + } + } } diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java index dcc60e25f..51861b522 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java @@ -473,12 +473,10 @@ public class MainCLI extends CLI { "--empty-password" }; - Runtime rt = Runtime.getRuntime(); - Process p = rt.exec(commands); - - int rc = p.waitFor(); - if (rc != 0) { - throw new Exception("Unable to create security database: " + certDatabase.getAbsolutePath() + " (rc: " + rc + ")"); + try { + runExternal(commands); + } catch (Exception e) { + throw new Exception("Unable to create security database", e); } } diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java index 9cb3e6723..687dfc419 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java @@ -21,14 +21,12 @@ package com.netscape.cmstools.client; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; -import java.io.IOException; import java.io.PrintWriter; import java.net.URI; import java.util.Arrays; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.lang.StringUtils; import com.netscape.certsrv.cert.CertClient; import com.netscape.certsrv.cert.CertData; @@ -283,8 +281,7 @@ public class ClientCertImportCLI extends CLI { }; try { - run(command); - + runExternal(command); } catch (Exception e) { throw new Exception("Unable to import certificate file", e); } @@ -305,25 +302,9 @@ public class ClientCertImportCLI extends CLI { }; try { - run(command); - + runExternal(command); } catch (Exception e) { throw new Exception("Unable to import PKCS #12 file", e); } } - - public void run(String[] command) throws IOException, InterruptedException { - - if (verbose) { - System.out.println("Command: " + StringUtils.join(command, " ")); - } - - Runtime rt = Runtime.getRuntime(); - Process p = rt.exec(command); - int rc = p.waitFor(); - - if (rc != 0) { - throw new IOException("Command failed. RC: " + rc); - } - } } diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java index f229e6718..8ae7c6d0b 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java @@ -18,8 +18,6 @@ package com.netscape.cmstools.client; -import java.io.IOException; - import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; @@ -75,38 +73,19 @@ public class ClientCertModifyCLI extends CLI { String trustAttributes = cmd.getOptionValue("trust", "u,u,u"); - int rc = modifyCert( - mainCLI.certDatabase.getAbsolutePath(), - nickname, - trustAttributes); - - if (rc != 0) { - MainCLI.printMessage("Modified failed"); - return; - } - - MainCLI.printMessage("Modified certificate \"" + nickname + "\""); - } - - public int modifyCert( - String dbPath, - String nickname, - String trustAttributes) throws IOException, InterruptedException { - String[] command = { "/usr/bin/certutil", "-M", - "-d", dbPath, + "-d", mainCLI.certDatabase.getAbsolutePath(), "-n", nickname, "-t", trustAttributes }; - return run(command); - } - - public int run(String[] command) throws IOException, InterruptedException { + try { + runExternal(command); + } catch (Exception e) { + throw new Exception("Unable to modify certificate", e); + } - Runtime rt = Runtime.getRuntime(); - Process p = rt.exec(command); - return p.waitFor(); + MainCLI.printMessage("Modified certificate \"" + nickname + "\""); } } diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java index 696ab8ba3..a14bb242d 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java @@ -386,12 +386,10 @@ public class ClientCertRequestCLI extends CLI { "-n", subjectDN }; - Runtime rt = Runtime.getRuntime(); - Process p = rt.exec(commands); - - int rc = p.waitFor(); - if (rc != 0) { - throw new Exception("CSR generation failed"); + try { + runExternal(commands); + } catch (Exception e) { + throw new Exception("CSR generation failed", e); } if (verbose) { diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java index 2242b37f8..bb60fbfdd 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java @@ -20,13 +20,11 @@ package com.netscape.cmstools.client; import java.io.File; import java.io.FileWriter; -import java.io.IOException; import java.io.PrintWriter; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.lang.RandomStringUtils; -import org.apache.commons.lang.StringUtils; import org.mozilla.jss.crypto.X509Certificate; import com.netscape.certsrv.client.PKIClient; @@ -192,8 +190,7 @@ public class ClientCertShowCLI extends CLI { }; try { - run(command); - + runExternal(command); } catch (Exception e) { throw new Exception("Unable to export PKCS #12 file", e); } @@ -215,8 +212,7 @@ public class ClientCertShowCLI extends CLI { }; try { - run(command); - + runExternal(command); } catch (Exception e) { throw new Exception("Unable to export certificate", e); } @@ -238,8 +234,7 @@ public class ClientCertShowCLI extends CLI { }; try { - run(command); - + runExternal(command); } catch (Exception e) { throw new Exception("Unable to export private key", e); } @@ -261,23 +256,9 @@ public class ClientCertShowCLI extends CLI { }; try { - run(command); - + runExternal(command); } catch (Exception e) { throw new Exception("Unable to export client certificate and private key", e); } } - - public void run(String[] command) throws IOException, InterruptedException { - - if (verbose) System.out.println("Command: " + StringUtils.join(command, " ")); - - Runtime rt = Runtime.getRuntime(); - Process p = rt.exec(command); - int rc = p.waitFor(); - - if (rc != 0) { - throw new IOException("Command failed. RC: " + rc); - } - } } diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java index 893b40b34..7e018de3a 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java @@ -95,12 +95,11 @@ public class ClientInitCLI extends CLI { File passwordFile = new File(certDatabase, "password.txt"); try { - String[] commands = { - "/usr/bin/certutil", "-N", - "-d", certDatabase.getAbsolutePath(), - }; - - List list = new ArrayList<>(Arrays.asList(commands)); + List list = new ArrayList<>(); + list.add("/usr/bin/certutil"); + list.add("-N"); + list.add("-d"); + list.add(certDatabase.getAbsolutePath()); if (mainCLI.config.getCertPassword() == null) { list.add("--empty-password"); @@ -114,16 +113,10 @@ public class ClientInitCLI extends CLI { list.add(passwordFile.getAbsolutePath()); } - commands = new String[list.size()]; - list.toArray(commands); - - Runtime rt = Runtime.getRuntime(); - Process p = rt.exec(commands); - - int rc = p.waitFor(); - if (rc != 0) { - MainCLI.printMessage("Client initialization failed"); - return; + try { + runExternal(list); + } catch (Exception e) { + throw new Exception("Client initialization failed", e); } MainCLI.printMessage("Client initialized"); -- cgit