Skip to content

Commit

Permalink
#2125 Be able to update the type of observable
Browse files Browse the repository at this point in the history
  • Loading branch information
vdebergue authored and To-om committed Oct 1, 2021
1 parent 20911e7 commit c88f3e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion thehive/app/org/thp/thehive/controllers/v0/ObservableCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class ObservableCtrl @Inject() (

@Singleton
class PublicObservable @Inject() (
observableTypeSrv: ObservableTypeSrv,
observableSrv: ObservableSrv,
organisationSrv: OrganisationSrv
) extends PublicData
Expand Down Expand Up @@ -424,7 +425,16 @@ class PublicObservable @Inject() (
)
.property("message", UMapping.string)(_.field.updatable)
.property("tlp", UMapping.int)(_.field.updatable)
.property("dataType", UMapping.string)(_.field.readonly)
.property("dataType", UMapping.string)(_.field.custom { (_, value, vertex, graph, _) =>
val observable = observableSrv.model.converter(vertex)
for {
currentDataType <- observableTypeSrv.getByName(observable.dataType)(graph).getOrFail("ObservableType")
newDataType <- observableTypeSrv.getByName(value)(graph).getOrFail("ObservableType")
isSameType = currentDataType.isAttachment == newDataType.isAttachment
_ <- if (isSameType) Success(()) else Failure(BadRequestError("Can not update dataType: isAttachment does not match"))
_ <- Try(observableSrv.get(vertex)(graph).update(_.dataType, value).iterate())
} yield Json.obj("dataType" -> value)
})
.property("data", UMapping.string.optional)(_.field.readonly)
.property("attachment.name", UMapping.string.optional)(_.select(_.attachments.value(_.name)).readonly)
.property("attachment.hashes", UMapping.hash.sequence)(_.select(_.attachments.value(_.hashes)).readonly)
Expand Down
14 changes: 12 additions & 2 deletions thehive/app/org/thp/thehive/controllers/v1/Properties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.thp.thehive.services._
import play.api.libs.json.{JsObject, JsValue, Json}

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

@Singleton
class Properties @Inject() (
Expand All @@ -39,6 +39,7 @@ class Properties @Inject() (
dashboardSrv: DashboardSrv,
caseTemplateSrv: CaseTemplateSrv,
observableSrv: ObservableSrv,
observableTypeSrv: ObservableTypeSrv,
customFieldSrv: CustomFieldSrv,
organisationSrv: OrganisationSrv,
db: Database
Expand Down Expand Up @@ -440,7 +441,16 @@ class Properties @Inject() (
)
.property("message", UMapping.string)(_.field.updatable)
.property("tlp", UMapping.int)(_.field.updatable)
.property("dataType", UMapping.string)(_.field.readonly)
.property("dataType", UMapping.string)(_.field.custom { (_, value, vertex, graph, _) =>
val observable = observableSrv.model.converter(vertex)
for {
currentDataType <- observableTypeSrv.getByName(observable.dataType)(graph).getOrFail("ObservableType")
newDataType <- observableTypeSrv.getByName(value)(graph).getOrFail("ObservableType")
isSameType = currentDataType.isAttachment == newDataType.isAttachment
_ <- if (isSameType) Success(()) else Failure(BadRequestError("Can not update dataType: isAttachment does not match"))
_ <- Try(observableSrv.get(vertex)(graph).update(_.dataType, value).iterate())
} yield Json.obj("dataType" -> value)
})
.property("data", UMapping.string.optional)(_.field.readonly)
.property("attachment.name", UMapping.string.optional)(_.select(_.attachments.value(_.name)).readonly)
.property("attachment.hashes", UMapping.hash.sequence)(_.select(_.attachments.value(_.hashes)).readonly)
Expand Down

0 comments on commit c88f3e9

Please sign in to comment.