Skip to content

Commit

Permalink
#466 Add compatibility with Cortex 2
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Feb 5, 2018
1 parent 24536d0 commit a33e629
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
5 changes: 2 additions & 3 deletions thehive-cortex/app/connectors/cortex/models/Analyzer.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package connectors.cortex.models

case class Analyzer(
id: String,
name: String,
version: String,
description: String,
dataTypeList: Seq[String],
cortexIds: List[String] = Nil) {
def id: String = (name + "_" + version).replaceAll("\\.", "_")
}
cortexIds: List[String] = Nil)
2 changes: 1 addition & 1 deletion thehive-cortex/app/connectors/cortex/models/Job.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import services.AuditedModel

object JobStatus extends Enumeration with HiveEnumeration {
type Type = Value
val InProgress, Success, Failure = Value
val InProgress, Success, Failure, Waiting = Value
}

trait JobAttributes { _: AttributeDef
Expand Down
20 changes: 15 additions & 5 deletions thehive-cortex/app/connectors/cortex/models/JsonFormat.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package connectors.cortex.models

import akka.stream.scaladsl.Source

import play.api.libs.json.{ Format, JsObject, Json }
import play.api.libs.json.{ OFormat, OWrites, Reads, Writes }
import play.api.libs.json._
import play.api.libs.json.Json.toJsFieldJsValueWrapper

import org.elastic4play.models.JsonFormat.enumFormat
Expand All @@ -21,9 +19,10 @@ object JsonFormat {
for {
name (json \ "name").validate[String]
version (json \ "version").validate[String]
id = (json \ "id").asOpt[String].getOrElse((name + "_" + version).replaceAll("\\.", "_"))
description (json \ "description").validate[String]
dataTypeList (json \ "dataTypeList").validate[Seq[String]]
} yield Analyzer(name, version, description, dataTypeList))
} yield Analyzer(id, name, version, description, dataTypeList))
implicit val analyzerFormats: Format[Analyzer] = Format(analyzerReads, analyzerWrites)

private val fileArtifactWrites = OWrites[FileArtifact](fileArtifact Json.obj(
Expand All @@ -47,14 +46,25 @@ object JsonFormat {

implicit val artifactFormat: OFormat[CortexArtifact] = OFormat(artifactReads, artifactWrites)
implicit val jobStatusFormat: Format[JobStatus.Type] = enumFormat(JobStatus)

private def filterObject(json: JsObject, attributes: String*): JsObject = {
JsObject(attributes.flatMap(a (json \ a).asOpt[JsValue].map(a -> _)))
}

private val cortexJobReads = Reads[CortexJob](json
for {
id (json \ "id").validate[String]
analyzerId (json \ "analyzerId").validate[String]
artifact (json \ "artifact").validate[CortexArtifact]
attributes = filterObject(json.as[JsObject], "tlp", "message", "parameters")
artifact = (json \ "artifact").validate[CortexArtifact]
.getOrElse {
(json \ "data").asOpt[String].map(DataArtifact(_, attributes))
.getOrElse(FileArtifact(Source.empty, attributes))
}
date (json \ "date").validate[Date]
status (json \ "status").validate[JobStatus.Type]
} yield CortexJob(id, analyzerId, artifact, date, status, Nil))

private val cortexJobWrites = Json.writes[CortexJob]
implicit val cortexJobFormat: Format[CortexJob] = Format(cortexJobReads, cortexJobWrites)
implicit val reportTypeFormat: Format[ReportType.Type] = enumFormat(ReportType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class CortexSrv @Inject() (
cortex.waitReport(cortexJobId, 1.minute) andThen {
case Success(j)
val status = (j \ "status").asOpt[JobStatus.Type].getOrElse(JobStatus.Failure)
if (status == JobStatus.InProgress)
if (status == JobStatus.InProgress || status == JobStatus.Waiting)
updateJobWithCortex(jobId, cortexJobId, cortex)
else {
val report = (j \ "report").asOpt[JsObject].getOrElse(JsObject.empty).toString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h3 class="pv-xxs pr-xxs text-primary">
<a ng-if="jobs.length > 1" class="noline mr-xxs" href ng-click="analyzers[analyzerId].showRows = !analyzers[analyzerId].showRows">
<i class="fa" ng-class="{ true:'fa-minus-square-o', false:'fa-plus-square-o' }[analyzers[analyzerId].showRows]"></i>
</a>
<span uib-tooltip="{{analyzer.description}}">{{::analyzerId}}</span>
<span uib-tooltip="{{analyzer.description}}">{{analyzer.name}} {{analyzer.version}}</span>
<!-- <div class="text-muted">{{}}</div> -->
</td>
<!-- <td>
Expand Down

0 comments on commit a33e629

Please sign in to comment.