Skip to content

Commit

Permalink
Refactored CLI.runExternal().
Browse files Browse the repository at this point in the history
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
  • Loading branch information
edewata committed Jun 3, 2017
1 parent 729468e commit d4e5176
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 100 deletions.
35 changes: 35 additions & 0 deletions base/java-tools/src/com/netscape/cmstools/cli/CLI.java
Expand Up @@ -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;
Expand Down Expand Up @@ -351,4 +352,38 @@ public static boolean isVerbose() {
public static void setVerbose(boolean verbose) {
CLI.verbose = verbose;
}

public void runExternal(List<String> 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);
}
}
}
10 changes: 4 additions & 6 deletions base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
Expand Up @@ -473,12 +473,10 @@ public void init() throws Exception {
"--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);
}
}

Expand Down
Expand Up @@ -21,14 +21,12 @@
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;
Expand Down Expand Up @@ -283,8 +281,7 @@ public void importCert(
};

try {
run(command);

runExternal(command);
} catch (Exception e) {
throw new Exception("Unable to import certificate file", e);
}
Expand All @@ -305,25 +302,9 @@ public void importPKCS12(
};

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);
}
}
}
Expand Up @@ -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;

Expand Down Expand Up @@ -75,38 +73,19 @@ public void execute(String[] args) throws Exception {

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 + "\"");
}
}
Expand Up @@ -386,12 +386,10 @@ public String generatePkcs10Request(
"-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) {
Expand Down
Expand Up @@ -20,13 +20,11 @@

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;
Expand Down Expand Up @@ -192,8 +190,7 @@ public void exportPKCS12(
};

try {
run(command);

runExternal(command);
} catch (Exception e) {
throw new Exception("Unable to export PKCS #12 file", e);
}
Expand All @@ -215,8 +212,7 @@ public void exportCertificate(
};

try {
run(command);

runExternal(command);
} catch (Exception e) {
throw new Exception("Unable to export certificate", e);
}
Expand All @@ -238,8 +234,7 @@ public void exportPrivateKey(
};

try {
run(command);

runExternal(command);
} catch (Exception e) {
throw new Exception("Unable to export private key", e);
}
Expand All @@ -261,23 +256,9 @@ public void exportClientCertificateAndPrivateKey(
};

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);
}
}
}
Expand Up @@ -95,12 +95,11 @@ public void execute(String[] args) throws Exception {
File passwordFile = new File(certDatabase, "password.txt");

try {
String[] commands = {
"/usr/bin/certutil", "-N",
"-d", certDatabase.getAbsolutePath(),
};

List<String> list = new ArrayList<>(Arrays.asList(commands));
List<String> 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");
Expand All @@ -114,16 +113,10 @@ public void execute(String[] args) throws Exception {
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");
Expand Down

0 comments on commit d4e5176

Please sign in to comment.