Skip to content

Commit

Permalink
Merge branch 'psk-ixolit-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
moliware committed May 21, 2019
2 parents 6a3e60e + dc0b5e9 commit f2ed031
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 55 deletions.
2 changes: 1 addition & 1 deletion app/controllers/RestController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions app/elastic/HTTPElasticClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 7 additions & 4 deletions app/models/CerebroRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion app/models/ElasticServer.scala
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions app/models/Host.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package models

case class Host(name: String, authentication: Option[ESAuth] = None, headersWhitelist: Seq[String] = Seq.empty)
11 changes: 6 additions & 5 deletions app/models/Hosts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ 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()
}

def getHostNames() = hosts.keys.toSeq

def getServer(name: String) = hosts.get(name)
def getHost(name: String) = hosts.get(name)

}
5 changes: 3 additions & 2 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions test/controllers/AnalysisControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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"))
}
Expand All @@ -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"))
}
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/ClusterChangesControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/ClusterSettingsControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/CommonsControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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"))
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/IndexSettingsControllerSpec.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit f2ed031

Please sign in to comment.