Skip to content

Commit a8d68c3

Browse files
committed
Cleaned-up logging for client-cert test
1 parent c724aa4 commit a8d68c3

File tree

2 files changed

+84
-61
lines changed

2 files changed

+84
-61
lines changed

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

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.ArrayList;
3333
import java.util.Date;
3434
import java.util.List;
35-
import java.util.logging.Level;
3635
import java.util.logging.Logger;
3736

3837
import javax.net.ssl.SSLContext;
@@ -84,6 +83,8 @@ public class SecureServletTest {
8483
@Deployment(testable = false)
8584
public static WebArchive createDeployment() throws FileNotFoundException, IOException {
8685

86+
System.out.println("\n*********** DEPLOYMENT START ***************************");
87+
8788
Provider provider = new BouncyCastleProvider();
8889
Security.addProvider(provider);
8990

@@ -114,93 +115,78 @@ public static WebArchive createDeployment() throws FileNotFoundException, IOExce
114115
addCertificateToContainerTrustStore(clientCertificate);
115116

116117
return create(WebArchive.class)
117-
.addClass(SecureServlet.class)
118+
.addClasses(SecureServlet.class)
118119
.addAsWebInfResource((new File(WEBAPP_SRC + "/WEB-INF", "web.xml")))
119120
.addAsWebInfResource((new File(WEBAPP_SRC + "/WEB-INF", "glassfish-web.xml")));
120121
}
121122

122123
@Before
123124
public void setup() throws FileNotFoundException, IOException {
125+
126+
System.out.println("\n*********** SETUP START ***************************");
127+
128+
webClient = new WebClient();
124129

125-
// ### Ask the server for its certificate and add that to a new local trust store
126-
127-
// First get the HTTPS url for which the server is listening
130+
// First get the HTTPS URL for which the server is listening
128131
baseHttps = ServerOperations.toContainerHttps(base);
132+
if (baseHttps == null) {
133+
throw new IllegalStateException("No https URL could be created from " + base);
134+
}
129135

130-
System.out.println("***************************************");
131-
132-
if (baseHttps != null) {
133-
System.out.println("Created " + baseHttps);
134-
X509Certificate[] serverCertificateChain = getCertificateChainFromServer(baseHttps.getHost(), baseHttps.getPort());
136+
137+
138+
// ### Ask the server for its certificate and add that to a new local trust store
139+
140+
// Server -> client : the trust store certificates are used to validate the certificate sent
141+
// by the server
142+
X509Certificate[] serverCertificateChain = getCertificateChainFromServer(baseHttps.getHost(), baseHttps.getPort());
143+
144+
if (serverCertificateChain != null && serverCertificateChain.length > 0) {
145+
146+
System.out.println("Obtained certificate from server. Storing it in client trust store");
147+
135148
createTrustStore(serverCertificateChain);
149+
150+
String trustStorePath = System.getProperty("buildDirectory", "") + "/clientTrustStore.jks";
151+
System.out.println("Reading trust store from: " + trustStorePath);
152+
153+
webClient.getOptions().setSSLTrustStore(new File(trustStorePath).toURI().toURL(), "changeit", "jks");
136154

155+
// If the use.cnHost property is we try to extract the host from the server
156+
// certificate and use exactly that host for our requests.
157+
// This is needed if a server is listening to multiple host names, for instance
158+
// localhost and example.com. If the certificate is for example.com, we can't
159+
// localhost for the request, as that will not be accepted.
137160
if (System.getProperty("use.cnHost") != null) {
138-
if (serverCertificateChain != null && serverCertificateChain.length > 0) {
139-
X509Certificate firstCert = serverCertificateChain[0];
140-
String name = firstCert.getIssuerX500Principal().getName();
141-
System.out.println("Full certificate issuer name " + name);
142-
String[] names = name.split(",");
143-
// cn should be first
144-
if (names != null && names.length > 0) {
145-
String cnNameString = names[0];
146-
String cn = cnNameString.substring(cnNameString.indexOf('=') + 1).trim();
147-
System.out.println("Issuer CN name " + cn);
148-
149-
try {
150-
URL httpsUrl = new URL(
151-
baseHttps.getProtocol(),
152-
cn,
153-
baseHttps.getPort(),
154-
baseHttps.getFile()
155-
);
156-
157-
System.out.println("Changing to " + httpsUrl + " from " + baseHttps);
158-
159-
baseHttps = httpsUrl;
160-
161-
} catch (MalformedURLException e) {
162-
System.out.println("Failure creating HTTPS URL");
163-
e.printStackTrace();
164-
}
165-
166-
}
167-
168-
}
161+
System.out.println("use.cnHost set. Trying to grab CN from certificate and use as host for requests.");
162+
baseHttps = getHostFromCertificate(serverCertificateChain, baseHttps);
169163
}
170-
171164
} else {
172-
System.out.println("No https URL could be created from " + base);
165+
System.out.println("Could not obtain certificates from server. Continuing without custom truststore");
173166
}
174-
175-
176-
webClient = new WebClient();
177-
178-
// Server -> client : the trust store certificates are used to validate the certificate sent
179-
// by the server
180-
181-
String trustStorePath = System.getProperty("buildDirectory", "") + "/clientTrustStore.jks";
182-
System.out.println("Reading trust store from: " + trustStorePath);
183-
184-
webClient.getOptions().setSSLTrustStore(new File(trustStorePath).toURI().toURL(), "changeit", "jks");
185-
167+
186168
String keyStorePath = System.getProperty("buildDirectory", "") + "/clientKeyStore.jks";
187169
System.out.println("Reading key store from: " + keyStorePath);
188170

189171
// Client -> Server : the key store private keys and certificates are used to sign
190172
// and sent a reply to the server
191173
webClient.getOptions().setSSLClientCertificate(new File(keyStorePath).toURI().toURL(), "changeit", "jks");
192-
193174

175+
System.out.println("*********** SETUP DONE ***************************\n");
194176
}
195177

196178
@After
197179
public void tearDown() {
198180
webClient.getCookieManager().clearCookies();
199181
webClient.close();
182+
System.out.println("\n*********** TEST END ***************************\n");
200183
}
201184

202185
@Test
203186
public void testGetWithCorrectCredentials() throws Exception {
187+
188+
System.out.println("\n*********** TEST START ***************************\n");
189+
204190
try {
205191
TextPage page = webClient.getPage(baseHttps + "SecureServlet");
206192

@@ -316,7 +302,7 @@ private static void createKeyStore(PrivateKey privateKey, X509Certificate certif
316302

317303
String path = System.getProperty("buildDirectory", "") + "/clientKeyStore.jks";
318304

319-
System.out.println("Storing key store at: " + path);
305+
System.out.println("Storing client key store at: " + path);
320306

321307
keyStore.store(new FileOutputStream(path), "changeit".toCharArray());
322308
} catch (Exception ex) {
@@ -343,6 +329,43 @@ private static void createTrustStore(X509Certificate[] certificates) {
343329
}
344330
}
345331

332+
private static URL getHostFromCertificate(X509Certificate[] serverCertificateChain, URL existingURL) {
333+
X509Certificate firstCert = serverCertificateChain[0];
334+
String name = firstCert.getIssuerX500Principal().getName();
335+
System.out.println("Full certificate issuer name " + name);
336+
337+
String[] names = name.split(",");
338+
339+
// cn should be first
340+
if (names != null && names.length > 0) {
341+
String cnNameString = names[0];
342+
String cn = cnNameString.substring(cnNameString.indexOf('=') + 1).trim();
343+
System.out.println("Issuer CN name: \"" + cn + "\"");
344+
345+
try {
346+
URL httpsUrl = new URL(
347+
existingURL.getProtocol(),
348+
cn,
349+
existingURL.getPort(),
350+
existingURL.getFile()
351+
);
352+
353+
System.out.println("Changing base URL from " + existingURL + " into " + httpsUrl + "\n");
354+
355+
return httpsUrl;
356+
357+
} catch (MalformedURLException e) {
358+
System.out.println("Failure creating HTTPS URL");
359+
e.printStackTrace();
360+
}
361+
362+
}
363+
364+
System.out.println("FAILED to get CN. Using existing URL: " + existingURL);
365+
366+
return existingURL;
367+
}
368+
346369
private static void enableSSLDebug() {
347370
System.setProperty("javax.net.debug", "ssl:handshake");
348371

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ public static void addCertificateToContainerTrustStore(Certificate clientCertifi
9898
Path cacertsPath = gfHomePath.resolve("glassfish/domains/" + domain + "/config/cacerts.jks");
9999

100100
if (!cacertsPath.toFile().exists()) {
101-
logger.severe("The trust store at " + cacertsPath.toAbsolutePath() + " does not exists");
101+
logger.severe("The container trust store at " + cacertsPath.toAbsolutePath() + " does not exists");
102+
logger.severe("Is the domain \"" + domain + "\" correct?");
102103
return;
103104
}
104105

105-
logger.info("*** Adding certificate to: " + cacertsPath.toAbsolutePath());
106+
logger.info("*** Adding certificate to container trust store: " + cacertsPath.toAbsolutePath());
106107

107108
KeyStore keyStore = null;
108109
try (InputStream in = new FileInputStream(cacertsPath.toAbsolutePath().toFile())) {
@@ -145,8 +146,7 @@ public static URL toContainerHttps(URL url) {
145146
url.getFile()
146147
);
147148

148-
System.out.println("Returning " + httpsUrl + " for " + url);
149-
logger.info("Returning " + httpsUrl + " for " + url);
149+
System.out.println("Changing base URL from " + url + " into " + httpsUrl);
150150

151151
return httpsUrl;
152152

0 commit comments

Comments
 (0)