sarankumarv sarankumarv - 25 days ago 13
Java Question

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated error

This is a question regarding an exception that is occurring in my code which makes a call to an https server.

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated


It internally uses an instance of
CloseableHttpClient
to execute the PUT call.

Also, this code is a functional test that would be running on a remote machine as a CI job. I have seen some solutions with the SSL certificate error that mention how we can disable the SSL certificate validation in Java or add the certificate in the local JVM, one of them being here -

'peer not authenticated' SSL certificate error usng DefaultHttpClient

Unfortunately, it doesn't seem to be working as this is a remote machine and we cannot import certificates into that machine.

String endPoint = "https://" + hostName + ":" + port + "/v1/service/data/put";


endPoint is set in the code that is called from a jar. So, there is no scope that we'd be able to change it either.

If I am running the code that makes a PUT call to the endPoint from a standalone class (through the main method), it seems to be running fine, returning a 200/OK. Currently, the exception occurs if it is being run as a TestNG class from the .xml file.

The code added as a Github gist is here.

Let me know if you need more details.

Answer

As it was mentioned in the question, the code works fine if it were running in a standalone class, through the main method.

I was able to resolve the issue by placing my code in a static block. It might be related to the certificates being disabled during class load and thus, works fine now.