Search code examples
javasslcitrus-framework

Is there a way to turn off ssl validation in citrus framework?


I want to test a https (api) client.

But I want to turn off the ssl validation in citrus. Could you please let me know how to turn off the ssl validation?

(httpActionBuilder -> httpActionBuilder 
.client("https://localhost") .send() .get() .header(....) 

this is automatically checking for ssl certificate.

I am getting the following exception :

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException.

Is there any way to turn off ssl validation (just like we disable the ssl validation in postman or anyother framework ).

how will i add the context to the ssl context to the citrus test

http(httpActionBuilder -> httpActionBuilder .client("https://....com") .send() .get() .header(....)


Solution

  • Add following class

    class NonValidatingTrustManager implements X509TrustManager {
    
        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }
    
        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }
    

    And then in Spring @Configuration override default SSLContext by declaring new bean

    @Bean
    public SSLContext sslContext() throws KeyManagementException, NoSuchAlgorithmException {
        SSLContext ctx = SSLContext.getInstance("TLS"); // or try "SSL"
        ctx.init(null, new TrustManager[] { new NonValidatingTrustManager() }, null);
        return ctx;
    }
    

    UPDATE

    For tests you create separate config class and put there overridden SSLContext too:

    @TestConfiguration
    class TestConfig {
    
        @Bean
        public SSLContext sslContext() throws Exception {
            SSLContext ctx = SSLContext.getInstance("TLS"); // or try "SSL"
            ctx.init(null, new TrustManager[] { new NonValidatingTrustManager() }, null);
            return ctx;
        }
    }
    

    And use it in unit test like:

    @SpringBootTest(classes = { TestConfig.class })
    class MyTest {
        ...
    }