Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XPack authentication support #39

Merged
merged 1 commit into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions app/org/elastic4play/database/DBConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.sksamuel.elastic4s.streams.ReactiveElastic.ReactiveElastic
import com.sksamuel.elastic4s.streams.{ RequestBuilder, ResponseListener }
import com.sksamuel.elastic4s.update.{ RichUpdateResponse, UpdateDefinition }
import com.sksamuel.elastic4s.{ ElasticsearchClientUri, TcpClient }
import com.sksamuel.elastic4s.xpack.security.XPackElasticClient
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse
Expand All @@ -42,6 +43,8 @@ class DBConfiguration(
searchHost: Seq[String],
searchCluster: String,
baseIndexName: String,
xpackUsername: String,
xpackPassword: String,
lifecycle: ApplicationLifecycle,
val version: Int,
implicit val ec: ExecutionContext,
Expand All @@ -57,6 +60,8 @@ class DBConfiguration(
configuration.get[Seq[String]]("search.host"),
configuration.get[String]("search.cluster"),
configuration.get[String]("search.index"),
configuration.get[String]("search.username"),
configuration.get[String]("search.password"),
lifecycle,
version,
ec,
Expand All @@ -65,12 +70,23 @@ class DBConfiguration(

private[DBConfiguration] lazy val logger = Logger(getClass)

private def connect(): TcpClient = {
var uri = ElasticsearchClientUri(s"elasticsearch://${searchHost.mkString(",")}")
var settings = Settings.builder()
settings.put("cluster.name", searchCluster)
if (xpackUsername.isEmpty) {
return TcpClient.transport(settings.build(), uri)
}
else {
settings.put("xpack.security.user", s"${xpackUsername}:${xpackPassword}")
return XPackElasticClient(settings.build(), uri)
}
}

/**
* Underlying ElasticSearch client
*/
private[database] val client = TcpClient.transport(
Settings.builder().put("cluster.name", searchCluster).build(),
ElasticsearchClientUri(s"elasticsearch://${searchHost.mkString(",")}"))
private[database] val client = connect()
// when application close, close also ElasticSearch connection
lifecycle.addStopHook { () ⇒ Future { client.close() } }

Expand Down Expand Up @@ -133,5 +149,5 @@ class DBConfiguration(
/**
* return a new instance of DBConfiguration that points to the previous version of the index schema
*/
def previousVersion: DBConfiguration = new DBConfiguration(searchHost, searchCluster, baseIndexName, lifecycle, version - 1, ec, actorSystem)
}
def previousVersion: DBConfiguration = new DBConfiguration(searchHost, searchCluster, baseIndexName, xpackUsername, xpackPassword, lifecycle, version - 1, ec, actorSystem)
}
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ lazy val elastic4play = (project in file("."))

scalaVersion := "2.12.4"

resolvers += "elasticsearch-releases" at "https://artifacts.elastic.co/maven"

libraryDependencies ++= Seq(
cacheApi,
"com.sksamuel.elastic4s" %% "elastic4s-core" % "5.6.0",
"com.sksamuel.elastic4s" %% "elastic4s-streams" % "5.6.0",
"com.sksamuel.elastic4s" %% "elastic4s-tcp" % "5.6.0",
"com.sksamuel.elastic4s" %% "elastic4s-xpack-security" % "5.6.0",
"com.typesafe.akka" %% "akka-stream-testkit" % "2.5.6" % Test,
"org.scalactic" %% "scalactic" % "3.0.4",
"org.bouncycastle" % "bcprov-jdk15on" % "1.58",
Expand All @@ -42,4 +45,4 @@ scalacOptions in ThisBuild ++= Seq(
"-Ywarn-value-discard", // Warn when non-Unit expression results are unused
"-Ywarn-inaccessible",
"-Ywarn-dead-code"
)
)