Skip to content

Commit

Permalink
#39 make xpack credentials optional
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Dec 18, 2017
1 parent d02dfce commit 9f4d314
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions app/org/elastic4play/database/DBConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class DBConfiguration(
searchHost: Seq[String],
searchCluster: String,
baseIndexName: String,
xpackUsername: String,
xpackPassword: String,
xpackUsername: Option[String],
xpackPassword: Option[String],
lifecycle: ApplicationLifecycle,
val version: Int,
implicit val ec: ExecutionContext,
Expand All @@ -60,8 +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"),
configuration.getOptional[String]("search.username"),
configuration.getOptional[String]("search.password"),
lifecycle,
version,
ec,
Expand All @@ -71,16 +71,17 @@ class DBConfiguration(
private[DBConfiguration] lazy val logger = Logger(getClass)

private def connect(): TcpClient = {
var uri = ElasticsearchClientUri(s"elasticsearch://${searchHost.mkString(",")}")
var settings = Settings.builder()
val uri = ElasticsearchClientUri(s"elasticsearch://${searchHost.mkString(",")}")
val 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)
}

val xpackClient = for {
username xpackUsername
password xpackPassword
_ = settings.put("xpack.security.user", s"$username:$password")
} yield XPackElasticClient(settings.build(), uri)

xpackClient.getOrElse(TcpClient.transport(settings.build(), uri))
}

/**
Expand Down

0 comments on commit 9f4d314

Please sign in to comment.