Skip to content

Commit 471181c

Browse files
committed
Simplify keytool invocations.
1 parent 27dd37c commit 471181c

File tree

1 file changed

+24
-48
lines changed
  • substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx

1 file changed

+24
-48
lines changed

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jmx/JmxTest.java

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131
import static org.junit.Assert.assertTrue;
3232
import static org.junit.Assume.assumeTrue;
3333

34-
import java.io.BufferedReader;
3534
import java.io.IOException;
36-
import java.io.InputStreamReader;
37-
import java.io.OutputStream;
38-
import java.io.PrintWriter;
3935
import java.lang.management.ClassLoadingMXBean;
4036
import java.lang.management.GarbageCollectorMXBean;
4137
import java.lang.management.ManagementFactory;
@@ -49,13 +45,11 @@
4945
import java.nio.file.Files;
5046
import java.nio.file.Path;
5147
import java.nio.file.attribute.PosixFilePermission;
52-
import java.util.ArrayList;
5348
import java.util.HashMap;
5449
import java.util.List;
5550
import java.util.Map;
5651
import java.util.Set;
5752
import java.util.concurrent.TimeUnit;
58-
import java.util.stream.Collectors;
5953

6054
import javax.management.MBeanServer;
6155
import javax.management.MBeanServerConnection;
@@ -73,6 +67,7 @@
7367

7468
import com.oracle.svm.core.VMInspectionOptions;
7569
import com.oracle.svm.core.jdk.management.ManagementAgentStartupHook;
70+
import com.oracle.svm.hosted.c.util.FileUtils;
7671
import com.oracle.svm.test.AddExports;
7772

7873
import jdk.management.jfr.FlightRecorderMXBean;
@@ -148,63 +143,44 @@ public static void setup() throws IOException {
148143
}
149144

150145
private static void createClientKey(Path tempDirectory) throws IOException {
151-
final List<String> commandParameters = new ArrayList<>(List.of("keytool", "-genkey"));
152-
commandParameters.addAll(List.of("-keystore", "clientkeystore"));
153-
commandParameters.addAll(List.of("-alias", "clientkey"));
154-
commandParameters.addAll(List.of("-storepass", "clientpass"));
155-
commandParameters.addAll(List.of("-keypass", "clientpass"));
156-
commandParameters.addAll(List.of("-dname", "CN=test, OU=test, O=test, L=test, ST=test, C=test, EMAILADDRESS=test"));
157-
commandParameters.addAll(List.of("-validity", "99999"));
158-
commandParameters.addAll(List.of("-keyalg", "rsa"));
159-
160-
ProcessBuilder pb = new ProcessBuilder().command(commandParameters);
161-
pb.directory(tempDirectory.toFile());
162-
final Process process = pb.start();
163-
waitForProcess(process, commandParameters);
146+
runCommand(tempDirectory, List.of("keytool", "-genkey",
147+
"-keystore", "clientkeystore",
148+
"-alias", "clientkey",
149+
"-storepass", "clientpass",
150+
"-keypass", "clientpass",
151+
"-dname", "CN=test, OU=test, O=test, L=test, ST=test, C=test, EMAILADDRESS=test",
152+
"-validity", "99999",
153+
"-keyalg", "rsa"));
164154
}
165155

166156
private static void createClientCert(Path tempDirectory) throws IOException {
167-
final List<String> commandParameters = new ArrayList<>(List.of("keytool", "-exportcert"));
168-
commandParameters.addAll(List.of("-keystore", "clientkeystore"));
169-
commandParameters.addAll(List.of("-alias", "clientkey"));
170-
commandParameters.addAll(List.of("-storepass", "clientpass"));
171-
commandParameters.addAll(List.of("-file", "client.cer"));
172-
173-
ProcessBuilder pb = new ProcessBuilder().command(commandParameters);
174-
pb.directory(tempDirectory.toFile());
175-
final Process process = pb.start();
176-
waitForProcess(process, commandParameters);
157+
runCommand(tempDirectory, List.of("keytool", "-exportcert",
158+
"-keystore", "clientkeystore",
159+
"-alias", "clientkey",
160+
"-storepass", "clientpass",
161+
"-file", "client.cer"));
177162
}
178163

179164
private static void createServerTrustStore(Path tempDirectory) throws IOException {
180-
final List<String> commandParameters = new ArrayList<>(List.of("keytool", "-importcert"));
181-
commandParameters.addAll(List.of("-file", "client.cer"));
182-
commandParameters.addAll(List.of("-keystore", "servertruststore"));
183-
commandParameters.addAll(List.of("-storepass", "servertrustpass"));
165+
runCommand(tempDirectory, List.of("keytool", "-importcert",
166+
"-noprompt",
167+
"-file", "client.cer",
168+
"-keystore", "servertruststore",
169+
"-storepass", "servertrustpass"));
170+
}
184171

185-
ProcessBuilder pb = new ProcessBuilder().command(commandParameters);
172+
private static void runCommand(Path tempDirectory, List<String> command) throws IOException {
173+
ProcessBuilder pb = new ProcessBuilder().command(command);
186174
pb.directory(tempDirectory.toFile());
187175
final Process process = pb.start();
188-
// Prompted about whether the cert should be trusted.
189-
OutputStream os = process.getOutputStream();
190-
PrintWriter writer = new PrintWriter(os);
191-
writer.write("y\n");
192-
writer.flush();
193-
waitForProcess(process, commandParameters);
194-
}
195-
196-
private static void waitForProcess(Process process, List<String> command) throws IOException {
197176
try {
198177
process.waitFor(5, TimeUnit.SECONDS);
199178
} catch (InterruptedException e) {
200179
throw new IOException("Keytool execution error");
201180
}
202-
203181
if (process.exitValue() > 0) {
204-
final String processError = (new BufferedReader(new InputStreamReader(process.getErrorStream()))).lines()
205-
.collect(Collectors.joining(" \\ "));
206-
final String processOutput = (new BufferedReader(new InputStreamReader(process.getInputStream()))).lines()
207-
.collect(Collectors.joining(" \\ "));
182+
final String processError = String.join(" \\ ", FileUtils.readAllLines(process.getErrorStream()));
183+
final String processOutput = String.join(" \\ ", FileUtils.readAllLines(process.getInputStream()));
208184
throw new IOException(
209185
"Keytool execution error: " + processError + ", output: " + processOutput + ", command: " + command);
210186
}

0 commit comments

Comments
 (0)