From f9fab938f25ea2b7272b3ecfc9a5b7f20a7babac Mon Sep 17 00:00:00 2001 From: To-om Date: Fri, 4 Feb 2022 17:07:24 +0100 Subject: [PATCH] #2331 Use index version to determine if it is single type --- .../thehive/migration/th3/ElasticClient.scala | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/migration/src/main/scala/org/thp/thehive/migration/th3/ElasticClient.scala b/migration/src/main/scala/org/thp/thehive/migration/th3/ElasticClient.scala index 64c0798c05..31fbbd4ec2 100644 --- a/migration/src/main/scala/org/thp/thehive/migration/th3/ElasticClient.scala +++ b/migration/src/main/scala/org/thp/thehive/migration/th3/ElasticClient.scala @@ -19,6 +19,7 @@ import java.net.{URI, URLEncoder} import javax.inject.{Inject, Provider, Singleton} import scala.concurrent.duration.{Duration, DurationInt, DurationLong, FiniteDuration} import scala.concurrent.{Await, ExecutionContext, Future} +import scala.util.Try @Singleton class ElasticClientProvider @Inject() ( @@ -196,17 +197,22 @@ class ElasticConfig( ) .status == 200 - def isSingleType(indexName: String): Boolean = { - val response = Await - .result( - authentication(ws.url(stripUrl(s"$esUri/$indexName"))) - .get(), - 10.seconds - ) - if (response.status != 200) - throw InternalError(s"Unexpected response from Elasticsearch: ${response.status} ${response.statusText}\n${response.body}") - (response.json \ indexName \ "settings" \ "index" \ "mapping" \ "single_type").asOpt[String].fold(version.head > '6')(_.toBoolean) - } + def isSingleType(indexName: String): Boolean = + indexName + .split('_') + .lastOption + .flatMap(version => Try(version.toInt).toOption) + .fold { + val response = Await + .result( + authentication(ws.url(stripUrl(s"$esUri/$indexName"))) + .get(), + 10.seconds + ) + if (response.status != 200) + throw InternalError(s"Unexpected response from Elasticsearch: ${response.status} ${response.statusText}\n${response.body}") + (response.json \ indexName \ "settings" \ "index" \ "mapping" \ "single_type").asOpt[String].fold(version.head > '6')(_.toBoolean) + }(version => version >= 15) def version: String = { val response = Await.result(authentication(ws.url(stripUrl(esUri))).get(), 10.seconds)