Skip to content

Commit 2475274

Browse files
authored
Merge pull request #1570 from marklogic/feature/ssl-test
Added test for a custom X509TrustManager
2 parents 96a2c76 + 02c832d commit 2475274

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

marklogic-client-api/src/test/java/com/marklogic/client/test/CheckSSLConnectionTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import javax.net.ssl.SSLContext;
1212
import javax.net.ssl.SSLException;
13-
import javax.net.ssl.SSLHandshakeException;
1413
import javax.net.ssl.TrustManager;
1514

1615
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -54,6 +53,27 @@ void trustAllManager() throws Exception {
5453
assertNull(result.getErrorMessage());
5554
}
5655

56+
/**
57+
* Demonstrates using a custom X509TrustManager that only accepts the issuer of the public certificate associated
58+
* with the certificate template created via RequireSSLExtension.
59+
*/
60+
@Test
61+
void customTrustManager() {
62+
if (Common.USE_REVERSE_PROXY_SERVER) {
63+
return;
64+
}
65+
66+
DatabaseClient client = Common.newClientBuilder()
67+
.withSSLProtocol("TLSv1.2")
68+
.withTrustManager(RequireSSLExtension.newTrustManager())
69+
.withSSLHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.ANY)
70+
.build();
71+
72+
DatabaseClient.ConnectionResult result = client.checkConnection();
73+
assertEquals(0, result.getStatusCode());
74+
assertNull(result.getErrorMessage());
75+
}
76+
5777
@Test
5878
void defaultSslContext() throws Exception {
5979
DatabaseClient client = Common.newClientBuilder()

marklogic-client-api/src/test/java/com/marklogic/client/test/junit5/RequireSSLExtension.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
import com.marklogic.mgmt.ManageClient;
66
import com.marklogic.mgmt.resource.appservers.ServerManager;
77
import com.marklogic.mgmt.resource.security.CertificateTemplateManager;
8+
import com.marklogic.rest.util.Fragment;
89
import org.junit.jupiter.api.extension.AfterAllCallback;
910
import org.junit.jupiter.api.extension.BeforeAllCallback;
1011
import org.junit.jupiter.api.extension.ExtensionContext;
1112

13+
import javax.net.ssl.X509TrustManager;
14+
import java.io.ByteArrayInputStream;
15+
import java.security.cert.CertificateException;
16+
import java.security.cert.CertificateFactory;
17+
import java.security.cert.X509Certificate;
18+
1219
/**
1320
* Use this on tests that require an app server to require SSL connections. The app server will be modified to require
1421
* SSL connections before any test runs and will then be restored back to normal after all tests in the test class run.
@@ -52,6 +59,39 @@ public void afterAll(ExtensionContext context) {
5259
new CertificateTemplateManager(manageClient).delete(TEMPLATE);
5360
}
5461

62+
/**
63+
* @return a trust manager that accepts the public certificate associated with the certificate template created
64+
* by this class.
65+
*/
66+
public static X509TrustManager newTrustManager() {
67+
return new X509TrustManager() {
68+
@Override
69+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
70+
}
71+
72+
@Override
73+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
74+
}
75+
76+
@Override
77+
public X509Certificate[] getAcceptedIssuers() {
78+
return new X509Certificate[]{getCertificate()};
79+
}
80+
};
81+
}
82+
83+
private static X509Certificate getCertificate() {
84+
CertificateTemplateManager mgr = new CertificateTemplateManager(Common.newManageClient());
85+
86+
Fragment response = mgr.getCertificatesForTemplate(TEMPLATE_NAME);
87+
String cert = response.getElementValue("/msec:certificate-list/msec:certificate/msec:pem");
88+
try {
89+
return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(cert.getBytes()));
90+
} catch (CertificateException e) {
91+
throw new RuntimeException("Unable to generate X509Certificate: " + e.getMessage(), e);
92+
}
93+
}
94+
5595
private void setSslCertificateTemplate(String templateName) {
5696
new ServerManager(manageClient).save(
5797
Common.newServerPayload()

0 commit comments

Comments
 (0)