-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/misp' into develop
- Loading branch information
Showing
23 changed files
with
820 additions
and
285 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package controllers | ||
|
||
import javax.inject.Inject | ||
|
||
import play.api.Logger | ||
import play.api.libs.json.{ JsObject, JsValue } | ||
import play.api.mvc.{ Action, AnyContent, Controller } | ||
import services.MispSrv | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class MispCtrl @Inject() (mispSrv: MispSrv, implicit val ec: ExecutionContext) extends Controller { | ||
|
||
private[MispCtrl] lazy val logger = Logger(getClass) | ||
def modules: Action[AnyContent] = Action { _ ⇒ | ||
Ok(mispSrv.moduleList) | ||
} | ||
|
||
def query: Action[JsValue] = Action.async(parse.json) { request ⇒ | ||
val module = (request.body \ "module").asOpt[String].getOrElse(sys.error("module not present in request")) | ||
val (mispType, dataJson) = request.body.as[JsObject].fields | ||
.collectFirst { | ||
case kv @ (k, _) if k != "module" ⇒ kv | ||
} | ||
.getOrElse(sys.error("invalid request")) | ||
val data = dataJson.asOpt[String].getOrElse(sys.error("data has invalid type (expected string)")) | ||
mispSrv.query(module, mispType, data) | ||
.map(Ok(_)) | ||
} | ||
} | ||
|
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,25 @@ | ||
package controllers | ||
|
||
import javax.inject.{ Inject, Singleton } | ||
|
||
import play.api.Configuration | ||
import play.api.libs.json.Json | ||
import play.api.libs.json.Json.toJsFieldJsValueWrapper | ||
import play.api.mvc.{ Action, Controller } | ||
|
||
@Singleton | ||
class StatusCtrl @Inject() ( | ||
configuration: Configuration) extends Controller { | ||
|
||
private[controllers] def getVersion(c: Class[_]) = Option(c.getPackage.getImplementationVersion).getOrElse("SNAPSHOT") | ||
|
||
def get = Action { _ ⇒ | ||
Ok(Json.obj( | ||
"versions" → Json.obj( | ||
"Cortex" → getVersion(classOf[models.Artifact]), | ||
"Play" → getVersion(classOf[Controller])), | ||
"config" → Json.obj( | ||
"authType" → "none", | ||
"capabilities" → Json.arr()))) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,16 @@ | ||
package models | ||
|
||
import java.io.{ BufferedReader, InputStreamReader } | ||
import java.nio.file.Path | ||
|
||
import scala.concurrent.{ ExecutionContext, Future, blocking } | ||
import scala.sys.process.{ BasicIO, Process, ProcessIO } | ||
|
||
import akka.stream.Materializer | ||
|
||
import play.api.Logger | ||
import play.api.libs.json.{ JsObject, JsString, Json } | ||
|
||
import com.fasterxml.jackson.core.JsonParseException | ||
import com.fasterxml.jackson.databind.JsonMappingException | ||
import play.api.libs.json.JsObject | ||
|
||
case class ExternalAnalyzer( | ||
name: String, | ||
version: String, | ||
description: String, | ||
dataTypeList: Seq[String], | ||
author: String, | ||
url: String, | ||
license: String, | ||
command: Path, | ||
config: JsObject)(implicit val ec: ExecutionContext) extends Analyzer { | ||
|
||
val log = Logger(getClass) | ||
private val osexec = if (System.getProperty("os.name").toLowerCase.contains("win")) | ||
(c: String) ⇒ s"""cmd /c $c""" | ||
else | ||
(c: String) ⇒ s"""sh -c "./$c" """ | ||
|
||
override def analyze(artifact: Artifact): Future[JsObject] = { | ||
Future { | ||
val input = artifact match { | ||
case FileArtifact(file, attributes) ⇒ attributes + ("file" → JsString(file.getAbsoluteFile.toString)) + ("config" → config) | ||
case DataArtifact(data, attributes) ⇒ attributes + ("data" → JsString(data)) + ("config" → config) | ||
} | ||
val output = new StringBuffer | ||
val error = new StringBuffer | ||
try { | ||
log.info(s"Execute ${osexec(command.getFileName.toString)} in ${command.getParent.toFile.getAbsoluteFile.getName}") | ||
val exitValue = Process(osexec(command.getFileName.toString), command.getParent.toFile).run( | ||
new ProcessIO( | ||
{ stdin ⇒ | ||
try stdin.write(input.toString.getBytes("UTF-8")) | ||
finally stdin.close() | ||
}, | ||
{ stdout ⇒ | ||
val reader = new BufferedReader(new InputStreamReader(stdout, "UTF-8")) | ||
try BasicIO.processLinesFully { line ⇒ | ||
output.append(line).append(System.lineSeparator()) | ||
() | ||
}(reader.readLine) | ||
finally reader.close() | ||
}, | ||
{ stderr ⇒ | ||
val reader = new BufferedReader(new InputStreamReader(stderr, "UTF-8")) | ||
try BasicIO.processLinesFully { line ⇒ | ||
error.append(line).append(System.lineSeparator()) | ||
() | ||
}(reader.readLine) | ||
finally reader.close() | ||
})).exitValue | ||
Json.parse(output.toString).as[JsObject] | ||
} | ||
catch { | ||
case _: JsonMappingException ⇒ | ||
error.append(output) | ||
JsObject(Seq("errorMessage" → JsString(s"Error: Invalid output\n$error"))) | ||
case _: JsonParseException ⇒ | ||
error.append(output) | ||
JsObject(Seq("errorMessage" → JsString(s"Error: Invalid output\n$error"))) | ||
case t: Throwable ⇒ | ||
JsObject(Seq("errorMessage" → JsString(t.getMessage + ":" + t.getStackTrace().mkString("", "\n\t", "\n")))) | ||
} | ||
} | ||
} | ||
} | ||
name: String, | ||
version: String, | ||
description: String, | ||
dataTypeList: Seq[String], | ||
author: String, | ||
url: String, | ||
license: String, | ||
command: Path, | ||
config: JsObject) extends Analyzer |
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 |
---|---|---|
@@ -1,24 +1,22 @@ | ||
package models | ||
|
||
import play.api.libs.json.JsObject | ||
import scala.concurrent.Future | ||
import java.util.Date | ||
import scala.util.Success | ||
import scala.util.Failure | ||
|
||
import scala.concurrent.Future | ||
import scala.util.{ Failure, Success } | ||
|
||
object JobStatus extends Enumeration { | ||
type Type = Value | ||
val InProgress, Success, Failure = Value | ||
} | ||
case class Job(id: String, analyzerId: String, artifact: Artifact, report: Future[JsObject]) { | ||
|
||
case class Job(id: String, analyzer: Analyzer, artifact: Artifact, report: Future[Report]) { | ||
val date: Date = new Date() | ||
|
||
def status: JobStatus.Type = report.value match { | ||
case Some(Success(x)) ⇒ (x \ "success").asOpt[Boolean] match { | ||
case Some(true) ⇒ JobStatus.Success | ||
case _ ⇒ JobStatus.Failure | ||
} | ||
case Some(Failure(_)) ⇒ JobStatus.Failure | ||
case None ⇒ JobStatus.InProgress | ||
case Some(Success(SuccessReport(_, _, _))) ⇒ JobStatus.Success | ||
case Some(Success(FailureReport(_))) ⇒ JobStatus.Failure | ||
case Some(Failure(_)) ⇒ JobStatus.Failure | ||
case None ⇒ JobStatus.InProgress | ||
} | ||
} |
Oops, something went wrong.