diff --git a/app/controllers/RestController.scala b/app/controllers/RestController.scala index b79cd58f..6e3e0de0 100644 --- a/app/controllers/RestController.scala +++ b/app/controllers/RestController.scala @@ -47,7 +47,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 80a05c21..fd1ffab8 100644 --- a/app/elastic/HTTPElasticClient.scala +++ b/app/elastic/HTTPElasticClient.scala @@ -319,10 +319,13 @@ 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.headers + 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..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,10 +48,13 @@ object CerebroRequest { case _ => None } - val server = hosts.getServer(host) match { - case Some(ElasticServer(h, a)) => ElasticServer(h, a.orElse(requestAuth)) - 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)) } + CerebroRequest(server, body, request.user) } diff --git a/app/models/ElasticServer.scala b/app/models/ElasticServer.scala index 57c16cde..f0892190 100644 --- a/app/models/ElasticServer.scala +++ b/app/models/ElasticServer.scala @@ -1,3 +1,3 @@ package models -case class ElasticServer(host: String, authentication: Option[ESAuth] = None) +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 05160783..e6bf82e0 100644 --- a/app/models/Hosts.scala +++ b/app/models/Hosts.scala @@ -14,22 +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 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)))) - case _ => (name -> ElasticServer(host, None)) + case (Some(username), Some(password)) => name -> Host(host, Some(ESAuth(username, password)), headersWhitelist) + case _ => name -> Host(host, None, headersWhitelist) } }.toMap case Failure(_) => Map() @@ -37,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 ff82241b..c6e8240c 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -65,8 +65,9 @@ auth = { hosts = [ #{ # host = "http://localhost:9200" - # name = "Some Cluster" - #}, + # name = "Localhost cluster" + # headers-whitelist = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ] + #} # Example of host with authentication #{ # host = "http://some-authenticated-host:9200" 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) + } + +}