Skip to content

Commit

Permalink
#1963 Prevent failure if observable already exist while creating an a…
Browse files Browse the repository at this point in the history
…lert
  • Loading branch information
To-om committed May 10, 2021
1 parent 937eee1 commit 2398b07
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.thp.scalligraph.traversal._
import org.thp.scalligraph.{
AuthorizationError,
BadRequestError,
CreateError,
EntityId,
EntityIdOrName,
EntityName,
Expand Down Expand Up @@ -322,23 +323,42 @@ class AlertCtrl @Inject() (
.getOrFail(EntityName(observable.dataType))
.flatMap {
case attachmentType if attachmentType.isAttachment =>
observable.data.map(_.split(';')).toTry {
case Array(filename, contentType, value) =>
val data = Base64.getDecoder.decode(value)
attachmentSrv
.create(filename, contentType, data)
.flatMap(attachment => alertSrv.createObservable(alert, observable.toObservable, attachment))
case Array(filename, contentType) =>
attachmentSrv
.create(filename, contentType, Array.emptyByteArray)
.flatMap(attachment => alertSrv.createObservable(alert, observable.toObservable, attachment))
case data =>
Failure(InvalidFormatAttributeError("artifacts.data", "filename;contentType;base64value", Set.empty, FString(data.mkString(";"))))
}
observable
.data
.map(_.split(';'))
.toTry {
case Array(filename, contentType, value) =>
val data = Base64.getDecoder.decode(value)
attachmentSrv
.create(filename, contentType, data)
.flatMap(attachment => alertSrv.createObservable(alert, observable.toObservable, attachment)) match {
case Success(o) => Success(Some(o))
case Failure(_: CreateError) => Success(None)
case Failure(otherError) => Failure(otherError)
}
case Array(filename, contentType) =>
attachmentSrv
.create(filename, contentType, Array.emptyByteArray)
.flatMap(attachment => alertSrv.createObservable(alert, observable.toObservable, attachment)) match {
case Success(o) => Success(Some(o))
case Failure(_: CreateError) => Success(None)
case Failure(otherError) => Failure(otherError)
}
case data =>
Failure(InvalidFormatAttributeError("artifacts.data", "filename;contentType;base64value", Set.empty, FString(data.mkString(";"))))
}
.map(_.flatten)
case _ =>
observable
.data
.toTry(d => alertSrv.createObservable(alert, observable.toObservable, d))
.toTry { data =>
alertSrv.createObservable(alert, observable.toObservable, data) match {
case Success(o) => Success(Some(o))
case Failure(_: CreateError) => Success(None)
case Failure(otherError) => Failure(otherError)
}
}
.map(_.flatten)
}
}

Expand Down

0 comments on commit 2398b07

Please sign in to comment.