33
44package io .prometheus .wls .rest ;
55
6+ import java .io .ByteArrayOutputStream ;
7+ import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .net .ConnectException ;
10+ import java .net .UnknownHostException ;
11+ import java .security .GeneralSecurityException ;
12+ import java .util .ArrayList ;
13+ import java .util .Collection ;
14+ import java .util .List ;
15+ import java .util .Optional ;
16+ import javax .net .ssl .SSLContext ;
17+
618import org .apache .http .Header ;
719import org .apache .http .HttpEntity ;
820import org .apache .http .client .methods .CloseableHttpResponse ;
921import org .apache .http .client .methods .HttpGet ;
1022import org .apache .http .client .methods .HttpPost ;
1123import org .apache .http .client .methods .HttpPut ;
1224import org .apache .http .client .methods .HttpRequestBase ;
25+ import org .apache .http .config .Registry ;
26+ import org .apache .http .config .RegistryBuilder ;
1327import org .apache .http .conn .HttpHostConnectException ;
28+ import org .apache .http .conn .socket .ConnectionSocketFactory ;
29+ import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
30+ import org .apache .http .conn .ssl .NoopHostnameVerifier ;
31+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
1432import org .apache .http .entity .ContentType ;
1533import org .apache .http .entity .StringEntity ;
1634import org .apache .http .impl .client .CloseableHttpClient ;
1735import org .apache .http .impl .client .HttpClientBuilder ;
36+ import org .apache .http .impl .conn .BasicHttpClientConnectionManager ;
1837import org .apache .http .message .BasicHeader ;
19-
20- import java .io .ByteArrayOutputStream ;
21- import java .io .IOException ;
22- import java .io .InputStream ;
23- import java .net .ConnectException ;
24- import java .net .UnknownHostException ;
25- import java .util .ArrayList ;
26- import java .util .Collection ;
27- import java .util .List ;
28- import java .util .Optional ;
38+ import org .apache .http .ssl .SSLContexts ;
39+ import org .apache .http .ssl .TrustStrategy ;
2940
3041import static io .prometheus .wls .rest .ServletConstants .AUTHENTICATION_HEADER ;
3142import static io .prometheus .wls .rest .ServletConstants .COOKIE_HEADER ;
32- import static javax .servlet .http .HttpServletResponse .*;
43+ import static javax .servlet .http .HttpServletResponse .SC_BAD_GATEWAY ;
44+ import static javax .servlet .http .HttpServletResponse .SC_BAD_REQUEST ;
45+ import static javax .servlet .http .HttpServletResponse .SC_FORBIDDEN ;
46+ import static javax .servlet .http .HttpServletResponse .SC_GATEWAY_TIMEOUT ;
47+ import static javax .servlet .http .HttpServletResponse .SC_HTTP_VERSION_NOT_SUPPORTED ;
48+ import static javax .servlet .http .HttpServletResponse .SC_INTERNAL_SERVER_ERROR ;
49+ import static javax .servlet .http .HttpServletResponse .SC_NOT_IMPLEMENTED ;
50+ import static javax .servlet .http .HttpServletResponse .SC_OK ;
51+ import static javax .servlet .http .HttpServletResponse .SC_SERVICE_UNAVAILABLE ;
52+ import static javax .servlet .http .HttpServletResponse .SC_UNAUTHORIZED ;
3353
3454/**
3555 * A production implementation of the web client interface that uses Apache HttpClient code.
3858 */
3959public class WebClientImpl extends WebClient {
4060 private String url ;
41- private List <BasicHeader > addedHeaders = new ArrayList <>();
42- private List <BasicHeader > sessionHeaders = new ArrayList <>();
61+ private final List <BasicHeader > addedHeaders = new ArrayList <>();
62+ private final List <BasicHeader > sessionHeaders = new ArrayList <>();
4363 private String setCookieHeader ;
4464
4565 @ Override
@@ -64,7 +84,7 @@ private String sendRequest(HttpRequestBase request) throws IOException {
6484 return getReply (httpClient , request );
6585 } catch (HttpHostConnectException e ) {
6686 throw new RestPortConnectionException (e .getHost ());
67- } catch (UnknownHostException | ConnectException e ) {
87+ } catch (UnknownHostException | ConnectException | GeneralSecurityException e ) {
6888 throw new WebClientException (e , "Unable to execute %s request to %s" , request .getMethod (), request .getURI ());
6989 }
7090 }
@@ -172,9 +192,12 @@ private String extractSessionCookie(String setCookieHeaderValue) {
172192 return ExporterSession .getSessionCookie (setCookieHeaderValue );
173193 }
174194
175- private CloseableHttpClient createHttpClient () {
195+ private CloseableHttpClient createHttpClient () throws GeneralSecurityException {
196+ SelfSignedCertificateAcceptor acceptor = new SelfSignedCertificateAcceptor ();
176197 return HttpClientBuilder .create ()
177198 .setDefaultHeaders (getDefaultHeaders ())
199+ .setSSLSocketFactory (acceptor .getSslConnectionSocketFactory ())
200+ .setConnectionManager (acceptor .getConnectionManager ())
178201 .build ();
179202 }
180203
@@ -183,4 +206,44 @@ private Collection<? extends Header> getDefaultHeaders() {
183206 headers .addAll (sessionHeaders );
184207 return headers ;
185208 }
209+
210+ static class SelfSignedCertificateAcceptor {
211+ private final SSLConnectionSocketFactory sslConnectionSocketFactory ;
212+ private final Registry <ConnectionSocketFactory > socketFactoryRegistry ;
213+
214+ SelfSignedCertificateAcceptor () throws GeneralSecurityException {
215+ sslConnectionSocketFactory = createSSLConnectionSocketFactory ();
216+ socketFactoryRegistry = createSocketFactoryRegistry ();
217+ }
218+
219+ BasicHttpClientConnectionManager getConnectionManager () {
220+ return new BasicHttpClientConnectionManager (socketFactoryRegistry );
221+ }
222+
223+ SSLConnectionSocketFactory getSslConnectionSocketFactory () {
224+ return sslConnectionSocketFactory ;
225+ }
226+
227+ private SSLConnectionSocketFactory createSSLConnectionSocketFactory () throws GeneralSecurityException {
228+ return new SSLConnectionSocketFactory (createSSLContext (), NoopHostnameVerifier .INSTANCE );
229+ }
230+
231+ private SSLContext createSSLContext () throws GeneralSecurityException {
232+ return SSLContexts .custom ()
233+ .loadTrustMaterial (null , createAcceptingTrustStrategy ())
234+ .build ();
235+ }
236+
237+ private TrustStrategy createAcceptingTrustStrategy () {
238+ return (cert , authType ) -> true ;
239+ }
240+
241+ private Registry <ConnectionSocketFactory > createSocketFactoryRegistry () {
242+ return RegistryBuilder .<ConnectionSocketFactory > create ()
243+ .register ("http" , new PlainConnectionSocketFactory ())
244+ .register ("https" , sslConnectionSocketFactory )
245+ .build ();
246+ }
247+
248+ }
186249}
0 commit comments