Skip to content

Commit

Permalink
#1404 Improve integrity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jul 5, 2020
1 parent 0c4bf82 commit 5855f88
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
35 changes: 20 additions & 15 deletions thehive/app/org/thp/thehive/services/CaseTemplateSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package org.thp.thehive.services
import java.util.{Collection => JCollection, List => JList, Map => JMap}

import akka.actor.ActorRef
import gremlin.scala.{__, By, Element, Graph, GremlinScala, Key, P, StepLabel, Vertex}
import gremlin.scala.{__, By, Element, Graph, GremlinScala, Key, P, Vertex}
import javax.inject.{Inject, Named}
import org.apache.tinkerpop.gremlin.process.traversal.{Path, Scope}
import org.apache.tinkerpop.gremlin.structure.T
import org.thp.scalligraph.auth.{AuthContext, Permission}
import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.query.PropertyUpdater
Expand Down Expand Up @@ -251,24 +252,28 @@ class CaseTemplateSteps(raw: GremlinScala[Vertex])(implicit @Named("with-thehive
new CustomFieldValueSteps(raw.outToE[CaseTemplateCustomField])
}

class CaseTemplateIntegrityCheckOps @Inject() (@Named("with-thehive-schema") val db: Database, val service: CaseTemplateSrv)
extends IntegrityCheckOps[CaseTemplate] {
override def getDuplicates[A](property: String): List[List[CaseTemplate with Entity]] =
class CaseTemplateIntegrityCheckOps @Inject() (
@Named("with-thehive-schema") val db: Database,
val service: CaseTemplateSrv,
organisationSrv: OrganisationSrv
) extends IntegrityCheckOps[CaseTemplate] {
override def duplicateEntities: List[List[CaseTemplate with Entity]] =
db.roTransaction { implicit graph =>
val ctLabel = StepLabel()
val orgLabel = StepLabel()
service
organisationSrv
.initSteps
.as(ctLabel)
.organisation
.as(orgLabel)
.raw
.group(By(__.select(ctLabel.name, orgLabel.name).by("name").by()))
.unfold[JMap.Entry[Any, JCollection[Vertex]]]()
.selectValues
.where(_.count(Scope.local).is(P.gt(1)))
.traversal
.flatMap(
__.in("CaseTemplateOrganisation")
.group(By(Key[String]("name")), By(T.id))
.unfold[JMap.Entry[String, JCollection[Any]]]()
.selectValues
.where(_.count(Scope.local).is(P.gt(1)))
.traversal
)
.asScala
.map(ids => service.getByIds(ids.asScala.map(_.toString).toSeq: _*).toList)
.toList
.map(_.asScala.toList.map(service.model.toDomain(_)(db)))
}

override def resolve(entities: List[CaseTemplate with Entity])(implicit graph: Graph): Try[Unit] = entities match {
Expand Down
3 changes: 2 additions & 1 deletion thehive/app/org/thp/thehive/services/TagSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TagSrv @Inject() (appConfig: ApplicationConfig, @Named("integrity-check-ac
override def exists(e: Tag)(implicit graph: Graph): Boolean = initSteps.getByName(e.namespace, e.predicate, e.value).exists()
}

class TagSteps(raw: GremlinScala[Vertex])(implicit @Named("with-thehive-schema") db: Database, graph: Graph) extends VertexSteps[Tag](raw) {
class TagSteps(raw: GremlinScala[Vertex])(implicit db: Database, graph: Graph) extends VertexSteps[Tag](raw) {
override def newInstance(newRaw: GremlinScala[Vertex]): TagSteps = new TagSteps(newRaw)
override def newInstance(): TagSteps = new TagSteps(raw.clone())

Expand All @@ -84,6 +84,7 @@ class TagSteps(raw: GremlinScala[Vertex])(implicit @Named("with-thehive-schema")
}

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))
Expand Down
7 changes: 1 addition & 6 deletions thehive/app/org/thp/thehive/services/UserSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,7 @@ class UserIntegrityCheckOps @Inject() (
}

override def check(): Unit = {
duplicateEntities
.foreach { entities =>
db.tryTransaction { implicit graph =>
resolve(entities)
}
}
super.check()
db.tryTransaction { implicit graph =>
duplicateInEdges[TaskUser](service.initSteps.raw).flatMap(firstCreatedElement(_)).foreach(e => removeEdges(e._2))
duplicateInEdges[CaseUser](service.initSteps.raw).flatMap(firstCreatedElement(_)).foreach(e => removeEdges(e._2))
Expand Down

0 comments on commit 5855f88

Please sign in to comment.