Skip to content

Commit

Permalink
#1708 Add serializer for flow messages
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Dec 11, 2020
1 parent a053c59 commit f087c2f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
1 change: 0 additions & 1 deletion thehive/app/org/thp/thehive/controllers/v0/AuditCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.thp.scalligraph.traversal.{IteratorOutput, Traversal}
import org.thp.thehive.controllers.v0.Conversion._
import org.thp.thehive.models.{Audit, RichAudit}
import org.thp.thehive.services.AuditOps._
import org.thp.thehive.services.FlowActor.{AuditIds, FlowId}
import org.thp.thehive.services._
import play.api.libs.json.{JsArray, JsObject, Json}
import play.api.mvc.{Action, AnyContent, Results}
Expand Down
10 changes: 4 additions & 6 deletions thehive/app/org/thp/thehive/services/FlowActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import play.api.cache.SyncCacheApi

import scala.concurrent.duration.FiniteDuration

object FlowActor {
case class FlowId(organisation: EntityIdOrName, caseId: Option[EntityIdOrName]) {
override def toString: String = s"$organisation;${caseId.getOrElse("-")}"
}
case class AuditIds(ids: Seq[EntityId])
sealed trait FlowMessage
case class FlowId(organisation: EntityIdOrName, caseId: Option[EntityIdOrName]) extends FlowMessage {
override def toString: String = s"$organisation;${caseId.getOrElse("-")}"
}
case class AuditIds(ids: Seq[EntityId]) extends FlowMessage

class FlowActor extends Actor {
import FlowActor._

lazy val injector: Injector = GuiceAkkaExtension(context.system).injector
lazy val cache: SyncCacheApi = injector.getInstance(classOf[SyncCacheApi])
Expand Down
32 changes: 32 additions & 0 deletions thehive/app/org/thp/thehive/services/FlowSerializer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.thp.thehive.services

import akka.serialization.Serializer
import org.thp.scalligraph.{EntityId, EntityIdOrName}

import java.io.NotSerializableException

class FlowSerializer extends Serializer {
override def identifier: Int = -1165729876

override def includeManifest: Boolean = false

override def toBinary(o: AnyRef): Array[Byte] =
o match {
case FlowId(organisation, None) => 0.toByte +: organisation.toString.getBytes
case FlowId(organisation, Some(caseId)) => 1.toByte +: s"$organisation|$caseId".getBytes
case AuditIds(ids) => 2.toByte +: ids.map(_.value).mkString("|").getBytes
case _ => throw new NotSerializableException
}

override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef =
bytes(0) match {
case 0 => FlowId(EntityIdOrName(new String(bytes.tail)), None)
case 1 =>
new String(bytes.tail).split('|') match {
case Array(organisation, caseId) => FlowId(EntityIdOrName(organisation), Some(EntityIdOrName(caseId)))
case _ => throw new NotSerializableException
}
case 2 => AuditIds(new String(bytes.tail).split('|').toSeq.map(EntityId.apply))
case _ => throw new NotSerializableException
}
}

0 comments on commit f087c2f

Please sign in to comment.