Adding a function that disables SSL validation in a Groovy script can be especially useful in insecure test environments or when the validation process is undesirable. However, this function should be used with caution and only when necessary, as misuse can lead to security vulnerabilities.
The following disableSSLVerification
function is designed to disable SSL verification using Groovy. This function allows connections to pass through without validating SSL certificates and hostnames during connections.
The PKIX error is bypassed by adding the following method and calling it before opening the connection.
import javax.net.ssl.*
import java.security.cert.X509Certificate
def disableSSLVerification() {
TrustManager[] trustAllCerts = [
[
checkClientTrusted: { X509Certificate[] certs, String authType -> },
checkServerTrusted: { X509Certificate[] certs, String authType -> },
getAcceptedIssuers: { null }
] as X509TrustManager
]
SSLContext sc = SSLContext.getInstance("SSL")
sc.init(null, trustAllCerts, new java.security.SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
HttpsURLConnection.setDefaultHostnameVerifier({ hostname, session -> true } as HostnameVerifier)
}
GROOVY