From 2797ad0240146a85762164f1abc39423a50d9695 Mon Sep 17 00:00:00 2001 From: To-om Date: Thu, 4 Mar 2021 17:18:00 +0100 Subject: [PATCH] #1454 Invalidate describe cache when a customfield is added, removed and updated --- .../thehive/controllers/v0/DescribeCtrl.scala | 2 +- .../thehive/controllers/v1/DescribeCtrl.scala | 2 +- .../thp/thehive/services/CustomFieldSrv.scala | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/thehive/app/org/thp/thehive/controllers/v0/DescribeCtrl.scala b/thehive/app/org/thp/thehive/controllers/v0/DescribeCtrl.scala index 1acfc28812..cd3d96db2b 100644 --- a/thehive/app/org/thp/thehive/controllers/v0/DescribeCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v0/DescribeCtrl.scala @@ -88,7 +88,7 @@ class DescribeCtrl @Inject() ( ).toOption def entityDescriptions: Seq[EntityDescription] = - cacheApi.getOrElseUpdate(s"describe.v0", cacheExpire) { + cacheApi.getOrElseUpdate("describe.v0", cacheExpire) { Seq( EntityDescription("case", "/case", caseCtrl.publicData.publicProperties.list.flatMap(propertyToJson("case", _))), EntityDescription("case_task", "/case/task", taskCtrl.publicData.publicProperties.list.flatMap(propertyToJson("case_task", _))), diff --git a/thehive/app/org/thp/thehive/controllers/v1/DescribeCtrl.scala b/thehive/app/org/thp/thehive/controllers/v1/DescribeCtrl.scala index 337a079f8c..785dcf07b3 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/DescribeCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/DescribeCtrl.scala @@ -94,7 +94,7 @@ class DescribeCtrl @Inject() ( ).toOption def entityDescriptions: Seq[EntityDescription] = - cacheApi.getOrElseUpdate(s"describe.v1", cacheExpire) { + cacheApi.getOrElseUpdate("describe.v1", cacheExpire) { Seq( EntityDescription("alert", "listAlert", alertCtrl.publicProperties.list.flatMap(propertyToJson("alert", _))), EntityDescription("audit", "listAudit", auditCtrl.publicProperties.list.flatMap(propertyToJson("audit", _))), diff --git a/thehive/app/org/thp/thehive/services/CustomFieldSrv.scala b/thehive/app/org/thp/thehive/services/CustomFieldSrv.scala index edfeae0524..5b0c9af5be 100644 --- a/thehive/app/org/thp/thehive/services/CustomFieldSrv.scala +++ b/thehive/app/org/thp/thehive/services/CustomFieldSrv.scala @@ -12,6 +12,7 @@ import org.thp.scalligraph.{EntityIdOrName, RichSeq} import org.thp.thehive.controllers.v1.Conversion._ import org.thp.thehive.models._ import org.thp.thehive.services.CustomFieldOps._ +import play.api.cache.SyncCacheApi import play.api.libs.json.{JsObject, JsValue} import java.util.{Map => JMap} @@ -19,11 +20,17 @@ import javax.inject.{Inject, Named, Singleton} import scala.util.{Success, Try} @Singleton -class CustomFieldSrv @Inject() (auditSrv: AuditSrv, organisationSrv: OrganisationSrv, @Named("integrity-check-actor") integrityCheckActor: ActorRef) - extends VertexSrv[CustomField] { +class CustomFieldSrv @Inject() ( + auditSrv: AuditSrv, + organisationSrv: OrganisationSrv, + @Named("integrity-check-actor") integrityCheckActor: ActorRef, + cacheApi: SyncCacheApi +) extends VertexSrv[CustomField] { override def createEntity(e: CustomField)(implicit graph: Graph, authContext: AuthContext): Try[CustomField with Entity] = { integrityCheckActor ! EntityAdded("CustomField") + cacheApi.remove("describe.v0") + cacheApi.remove("describe.v1") super.createEntity(e) } @@ -37,6 +44,8 @@ class CustomFieldSrv @Inject() (auditSrv: AuditSrv, organisationSrv: Organisatio def delete(c: CustomField with Entity, force: Boolean)(implicit graph: Graph, authContext: AuthContext): Try[Unit] = { get(c).remove() // TODO use force + cacheApi.remove("describe.v0") + cacheApi.remove("describe.v1") organisationSrv.getOrFail(authContext.organisation).flatMap { organisation => auditSrv.customField.delete(c, organisation) } @@ -58,7 +67,11 @@ class CustomFieldSrv @Inject() (auditSrv: AuditSrv, organisationSrv: Organisatio customFieldSteps .clone() .getOrFail("CustomFields") - .flatMap(auditSrv.customField.update(_, updatedFields)) + .flatMap { cf => + cacheApi.remove("describe.v0") + cacheApi.remove("describe.v1") + auditSrv.customField.update(cf, updatedFields) + } } override def getByName(name: String)(implicit graph: Graph): Traversal.V[CustomField] =