Skip to content

Commit

Permalink
#1387 Prevent duplicated observable type, impact status and resolutio…
Browse files Browse the repository at this point in the history
…n status
  • Loading branch information
To-om committed Jun 29, 2020
1 parent f9a6d69 commit 3a56149
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
2 changes: 2 additions & 0 deletions thehive/app/org/thp/thehive/models/Case.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ object CaseStatus extends Enumeration {
}

@VertexEntity
@DefineIndex(IndexType.unique, "value")
case class ResolutionStatus(value: String) {
require(!value.isEmpty, "ResolutionStatus can't be empty")
}
Expand All @@ -32,6 +33,7 @@ object ResolutionStatus {
case class CaseResolutionStatus()

@VertexEntity
@DefineIndex(IndexType.unique, "value")
case class ImpactStatus(value: String) {
require(!value.isEmpty, "ImpactStatus can't be empty")
}
Expand Down
2 changes: 2 additions & 0 deletions thehive/app/org/thp/thehive/models/ObservableType.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.thp.thehive.models

import org.thp.scalligraph.models.{DefineIndex, IndexType}
import org.thp.scalligraph.{EdgeEntity, VertexEntity}

@EdgeEntity[Observable, ObservableType]
case class ObservableObservableType()

@VertexEntity
@DefineIndex(IndexType.unique, "name")
case class ObservableType(name: String, isAttachment: Boolean)

object ObservableType {
Expand Down
10 changes: 7 additions & 3 deletions thehive/app/org/thp/thehive/services/ImpactStatusSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package org.thp.thehive.services
import akka.actor.ActorRef
import gremlin.scala._
import javax.inject.{Inject, Named, Singleton}
import org.thp.scalligraph.EntitySteps
import org.thp.scalligraph.{CreateError, EntitySteps}
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.services.{IntegrityCheckOps, VertexSrv}
import org.thp.scalligraph.steps.StepsOps._
import org.thp.scalligraph.steps.VertexSteps
import org.thp.thehive.models.ImpactStatus

import scala.util.{Success, Try}
import scala.util.{Failure, Success, Try}

@Singleton
class ImpactStatusSrv @Inject() (@Named("integrity-check-actor") integrityCheckActor: ActorRef)(
Expand All @@ -29,7 +29,11 @@ class ImpactStatusSrv @Inject() (@Named("integrity-check-actor") integrityCheckA
super.createEntity(e)
}

def create(impactStatus: ImpactStatus)(implicit graph: Graph, authContext: AuthContext): Try[ImpactStatus with Entity] = createEntity(impactStatus)
def create(impactStatus: ImpactStatus)(implicit graph: Graph, authContext: AuthContext): Try[ImpactStatus with Entity] =
if (exists(impactStatus))
Failure(CreateError(s"Impact status ${impactStatus.value} already exists"))
else
createEntity(impactStatus)

override def exists(e: ImpactStatus)(implicit graph: Graph): Boolean = initSteps.getByName(e.value).exists()
}
Expand Down
7 changes: 5 additions & 2 deletions thehive/app/org/thp/thehive/services/ObservableTypeSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.services._
import org.thp.scalligraph.steps.StepsOps._
import org.thp.scalligraph.steps.VertexSteps
import org.thp.scalligraph.{BadRequestError, EntitySteps}
import org.thp.scalligraph.{BadRequestError, CreateError, EntitySteps}
import org.thp.thehive.models._

import scala.util.{Failure, Success, Try}
Expand All @@ -33,7 +33,10 @@ class ObservableTypeSrv @Inject() (@Named("integrity-check-actor") integrityChec
}

def create(observableType: ObservableType)(implicit graph: Graph, authContext: AuthContext): Try[ObservableType with Entity] =
createEntity(observableType)
if (exists(observableType))
Failure(CreateError(s"Observable type ${observableType.name} already exists"))
else
createEntity(observableType)

def remove(idOrName: String)(implicit graph: Graph): Try[Unit] =
if (useCount(idOrName) == 0) Success(get(idOrName).remove())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package org.thp.thehive.services
import akka.actor.ActorRef
import gremlin.scala._
import javax.inject.{Inject, Named, Singleton}
import org.thp.scalligraph.EntitySteps
import org.thp.scalligraph.{CreateError, EntitySteps}
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.services.{IntegrityCheckOps, VertexSrv}
import org.thp.scalligraph.steps.StepsOps._
import org.thp.scalligraph.steps.VertexSteps
import org.thp.thehive.models.ResolutionStatus

import scala.util.{Success, Try}
import scala.util.{Failure, Success, Try}

@Singleton
class ResolutionStatusSrv @Inject() (@Named("integrity-check-actor") integrityCheckActor: ActorRef)(
Expand All @@ -30,7 +30,10 @@ class ResolutionStatusSrv @Inject() (@Named("integrity-check-actor") integrityCh
}

def create(resolutionStatus: ResolutionStatus)(implicit graph: Graph, authContext: AuthContext): Try[ResolutionStatus with Entity] =
createEntity(resolutionStatus)
if (exists(resolutionStatus))
Failure(CreateError(s"Resolution status ${resolutionStatus.value} already exists"))
else
createEntity(resolutionStatus)

override def exists(e: ResolutionStatus)(implicit graph: Graph): Boolean = initSteps.getByName(e.value).exists()
}
Expand Down

0 comments on commit 3a56149

Please sign in to comment.