|
16 | 16 | */ |
17 | 17 | package org.apache.tomcat.util.net.ocsp; |
18 | 18 |
|
19 | | -import java.io.IOException; |
20 | | -import java.io.PrintStream; |
21 | | -import java.io.Reader; |
22 | | -import java.util.ArrayList; |
23 | | -import java.util.Arrays; |
24 | | -import java.util.List; |
25 | | -import java.util.Map; |
26 | | -import java.util.concurrent.TimeUnit; |
| 19 | +import java.io.File; |
| 20 | +import java.nio.file.FileSystems; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
27 | 23 |
|
28 | | -import org.junit.Assert; |
| 24 | +import org.apache.catalina.Context; |
| 25 | +import org.apache.catalina.LifecycleException; |
| 26 | +import org.apache.catalina.connector.Connector; |
| 27 | +import org.apache.catalina.startup.ExpandWar; |
| 28 | +import org.apache.catalina.startup.Tomcat; |
29 | 29 |
|
30 | | -import org.apache.tomcat.util.net.TesterSupport; |
31 | | - |
32 | | -/* |
33 | | - * The OpenSSL ocsp tool is great, but it does generate a lot of output. That output needs to be swallowed, else the |
34 | | - * process will freeze when the output buffers (stdout and stderr) are full. |
35 | | - * |
36 | | - * There is a command line option to redirect stdout (which could be redirected to /dev/null), but there is no option to |
37 | | - * redirect stderr. Therefore, this class uses a couple of dedicated threads to read stdout and stderr. By default, the |
38 | | - * output is ignored, but it can be dumped to Java's stdout/stderr if required for debugging purposes. |
39 | | - */ |
40 | 30 | public class TesterOcspResponder { |
41 | 31 |
|
42 | | - private static List<String> ocspArgs = Arrays.asList("ocsp", "-port", "8888", "-text", "-index", |
43 | | - TesterSupport.DB_INDEX, "-CA", TesterSupport.CA_CERT_PEM, "-rkey", TesterSupport.OCSP_RESPONDER_RSA_KEY, |
44 | | - "-rsigner", TesterSupport.OCSP_RESPONDER_RSA_CERT, "-nmin", "60"); |
| 32 | + private File catalinaBase; |
| 33 | + private Tomcat ocspResponder; |
45 | 34 |
|
46 | | - private Process p; |
| 35 | + public void start() throws Exception { |
| 36 | + ocspResponder = new Tomcat(); |
47 | 37 |
|
48 | | - public void start() throws IOException { |
49 | | - if (p != null) { |
50 | | - throw new IllegalStateException("Already started"); |
51 | | - } |
| 38 | + Connector connector = new Connector("HTTP/1.1"); |
| 39 | + connector.setPort(8888); |
| 40 | + connector.setThrowOnFailure(true); |
| 41 | + connector.setEncodedSolidusHandling("passthrough"); |
| 42 | + ocspResponder.getService().addConnector(connector); |
52 | 43 |
|
53 | | - String openSSLPath = System.getProperty("tomcat.test.openssl.path"); |
54 | | - String openSSLLibPath = null; |
55 | | - if (openSSLPath == null || openSSLPath.length() == 0) { |
56 | | - openSSLPath = "openssl"; |
57 | | - } else { |
58 | | - // Explicit OpenSSL path may also need explicit lib path |
59 | | - // (e.g. Gump needs this) |
60 | | - openSSLLibPath = openSSLPath.substring(0, openSSLPath.lastIndexOf('/')); |
61 | | - openSSLLibPath = openSSLLibPath + "/../:" + openSSLLibPath + "/../lib:" + openSSLLibPath + "/../lib64"; |
| 44 | + // Create a temporary directory structure for the OCSP responder |
| 45 | + File tempBase = new File(System.getProperty("tomcat.test.temp", "output/tmp")); |
| 46 | + if (!tempBase.mkdirs() && !tempBase.isDirectory()) { |
| 47 | + throw new IllegalStateException("Unable to create tempBase"); |
62 | 48 | } |
63 | | - List<String> cmd = new ArrayList<>(); |
64 | | - cmd.add(openSSLPath); |
65 | | - cmd.addAll(ocspArgs); |
66 | 49 |
|
67 | | - ProcessBuilder pb = new ProcessBuilder(cmd.toArray(new String[0])); |
68 | | - |
69 | | - if (openSSLLibPath != null) { |
70 | | - Map<String,String> env = pb.environment(); |
71 | | - String libraryPath = env.get("LD_LIBRARY_PATH"); |
72 | | - if (libraryPath == null) { |
73 | | - libraryPath = openSSLLibPath; |
74 | | - } else { |
75 | | - libraryPath = libraryPath + ":" + openSSLLibPath; |
76 | | - } |
77 | | - env.put("LD_LIBRARY_PATH", libraryPath); |
| 50 | + // Create and configure CATALINA_BASE |
| 51 | + Path tempBasePath = FileSystems.getDefault().getPath(tempBase.getAbsolutePath()); |
| 52 | + catalinaBase = Files.createTempDirectory(tempBasePath, "ocsp").toFile(); |
| 53 | + if (!catalinaBase.isDirectory()) { |
| 54 | + throw new IllegalStateException("Unable to create CATALINA_BASE for OCSP responder"); |
78 | 55 | } |
| 56 | + ocspResponder.setBaseDir(catalinaBase.getAbsolutePath()); |
79 | 57 |
|
80 | | - p = pb.start(); |
| 58 | + // Create and configure a web apps directory |
| 59 | + File appBase = new File(catalinaBase, "webapps"); |
| 60 | + if (!appBase.exists() && !appBase.mkdir()) { |
| 61 | + throw new IllegalStateException("Unable to create appBase for OCSP responder"); |
| 62 | + } |
| 63 | + ocspResponder.getHost().setAppBase(appBase.getAbsolutePath()); |
81 | 64 |
|
82 | | - redirect(p.inputReader(), System.out, true); |
83 | | - redirect(p.errorReader(), System.err, true); |
| 65 | + // Configure the ROOT web application |
| 66 | + // No file system docBase required |
| 67 | + Context ctx = ocspResponder.addContext("", null); |
| 68 | + Tomcat.addServlet(ctx, "responder", new TesterOcspResponderServlet()); |
| 69 | + ctx.addServletMappingDecoded("/", "responder"); |
84 | 70 |
|
85 | | - Assert.assertTrue(p.isAlive()); |
| 71 | + // Start the responder |
| 72 | + ocspResponder.start(); |
86 | 73 | } |
87 | 74 |
|
88 | 75 | public void stop() { |
89 | | - if (p == null) { |
90 | | - throw new IllegalStateException("Not started"); |
91 | | - } |
92 | | - p.destroy(); |
93 | | - |
94 | | - try { |
95 | | - if (!p.waitFor(30, TimeUnit.SECONDS)) { |
96 | | - throw new IllegalStateException("Failed to stop"); |
| 76 | + if (ocspResponder != null) { |
| 77 | + try { |
| 78 | + ocspResponder.stop(); |
| 79 | + } catch (LifecycleException e) { |
| 80 | + // Good enough for testing |
| 81 | + e.printStackTrace(); |
97 | 82 | } |
98 | | - } catch (InterruptedException e) { |
99 | | - throw new IllegalStateException("Interrupted while waiting to stop", e); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - |
104 | | - private void redirect(final Reader r, final PrintStream os, final boolean swallow) { |
105 | | - /* |
106 | | - * InputStream will close when process ends. Thread will exit once stream closes. |
107 | | - */ |
108 | | - new Thread( () -> { |
109 | | - char[] cbuf = new char[1024]; |
110 | 83 | try { |
111 | | - int read; |
112 | | - while ((read = r.read(cbuf)) > 0) { |
113 | | - if (!swallow) { |
114 | | - os.print(new String(cbuf, 0, read)); |
115 | | - } |
116 | | - } |
117 | | - } catch (IOException ignore) { |
118 | | - // Ignore |
| 84 | + ocspResponder.destroy(); |
| 85 | + } catch (LifecycleException e) { |
| 86 | + // Good enough for testing |
| 87 | + e.printStackTrace(); |
119 | 88 | } |
120 | | - |
121 | | - }).start(); |
| 89 | + } |
| 90 | + if (catalinaBase != null) { |
| 91 | + ExpandWar.deleteDir(catalinaBase); |
| 92 | + } |
122 | 93 | } |
123 | 94 | } |
0 commit comments