From 2c9d0b2dbd2f4a75fcd13fac3d1fc71429326425 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Thu, 26 Mar 2020 16:47:08 +0200 Subject: Trust any certificate by API tests Issue-ID: VID-647 Change-Id: I7ecfe746c57d6a178704b4f3616a637005f8f7c4 Signed-off-by: Ittay Stern --- .../automation/test/utils/InsecureHttpsClient.java | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'vid-automation') diff --git a/vid-automation/src/main/java/vid/automation/test/utils/InsecureHttpsClient.java b/vid-automation/src/main/java/vid/automation/test/utils/InsecureHttpsClient.java index 6106ae4a2..68ebeb122 100644 --- a/vid-automation/src/main/java/vid/automation/test/utils/InsecureHttpsClient.java +++ b/vid-automation/src/main/java/vid/automation/test/utils/InsecureHttpsClient.java @@ -1,25 +1,44 @@ package vid.automation.test.utils; +import javax.net.ssl.SSLContext; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.TrustAllStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; public class InsecureHttpsClient { public static RestTemplate newRestTemplate() { - CloseableHttpClient insecureTLSHttpClient - = HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build(); + + CloseableHttpClient insecureTLSHttpClient = HttpClients.custom() + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSSLContext(trustAllCertificates()) + .build(); + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(insecureTLSHttpClient); return new RestTemplate(factory); } + private static SSLContext trustAllCertificates() { + try { + return new SSLContextBuilder() + .loadTrustMaterial(null, TrustAllStrategy.INSTANCE) + .build(); + } catch (Exception e) { + return ExceptionUtils.rethrow(e); + } + } + public static Client newJaxrsClient() { return ClientBuilder.newBuilder() .hostnameVerifier(NoopHostnameVerifier.INSTANCE) + .sslContext(trustAllCertificates()) .build(); } -- cgit 1.2.3-korg