Skip to content

Commit

Permalink
#1264 fixing merge & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rriclet committed Mar 8, 2021
1 parent 4504a11 commit c1386ff
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 32 deletions.
6 changes: 3 additions & 3 deletions thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.thp.thehive.controllers.v0

import org.apache.tinkerpop.gremlin.process.traversal.P
import org.thp.scalligraph._
import org.apache.tinkerpop.gremlin.structure.Graph
import org.thp.scalligraph.traversal.Graph
import org.thp.scalligraph.controllers.{Entrypoint, FPathElem, FPathEmpty, FieldsParser}
import org.thp.scalligraph.models.{Database, Entity, UMapping}
import org.thp.scalligraph.query._
Expand Down Expand Up @@ -151,8 +151,8 @@ class CaseCtrl @Inject() (
entrypoint("merge cases")
.authTransaction(db) { implicit request => implicit graph =>
for {
caze <- caseSrv.get(EntityIdOrName(caseId)).visible.getOrFail("Case")
toMerge <- caseSrv.get(EntityIdOrName(caseToMerge)).visible.getOrFail("Case")
caze <- caseSrv.get(EntityIdOrName(caseId)).visible(organisationSrv).getOrFail("Case")
toMerge <- caseSrv.get(EntityIdOrName(caseToMerge)).visible(organisationSrv).getOrFail("Case")
_ <- sameOrga(Seq(caze, toMerge))
_ <- sameProfile(Seq(caze, toMerge))
merged <- caseSrv.merge(Seq(caze, toMerge))
Expand Down
5 changes: 2 additions & 3 deletions thehive/app/org/thp/thehive/controllers/v1/CaseCtrl.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.thp.thehive.controllers.v1

import org.apache.tinkerpop.gremlin.process.traversal.P
import org.apache.tinkerpop.gremlin.structure.Graph
import org.thp.scalligraph.controllers.{Entrypoint, FieldsParser}
import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.query.{ParamQuery, PropertyUpdater, PublicProperties, Query}
import org.thp.scalligraph.traversal.TraversalOps._
import org.thp.scalligraph.traversal.{IteratorOutput, Traversal}
import org.thp.scalligraph.traversal.{Graph, IteratorOutput, Traversal}
import org.thp.scalligraph.{BadRequestError, EntityIdOrName, RichOptionTry, RichSeq}
import org.thp.thehive.controllers.v1.Conversion._
import org.thp.thehive.dto.v1.{InputCase, InputTask}
Expand All @@ -22,7 +21,7 @@ import org.thp.thehive.services.UserOps._
import org.thp.thehive.services._
import play.api.mvc.{Action, AnyContent, Results}

import javax.inject.{Inject, Named, Singleton}
import javax.inject.{Inject, Singleton}
import scala.util.{Failure, Success, Try}

@Singleton
Expand Down
15 changes: 7 additions & 8 deletions thehive/app/org/thp/thehive/services/CaseSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import play.api.libs.json.{JsNull, JsObject, JsValue, Json}
import java.lang.{Long => JLong}
import java.util.{Date, List => JList, Map => JMap}
import javax.inject.{Inject, Named, Provider, Singleton}
import scala.util.{Failure, Success, Try}
import scala.util.{Success, Try}

@Singleton
class CaseSrv @Inject() (
Expand All @@ -44,6 +44,7 @@ class CaseSrv @Inject() (
impactStatusSrv: ImpactStatusSrv,
observableSrv: ObservableSrv,
attachmentSrv: AttachmentSrv,
userSrv: UserSrv,
alertSrvProvider: Provider[AlertSrv],
@Named("integrity-check-actor") integrityCheckActor: ActorRef,
cache: SyncCacheApi
Expand Down Expand Up @@ -321,7 +322,6 @@ class CaseSrv @Inject() (

def merge(cases: Seq[Case with Entity])(implicit graph: Graph, authContext: AuthContext): Try[RichCase] = {
val mergedCase = Case(
nextCaseNumber,
cases.map(_.title).mkString(" / "),
cases.map(_.description).mkString("\n\n"),
cases.map(_.severity).max,
Expand All @@ -331,28 +331,27 @@ class CaseSrv @Inject() (
cases.map(_.tlp).max,
cases.map(_.pap).max,
CaseStatus.Open,
cases.map(_.summary).fold(None)((s1, s2) => (s1 ++ s2).reduceOption(_ + "\n\n" + _))
cases.map(_.summary).fold(None)((s1, s2) => (s1 ++ s2).reduceOption(_ + "\n\n" + _)),
cases.flatMap(_.tags).distinct
)

val tags = cases.flatMap(c => get(c).tags.toSeq)
for {
user <- userSrv.get(EntityIdOrName(authContext.userId)).getOrFail("User")
orga <- organisationSrv.get(authContext.organisation).getOrFail("Organisation")
richCase <- create(mergedCase, Some(user), orga, tags.toSet, Seq(), None, Seq())
richCase <- create(mergedCase, Some(user), orga, Seq(), None, Seq())
_ <- cases.toTry { c =>
for {
_ <-
get(c)
.tasks
.richTask
.toList
.toTry(shareSrv.shareTask(_, richCase.`case`, orga))
.toTry(shareSrv.shareTask(_, richCase.`case`, orga._id))
_ <-
get(c)
.observables
.richObservable
.toList
.toTry(shareSrv.shareObservable(_, richCase.`case`, orga))
.toTry(shareSrv.shareObservable(_, richCase.`case`, orga._id))
_ <-
get(c)
.alert
Expand Down
3 changes: 2 additions & 1 deletion thehive/test/org/thp/thehive/services/TagSrvTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class TagSrvTest extends PlaySpecification with TestAppBuilder {

"getOrCreate" should {
"get a tag from a taxonomy" in testApp { app =>
app[Database].roTransaction { implicit graph =>
// TODO add tags property in Taxonomy.json to test get
app[Database].transaction { implicit graph =>
val tag = app[TagSrv].getOrCreate("taxonomy1:pred1=value1")
tag.map(_.toString) must beEqualTo(Success("taxonomy1:pred1=\"value1\""))
}
Expand Down
6 changes: 4 additions & 2 deletions thehive/test/resources/data/Case.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"flag": false,
"tlp": 4,
"pap": 3,
"status": "Open"
"status": "Open",
"tags": ["toMerge:pred1=\"value1\""]
},
{
"id": "caseMerge22",
Expand All @@ -89,7 +90,8 @@
"flag": true,
"tlp": 1,
"pap": 2,
"status": "Open"
"status": "Open",
"tags": ["toMerge:pred2=\"value2\""]
},
{
"id": "caseMerge23",
Expand Down
16 changes: 1 addition & 15 deletions thehive/test/resources/data/Tag.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,5 @@
"predicate": "pred1",
"value": "value1",
"colour": "#00f300"
},
{
"id": "tagMerge1",
"namespace": "toMerge",
"predicate": "pred1",
"value": "value1",
"colour": "#00f300"
},
{
"id": "tagMerge2",
"namespace": "toMerge",
"predicate": "pred2",
"value": "value2",
"colour": "#00f300"
}
]
]

0 comments on commit c1386ff

Please sign in to comment.