Created
November 3, 2016 22:14
-
-
Save thomastaylor312/80fcb016020e4115aa64320b98fb0017 to your computer and use it in GitHub Desktop.
Disable SSL validation in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nullTrustManager = [ | |
checkClientTrusted: { chain, authType -> }, | |
checkServerTrusted: { chain, authType -> }, | |
getAcceptedIssuers: { null } | |
] | |
def nullHostnameVerifier = [ | |
verify: { hostname, session -> true } | |
] | |
SSLContext sc = SSLContext.getInstance("SSL") | |
sc.init(null, [nullTrustManager as X509TrustManager] as TrustManager[], null) | |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()) | |
HttpsURLConnection.setDefaultHostnameVerifier(nullHostnameVerifier as HostnameVerifier) |
Tried this in a Jenkins pipeline:
groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.checkClientTrusted() is applicable for argument types: (null, null) values: [null, null]
In Jenkins you may need to put @NonCPS in front of your function/method to make this work.
Mine works. Tested on latest v of jenkins.
I managed to do this in jenkins pipeline by :
- put the script above in a @NonCPS function
- disable groovy sandbox and call the function
If you're using okhttp3
client from 3.1.2
onwards, use the following code instead:
import java.security.cert.X509Certificate
def nullTrustManager = [
checkClientTrusted: { chain, authType -> },
checkServerTrusted: { chain, authType -> },
getAcceptedIssuers: { -> new X509Certificate[0] }
]
See this commit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. Works for me. Using in grails restBuilder for integration tests.