Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade remove #1606

Merged
merged 19 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ maxColumn = 150

align.openParenCallSite = false
align.openParenDefnSite = false
align.tokens.add = [{code = "must"}]
newlines.alwaysBeforeTopLevelStatements = false
rewrite.rules = [
RedundantBraces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ActionOperationSrv @Inject() (
case AddLogToTask(content, _) =>
for {
t <- relatedTask.fold[Try[Task with Entity]](Failure(InternalError("Unable to apply action AddLogToTask without task")))(Success(_))
_ <- logSrv.create(Log(content, new Date(), deleted = false), t, None)
_ <- logSrv.create(Log(content, new Date()), t, None)
} yield updateOperation(operation)

case AddArtifactToCase(data, dataType, message) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import org.thp.thehive.services.CaseOps._
import org.thp.thehive.services.ObservableOps._
import org.thp.thehive.services.OrganisationOps._
import org.thp.thehive.services.{AttachmentSrv, ObservableSrv, ObservableTypeSrv, ReportTagSrv}
import play.api.libs.json.Json
import play.api.libs.json.{JsObject, JsString, Json}

import java.nio.file.Files
import java.util.{Date, Map => JMap}
Expand Down Expand Up @@ -92,7 +92,13 @@ class JobSrv @Inject() (
create(fromCortexOutputJob(cortexOutputJob).copy(cortexId = cortexId), observable.observable)
})
_ <- Future.fromTry(db.tryTransaction { implicit graph =>
auditSrv.job.create(createdJob.job, observable.observable, createdJob.toJson)
auditSrv
.job
.create(
createdJob.job,
observable.observable,
createdJob.toJson.as[JsObject] + ("objectType" -> JsString("Observable")) + ("objectId" -> JsString(observable._id.toString))
)
})
_ = cortexActor ! CheckJob(Some(createdJob._id), cortexOutputJob.id, None, cortexClient.name, authContext)
} yield createdJob
Expand Down Expand Up @@ -171,7 +177,14 @@ class JobSrv @Inject() (
.update(_.endDate, endDate)
.getOrFail("Job")
observable <- get(job).observable.getOrFail("Observable")
_ <- auditSrv.job.update(job, observable, Json.obj("status" -> status, "endDate" -> endDate))
_ <-
auditSrv
.job
.update(
job,
observable,
Json.obj("status" -> status, "endDate" -> endDate, "objectType" -> "Observable", "objectId" -> observable._id.toString)
)
} yield job
}
}
Expand Down
2 changes: 0 additions & 2 deletions dto/src/main/scala/org/thp/thehive/dto/v1/Case.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package org.thp.thehive.dto.v1

import ai.x.play.json.Encoders.encoder
import ai.x.play.json.Jsonx

import java.util.Date
import org.thp.scalligraph.controllers.WithParser
import play.api.libs.json._

Expand Down
1 change: 0 additions & 1 deletion dto/src/main/scala/org/thp/thehive/dto/v1/Log.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ case class OutputLog(
message: String,
date: Date,
attachment: Option[OutputAttachment] = None,
deleted: Boolean,
owner: String,
extraData: JsObject
)
Expand Down
2 changes: 0 additions & 2 deletions dto/src/main/scala/org/thp/thehive/dto/v1/Taxonomy.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.thp.thehive.dto.v1

import play.api.libs.json.Json.WithDefaultValues

import java.util.Date
import play.api.libs.json.{JsObject, Json, OFormat}

import java.util.Date
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.thp.thehive.migration

import java.io.File

import akka.actor.ActorSystem
import akka.stream.Materializer
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import play.api.libs.logback.LogbackLoggerConfigurator
import play.api.{Configuration, Environment}
import scopt.OParser

import java.io.File
import scala.collection.JavaConverters._
import scala.concurrent.duration.{Duration, DurationInt}
import scala.concurrent.{Await, ExecutionContext}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,11 @@ trait Conversion {
metaData <- json.validate[MetaData]
message <- (json \ "message").validate[String]
date <- (json \ "startDate").validate[Date]
deleted = (json \ "status").asOpt[String].contains("Deleted")
attachment =
(json \ "attachment")
.asOpt[Attachment]
.map(a => InputAttachment(a.name, a.size, a.contentType, a.hashes.map(_.toString), readAttachment(a.id)))
} yield InputLog(metaData, Log(message, date, deleted), attachment.toSeq)
} yield InputLog(metaData, Log(message, date), attachment.toSeq)
}

implicit val alertReads: Reads[InputAlert] = Reads[InputAlert] { json =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.thp.thehive.migration.th3

import java.nio.file.{Files, Paths}
import java.security.KeyStore
import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Sink, Source}
Expand All @@ -12,9 +10,6 @@ import com.sksamuel.elastic4s.requests.bulk.BulkResponseItem
import com.sksamuel.elastic4s.requests.searches.{SearchHit, SearchRequest}
import com.sksamuel.elastic4s.streams.ReactiveElastic.ReactiveElastic
import com.sksamuel.elastic4s.streams.{RequestBuilder, ResponseListener}

