12
12
*******************************************************************************/
13
13
package com .ibm .ws .jdbc .fat .postgresql ;
14
14
15
- import java .io .IOException ;
16
- import java .io .InputStream ;
17
15
import java .sql .Connection ;
18
16
import java .sql .Statement ;
19
17
import java .util .Arrays ;
20
- import java .util .Scanner ;
21
- import java .util .concurrent .TimeUnit ;
22
18
23
19
import org .junit .AfterClass ;
24
20
import org .junit .BeforeClass ;
31
27
import componenttest .annotation .Server ;
32
28
import componenttest .annotation .TestServlet ;
33
29
import componenttest .containers .ImageBuilder ;
30
+ import componenttest .containers .KeystoreBuilder ;
31
+ import componenttest .containers .KeystoreBuilder .STORE_TYPE ;
34
32
import componenttest .containers .SimpleLogConsumer ;
35
33
import componenttest .custom .junit .runner .FATRunner ;
36
34
import componenttest .custom .junit .runner .Mode ;
@@ -108,9 +106,13 @@ public static void setUp() throws Exception {
108
106
}
109
107
110
108
postgre .copyFileFromContainer ("/tmp/clientKeystore.p12" , serverLibertySSL .getServerRoot () + "/resources/security/outboundKeys.p12" );
111
- postgre .copyFileFromContainer ("/var/lib/postgresql/server.crt" , serverLibertySSL .getServerRoot () + "/resources/security/server.crt" );
112
- importServerCert (serverLibertySSL .getServerRoot () + "/resources/security/outboundKeys.p12" ,
113
- serverLibertySSL .getServerRoot () + "/resources/security/server.crt" );
109
+ KeystoreBuilder .of (serverLibertySSL , postgre )
110
+ .withCertificate ("server" , "/var/lib/postgresql/server.crt" )
111
+ .withDirectory (serverLibertySSL .getServerRoot () + "/resources/security/" )
112
+ .withFilename ("outboundKeys" )
113
+ .withStoreType (STORE_TYPE .PKCS12 )
114
+ .withPassword ("liberty" )
115
+ .export ();
114
116
115
117
postgre .copyFileFromContainer ("/tmp/clientKeystore.p12" , serverNativeSSL .getServerRoot () + "/resources/security/outboundKeys.p12" );
116
118
postgre .copyFileFromContainer ("/var/lib/postgresql/server.crt" , serverNativeSSL .getServerRoot () + "/resources/security/server.crt" );
@@ -120,51 +122,6 @@ public static void setUp() throws Exception {
120
122
serverNativeSSL .startServer ();
121
123
}
122
124
123
- private static void importServerCert (String source , String serverCert ) {
124
- final String m = "importServerCert" ;
125
-
126
- String [] command = new String [] {
127
- "keytool" , "-import" , //
128
- "-alias" , "server" , //
129
- "-file" , serverCert , //
130
- "-keystore" , source , //
131
- "-storetype" , "pkcs12" , //
132
- "-storepass" , "liberty" , //
133
- "-noprompt"
134
- };
135
-
136
- String errorPrelude = "Could not import server certificate into client keystore: " + source ;
137
- try {
138
- Process p = Runtime .getRuntime ().exec (command );
139
- if (!p .waitFor (FATRunner .FAT_TEST_LOCALRUN ? 10 : 20 , TimeUnit .SECONDS )) {
140
- p .destroyForcibly ();
141
- dumpOutput (m , "Keytool process timed out" , p );
142
- throw new RuntimeException (errorPrelude + " timed out waiting for process to finish." );
143
- }
144
- if (p .exitValue () != 0 ) {
145
- dumpOutput (m , "Non 0 exit code from keytool" , p );
146
- throw new RuntimeException (errorPrelude + " see logs for details" );
147
- }
148
- dumpOutput (m , "Keytool command completed successfully" , p );
149
- } catch (InterruptedException | IOException e ) {
150
- throw new RuntimeException (errorPrelude , e );
151
- }
152
- }
153
-
154
- private static void dumpOutput (String method , String message , Process p ) {
155
- String out = "stdOut:" + System .lineSeparator () + readInputStream (p .getInputStream ());
156
- String err = "stdErr:" + System .lineSeparator () + readInputStream (p .getErrorStream ());
157
- Log .info (c , method , message + //
158
- System .lineSeparator () + out + //
159
- System .lineSeparator () + err );
160
- }
161
-
162
- private static String readInputStream (InputStream is ) {
163
- @ SuppressWarnings ("resource" )
164
- Scanner s = new Scanner (is ).useDelimiter ("\\ A" );
165
- return s .hasNext () ? s .next () : "" ;
166
- }
167
-
168
125
@ AfterClass
169
126
public static void tearDown () throws Exception {
170
127
try {
0 commit comments