@@ -3,12 +3,12 @@ package core.health.impl
3
3
4
4
import java .net .{ InetSocketAddress , Socket }
5
5
import java .security .cert .X509Certificate
6
- import javax .net .ssl .{ KeyManager , SSLContext , X509TrustManager }
7
6
7
+ import javax .net .ssl .{ KeyManager , SSLContext , X509TrustManager }
8
8
import akka .actor .{ Actor , PoisonPill }
9
9
import akka .http .scaladsl .settings .ClientConnectionSettings
10
10
import akka .http .scaladsl .client .RequestBuilding
11
- import akka .http .scaladsl .model .{ HttpRequest , HttpResponse }
11
+ import akka .http .scaladsl .model .{ HttpRequest , HttpResponse , headers }
12
12
import akka .http .scaladsl .{ ConnectionContext , Http }
13
13
import akka .stream .{ ActorMaterializer , ActorMaterializerSettings , Materializer }
14
14
import akka .stream .scaladsl .{ Sink , Source }
@@ -150,7 +150,7 @@ class HealthCheckWorkerActor(implicit mat: Materializer) extends Actor with Stri
150
150
val url = s " https:// $host: $port$absolutePath"
151
151
logger.debug(s " Checking the health of [ $url] for instance= ${instance.instanceId} via HTTPS " )
152
152
153
- singleRequest (
153
+ singleRequestHttps (
154
154
RequestBuilding .Get (url),
155
155
check.timeout
156
156
).map { response =>
@@ -169,34 +169,45 @@ class HealthCheckWorkerActor(implicit mat: Materializer) extends Actor with Stri
169
169
}
170
170
171
171
def singleRequest (httpRequest : HttpRequest , timeout : FiniteDuration )(implicit mat : Materializer ): Future [HttpResponse ] = {
172
- if (httpRequest.uri.scheme.equalsIgnoreCase(" https" )) {
173
- // This is only a health check, so we are going to allow _very_ bad SSL configuration.
174
- val disabledSslConfig = AkkaSSLConfig ().mapSettings(s => s.withLoose {
175
- s.loose.withAcceptAnyCertificate(true )
176
- .withAllowLegacyHelloMessages(Some (true ))
177
- .withAllowUnsafeRenegotiation(Some (true ))
178
- .withAllowWeakCiphers(true )
179
- .withAllowWeakProtocols(true )
180
- .withDisableHostnameVerification(true )
181
- .withDisableSNI(true )
182
- })
183
- val authority = httpRequest.uri.authority
184
- val connectionFlow = Http ().outgoingConnectionHttps(
185
- authority.host.toString(),
186
- authority.port,
187
- ConnectionContext .https(disabledSslContext, sslConfig = Some (disabledSslConfig)),
188
- settings = ClientConnectionSettings (system).withIdleTimeout(timeout)
189
- )
190
- Source .single(httpRequest).via(connectionFlow).runWith(Sink .head)
191
- } else {
192
- val authority = httpRequest.uri.authority
193
- val connectionFlow = Http ().outgoingConnection(
194
- authority.host.toString(),
195
- authority.port,
196
- settings = ClientConnectionSettings (system).withIdleTimeout(timeout)
197
- )
198
- Source .single(httpRequest).via(connectionFlow).runWith(Sink .head)
199
- }
172
+ val host = httpRequest.uri.authority.host.toString()
173
+ val port = httpRequest.uri.effectivePort
174
+ val hostHeader = headers.Host (host, port)
175
+ val effectiveRequest = httpRequest
176
+ .withUri(httpRequest.uri.toHttpRequestTargetOriginForm)
177
+ .withDefaultHeaders(hostHeader)
178
+
179
+ val connectionFlow = Http ().outgoingConnection(
180
+ host,
181
+ port,
182
+ settings = ClientConnectionSettings (system).withIdleTimeout(timeout)
183
+ )
184
+ Source .single(effectiveRequest).via(connectionFlow).runWith(Sink .head)
185
+ }
186
+
187
+ def singleRequestHttps (httpRequest : HttpRequest , timeout : FiniteDuration )(implicit mat : Materializer ): Future [HttpResponse ] = {
188
+ val host = httpRequest.uri.authority.host.toString()
189
+ val port = httpRequest.uri.effectivePort
190
+ val hostHeader = headers.Host (host, port)
191
+ val effectiveRequest = httpRequest
192
+ .withUri(httpRequest.uri.toHttpRequestTargetOriginForm)
193
+ .withDefaultHeaders(hostHeader)
194
+ // This is only a health check, so we are going to allow _very_ bad SSL configuration.
195
+ val disabledSslConfig = AkkaSSLConfig ().mapSettings(s => s.withLoose {
196
+ s.loose.withAcceptAnyCertificate(true )
197
+ .withAllowLegacyHelloMessages(Some (true ))
198
+ .withAllowUnsafeRenegotiation(Some (true ))
199
+ .withAllowWeakCiphers(true )
200
+ .withAllowWeakProtocols(true )
201
+ .withDisableHostnameVerification(true )
202
+ .withDisableSNI(true )
203
+ })
204
+ val connectionFlow = Http ().outgoingConnectionHttps(
205
+ host,
206
+ port,
207
+ ConnectionContext .https(disabledSslContext, sslConfig = Some (disabledSslConfig)),
208
+ settings = ClientConnectionSettings (system).withIdleTimeout(timeout)
209
+ )
210
+ Source .single(effectiveRequest).via(connectionFlow).runWith(Sink .head)
200
211
}
201
212
}
202
213
0 commit comments