|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2023 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package com.redhat.devtools.intellij.kubernetes.model.dashboard |
| 12 | + |
| 13 | +import io.fabric8.kubernetes.client.http.HttpResponse |
| 14 | +import java.net.HttpURLConnection |
| 15 | +import java.security.SecureRandom |
| 16 | +import java.security.cert.CertificateException |
| 17 | +import java.security.cert.CertificateParsingException |
| 18 | +import java.security.cert.X509Certificate |
| 19 | +import javax.net.ssl.SSLContext |
| 20 | +import javax.net.ssl.SSLHandshakeException |
| 21 | +import javax.net.ssl.TrustManager |
| 22 | +import javax.net.ssl.X509TrustManager |
| 23 | +import okhttp3.OkHttpClient |
| 24 | +import okhttp3.Request |
| 25 | +import okhttp3.Response |
| 26 | + |
| 27 | +class HttpRequest { |
| 28 | + |
| 29 | + companion object { |
| 30 | + private val trustAllTrustManager = object : X509TrustManager { |
| 31 | + |
| 32 | + @Throws(CertificateException::class) |
| 33 | + override fun checkServerTrusted(chain: Array<X509Certificate>?, authType: String?) { |
| 34 | + // ignore aka trust |
| 35 | + } |
| 36 | + |
| 37 | + override fun getAcceptedIssuers(): Array<X509Certificate> { |
| 38 | + return emptyArray() |
| 39 | + } |
| 40 | + |
| 41 | + @Throws(CertificateException::class) |
| 42 | + override fun checkClientTrusted(chain: Array<X509Certificate>?, authType: String?) { |
| 43 | + // ignore aka trust |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fun request(url: String): HttpStatusCode { |
| 49 | + return requestHttpStatusCode(url) |
| 50 | + } |
| 51 | + |
| 52 | + fun request(host: String, port: Int): HttpStatusCode { |
| 53 | + var status = requestHttpStatusCode("http://$host:$port") |
| 54 | + if (status.isSuccessful) { |
| 55 | + return status |
| 56 | + } else { |
| 57 | + status = requestHttpStatusCode("https://$host:$port") |
| 58 | + if (status.isSuccessful) { |
| 59 | + return status |
| 60 | + } |
| 61 | + } |
| 62 | + return status |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Requests the https status code for the given url. |
| 67 | + * Return [HttpStatusCode] and throws if connecting fails. |
| 68 | + * |
| 69 | + * @param url the url to request the http status code for |
| 70 | + * |
| 71 | + * The implementation is ignores (private) SSL certificates and doesn't verify the hostname. |
| 72 | + * All that matters is whether we can connect successfully or not. |
| 73 | + * OkHttp is used because it allows to set a [javax.net.ssl.HostnameVerifier] on a per connection base. |
| 74 | + */ |
| 75 | + private fun requestHttpStatusCode(url: String): HttpStatusCode { |
| 76 | + val sslContext = createSSLContext() |
| 77 | + var response: Response? = null |
| 78 | + try { |
| 79 | + response = OkHttpClient.Builder() |
| 80 | + .sslSocketFactory(sslContext.socketFactory, trustAllTrustManager) |
| 81 | + .hostnameVerifier { _, _ -> true } |
| 82 | + .build() |
| 83 | + .newCall( |
| 84 | + Request.Builder() |
| 85 | + .url(url) |
| 86 | + .build() |
| 87 | + ) |
| 88 | + .execute() |
| 89 | + return HttpStatusCode(url, response.code) |
| 90 | + } catch (e: SSLHandshakeException) { |
| 91 | + if (e.cause is CertificateParsingException) { |
| 92 | + /** |
| 93 | + * Fake 200 OK response in case ssl handshake certificate could not be parsed. |
| 94 | + * This happens with azure dashboard where a certificate is used that the jdk cannot handle: |
| 95 | + * ``` |
| 96 | + * javax.net.ssl.SSLHandshakeException: Failed to parse server certificates |
| 97 | + * java.security.cert.CertificateParsingException: Empty issuer DN not allowed in X509Certificates |
| 98 | + * ``` |
| 99 | + * @see [Stackoverflow question 65692099](https://stackoverflow.com/questions/65692099/java-empty-issuer-dn-not-allowed-in-x509certificate-libimobiledevice-implementa) |
| 100 | + * @see [kubernetes cert-manager issue #3634](https://github.com/cert-manager/cert-manager/issues/3634) |
| 101 | + */ |
| 102 | + return HttpStatusCode(url, HttpURLConnection.HTTP_OK) |
| 103 | + } else { |
| 104 | + throw e |
| 105 | + } |
| 106 | + } finally { |
| 107 | + response?.close() |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private fun createSSLContext(): SSLContext { |
| 112 | + val sslContext: SSLContext = SSLContext.getDefault() |
| 113 | + sslContext.init(null, arrayOf<TrustManager>(trustAllTrustManager), SecureRandom()) |
| 114 | + return sslContext |
| 115 | + } |
| 116 | + |
| 117 | + class HttpStatusCode(val url: String, val status: Int?) { |
| 118 | + val isSuccessful: Boolean |
| 119 | + get() { |
| 120 | + return status != null |
| 121 | + && HttpResponse.isSuccessful(status) |
| 122 | + } |
| 123 | + val isForbidden: Boolean |
| 124 | + get() { |
| 125 | + return status != null |
| 126 | + && HttpURLConnection.HTTP_FORBIDDEN == status |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments