Skip to content

Commit

Permalink
#1501 Enforce type of "has" and "choose" steps
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Oct 5, 2020
1 parent 91f3527 commit 8472167
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 259 deletions.
2 changes: 1 addition & 1 deletion ScalliGraph
Submodule ScalliGraph updated 38 files
+1 −3 core-test/src/test/scala/org/thp/scalligraph/models/CardinalityTest.scala
+5 −2 core-test/src/test/scala/org/thp/scalligraph/models/DummyUserSrv.scala
+2 −2 core-test/src/test/scala/org/thp/scalligraph/models/IndexTest.scala
+8 −8 core-test/src/test/scala/org/thp/scalligraph/models/Modern.scala
+1 −1 core-test/src/test/scala/org/thp/scalligraph/models/ModernQuery.scala
+2 −2 core-test/src/test/scala/org/thp/scalligraph/models/ModernTest.scala
+2 −2 core-test/src/test/scala/org/thp/scalligraph/models/SimpleEntityTest.scala
+3 −3 core-test/src/test/scala/org/thp/scalligraph/services/IntegrityCheckTest.scala
+3 −2 core-test/src/test/scala/org/thp/scalligraph/services/StorageSrvTest.scala
+32 −0 core/src/main/scala/org/thp/scalligraph/EntityId.scala
+10 −9 core/src/main/scala/org/thp/scalligraph/auth/ADAuthSrv.scala
+8 −8 core/src/main/scala/org/thp/scalligraph/auth/AuthSrv.scala
+5 −3 core/src/main/scala/org/thp/scalligraph/auth/BasicAuthSrv.scala
+10 −9 core/src/main/scala/org/thp/scalligraph/auth/LdapAuthSrv.scala
+4 −4 core/src/main/scala/org/thp/scalligraph/auth/MultiAuthSrv.scala
+24 −26 core/src/main/scala/org/thp/scalligraph/auth/OAuth2Srv.scala
+7 −5 core/src/main/scala/org/thp/scalligraph/auth/SessionAuthSrv.scala
+8 −7 core/src/main/scala/org/thp/scalligraph/auth/UserSrv.scala
+3 −2 core/src/main/scala/org/thp/scalligraph/controllers/AuthenticatedRequest.scala
+6 −6 core/src/main/scala/org/thp/scalligraph/macros/ModelMacro.scala
+39 −2 core/src/main/scala/org/thp/scalligraph/macros/TraversalMacro.scala
+20 −18 core/src/main/scala/org/thp/scalligraph/models/Database.scala
+52 −113 core/src/main/scala/org/thp/scalligraph/models/Mapping.scala
+5 −9 core/src/main/scala/org/thp/scalligraph/models/Model.scala
+12 −6 core/src/main/scala/org/thp/scalligraph/query/Filter.scala
+9 −9 core/src/main/scala/org/thp/scalligraph/services/EdgeSrv.scala
+7 −2 core/src/main/scala/org/thp/scalligraph/services/ElementSrv.scala
+9 −8 core/src/main/scala/org/thp/scalligraph/services/IntegrityCheckOps.scala
+22 −19 core/src/main/scala/org/thp/scalligraph/services/StorageSrv.scala
+21 −22 core/src/main/scala/org/thp/scalligraph/services/VertexSrv.scala
+0 −35 core/src/main/scala/org/thp/scalligraph/services/package.scala
+20 −5 core/src/main/scala/org/thp/scalligraph/traversal/Converter.scala
+3 −3 core/src/main/scala/org/thp/scalligraph/traversal/Selectors.scala
+5 −4 core/src/main/scala/org/thp/scalligraph/traversal/Traversal.scala
+50 −50 core/src/main/scala/org/thp/scalligraph/traversal/TraversalOps.scala
+22 −7 database/janusgraph/src/main/scala/org/thp/scalligraph/janus/JanusDatabase.scala
+0 −1 database/neo4j/src/main/scala/org/thp/scalligraph/neo4j/Neo4jDatabase.scala
+0 −1 database/orientdb/src/main/scala/org/thp/scalligraph/orientdb/OrientDatabase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package org.thp.thehive.connector.cortex.services

import org.thp.cortex.client.{CortexClient, TestCortexClientProvider}
import org.thp.cortex.dto.v0.OutputJob
import org.thp.scalligraph.AppBuilder
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.models._
import org.thp.scalligraph.traversal.TraversalOps._
import org.thp.scalligraph.{AppBuilder, EntityName}
import org.thp.thehive.connector.cortex.controllers.v0.ActionCtrl
import org.thp.thehive.connector.cortex.models.{JobStatus, TheHiveCortexSchemaProvider}
import org.thp.thehive.models._
Expand Down Expand Up @@ -35,23 +35,24 @@ class ActionSrvTest extends PlaySpecification with TestAppBuilder {
)
.bindNamedToProvider[Database, BasicDatabaseProvider]("with-thehive-cortex-schema")

def testAppBuilder[A](body: AppBuilder => A): A = testApp { app =>
body(
app
.`override`(
_.bindActor[CortexActor]("cortex-actor")
.bindToProvider[CortexClient, TestCortexClientProvider]
.bind[Connector, TestConnector]
.bindToProvider[Schema, TheHiveCortexSchemaProvider]
)
)
}
def testAppBuilder[A](body: AppBuilder => A): A =
testApp { app =>
body(
app
.`override`(
_.bindActor[CortexActor]("cortex-actor")
.bindToProvider[CortexClient, TestCortexClientProvider]
.bind[Connector, TestConnector]
.bindToProvider[Schema, TheHiveCortexSchemaProvider]
)
)
}

"action service" should {
"execute, create and handle finished action operations" in testApp { app =>
app[Database].roTransaction { implicit graph =>
implicit val entityWrites: OWrites[Entity] = app[ActionCtrl].entityWrites
val task1: Task with Entity = app[TaskSrv].startTraversal.has("title", "case 1 task 1").head
val task1: Task with Entity = app[TaskSrv].startTraversal.has(_.title, "case 1 task 1").head

val richAction = await(app[ActionSrv].execute(task1, None, "respTest1", JsObject.empty))
richAction.workerId shouldEqual "respTest1"
Expand All @@ -77,7 +78,7 @@ class ActionSrvTest extends PlaySpecification with TestAppBuilder {
"handle action related to Task and Log" in testApp { app =>
app[Database].roTransaction { implicit graph =>
implicit val entityWrites: OWrites[Entity] = app[ActionCtrl].entityWrites
val log1 = app[LogSrv].startTraversal.has("message", "log for action test").head
val log1 = app[LogSrv].startTraversal.has(_.message, "log for action test").head

val richAction = await(app[ActionSrv].execute(log1, None, "respTest1", JsObject.empty))
richAction.workerId shouldEqual "respTest1"
Expand All @@ -101,15 +102,15 @@ class ActionSrvTest extends PlaySpecification with TestAppBuilder {
}

app[Database].roTransaction { implicit graph =>
app[TaskSrv].startTraversal.has("title", "case 2 task 2").has("status", "Completed").exists must beTrue
app[TaskSrv].startTraversal.has("title", "case 2 task 2").logs.has("message", "test log from action").exists must beTrue
app[TaskSrv].startTraversal.has(_.title, "case 2 task 2").has(_.status, TaskStatus.Completed).exists must beTrue
app[TaskSrv].startTraversal.has(_.title, "case 2 task 2").logs.has(_.message, "test log from action").exists must beTrue
}
}

"handle action related to an Alert" in testApp { app =>
implicit val entityWrites: OWrites[Entity] = app[ActionCtrl].entityWrites
val alert = app[Database].roTransaction { implicit graph =>
app[AlertSrv].get("testType;testSource;ref2").visible.head
app[AlertSrv].get(EntityName("testType;testSource;ref2")).visible.head
}
alert.read must beFalse
val richAction = await(app[ActionSrv].execute(alert, None, "respTest1", JsObject.empty))
Expand All @@ -122,7 +123,7 @@ class ActionSrvTest extends PlaySpecification with TestAppBuilder {
updatedActionTry must beSuccessfulTry

app[Database].roTransaction { implicit graph =>
val updatedAlert = app[AlertSrv].get("testType;testSource;ref2").visible.richAlert.head // FIXME
val updatedAlert = app[AlertSrv].get(EntityName("testType;testSource;ref2")).visible.richAlert.head // FIXME
updatedAlert.read must beTrue
updatedAlert.tags.map(_.toString) must contain("test tag from action") // TODO
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.thp.thehive.connector.cortex.services

import org.thp.scalligraph.EntityName
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.models.{Database, DummyUserSrv}
import org.thp.scalligraph.traversal.TraversalOps._
Expand All @@ -17,7 +18,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"return task info" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
task <- app[TaskSrv].startTraversal.has("title", "case 1 task 1").getOrFail("Task")
task <- app[TaskSrv].startTraversal.has(_.title, "case 1 task 1").getOrFail("Task")
(title, tlp, pap) <- app[EntityHelper].entityInfo(task)
} yield (title, tlp, pap)
} must beASuccessfulTry.which {
Expand All @@ -31,7 +32,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"return observable info" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
observable <- app[ObservableSrv].startTraversal.has("message", "Some weird domain").getOrFail("Observable")
observable <- app[ObservableSrv].startTraversal.has(_.message, "Some weird domain").getOrFail("Observable")
(title, tlp, pap) <- app[EntityHelper].entityInfo(observable)
} yield (title, tlp, pap)
} must beASuccessfulTry.which {
Expand All @@ -45,7 +46,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"find a manageable entity only (task)" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
task <- app[TaskSrv].startTraversal.has("title", "case 1 task 1").getOrFail("Task")
task <- app[TaskSrv].startTraversal.has(_.title, "case 1 task 1").getOrFail("Task")
t <- app[EntityHelper].get("Task", task._id, Permissions.manageAction)
} yield t
} must beSuccessfulTry
Expand All @@ -54,7 +55,7 @@ class EntityHelperTest extends PlaySpecification with TestAppBuilder {
"find a manageable entity only (alert)" in testApp { app =>
app[Database].roTransaction { implicit graph =>
for {
alert <- app[AlertSrv].get("testType;testSource;ref2").visible.getOrFail("Alert")
alert <- app[AlertSrv].get(EntityName("testType;testSource;ref2")).visible.getOrFail("Alert")
t <- app[EntityHelper].get("Alert", alert._id, Permissions.manageAction)
} yield t
} must beSuccessfulTry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class JobSrvTest extends PlaySpecification with TestAppBuilder {

val createdJobTry = app[Database].tryTransaction { implicit graph =>
for {
observable <- app[ObservableSrv].startTraversal.has("message", "hello world").getOrFail("Observable")
observable <- app[ObservableSrv].startTraversal.has(_.message, "hello world").getOrFail("Observable")
createdJob <- app[JobSrv].create(job, observable)
} yield createdJob
}
Expand All @@ -69,14 +69,14 @@ class JobSrvTest extends PlaySpecification with TestAppBuilder {
(updatedJob.report.get \ "data").as[String] shouldEqual "imageedit_2_3904987689.jpg"

app[Database].roTransaction { implicit graph =>
app[JobSrv].get(updatedJob).observable.has("message", "hello world").exists must beTrue
app[JobSrv].get(updatedJob).observable.has(_.message, "hello world").exists must beTrue
app[JobSrv].get(updatedJob).reportObservables.toList.length must equalTo(2).updateMessage { s =>
s"$s\nreport observables are : ${app[JobSrv].get(updatedJob).reportObservables.richObservable.toList.mkString("\n")}"
}

for {
audit <- app[AuditSrv].startTraversal.has("objectId", updatedJob._id).getOrFail("Audit")
organisation <- app[OrganisationSrv].get("cert").getOrFail("Organisation")
audit <- app[AuditSrv].startTraversal.has(_.objectId, updatedJob._id.toString).getOrFail("Audit")
organisation <- app[OrganisationSrv].getByName("cert").getOrFail("Organisation")
user <- app[UserSrv].startTraversal.getByName("certuser@thehive.local").getOrFail("User")
} yield new JobFinished().filter(audit, Some(updatedJob), organisation, Some(user))
} must beASuccessfulTry(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ResponderSrvTest extends PlaySpecification with TestAppBuilder {
"responder service" should {
"fetch responders by type" in testApp { app =>
val task = app[Database].roTransaction { implicit graph =>
app[TaskSrv].startTraversal.has("title", "case 1 task 1").head
app[TaskSrv].startTraversal.has(_.title, "case 1 task 1").head
}

val r = await(app[ResponderSrv].getRespondersByType("case_task", task._id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,21 @@ class MispImportSrv @Inject() (
Future.fromTry {
logger.info("Removing old observables")
db.tryTransaction { implicit graph =>
alertSrv
.get(alert)
.observables
.filter(
_.or(
_.has("_updatedAt", P.lt(startSyncDate)),
_.and(_.hasNot("_updatedAt"), _.has("_createdAt", P.lt(startSyncDate)))
)
alertSrv
.get(alert)
.observables
.filter(
_.or(
_.has(_._updatedAt, P.lt(startSyncDate)),
_.and(_.hasNot(_._updatedAt), _.has(_._createdAt, P.lt(startSyncDate)))
)
.toIterator
.toTry { obs =>
logger.info(s"Remove $obs")
observableSrv.remove(obs)
}
}
.map(_ => ())
)
.toIterator
.toTry { obs =>
logger.info(s"Remove $obs")
observableSrv.remove(obs)
}
}.map(_ => ())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,23 @@ class TheHiveMispClient(
whitelistTags
) {

@Inject() def this(config: TheHiveMispClientConfig, mat: Materializer) = this(
config.name,
config.url,
config.auth,
new ProxyWS(config.wsConfig, mat),
config.maxAge,
config.excludedOrganisations,
config.excludedTags,
config.whitelistTags,
config.purpose,
config.caseTemplate,
config.artifactTags,
config.exportCaseTags,
config.includedTheHiveOrganisations,
config.excludedTheHiveOrganisations
)
@Inject() def this(config: TheHiveMispClientConfig, mat: Materializer) =
this(
config.name,
config.url,
config.auth,
new ProxyWS(config.wsConfig, mat),
config.maxAge,
config.excludedOrganisations,
config.excludedTags,
config.whitelistTags,
config.purpose,
config.caseTemplate,
config.artifactTags,
config.exportCaseTags,
config.includedTheHiveOrganisations,
config.excludedTheHiveOrganisations
)

val (canImport, canExport) = purpose match {
case MispPurpose.ImportAndExport => (true, true)
Expand All @@ -140,9 +141,9 @@ class TheHiveMispClient(
def organisationFilter(organisationSteps: Traversal.V[Organisation]): Traversal.V[Organisation] = {
val includedOrgs =
if (includedTheHiveOrganisations.contains("*") || includedTheHiveOrganisations.isEmpty) organisationSteps
else organisationSteps.has("name", P.within(includedTheHiveOrganisations))
else organisationSteps.has(_.name, P.within(includedTheHiveOrganisations: _*))
if (excludedTheHiveOrganisations.isEmpty) includedOrgs
else includedOrgs.has("name", P.without(excludedTheHiveOrganisations))
else includedOrgs.has(_.name, P.without(excludedTheHiveOrganisations: _*))
}

override def getStatus(implicit ec: ExecutionContext): Future[JsObject] =
Expand Down
102 changes: 52 additions & 50 deletions thehive/app/org/thp/thehive/controllers/dav/VFS.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,57 @@ import org.thp.thehive.services.TaskOps._
@Singleton
class VFS @Inject() (caseSrv: CaseSrv) {

def get(path: List[String])(implicit graph: Graph, authContext: AuthContext): Seq[Resource] = path match {
case Nil | "" :: Nil => List(StaticResource(""))
case "cases" :: Nil => List(StaticResource(""))
case "cases" :: cid :: Nil => caseSrv.startTraversal.getByNumber(cid.toInt).toSeq.map(EntityResource(_, ""))
case "cases" :: cid :: "observables" :: Nil => List(StaticResource(""))
case "cases" :: cid :: "tasks" :: Nil => List(StaticResource(""))
case "cases" :: cid :: "observables" :: aid :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.observables
.attachments
.has("attachmentId", aid)
.toSeq
.map(AttachmentResource(_, emptyId = true))
case "cases" :: cid :: "tasks" :: aid :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.tasks
.logs
.attachments
.has("attachmentId", aid)
.toSeq
.map(AttachmentResource(_, emptyId = true))
case _ => Nil
}
def get(path: List[String])(implicit graph: Graph, authContext: AuthContext): Seq[Resource] =
path match {
case Nil | "" :: Nil => List(StaticResource(""))
case "cases" :: Nil => List(StaticResource(""))
case "cases" :: cid :: Nil => caseSrv.startTraversal.getByNumber(cid.toInt).toSeq.map(EntityResource(_, ""))
case "cases" :: cid :: "observables" :: Nil => List(StaticResource(""))
case "cases" :: cid :: "tasks" :: Nil => List(StaticResource(""))
case "cases" :: cid :: "observables" :: aid :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.observables
.attachments
.has(_.attachmentId, aid)
.toSeq
.map(AttachmentResource(_, emptyId = true))
case "cases" :: cid :: "tasks" :: aid :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.tasks
.logs
.attachments
.has(_.attachmentId, aid)
.toSeq
.map(AttachmentResource(_, emptyId = true))
case _ => Nil
}

def list(path: List[String])(implicit graph: Graph, authContext: AuthContext): Seq[Resource] = path match {
case Nil | "" :: Nil => List(StaticResource("cases"))
case "cases" :: Nil => caseSrv.startTraversal.visible.toSeq.map(c => EntityResource(c, c.number.toString))
case "cases" :: cid :: Nil => List(StaticResource("observables"), StaticResource("tasks"))
case "cases" :: cid :: "observables" :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.observables
.attachments
.domainMap(AttachmentResource(_, emptyId = false))
.toSeq
case "cases" :: cid :: "tasks" :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.tasks
.logs
.attachments
.domainMap(AttachmentResource(_, emptyId = false))
.toSeq
case _ => Nil
}
def list(path: List[String])(implicit graph: Graph, authContext: AuthContext): Seq[Resource] =
path match {
case Nil | "" :: Nil => List(StaticResource("cases"))
case "cases" :: Nil => caseSrv.startTraversal.visible.toSeq.map(c => EntityResource(c, c.number.toString))
case "cases" :: cid :: Nil => List(StaticResource("observables"), StaticResource("tasks"))
case "cases" :: cid :: "observables" :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.observables
.attachments
.domainMap(AttachmentResource(_, emptyId = false))
.toSeq
case "cases" :: cid :: "tasks" :: Nil =>
caseSrv
.startTraversal
.getByNumber(cid.toInt)
.tasks
.logs
.attachments
.domainMap(AttachmentResource(_, emptyId = false))
.toSeq
case _ => Nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ trait AuditRenderer {
def jsonSummary(auditSrv: AuditSrv, requestId: String)(implicit graph: Graph): JsObject =
auditSrv
.startTraversal
.has("requestId", requestId)
.has("mainAction", false)
.has(_.requestId, requestId)
.has(_.mainAction, false)
.group(
_.byValue(_.objectType),
_.by(_.groupCount(_.byValue(_.action)))
Expand Down
2 changes: 1 addition & 1 deletion thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class PublicCase @Inject() (
.property("computed.handlingDurationInHours", UMapping.long)(
_.select(
_.coalesce(
_.has("endDate")
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
Expand Down
Loading

0 comments on commit 8472167

Please sign in to comment.