import javax.inject.{Inject, Singleton}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials}
import org.apache.http.client.CredentialsProvider
import org.apache.http.client.config.RequestConfig
Expand All @@ -26,6 +21,10 @@ import play.api.inject.ApplicationLifecycle
import play.api.libs.json.JsObject
import play.api.{Configuration, Logger}

import java.nio.file.{Files, Paths}
import java.security.KeyStore
import javax.inject.{Inject, Singleton}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
import scala.collection.JavaConverters._
import scala.concurrent.duration.DurationInt
import scala.concurrent.{Await, ExecutionContext, Future, Promise}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import akka.stream.scaladsl.Source
import akka.stream.stage.{AsyncCallback, GraphStage, GraphStageLogic, OutHandler}
import akka.stream.{Attributes, Materializer, Outlet, SourceShape}
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.{ElasticRequest, Show}
import com.sksamuel.elastic4s.requests.searches.{SearchHit, SearchRequest, SearchResponse}
import javax.inject.{Inject, Singleton}
import com.sksamuel.elastic4s.{ElasticRequest, Show}
import org.thp.scalligraph.{InternalError, SearchError}
import play.api.libs.json._
import play.api.{Configuration, Logger}

import javax.inject.{Inject, Singleton}
import scala.collection.mutable
import scala.concurrent.duration.{DurationLong, FiniteDuration}
import scala.concurrent.{ExecutionContext, Future}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.thp.thehive.migration.th3

import com.sksamuel.elastic4s.ElasticDsl._
import javax.inject.{Inject, Singleton}
import org.thp.scalligraph.NotFoundError
import play.api.libs.json.JsObject

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

@Singleton
Expand Down
54 changes: 14 additions & 40 deletions migration/src/main/scala/org/thp/thehive/migration/th3/Input.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.stream.scaladsl.Source
import akka.util.ByteString
import com.google.inject.Guice
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.requests.searches.queries.RangeQuery
import com.sksamuel.elastic4s.requests.searches.queries.{Query, RangeQuery}
import com.sksamuel.elastic4s.requests.searches.queries.term.TermsQuery
import net.codingwell.scalaguice.ScalaModule
import org.thp.thehive.migration
Expand Down Expand Up @@ -221,63 +221,37 @@ class Input @Inject() (configuration: Configuration, dbFind: DBFind, dbGet: DBGe
)._2

override def listCaseTaskLogs(filter: Filter): Source[Try[(String, InputLog)], NotUsed] =
dbFind(Some("all"), Nil)(indexName =>
search(indexName).query(
bool(
Seq(
termQuery("relations", "case_task_log"),
hasParentQuery(
"case_task",
hasParentQuery("case", bool(caseFilter(filter), Nil, Nil), score = false),
score = false
)
),
Nil,
Nil
)
)
)._1
.readWithParent[InputLog](json => Try((json \ "_parent").as[String]))
listCaseTaskLogs(bool(caseFilter(filter), Nil, Nil))

override def countCaseTaskLogs(filter: Filter): Future[Long] =
dbFind(Some("0-0"), Nil)(indexName =>
search(indexName)
.query(
bool(
Seq(
termQuery("relations", "case_task_log"),
hasParentQuery(
"case_task",
hasParentQuery("case", bool(caseFilter(filter), Nil, Nil), score = false),
score = false
)
),
Nil,
Nil
)
)
)._2
countCaseTaskLogs(bool(caseFilter(filter), Nil, Nil))

override def listCaseTaskLogs(caseId: String): Source[Try[(String, InputLog)], NotUsed] =
listCaseTaskLogs(idsQuery(caseId))

override def countCaseTaskLogs(caseId: String): Future[Long] =
countCaseTaskLogs(idsQuery(caseId))

private def listCaseTaskLogs(query: Query): Source[Try[(String, InputLog)], NotUsed] =
dbFind(Some("all"), Nil)(indexName =>
search(indexName).query(
bool(
Seq(
termQuery("relations", "case_task_log"),
hasParentQuery(
"case_task",
hasParentQuery("case", idsQuery(caseId), score = false),
hasParentQuery("case", query, score = false),
score = false
)
),
Nil,
Nil
Seq(termQuery("status", "deleted"))
)
)
)._1
.readWithParent[InputLog](json => Try((json \ "_parent").as[String]))

