-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1708 Add serializer for Cortex messages
- Loading branch information
Showing
7 changed files
with
96 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...connector/src/test/scala/org/thp/thehive/connector/cortex/services/CortexSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.thp.thehive.connector.cortex.services | ||
|
||
import akka.serialization.Serializer | ||
import org.thp.cortex.dto.v0.OutputJob | ||
import org.thp.scalligraph.EntityIdOrName | ||
import org.thp.scalligraph.auth.{AuthContext, AuthContextImpl, Permission} | ||
import play.api.libs.json.{JsPath, Json, OFormat, Reads, Writes} | ||
import play.api.libs.functional.syntax._ | ||
|
||
import java.io.NotSerializableException | ||
|
||
object CortexSerializer { | ||
implicit val authContextReads: Reads[AuthContext] = | ||
((JsPath \ "userId").read[String] and | ||
(JsPath \ "userName").read[String] and | ||
(JsPath \ "organisation").read[String].map(EntityIdOrName.apply) and | ||
(JsPath \ "requestId").read[String] and | ||
(JsPath \ "permissions").read[Set[String]].map(Permission.apply))(AuthContextImpl.apply _) | ||
|
||
implicit val authContextWrites: Writes[AuthContext] = Writes[AuthContext] { authContext => | ||
Json.obj( | ||
"userId" -> authContext.userId, | ||
"userName" -> authContext.userName, | ||
"organisation" -> authContext.organisation.toString, | ||
"requestId" -> authContext.requestId, | ||
"permissions" -> authContext.permissions | ||
) | ||
} | ||
implicit val format: OFormat[CheckJob] = Json.format[CheckJob] | ||
} | ||
|
||
class CortexSerializer extends Serializer { | ||
import CortexSerializer._ | ||
override def identifier: Int = -414525848 | ||
|
||
override def includeManifest: Boolean = false | ||
|
||
override def toBinary(o: AnyRef): Array[Byte] = | ||
o match { | ||
case CheckJobs => Array(0) | ||
case FirstCheckJobs => Array(1) | ||
case RemoteJob(job) => 2.toByte +: Json.toJson(job).toString.getBytes | ||
case cj: CheckJob => 3.toByte +: Json.toJson(cj).toString().getBytes | ||
case _ => throw new NotSerializableException | ||
} | ||
|
||
override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = | ||
bytes(0) match { | ||
case 0 => CheckJobs | ||
case 1 => FirstCheckJobs | ||
case 2 => RemoteJob(Json.parse(bytes.tail).as[OutputJob]) | ||
case 3 => Json.parse(bytes.tail).as[CheckJob] | ||
case _ => throw new NotSerializableException | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters