From 0e28ae3c860da3f861665f613c09cc5627d4274e Mon Sep 17 00:00:00 2001 From: kcichonczyk <88378534+kcichonczyk@users.noreply.github.com> Date: Wed, 12 Apr 2023 12:21:04 +0200 Subject: [PATCH] [ACS-4460] add hostname verification flag to test utils (#779) --- .../alfresco/transform/base/MtlsTestUtils.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java b/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java index ecaea580..ed6d5560 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/MtlsTestUtils.java @@ -1,7 +1,9 @@ package org.alfresco.transform.base; +import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.springframework.http.client.ClientHttpRequestFactory; @@ -23,12 +25,18 @@ import java.security.cert.CertificateException; public class MtlsTestUtils { private static final boolean MTLS_ENABLED = Boolean.parseBoolean(System.getProperty("test-mtls-enabled")); + private static final boolean HOSTNAME_VERIFICATION_DISABLED = Boolean.parseBoolean(System.getProperty("test-client-disable-hostname-verification")); public static boolean isMtlsEnabled() { return MTLS_ENABLED; } + public static boolean isHostnameVerificationDisabled() + { + return HOSTNAME_VERIFICATION_DISABLED; + } + public static CloseableHttpClient httpClientWithMtls() throws NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, KeyStoreException, IOException, CertificateException { String keyStoreFile = System.getProperty("test-client-keystore-file"); @@ -53,7 +61,13 @@ public class MtlsTestUtils { SSLContext sslContext = sslContextBuilder.build(); SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext); - return HttpClients.custom().setSSLSocketFactory(sslContextFactory).build(); + + HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLSocketFactory(sslContextFactory); + if(HOSTNAME_VERIFICATION_DISABLED) + { + httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); + } + return httpClientBuilder.build(); } public static RestTemplate restTemplateWithMtls()