Skip to content

Commit 578560a

Browse files
committed
Fixed ordering of trust store creation and added logging for client-cert
1 parent c47bf21 commit 578560a

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

servlet/security-clientcert/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<artifactId>maven-surefire-plugin</artifactId>
3636
<configuration>
3737
<skipTests>${skipServletClientCertificate}</skipTests>
38+
<systemPropertyVariables>
39+
<buildDirectory>${project.build.directory}</buildDirectory>
40+
</systemPropertyVariables>
3841
</configuration>
3942
</plugin>
4043
</plugins>

servlet/security-clientcert/src/test/java/org/javaee7/servlet/security/clientcert/SecureServletTest.java

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,39 @@ public static WebArchive createDeployment() throws FileNotFoundException, IOExce
107107

108108
@Before
109109
public void setup() throws FileNotFoundException, IOException {
110+
111+
// ### Ask the server for its certificate and add that to a new local trust store
112+
113+
// First get the HTTPS url for which the server is listening
114+
baseHttps = ServerOperations.toContainerHttps(base);
115+
116+
System.out.println("***************************************");
117+
118+
if (baseHttps != null) {
119+
System.out.println("Created " + baseHttps);
120+
X509Certificate[] serverCertificateChain = getCertificateChainFromServer(baseHttps.getHost(), baseHttps.getPort());
121+
createTrustStore(serverCertificateChain);
122+
} else {
123+
System.out.println("No https URL could be created from " + base);
124+
}
125+
110126

111127
webClient = new WebClient();
112128

113129
// Server -> client : the trust store certificates are used to validate the certificate sent
114130
// by the server
115-
webClient.getOptions().setSSLTrustStore(new File("clientTrustStore.jks").toURI().toURL(), "changeit", "jks");
131+
132+
String trustStorePath = System.getProperty("buildDirectory", "") + "/clientTrustStore.jks";
133+
System.out.println("Reading trust store from: " + trustStorePath);
134+
135+
webClient.getOptions().setSSLTrustStore(new File(trustStorePath).toURI().toURL(), "changeit", "jks");
136+
137+
String keyStorePath = System.getProperty("buildDirectory", "") + "/clientKeyStore.jks";
138+
System.out.println("Reading key store from: " + keyStorePath);
116139

117140
// Client -> Server : the key store private keys and certificates are used to sign
118141
// and sent a reply to the server
119-
webClient.getOptions().setSSLClientCertificate(new File("clientKeyStore.jks").toURI().toURL(), "changeit", "jks");
142+
webClient.getOptions().setSSLClientCertificate(new File(keyStorePath).toURI().toURL(), "changeit", "jks");
120143

121144
}
122145

@@ -128,23 +151,16 @@ public void tearDown() {
128151

129152
@Test
130153
public void testGetWithCorrectCredentials() throws Exception {
131-
// ### Ask the server for its certificate and add that to a new local trust store
132-
133-
// First get the HTTPS url for which the server is listening
134-
baseHttps = ServerOperations.toContainerHttps(base);
135-
136-
if (baseHttps != null) {
137-
X509Certificate[] serverCertificateChain = getCertificateChainFromServer(baseHttps.getHost(), baseHttps.getPort());
138-
createTrustStore(serverCertificateChain);
139-
} else {
140-
log.severe("No https URL could be created from " + base);
154+
try {
155+
TextPage page = webClient.getPage(baseHttps + "SecureServlet");
156+
157+
log.info(page.getContent());
158+
159+
assertTrue("my GET", page.getContent().contains("principal C=UK, ST=lak, L=zak, O=kaz, OU=bar, CN=lfoo"));
160+
} catch (Exception e) {
161+
e.printStackTrace();
162+
throw e;
141163
}
142-
143-
TextPage page = webClient.getPage(baseHttps + "SecureServlet");
144-
145-
log.info(page.getContent());
146-
147-
assertTrue("my GET", page.getContent().contains("principal C=UK, ST=lak, L=zak, O=kaz, OU=bar, CN=lfoo"));
148164
}
149165

150166

@@ -244,8 +260,12 @@ private static void createKeyStore(PrivateKey privateKey, X509Certificate certif
244260
"clientKey",
245261
new PrivateKeyEntry(privateKey, new Certificate[] { certificate }),
246262
new PasswordProtection("changeit".toCharArray()));
263+
264+
String path = System.getProperty("buildDirectory", "") + "/clientKeyStore.jks";
265+
266+
System.out.println("Storing key store at: " + path);
247267

248-
keyStore.store(new FileOutputStream("clientKeyStore.jks"), "changeit".toCharArray());
268+
keyStore.store(new FileOutputStream(path), "changeit".toCharArray());
249269
} catch (Exception ex) {
250270
ex.printStackTrace();
251271
}
@@ -259,8 +279,12 @@ private static void createTrustStore(X509Certificate[] certificates) {
259279
for (int i = 0; i < certificates.length; i++) {
260280
keyStore.setCertificateEntry("serverCert" + i, certificates[i]);
261281
}
282+
283+
String path = System.getProperty("buildDirectory", "") + "/clientTrustStore.jks";
284+
285+
System.out.println("Storing trust store at: " + path);
262286

263-
keyStore.store(new FileOutputStream("clientTrustStore.jks"), "changeit".toCharArray());
287+
keyStore.store(new FileOutputStream(path), "changeit".toCharArray());
264288
} catch (Exception ex) {
265289
ex.printStackTrace();
266290
}

test-utils/src/main/java/org/javaee7/ServerOperations.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ public static URL toContainerHttps(URL url) {
145145
url.getFile()
146146
);
147147

148+
System.out.println("Returning " + httpsUrl + " for " + url);
148149
logger.info("Returning " + httpsUrl + " for " + url);
149150

150151
return httpsUrl;
151152

152153
} catch (MalformedURLException e) {
154+
System.out.println("Failure creating HTTPS URL");
155+
e.printStackTrace();
153156
logger.log(Level.SEVERE, "Failure creating HTTPS URL", e);
154157
}
155158

0 commit comments

Comments
 (0)