Skip to content

Commit 74fd2db

Browse files
feat: disable ssl certificates check on dev environments
1 parent 351a701 commit 74fd2db

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/main/kotlin/app/Main.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ import app.utils.RepoHelper
1818
import app.utils.UiHelper
1919
import com.beust.jcommander.JCommander
2020
import com.beust.jcommander.MissingCommandException
21+
import java.security.GeneralSecurityException
22+
import javax.net.ssl.TrustManager
23+
import javax.net.ssl.X509TrustManager
24+
import java.security.cert.X509Certificate
25+
import javax.net.ssl.HttpsURLConnection
26+
import javax.net.ssl.SSLContext
2127

2228
fun main(argv : Array<String>) {
2329
Thread.setDefaultUncaughtExceptionHandler { _, e: Throwable ->
2430
Logger.error(e, "Uncaught exception")
2531
}
32+
if (BuildConfig.ENV != "production") {
33+
disableSslChecks()
34+
}
2635
Main(argv)
2736
}
2837

@@ -147,3 +156,26 @@ class Main(argv: Array<String>) {
147156
jc.usage() // Will show detailed info about usage based on annotations.
148157
}
149158
}
159+
160+
fun disableSslChecks() {
161+
// Create a trust manager that does not validate certificate chains.
162+
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
163+
override fun getAcceptedIssuers(): Array<X509Certificate> {
164+
return arrayOf()
165+
}
166+
override fun checkClientTrusted(certs: Array<X509Certificate>,
167+
authType: String) {}
168+
override fun checkServerTrusted(certs: Array<X509Certificate>,
169+
authType: String) {}
170+
})
171+
172+
// Install the all-trusting trust manager.
173+
try {
174+
val sc = SSLContext.getInstance("SSL")
175+
sc.init(null, trustAllCerts, java.security.SecureRandom())
176+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
177+
} catch (e: GeneralSecurityException) {}
178+
179+
// Skip server name checking.
180+
HttpsURLConnection.setDefaultHostnameVerifier { _, _ -> true }
181+
}

0 commit comments

Comments
 (0)