Skip to content

Commit

Permalink
#2233 Fix output of alert observables
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Nov 5, 2021
1 parent 5372384 commit 98628ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object Conversion {
_.case_artifact,
jobWithParent._2.fold[Option[OutputObservable]](None) {
case (richObservable, richCase) =>
Some(observableWithExtraOutput.toValue((richObservable, JsObject.empty, Some(richCase))))
Some(observableWithExtraOutput.toValue((richObservable, JsObject.empty, Some(Left(richCase)))))
}
)
.enableMethodAccessors
Expand Down
1 change: 1 addition & 0 deletions dto/src/main/scala/org/thp/thehive/dto/v0/Observable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ case class OutputObservable(
stats: JsObject,
seen: Option[Boolean],
`case`: Option[OutputCase],
alert: Option[OutputAlert],
ignoreSimilarity: Option[Boolean]
)

Expand Down
11 changes: 7 additions & 4 deletions thehive/app/org/thp/thehive/controllers/v0/Conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,14 @@ object Conversion {
)
.withFieldConst(_.stats, JsObject.empty)
.withFieldConst(_.`case`, None)
.withFieldConst(_.alert, None)
.enableMethodAccessors
.transform
)

implicit val observableWithExtraOutput: Renderer.Aux[(RichObservable, JsObject, Option[RichCase]), OutputObservable] =
Renderer.toJson[(RichObservable, JsObject, Option[RichCase]), OutputObservable] {
case (richObservable, stats, richCase) =>
implicit val observableWithExtraOutput: Renderer.Aux[(RichObservable, JsObject, Option[Either[RichCase, RichAlert]]), OutputObservable] =
Renderer.toJson[(RichObservable, JsObject, Option[Either[RichCase, RichAlert]]), OutputObservable] {
case (richObservable, stats, caseOrAlert) =>
richObservable
.into[OutputObservable]
.withFieldConst(_._type, "case_artifact")
Expand All @@ -438,7 +439,8 @@ object Conversion {
})
)
.withFieldConst(_.stats, stats)
.withFieldConst(_.`case`, richCase.map(_.toValue))
.withFieldConst(_.`case`, caseOrAlert.flatMap(_.swap.map(_.toValue).toOption))
.withFieldConst(_.alert, caseOrAlert.flatMap(_.map(_.toValue).toOption))
.enableMethodAccessors
.transform
}
Expand Down Expand Up @@ -470,6 +472,7 @@ object Conversion {
)
.withFieldConst(_.stats, stats)
.withFieldConst(_.`case`, None)
.withFieldConst(_.alert, None)
.enableMethodAccessors
.transform
}
Expand Down
18 changes: 14 additions & 4 deletions thehive/app/org/thp/thehive/controllers/v0/ObservableCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.thp.thehive.services.AlertOps._
import org.thp.thehive.services.CaseOps._
import org.thp.thehive.services.ObservableOps._
import org.thp.thehive.services.OrganisationOps._
import org.thp.thehive.services.ShareOps._
import org.thp.thehive.services._
import play.api.Configuration
import play.api.libs.Files.DefaultTemporaryFileCreator
Expand Down Expand Up @@ -382,14 +381,25 @@ class PublicObservable @Inject() (
.richPage(from, to, withTotal = true) {
case o if withStats =>
o.richObservableWithCustomRenderer(organisationSrv, observableStatsRenderer(organisationSrv)(authContext))(authContext)
.domainMap(ros => (ros._1, ros._2, None: Option[RichCase]))
.domainMap(ros => (ros._1, ros._2, None: Option[Either[RichCase, RichAlert]]))
case o =>
o.richObservable.domainMap(ro => (ro, JsObject.empty, None))
}
case (OutputParam(from, to, _, _), observableSteps, authContext) =>
observableSteps.richPage(from, to, withTotal = true)(
_.richObservableWithCustomRenderer(organisationSrv, o => o.`case`.richCase(authContext))(authContext).domainMap(roc =>
(roc._1, JsObject.empty, Some(roc._2): Option[RichCase])
_.richObservableWithCustomRenderer(
organisationSrv,
o => o.project(_.by(_.`case`.richCase(authContext).option).by(_.alert.richAlert.option))
)(authContext).domainMap(roc =>
(
roc._1,
JsObject.empty,
roc._2 match {
case (Some(c), _) => Some(Left(c))
case (_, Some(a)) => Some(Right(a))
case _ => None
}
)
)
)
}
Expand Down

0 comments on commit 98628ac

Please sign in to comment.