Skip to content

Commit f2ed031

Browse files
committed
Merge branch 'psk-ixolit-master'
2 parents 6a3e60e + dc0b5e9 commit f2ed031

16 files changed

+102
-55
lines changed

app/controllers/RestController.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RestController @Inject()(val authentication: AuthenticationModule,
4747
case Success(status, body) =>
4848
val data = Json.obj(
4949
"mappings" -> ClusterMapping(body),
50-
"host" -> request.target.host
50+
"host" -> request.target.host.name
5151
)
5252
CerebroResponse(status, data)
5353

app/elastic/HTTPElasticClient.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,13 @@ class HTTPElasticClient @Inject()(client: WSClient) extends ElasticClient {
319319
body: Option[String] = None,
320320
target: ElasticServer,
321321
headers: Seq[(String, String)] = Seq()) = {
322-
val authentication = target.authentication
323-
val url = s"${target.host.replaceAll("/+$", "")}$uri"
322+
val authentication = target.host.authentication
323+
val url = s"${target.host.name.replaceAll("/+$", "")}$uri"
324+
325+
val mergedHeaders = headers ++ target.headers
326+
324327
val request =
325-
authentication.foldLeft(client.url(url).withMethod(method).withHttpHeaders(headers: _*)) {
328+
authentication.foldLeft(client.url(url).withMethod(method).withHttpHeaders(mergedHeaders: _*)) {
326329
case (request, auth) =>
327330
request.withAuth(auth.username, auth.password, WSAuthScheme.BASIC)
328331
}

app/models/CerebroRequest.scala

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object CerebroRequest {
3939
def apply(request: AuthRequest[JsValue], hosts: Hosts): CerebroRequest = {
4040
val body = request.body
4141

42-
val host = (body \ "host").asOpt[String].getOrElse(throw MissingTargetHostException)
42+
val hostName = (body \ "host").asOpt[String].getOrElse(throw MissingTargetHostException)
4343
val username = (body \ "username").asOpt[String]
4444
val password = (body \ "password").asOpt[String]
4545

@@ -48,10 +48,13 @@ object CerebroRequest {
4848
case _ => None
4949
}
5050

51-
val server = hosts.getServer(host) match {
52-
case Some(ElasticServer(h, a)) => ElasticServer(h, a.orElse(requestAuth))
53-
case None => ElasticServer(host, requestAuth)
51+
val server = hosts.getHost(hostName) match {
52+
case Some(host @ Host(h, a, headersWhitelist)) =>
53+
val headers = headersWhitelist.flatMap(headerName => request.headers.get(headerName).map(headerName -> _))
54+
ElasticServer(host.copy(authentication = a.orElse(requestAuth)), headers)
55+
case None => ElasticServer(Host(hostName, requestAuth))
5456
}
57+
5558
CerebroRequest(server, body, request.user)
5659
}
5760

app/models/ElasticServer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package models
22

3-
case class ElasticServer(host: String, authentication: Option[ESAuth] = None)
3+
case class ElasticServer(host: Host, headers: Seq[(String, String)] = Seq.empty)

app/models/Host.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package models
2+
3+
case class Host(name: String, authentication: Option[ESAuth] = None, headersWhitelist: Seq[String] = Seq.empty)

app/models/Hosts.scala

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@ trait Hosts {
1414

1515
def getHostNames(): Seq[String]
1616

17-
def getServer(name: String): Option[ElasticServer]
17+
def getHost(name: String): Option[Host]
1818

1919
}
2020

2121
@Singleton
2222
class HostsImpl @Inject()(config: Configuration) extends Hosts {
2323

24-
val hosts: Map[String, ElasticServer] = Try(config.underlying.getConfigList("hosts").asScala.map(Configuration(_))) match {
24+
val hosts: Map[String, Host] = Try(config.underlying.getConfigList("hosts").asScala.map(Configuration(_))) match {
2525
case Success(hostsConf) => hostsConf.map { hostConf =>
2626
val host = hostConf.getOptional[String]("host").get
2727
val name = hostConf.getOptional[String]("name").getOrElse(host)
2828
val username = hostConf.getOptional[String]("auth.username")
2929
val password = hostConf.getOptional[String]("auth.password")
30+
val headersWhitelist = hostConf.getOptional[Seq[String]](path = "headers-whitelist") .getOrElse(Seq.empty[String])
3031
(username, password) match {
31-
case (Some(username), Some(password)) => (name -> ElasticServer(host, Some(ESAuth(username, password))))
32-
case _ => (name -> ElasticServer(host, None))
32+
case (Some(username), Some(password)) => name -> Host(host, Some(ESAuth(username, password)), headersWhitelist)
33+
case _ => name -> Host(host, None, headersWhitelist)
3334
}
3435
}.toMap
3536
case Failure(_) => Map()
3637
}
3738

3839
def getHostNames() = hosts.keys.toSeq
3940

40-
def getServer(name: String) = hosts.get(name)
41+
def getHost(name: String) = hosts.get(name)
4142

4243
}

conf/application.conf

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ auth = {
6565
hosts = [
6666
#{
6767
# host = "http://localhost:9200"
68-
# name = "Some Cluster"
69-
#},
68+
# name = "Localhost cluster"
69+
# headers-whitelist = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]
70+
#}
7071
# Example of host with authentication
7172
#{
7273
# host = "http://some-authenticated-host:9200"

test/controllers/AnalysisControllerSpec.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers
22

33
import elastic.{ElasticResponse, Success}
4-
import models.ElasticServer
4+
import models.{ElasticServer, Host}
55
import play.api.libs.json.Json
66
import play.api.test.FakeRequest
77
import play.api.test.Helpers._
@@ -30,7 +30,7 @@ object AnalysisControllerSpec extends MockedServices {
3030
|]
3131
""".stripMargin
3232
)
33-
client.getIndices(ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
33+
client.getIndices(ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
3434
val response = route(application, FakeRequest(POST, "/analysis/indices").withBody(Json.obj("host" -> "somehost"))).get
3535
ensure(response, 200, Json.arr("index1", "index2"))
3636
}
@@ -58,7 +58,7 @@ object AnalysisControllerSpec extends MockedServices {
5858
|}
5959
""".stripMargin
6060
)
61-
client.getIndexSettings("foo", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
61+
client.getIndexSettings("foo", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
6262
val response = route(application, FakeRequest(POST, "/analysis/analyzers").withBody(Json.obj("host" -> "somehost", "index" -> "foo"))).get
6363
ensure(response, 200, Json.arr("foo_analyzer"))
6464
}
@@ -83,7 +83,7 @@ object AnalysisControllerSpec extends MockedServices {
8383
|}
8484
""".stripMargin
8585
)
86-
client.getIndexMapping("foo", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
86+
client.getIndexMapping("foo", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
8787
val response = route(application, FakeRequest(POST, "/analysis/fields").withBody(Json.obj("host" -> "somehost", "index" -> "foo"))).get
8888
ensure(response, 200, Json.arr("name"))
8989
}
@@ -104,7 +104,7 @@ object AnalysisControllerSpec extends MockedServices {
104104
|}
105105
""".stripMargin
106106
)
107-
client.analyzeTextByAnalyzer("foo", "bar", "qux", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
107+
client.analyzeTextByAnalyzer("foo", "bar", "qux", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
108108
val params = Json.obj("host" -> "somehost", "index" -> "foo", "analyzer" -> "bar", "text" -> "qux")
109109
val response = route(application, FakeRequest(POST, "/analysis/analyze/analyzer").withBody(params)).get
110110
val expected = Json.parse(
@@ -138,7 +138,7 @@ object AnalysisControllerSpec extends MockedServices {
138138
|}
139139
""".stripMargin
140140
)
141-
client.analyzeTextByField("foo", "bar", "qux", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
141+
client.analyzeTextByField("foo", "bar", "qux", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
142142
val params = Json.obj("host" -> "somehost", "index" -> "foo", "field" -> "bar", "text" -> "qux")
143143
val response = route(application, FakeRequest(POST, "/analysis/analyze/field").withBody(params)).get
144144
val expected = Json.parse(

test/controllers/ClusterChangesControllerSpec.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers
22

33
import elastic.Success
4-
import models.ElasticServer
4+
import models.{ElasticServer, Host}
55
import play.api.libs.json.Json
66
import play.api.test.FakeRequest
77
import play.api.test.Helpers._
@@ -117,9 +117,9 @@ object ClusterChangesControllerSpec extends MockedServices {
117117
|}
118118
""".stripMargin
119119
)
120-
client.executeRequest("GET", "_cluster/state/blocks", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, stateResponse))
121-
client.executeRequest("GET", "_nodes/transport", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse))
122-
client.executeRequest("GET", "_aliases", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, aliasesResponse))
120+
client.executeRequest("GET", "_cluster/state/blocks", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, stateResponse))
121+
client.executeRequest("GET", "_nodes/transport", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, nodesResponse))
122+
client.executeRequest("GET", "_aliases", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, aliasesResponse))
123123
val response = route(application, FakeRequest(POST, "/cluster_changes").withBody(Json.obj("host" -> "somehost"))).get
124124
ensure(response, 200, expectedResponse)
125125
}

test/controllers/ClusterSettingsControllerSpec.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers
22

33
import elastic.Success
4-
import models.ElasticServer
4+
import models.{ElasticServer, Host}
55
import play.api.libs.json.Json
66
import play.api.test.FakeRequest
77
import play.api.test.Helpers._
@@ -28,7 +28,7 @@ object ClusterSettingsControllerSpec extends MockedServices {
2828
|}
2929
""".stripMargin
3030
)
31-
client.getClusterSettings(ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
31+
client.getClusterSettings(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
3232
val response = route(application, FakeRequest(POST, "/cluster_settings").withBody(Json.obj("host" -> "somehost"))).get
3333
ensure(response, 200, expectedResponse)
3434
}
@@ -50,7 +50,7 @@ object ClusterSettingsControllerSpec extends MockedServices {
5050
| "transient": {}
5151
|}
5252
""".stripMargin)
53-
client.saveClusterSettings(body, ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
53+
client.saveClusterSettings(body, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
5454
val response = route(application, FakeRequest(POST, "/cluster_settings/save").withBody(Json.obj("host" -> "somehost", "settings" -> body))).get
5555
ensure(response, 200, expectedResponse)
5656
}

test/controllers/CommonsControllerSpec.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers
22

33
import elastic.Success
4-
import models.ElasticServer
4+
import models.{ElasticServer, Host}
55
import play.api.libs.json.Json
66
import play.api.test.FakeRequest
77
import play.api.test.Helpers._
@@ -36,7 +36,7 @@ class CommonsControllerSpec extends MockedServices {
3636
|]
3737
""".stripMargin
3838
)
39-
client.getIndices(ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
39+
client.getIndices(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
4040
val response = route(application, FakeRequest(POST, "/commons/indices").withBody(Json.obj("host" -> "somehost"))).get
4141
ensure(response, 200, Json.arr("index1", "index2"))
4242
}
@@ -52,7 +52,7 @@ class CommonsControllerSpec extends MockedServices {
5252
""".stripMargin
5353
)
5454
val body = Json.obj("host" -> "somehost", "index" -> "someIndex")
55-
client.getIndexMapping("someIndex", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
55+
client.getIndexMapping("someIndex", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
5656
val response = route(application, FakeRequest(POST, "/commons/get_index_mapping").withBody(body)).get
5757
ensure(response, 200, expectedResponse)
5858
}
@@ -84,7 +84,7 @@ class CommonsControllerSpec extends MockedServices {
8484
""".stripMargin
8585
)
8686
val body = Json.obj("host" -> "somehost", "index" -> "someIndex")
87-
client.getIndexSettings("someIndex", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
87+
client.getIndexSettings("someIndex", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
8888
val response = route(application, FakeRequest(POST, "/commons/get_index_settings").withBody(body)).get
8989
ensure(response, 200, expectedResponse)
9090
}
@@ -524,7 +524,7 @@ class CommonsControllerSpec extends MockedServices {
524524
""".stripMargin
525525
)
526526
val body = Json.obj("host" -> "somehost", "node" -> "someNode")
527-
client.nodeStats("someNode", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
527+
client.nodeStats("someNode", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
528528
val response = route(application, FakeRequest(POST, "/commons/get_node_stats").withBody(body)).get
529529
ensure(response, 200, expectedResponse)
530530
}

test/controllers/IndexSettingsControllerSpec.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers
22

33
import elastic.Success
4-
import models.ElasticServer
4+
import models.{ElasticServer, Host}
55
import play.api.libs.json.Json
66
import play.api.test.FakeRequest
77
import play.api.test.Helpers._
@@ -35,7 +35,7 @@ object IndexSettingsControllerSpec extends MockedServices {
3535
|}
3636
""".stripMargin
3737
)
38-
client.getIndexSettingsFlat("foo", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
38+
client.getIndexSettingsFlat("foo", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
3939
val response = route(application, FakeRequest(POST, "/index_settings").withBody(Json.obj("host" -> "somehost", "index" -> "foo"))).get
4040
ensure(response, 200, expectedResponse)
4141
}
@@ -57,7 +57,7 @@ object IndexSettingsControllerSpec extends MockedServices {
5757
| "acknowledged":true
5858
|}
5959
""".stripMargin)
60-
client.updateIndexSettings("foo", body, ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
60+
client.updateIndexSettings("foo", body, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
6161
val response = route(application, FakeRequest(POST, "/index_settings/update").withBody(Json.obj("host" -> "somehost", "index" -> "foo", "settings" -> body))).get
6262
ensure(response, 200, expectedResponse)
6363
}

0 commit comments

Comments
 (0)