-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
319 additions
and
87 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
108 changes: 108 additions & 0 deletions
108
thehive/app/org/thp/thehive/services/IntegrityCheckActor.scala
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,108 @@ | ||
package org.thp.thehive.services | ||
|
||
import java.util.{Set => JSet} | ||
|
||
import akka.actor.{Actor, ActorRef, ActorSystem, Cancellable, PoisonPill, Props} | ||
import akka.cluster.singleton.{ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings} | ||
import com.google.inject.util.Types | ||
import com.google.inject.{Injector, Key, TypeLiteral} | ||
import javax.inject.{Inject, Provider, Singleton} | ||
import org.thp.scalligraph.auth.AuthContext | ||
import org.thp.scalligraph.models.{Database, Schema} | ||
import org.thp.scalligraph.services.{GenIntegrityCheckOps, IntegrityCheckOps} | ||
import org.thp.thehive.GuiceAkkaExtension | ||
import play.api.Configuration | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.immutable | ||
import scala.concurrent.duration.{Duration, FiniteDuration} | ||
import scala.util.Success | ||
|
||
object IntegrityCheckActor { | ||
case class EntityAdded(name: String) | ||
} | ||
|
||
class IntegrityCheckActor() extends Actor { | ||
case class NeedCheck(name: String) | ||
case class Check(name: String) | ||
import IntegrityCheckActor._ | ||
|
||
lazy val injector: Injector = GuiceAkkaExtension(context.system).injector | ||
lazy val configuration: Configuration = injector.getInstance(classOf[Configuration]) | ||
lazy val integrityCheckOps: immutable.Set[IntegrityCheckOps[_ <: Product]] = injector | ||
.getInstance(Key.get(TypeLiteral.get(Types.setOf(classOf[GenIntegrityCheckOps])))) | ||
.asInstanceOf[JSet[IntegrityCheckOps[_ <: Product]]] | ||
.asScala | ||
.toSet | ||
lazy val db: Database = injector.getInstance(classOf[Database]) | ||
lazy val schema: Schema = injector.getInstance(classOf[Schema]) | ||
lazy val defaultInitalDelay: FiniteDuration = configuration.get[FiniteDuration]("integrityCheck.default.initialDelay") | ||
def initialDelay(name: String): FiniteDuration = | ||
configuration.getOptional[FiniteDuration](s"integrityCheck.$name.initialDelay").getOrElse(defaultInitalDelay) | ||
lazy val defaultInterval: FiniteDuration = configuration.get[FiniteDuration]("integrityCheck.default.interval") | ||
def interval(name: String): FiniteDuration = | ||
configuration.getOptional[FiniteDuration](s"integrityCheck.$name.interval").getOrElse(defaultInitalDelay) | ||
|
||
lazy val integrityCheckMap: Map[String, IntegrityCheckOps[_]] = { | ||
|
||
integrityCheckOps.map(d => d.name -> d).toMap | ||
} | ||
def check(name: String): Unit = integrityCheckMap.get(name).foreach(_.check()) | ||
|
||
override def preStart(): Unit = { | ||
implicit val authContext: AuthContext = LocalUserSrv.getSystemAuthContext | ||
integrityCheckOps.foreach { integrityCheck => | ||
db.tryTransaction { implicit graph => | ||
Success(integrityCheck.initialCheck()) | ||
} | ||
} | ||
integrityCheckOps.foreach { integrityCheck => | ||
Success(integrityCheck.check()) | ||
} | ||
} | ||
override def receive: Receive = receive(Map.empty) | ||
def receive(states: Map[String, (Boolean, Cancellable)]): Receive = { | ||
case EntityAdded(name) => | ||
context.system.scheduler.scheduleOnce(initialDelay(name), self, NeedCheck(name))(context.system.dispatcher) | ||
() | ||
case NeedCheck(name) if !states.contains(name) => // initial check | ||
check(name) | ||
val timer = context.system.scheduler.scheduleAtFixedRate(Duration.Zero, interval(name), self, Check(name))(context.system.dispatcher) | ||
context.become(receive(states + (name -> (false -> timer)))) | ||
case NeedCheck(name) => | ||
if (!states(name)._1) { | ||
val timer = states(name)._2 | ||
context.become(receive(states + (name -> (true -> timer)))) | ||
} | ||
case Check(name) if states.get(name).fold(false)(_._1) => // stats.needCheck == true | ||
check(name) | ||
val timer = states(name)._2 | ||
context.become(receive(states + (name -> (false -> timer)))) | ||
case Check(name) => | ||
states(name)._2.cancel() | ||
context.become(receive(states - name)) | ||
} | ||
} | ||
|
||
@Singleton | ||
class IntegrityCheckActorProvider @Inject() (system: ActorSystem) extends Provider[ActorRef] { | ||
override lazy val get: ActorRef = { | ||
val singletonManager = | ||
system.actorOf( | ||
ClusterSingletonManager.props( | ||
singletonProps = Props[IntegrityCheckActor], | ||
terminationMessage = PoisonPill, | ||
settings = ClusterSingletonManagerSettings(system) | ||
), | ||
name = "integrityCheckSingletonManager" | ||
) | ||
|
||
system.actorOf( | ||
ClusterSingletonProxy.props( | ||
singletonManagerPath = singletonManager.path.toStringWithoutAddress, | ||
settings = ClusterSingletonProxySettings(system) | ||
), | ||
name = "integrityCheckSingletonProxy" | ||
) | ||
} | ||
} |
Oops, something went wrong.