Skip to content

Commit

Permalink
Allow forwarding of proxy headers to ES
Browse files Browse the repository at this point in the history
  • Loading branch information
psk-ixolit committed May 2, 2019
1 parent fd046ee commit 4342348
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
5 changes: 4 additions & 1 deletion app/elastic/HTTPElasticClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,11 @@ class HTTPElasticClient @Inject()(client: WSClient) extends ElasticClient {
headers: Seq[(String, String)] = Seq()) = {
val authentication = target.authentication
val url = s"${target.host.replaceAll("/+$", "")}$uri"

val mergedHeaders = headers ++ target.forwardHeaders()

val request =
authentication.foldLeft(client.url(url).withMethod(method).withHttpHeaders(headers: _*)) {
authentication.foldLeft(client.url(url).withMethod(method).withHttpHeaders(mergedHeaders: _*)) {
case (request, auth) =>
request.withAuth(auth.username, auth.password, WSAuthScheme.BASIC)
}
Expand Down
4 changes: 3 additions & 1 deletion app/models/CerebroRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ object CerebroRequest {
}

val server = hosts.getServer(host) match {
case Some(ElasticServer(h, a)) => ElasticServer(h, a.orElse(requestAuth))
case Some(ElasticServer(h, a, forward_header)) => ElasticServer(h, a.orElse(requestAuth), forward_header)
case None => ElasticServer(host, requestAuth)
}

server.setForwardHeader(request.headers)
CerebroRequest(server, body, request.user)
}

Expand Down
17 changes: 16 additions & 1 deletion app/models/ElasticServer.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package models

case class ElasticServer(host: String, authentication: Option[ESAuth] = None)
import play.api.mvc.Headers

case class ElasticServer(host: String, authentication: Option[ESAuth] = None, forwardHeadersNames: Seq[String] = Seq.empty) {

protected var _headers: Seq[(String, String)] = Seq()

def setForwardHeader(headers : Headers) {
for (e <- forwardHeadersNames) {
if (headers.hasHeader(e)) {
_headers = _headers :+ (e, headers.get(e).orNull)
}
}
}

def forwardHeaders(): Seq[(String, String)] = _headers
}
5 changes: 3 additions & 2 deletions app/models/Hosts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class HostsImpl @Inject()(config: Configuration) extends Hosts {
val name = hostConf.getOptional[String]("name").getOrElse(host)
val username = hostConf.getOptional[String]("auth.username")
val password = hostConf.getOptional[String]("auth.password")
val forward_headers = hostConf.getOptional[Seq[String]](path = "forward_headers").getOrElse(Seq.empty[String])
(username, password) match {
case (Some(username), Some(password)) => (name -> ElasticServer(host, Some(ESAuth(username, password))))
case _ => (name -> ElasticServer(host, None))
case (Some(username), Some(password)) => (name -> ElasticServer(host, Some(ESAuth(username, password)), forward_headers))
case _ => (name -> ElasticServer(host, None, forward_headers))
}
}.toMap
case Failure(_) => Map()
Expand Down
5 changes: 3 additions & 2 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ auth = {
hosts = [
#{
# host = "http://localhost:9200"
# name = "Some Cluster"
#},
# name = "Localhost cluster"
# forward_headers = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]
#}
# Example of host with authentication
#{
# host = "http://some-authenticated-host:9200"
Expand Down

0 comments on commit 4342348

Please sign in to comment.