From 4342348b475536c334019a90a71861819789b852 Mon Sep 17 00:00:00 2001
From: Peter Spiess-Knafl
Date: Fri, 15 Mar 2019 11:41:39 +0100
Subject: [PATCH 1/2] Allow forwarding of proxy headers to ES
---
app/elastic/HTTPElasticClient.scala | 5 ++++-
app/models/CerebroRequest.scala | 4 +++-
app/models/ElasticServer.scala | 17 ++++++++++++++++-
app/models/Hosts.scala | 5 +++--
conf/application.conf | 5 +++--
5 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/app/elastic/HTTPElasticClient.scala b/app/elastic/HTTPElasticClient.scala
index 80a05c21..3ab32279 100644
--- a/app/elastic/HTTPElasticClient.scala
+++ b/app/elastic/HTTPElasticClient.scala
@@ -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)
}
diff --git a/app/models/CerebroRequest.scala b/app/models/CerebroRequest.scala
index 829cc220..5e128e50 100644
--- a/app/models/CerebroRequest.scala
+++ b/app/models/CerebroRequest.scala
@@ -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)
}
diff --git a/app/models/ElasticServer.scala b/app/models/ElasticServer.scala
index 57c16cde..3fa30182 100644
--- a/app/models/ElasticServer.scala
+++ b/app/models/ElasticServer.scala
@@ -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
+}
diff --git a/app/models/Hosts.scala b/app/models/Hosts.scala
index 05160783..3f203913 100644
--- a/app/models/Hosts.scala
+++ b/app/models/Hosts.scala
@@ -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()
diff --git a/conf/application.conf b/conf/application.conf
index a16d1297..e74695c8 100644
--- a/conf/application.conf
+++ b/conf/application.conf
@@ -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"
From dc0b5e9506c6a8b70812fb022911d4338cb842b0 Mon Sep 17 00:00:00 2001
From: moliware
Date: Tue, 21 May 2019 14:57:18 +0200
Subject: [PATCH 2/2] Refactor the way of loading hosts from configuration. Add
test case for CerebroRequest
---
app/controllers/RestController.scala | 2 +-
app/elastic/HTTPElasticClient.scala | 6 ++--
app/models/CerebroRequest.scala | 11 +++---
app/models/ElasticServer.scala | 17 +--------
app/models/Host.scala | 3 ++
app/models/Hosts.scala | 12 +++----
conf/application.conf | 2 +-
test/controllers/AnalysisControllerSpec.scala | 12 +++----
.../ClusterChangesControllerSpec.scala | 8 ++---
.../ClusterSettingsControllerSpec.scala | 6 ++--
test/controllers/CommonsControllerSpec.scala | 10 +++---
.../IndexSettingsControllerSpec.scala | 6 ++--
test/controllers/OverviewControllerSpec.scala | 24 ++++++-------
test/controllers/RestControllerSpec.scala | 4 +--
.../controllers/TemplatesControllerSpec.scala | 8 ++---
test/models/CerebroRequestSpec.scala | 36 +++++++++++++++++++
16 files changed, 96 insertions(+), 71 deletions(-)
create mode 100644 app/models/Host.scala
create mode 100644 test/models/CerebroRequestSpec.scala
diff --git a/app/controllers/RestController.scala b/app/controllers/RestController.scala
index 5246a7e1..92d0a8df 100644
--- a/app/controllers/RestController.scala
+++ b/app/controllers/RestController.scala
@@ -45,7 +45,7 @@ class RestController @Inject()(val authentication: AuthenticationModule,
case Success(status, body) =>
val data = Json.obj(
"mappings" -> ClusterMapping(body),
- "host" -> request.target.host
+ "host" -> request.target.host.name
)
CerebroResponse(status, data)
diff --git a/app/elastic/HTTPElasticClient.scala b/app/elastic/HTTPElasticClient.scala
index 3ab32279..fd1ffab8 100644
--- a/app/elastic/HTTPElasticClient.scala
+++ b/app/elastic/HTTPElasticClient.scala
@@ -319,10 +319,10 @@ class HTTPElasticClient @Inject()(client: WSClient) extends ElasticClient {
body: Option[String] = None,
target: ElasticServer,
headers: Seq[(String, String)] = Seq()) = {
- val authentication = target.authentication
- val url = s"${target.host.replaceAll("/+$", "")}$uri"
+ val authentication = target.host.authentication
+ val url = s"${target.host.name.replaceAll("/+$", "")}$uri"
- val mergedHeaders = headers ++ target.forwardHeaders()
+ val mergedHeaders = headers ++ target.headers
val request =
authentication.foldLeft(client.url(url).withMethod(method).withHttpHeaders(mergedHeaders: _*)) {
diff --git a/app/models/CerebroRequest.scala b/app/models/CerebroRequest.scala
index 5e128e50..39585b1a 100644
--- a/app/models/CerebroRequest.scala
+++ b/app/models/CerebroRequest.scala
@@ -39,7 +39,7 @@ object CerebroRequest {
def apply(request: AuthRequest[JsValue], hosts: Hosts): CerebroRequest = {
val body = request.body
- val host = (body \ "host").asOpt[String].getOrElse(throw MissingTargetHostException)
+ val hostName = (body \ "host").asOpt[String].getOrElse(throw MissingTargetHostException)
val username = (body \ "username").asOpt[String]
val password = (body \ "password").asOpt[String]
@@ -48,12 +48,13 @@ object CerebroRequest {
case _ => None
}
- val server = hosts.getServer(host) match {
- case Some(ElasticServer(h, a, forward_header)) => ElasticServer(h, a.orElse(requestAuth), forward_header)
- case None => ElasticServer(host, requestAuth)
+ val server = hosts.getHost(hostName) match {
+ case Some(host @ Host(h, a, headersWhitelist)) =>
+ val headers = headersWhitelist.flatMap(headerName => request.headers.get(headerName).map(headerName -> _))
+ ElasticServer(host.copy(authentication = a.orElse(requestAuth)), headers)
+ case None => ElasticServer(Host(hostName, requestAuth))
}
- server.setForwardHeader(request.headers)
CerebroRequest(server, body, request.user)
}
diff --git a/app/models/ElasticServer.scala b/app/models/ElasticServer.scala
index 3fa30182..f0892190 100644
--- a/app/models/ElasticServer.scala
+++ b/app/models/ElasticServer.scala
@@ -1,18 +1,3 @@
package models
-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
-}
+case class ElasticServer(host: Host, headers: Seq[(String, String)] = Seq.empty)
diff --git a/app/models/Host.scala b/app/models/Host.scala
new file mode 100644
index 00000000..51d5b80a
--- /dev/null
+++ b/app/models/Host.scala
@@ -0,0 +1,3 @@
+package models
+
+case class Host(name: String, authentication: Option[ESAuth] = None, headersWhitelist: Seq[String] = Seq.empty)
diff --git a/app/models/Hosts.scala b/app/models/Hosts.scala
index 3f203913..e6bf82e0 100644
--- a/app/models/Hosts.scala
+++ b/app/models/Hosts.scala
@@ -14,23 +14,23 @@ trait Hosts {
def getHostNames(): Seq[String]
- def getServer(name: String): Option[ElasticServer]
+ def getHost(name: String): Option[Host]
}
@Singleton
class HostsImpl @Inject()(config: Configuration) extends Hosts {
- val hosts: Map[String, ElasticServer] = Try(config.underlying.getConfigList("hosts").asScala.map(Configuration(_))) match {
+ val hosts: Map[String, Host] = Try(config.underlying.getConfigList("hosts").asScala.map(Configuration(_))) match {
case Success(hostsConf) => hostsConf.map { hostConf =>
val host = hostConf.getOptional[String]("host").get
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])
+ val headersWhitelist = hostConf.getOptional[Seq[String]](path = "headers-whitelist") .getOrElse(Seq.empty[String])
(username, password) match {
- case (Some(username), Some(password)) => (name -> ElasticServer(host, Some(ESAuth(username, password)), forward_headers))
- case _ => (name -> ElasticServer(host, None, forward_headers))
+ case (Some(username), Some(password)) => name -> Host(host, Some(ESAuth(username, password)), headersWhitelist)
+ case _ => name -> Host(host, None, headersWhitelist)
}
}.toMap
case Failure(_) => Map()
@@ -38,6 +38,6 @@ class HostsImpl @Inject()(config: Configuration) extends Hosts {
def getHostNames() = hosts.keys.toSeq
- def getServer(name: String) = hosts.get(name)
+ def getHost(name: String) = hosts.get(name)
}
diff --git a/conf/application.conf b/conf/application.conf
index e74695c8..752c5661 100644
--- a/conf/application.conf
+++ b/conf/application.conf
@@ -60,7 +60,7 @@ hosts = [
#{
# host = "http://localhost:9200"
# name = "Localhost cluster"
- # forward_headers = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]
+ # headers-whitelist = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]
#}
# Example of host with authentication
#{
diff --git a/test/controllers/AnalysisControllerSpec.scala b/test/controllers/AnalysisControllerSpec.scala
index fe9bc26a..838d7a8b 100644
--- a/test/controllers/AnalysisControllerSpec.scala
+++ b/test/controllers/AnalysisControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.{ElasticResponse, Success}
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -30,7 +30,7 @@ object AnalysisControllerSpec extends MockedServices {
|]
""".stripMargin
)
- client.getIndices(ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
+ client.getIndices(ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/analysis/indices").withBody(Json.obj("host" -> "somehost"))).get
ensure(response, 200, Json.arr("index1", "index2"))
}
@@ -58,7 +58,7 @@ object AnalysisControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.getIndexSettings("foo", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
+ client.getIndexSettings("foo", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/analysis/analyzers").withBody(Json.obj("host" -> "somehost", "index" -> "foo"))).get
ensure(response, 200, Json.arr("foo_analyzer"))
}
@@ -83,7 +83,7 @@ object AnalysisControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.getIndexMapping("foo", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
+ client.getIndexMapping("foo", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/analysis/fields").withBody(Json.obj("host" -> "somehost", "index" -> "foo"))).get
ensure(response, 200, Json.arr("name"))
}
@@ -104,7 +104,7 @@ object AnalysisControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.analyzeTextByAnalyzer("foo", "bar", "qux", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
+ client.analyzeTextByAnalyzer("foo", "bar", "qux", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
val params = Json.obj("host" -> "somehost", "index" -> "foo", "analyzer" -> "bar", "text" -> "qux")
val response = route(application, FakeRequest(POST, "/analysis/analyze/analyzer").withBody(params)).get
val expected = Json.parse(
@@ -138,7 +138,7 @@ object AnalysisControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.analyzeTextByField("foo", "bar", "qux", ElasticServer("somehost")) returns Future.successful(Success(200, expectedResponse))
+ client.analyzeTextByField("foo", "bar", "qux", ElasticServer(Host("somehost"))) returns Future.successful(Success(200, expectedResponse))
val params = Json.obj("host" -> "somehost", "index" -> "foo", "field" -> "bar", "text" -> "qux")
val response = route(application, FakeRequest(POST, "/analysis/analyze/field").withBody(params)).get
val expected = Json.parse(
diff --git a/test/controllers/ClusterChangesControllerSpec.scala b/test/controllers/ClusterChangesControllerSpec.scala
index a9ee779f..91db2bfb 100644
--- a/test/controllers/ClusterChangesControllerSpec.scala
+++ b/test/controllers/ClusterChangesControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -117,9 +117,9 @@ object ClusterChangesControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.executeRequest("GET", "_cluster/state/blocks", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, stateResponse))
- client.executeRequest("GET", "_nodes/transport", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse))
- client.executeRequest("GET", "_aliases", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, aliasesResponse))
+ client.executeRequest("GET", "_cluster/state/blocks", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, stateResponse))
+ client.executeRequest("GET", "_nodes/transport", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, nodesResponse))
+ client.executeRequest("GET", "_aliases", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, aliasesResponse))
val response = route(application, FakeRequest(POST, "/cluster_changes").withBody(Json.obj("host" -> "somehost"))).get
ensure(response, 200, expectedResponse)
}
diff --git a/test/controllers/ClusterSettingsControllerSpec.scala b/test/controllers/ClusterSettingsControllerSpec.scala
index 8a5140ab..ca94cb0f 100644
--- a/test/controllers/ClusterSettingsControllerSpec.scala
+++ b/test/controllers/ClusterSettingsControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -28,7 +28,7 @@ object ClusterSettingsControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.getClusterSettings(ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.getClusterSettings(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/cluster_settings").withBody(Json.obj("host" -> "somehost"))).get
ensure(response, 200, expectedResponse)
}
@@ -50,7 +50,7 @@ object ClusterSettingsControllerSpec extends MockedServices {
| "transient": {}
|}
""".stripMargin)
- client.saveClusterSettings(body, ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.saveClusterSettings(body, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/cluster_settings/save").withBody(Json.obj("host" -> "somehost", "settings" -> body))).get
ensure(response, 200, expectedResponse)
}
diff --git a/test/controllers/CommonsControllerSpec.scala b/test/controllers/CommonsControllerSpec.scala
index ac7fe73b..b2cbb52c 100644
--- a/test/controllers/CommonsControllerSpec.scala
+++ b/test/controllers/CommonsControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -36,7 +36,7 @@ class CommonsControllerSpec extends MockedServices {
|]
""".stripMargin
)
- client.getIndices(ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.getIndices(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/commons/indices").withBody(Json.obj("host" -> "somehost"))).get
ensure(response, 200, Json.arr("index1", "index2"))
}
@@ -52,7 +52,7 @@ class CommonsControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "index" -> "someIndex")
- client.getIndexMapping("someIndex", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.getIndexMapping("someIndex", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/commons/get_index_mapping").withBody(body)).get
ensure(response, 200, expectedResponse)
}
@@ -84,7 +84,7 @@ class CommonsControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "index" -> "someIndex")
- client.getIndexSettings("someIndex", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.getIndexSettings("someIndex", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/commons/get_index_settings").withBody(body)).get
ensure(response, 200, expectedResponse)
}
@@ -524,7 +524,7 @@ class CommonsControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "node" -> "someNode")
- client.nodeStats("someNode", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.nodeStats("someNode", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/commons/get_node_stats").withBody(body)).get
ensure(response, 200, expectedResponse)
}
diff --git a/test/controllers/IndexSettingsControllerSpec.scala b/test/controllers/IndexSettingsControllerSpec.scala
index 4cb12be5..65643f96 100644
--- a/test/controllers/IndexSettingsControllerSpec.scala
+++ b/test/controllers/IndexSettingsControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -35,7 +35,7 @@ object IndexSettingsControllerSpec extends MockedServices {
|}
""".stripMargin
)
- client.getIndexSettingsFlat("foo", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.getIndexSettingsFlat("foo", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/index_settings").withBody(Json.obj("host" -> "somehost", "index" -> "foo"))).get
ensure(response, 200, expectedResponse)
}
@@ -57,7 +57,7 @@ object IndexSettingsControllerSpec extends MockedServices {
| "acknowledged":true
|}
""".stripMargin)
- client.updateIndexSettings("foo", body, ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.updateIndexSettings("foo", body, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/index_settings/update").withBody(Json.obj("host" -> "somehost", "index" -> "foo", "settings" -> body))).get
ensure(response, 200, expectedResponse)
}
diff --git a/test/controllers/OverviewControllerSpec.scala b/test/controllers/OverviewControllerSpec.scala
index fff44ed3..e8749a4b 100644
--- a/test/controllers/OverviewControllerSpec.scala
+++ b/test/controllers/OverviewControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -53,7 +53,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.deleteIndex("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.deleteIndex("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/delete_indices").withBody(body)).get
ensure(result, 200, expectedResponse)
}
@@ -77,7 +77,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.refreshIndex("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.refreshIndex("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/refresh_indices").withBody(body)).get
ensure(result, 200, expectedResponse)
}
@@ -101,7 +101,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.flushIndex("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.flushIndex("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/flush_indices").withBody(body)).get
ensure(result, 200, expectedResponse)
}
@@ -125,7 +125,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.clearIndexCache("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.clearIndexCache("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/clear_indices_cache").withBody(body)).get
ensure(result, 200, expectedResponse)
}
@@ -149,7 +149,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.forceMerge("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.forceMerge("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/force_merge").withBody(body)).get
ensure(result, 200, expectedResponse)
}
@@ -169,7 +169,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.openIndex("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.openIndex("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/overview/open_indices").withBody(body)).get
ensure(response, 200, expectedResponse)
}
@@ -189,7 +189,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.closeIndex("a,b,c", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.closeIndex("a,b,c", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/overview/close_indices").withBody(body)).get
ensure(response, 200, expectedResponse)
}
@@ -202,8 +202,8 @@ object OverviewControllerSpec extends MockedServices {
def shardStats = {
val body = Json.obj("host" -> "somehost", "index" -> "someIndex", "node" -> "MCGlWc6ERF2N9pO0uh7-tA", "shard" -> 1)
- client.getShardStats("someIndex", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedStats))
- client.getIndexRecovery("someIndex", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedRecovery))
+ client.getShardStats("someIndex", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedStats))
+ client.getIndexRecovery("someIndex", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedRecovery))
val result = route(application, FakeRequest(POST, "/overview/get_shard_stats").withBody(body)).get
ensure(result, 200, expectedShardStats)
}
@@ -246,7 +246,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "indices" -> "a,b,c")
- client.enableShardAllocation(ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.enableShardAllocation(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/enable_shard_allocation").withBody(body)).get
ensure(result, 200, expectedResponse)
}
@@ -270,7 +270,7 @@ object OverviewControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost")
- client.disableShardAllocation(ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.disableShardAllocation(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val result = route(application, FakeRequest(POST, "/overview/disable_shard_allocation").withBody(body)).get
ensure(result, 200, expectedResponse)
}
diff --git a/test/controllers/RestControllerSpec.scala b/test/controllers/RestControllerSpec.scala
index 5a365b7e..a7e2ba41 100644
--- a/test/controllers/RestControllerSpec.scala
+++ b/test/controllers/RestControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -32,7 +32,7 @@ object RestControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "method" -> "GET", "path" -> "/someesapi")
- client.executeRequest("GET", "/someesapi", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.executeRequest("GET", "/someesapi", None, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/rest/request").withBody(body)).get
ensure(response, 200, expectedResponse)
}
diff --git a/test/controllers/TemplatesControllerSpec.scala b/test/controllers/TemplatesControllerSpec.scala
index 66f6038f..c37c6e82 100644
--- a/test/controllers/TemplatesControllerSpec.scala
+++ b/test/controllers/TemplatesControllerSpec.scala
@@ -1,7 +1,7 @@
package controllers
import elastic.Success
-import models.ElasticServer
+import models.{ElasticServer, Host}
import play.api.libs.json.Json
import play.api.test.FakeRequest
import play.api.test.Helpers._
@@ -68,7 +68,7 @@ object TemplatesControllerSpec extends MockedServices {
|]
""".stripMargin
)
- client.getTemplates(ElasticServer("somehost", None)) returns Future.successful(Success(200, mockedResponse))
+ client.getTemplates(ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, mockedResponse))
val response = route(application, FakeRequest(POST, "/templates").withBody(Json.obj("host" -> "somehost"))).get
ensure(response, 200, expectedResponse)
}
@@ -79,7 +79,7 @@ object TemplatesControllerSpec extends MockedServices {
|{"acknowledged":true}
""".stripMargin
)
- client.deleteTemplate("someTemplate", ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.deleteTemplate("someTemplate", ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/templates/delete").withBody(Json.obj("host" -> "somehost", "name" -> "someTemplate"))).get
ensure(response, 200, expectedResponse)
}
@@ -102,7 +102,7 @@ object TemplatesControllerSpec extends MockedServices {
""".stripMargin
)
val body = Json.obj("host" -> "somehost", "name" -> "someTemplate", "template" -> template)
- client.createTemplate("someTemplate", template, ElasticServer("somehost", None)) returns Future.successful(Success(200, expectedResponse))
+ client.createTemplate("someTemplate", template, ElasticServer(Host("somehost", None))) returns Future.successful(Success(200, expectedResponse))
val response = route(application, FakeRequest(POST, "/templates/create").withBody(body)).get
ensure(response, 200, expectedResponse)
}
diff --git a/test/models/CerebroRequestSpec.scala b/test/models/CerebroRequestSpec.scala
new file mode 100644
index 00000000..e725f832
--- /dev/null
+++ b/test/models/CerebroRequestSpec.scala
@@ -0,0 +1,36 @@
+package models
+
+import controllers.auth.AuthRequest
+import org.specs2.Specification
+import org.specs2.mock.Mockito
+import play.api.libs.json.{JsValue, Json}
+import play.api.mvc.Headers
+
+object CerebroRequestSpec extends Specification with Mockito {
+
+ def is =
+ s2"""
+ CerebroRequest should
+ should forward whitelisted headers $forwardHeaders
+
+ """
+
+ private def forwardHeaders = {
+ val user = None
+ val whitelistHeaders = Seq("a-header")
+ val body = Json.obj("host" -> "host1")
+ val host
+ = Host("host1", None, whitelistHeaders)
+
+ val request = mock[AuthRequest[JsValue]]
+ request.body returns body
+ request.headers returns Headers("a-header" -> "content")
+ request.user returns user
+
+ val hosts = mock[Hosts]
+ hosts.getHost("host1") returns Some(host)
+
+ CerebroRequest(request, hosts) === CerebroRequest(ElasticServer(host, Seq("a-header" -> "content")), body, user)
+ }
+
+}