override def countCaseTaskLogs(caseId: String): Future[Long] =
private def countCaseTaskLogs(query: Query): Future[Long] =
dbFind(Some("0-0"), Nil)(indexName =>
search(indexName)
.query(
Expand All @@ -286,12 +260,12 @@ class Input @Inject() (configuration: Configuration, dbFind: DBFind, dbGet: DBGe
termQuery("relations", "case_task_log"),
hasParentQuery(
"case_task",
hasParentQuery("case", idsQuery(caseId), score = false),
hasParentQuery("case", query, score = false),
score = false
)
),
Nil,
Nil
Seq(termQuery("status", "deleted"))
)
)
)._2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package org.thp.thehive.migration.th4

import akka.actor.ActorRef
import com.google.inject.name.Named

import javax.inject.{Inject, Provider, Singleton}
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.models.{Database, Entity}
import org.thp.scalligraph.services.EventSrv
import org.thp.scalligraph.traversal.Graph
import org.thp.thehive.models.Audit
import org.thp.thehive.services.{AuditSrv, UserSrv}

import javax.inject.{Inject, Provider, Singleton}
import scala.util.{Success, Try}

@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class MispImportSrv @Inject() (
.toIterator
.foreach { obs =>
logger.debug(s"Delete $obs")
observableSrv.remove(obs).recover {
observableSrv.delete(obs).recover {
case error => logger.error(s"Fail to delete observable $obs", error)
}
}
Expand Down
5 changes: 3 additions & 2 deletions thehive/app/org/thp/thehive/controllers/dav/VFS.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.thp.thehive.controllers.dav

import javax.inject.{Inject, Singleton}
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.traversal.Graph
import org.thp.scalligraph.traversal.TraversalOps._
import org.thp.thehive.services.CaseOps._
import org.thp.thehive.services.{CaseSrv, OrganisationSrv}
import org.thp.thehive.services.LogOps._
import org.thp.thehive.services.ObservableOps._
import org.thp.thehive.services.TaskOps._
import org.thp.thehive.services.{CaseSrv, OrganisationSrv}

import javax.inject.{Inject, Singleton}

@Singleton
class VFS @Inject() (caseSrv: CaseSrv, organisationSrv: OrganisationSrv) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import org.thp.scalligraph.models.UMapping
import org.thp.scalligraph.traversal.TraversalOps._
import org.thp.scalligraph.traversal._
import org.thp.thehive.controllers.v0.Conversion._
import org.thp.thehive.controllers.v1.Conversion.{patternOutput, richProcedureRenderer}
import org.thp.thehive.models._
import org.thp.thehive.services.AlertOps._
import org.thp.thehive.services.AuditOps._
import org.thp.thehive.services.CaseOps._
import org.thp.thehive.services.LogOps._
import org.thp.thehive.services.ProcedureOps._
import org.thp.thehive.services.ObservableOps._
import org.thp.thehive.services.PatternOps._
import org.thp.thehive.services.ProcedureOps._
import org.thp.thehive.services.TaskOps._
import org.thp.thehive.services._
import play.api.libs.json.{JsNumber, JsObject, JsString}
import org.thp.thehive.controllers.v1.Conversion.{patternOutput, richProcedureRenderer}

import java.util.{Date, List => JList, Map => JMap}

trait AuditRenderer {
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 @@ -140,7 +140,7 @@ class CaseCtrl @Inject() (
.get(EntityIdOrName(caseIdOrNumber))
.can(Permissions.manageCase)
.getOrFail("Case")
_ <- caseSrv.remove(c)
_ <- caseSrv.delete(c)
} yield Results.NoContent
}

Expand Down
3 changes: 1 addition & 2 deletions thehive/app/org/thp/thehive/controllers/v0/Conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ object Conversion {
.withFieldComputed(_.message, _.message)
.withFieldComputed(_.startDate, _._createdAt)
.withFieldComputed(_.owner, _._createdBy)
.withFieldComputed(_.status, l => if (l.deleted) "Deleted" else "Ok")
.withFieldConst(_.status, "Ok")
.withFieldComputed(_.attachment, _.attachments.headOption.map(_.toValue))
.transform
)
Expand All @@ -336,7 +336,6 @@ object Conversion {
inputLog
.into[Log]
.withFieldConst(_.date, new Date)
.withFieldConst(_.deleted, false)
.transform
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.thp.thehive.controllers.v0

import javax.inject.{Inject, Named, Singleton}
import org.thp.scalligraph.EntityIdOrName
import org.thp.scalligraph.controllers.{Entrypoint, FieldsParser}
import org.thp.scalligraph.models.{Database, Entity, UMapping}
Expand All @@ -14,6 +13,7 @@ import org.thp.thehive.services.CustomFieldSrv
import play.api.libs.json.{JsNumber, JsObject}
import play.api.mvc.{Action, AnyContent, Results}

import javax.inject.{Inject, Named, Singleton}
import scala.util.Success

@Singleton
Expand Down
Loading