Skip to content

Commit

Permalink
#1404 Fix tag integrity checks (index must not be used)
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jul 8, 2020
1 parent 6721498 commit 8a7819b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion thehive/app/org/thp/thehive/controllers/v0/TagCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TagCtrl @Inject() (
content.fold(Seq.empty[Tag])(parseTaxonomy)

tags
.filterNot(tagSrv.initSteps.get(_).exists())
.filterNot(tagSrv.initSteps.getTag(_).exists())
.toTry(tagSrv.create)
.map(ts => Results.Ok(JsNumber(ts.size)))
}
Expand Down
21 changes: 12 additions & 9 deletions thehive/app/org/thp/thehive/services/TagSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class TagSrv @Inject() (appConfig: ApplicationConfig, @Named("integrity-check-ac
override def get(idOrName: String)(implicit graph: Graph): TagSteps =
getByIds(idOrName)

def get(tag: Tag)(implicit graph: Graph): TagSteps = initSteps.get(tag)
def getTag(tag: Tag)(implicit graph: Graph): TagSteps = initSteps.getTag(tag)

def getOrCreate(tagName: String)(implicit graph: Graph, authContext: AuthContext): Try[Tag with Entity] = {
val tag = parseString(tagName)
get(tag).getOrFail("Tag").recoverWith {
getTag(tag).getOrFail("Tag").recoverWith {
case _ if autoCreate => create(tag)
}
}
Expand All @@ -69,7 +69,7 @@ class TagSteps(raw: GremlinScala[Vertex])(implicit db: Database, graph: Graph) e
override def newInstance(newRaw: GremlinScala[Vertex]): TagSteps = new TagSteps(newRaw)
override def newInstance(): TagSteps = new TagSteps(raw.clone())

def get(tag: Tag): TagSteps = getByName(tag.namespace, tag.predicate, tag.value)
def getTag(tag: Tag): TagSteps = getByName(tag.namespace, tag.predicate, tag.value)

def getByName(namespace: String, predicate: String, value: Option[String]): TagSteps = {
val step = newInstance(
Expand All @@ -89,11 +89,14 @@ class TagSteps(raw: GremlinScala[Vertex])(implicit db: Database, graph: Graph) e

class TagIntegrityCheckOps @Inject() (@Named("with-thehive-schema") val db: Database, val service: TagSrv) extends IntegrityCheckOps[Tag] {

override def resolve(entities: List[Tag with Entity])(implicit graph: Graph): Try[Unit] = entities match {
case head :: tail =>
tail.foreach(copyEdge(_, head))
tail.foreach(service.get(_).remove())
Success(())
case _ => Success(())
override def resolve(entities: List[Tag with Entity])(implicit graph: Graph): Try[Unit] = {
firstCreatedEntity(entities).foreach {
case (head, tail) =>
tail.foreach(copyEdge(_, head))
val tailIds = tail.map(_._id)
logger.debug(s"Remove duplicated vertex: ${tailIds.mkString(",")}")
service.getByIds(tailIds: _*).remove()
}
Success(())
}
}

0 comments on commit 8a7819b

Please sign in to comment.