|
5 | 5 | import com.marklogic.mgmt.ManageClient;
|
6 | 6 | import com.marklogic.mgmt.resource.appservers.ServerManager;
|
7 | 7 | import com.marklogic.mgmt.resource.security.CertificateTemplateManager;
|
| 8 | +import com.marklogic.rest.util.Fragment; |
8 | 9 | import org.junit.jupiter.api.extension.AfterAllCallback;
|
9 | 10 | import org.junit.jupiter.api.extension.BeforeAllCallback;
|
10 | 11 | import org.junit.jupiter.api.extension.ExtensionContext;
|
11 | 12 |
|
| 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 | + |
12 | 19 | /**
|
13 | 20 | * Use this on tests that require an app server to require SSL connections. The app server will be modified to require
|
14 | 21 | * 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) {
|
52 | 59 | new CertificateTemplateManager(manageClient).delete(TEMPLATE);
|
53 | 60 | }
|
54 | 61 |
|
| 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 | + |
55 | 95 | private void setSslCertificateTemplate(String templateName) {
|
56 | 96 | new ServerManager(manageClient).save(
|
57 | 97 | Common.newServerPayload()
|
|
0 commit comments