-
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.
#1404 Provide database instance to all service only when initialisati…
…on is complete
- Loading branch information
Showing
100 changed files
with
1,002 additions
and
643 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
12 changes: 6 additions & 6 deletions
12
...nector/src/main/scala/org/thp/thehive/connector/cortex/controllers/v0/ResponderCtrl.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
11 changes: 6 additions & 5 deletions
11
cortex/connector/src/main/scala/org/thp/thehive/connector/cortex/models/CortexSchema.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
10 changes: 0 additions & 10 deletions
10
cortex/connector/src/main/scala/org/thp/thehive/connector/cortex/models/SchemaUpdater.scala
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
...connector/src/main/scala/org/thp/thehive/connector/cortex/models/SchemaUpdaterActor.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,84 @@ | ||
package org.thp.thehive.connector.cortex.models | ||
|
||
import akka.actor.{Actor, ActorRef, ActorSystem, PoisonPill, Props} | ||
import akka.cluster.singleton.{ClusterSingletonManager, ClusterSingletonManagerSettings, ClusterSingletonProxy, ClusterSingletonProxySettings} | ||
import akka.pattern.ask | ||
import akka.util.Timeout | ||
import javax.inject.{Inject, Named, Provider, Singleton} | ||
import org.thp.scalligraph.models.Database | ||
import org.thp.thehive.services.LocalUserSrv | ||
import play.api.Logger | ||
|
||
import scala.concurrent.Await | ||
import scala.concurrent.duration.DurationInt | ||
import scala.util.Try | ||
|
||
@Singleton | ||
class DatabaseProvider @Inject() ( | ||
cortexSchema: CortexSchemaDefinition, | ||
@Named("with-thehive-schema") database: Database, | ||
actorSystem: ActorSystem | ||
) extends Provider[Database] { | ||
import SchemaUpdaterActor._ | ||
lazy val schemaUpdaterActor: ActorRef = { | ||
val singletonManager = | ||
actorSystem.actorOf( | ||
ClusterSingletonManager.props( | ||
singletonProps = Props(classOf[SchemaUpdaterActor], cortexSchema, database), | ||
terminationMessage = PoisonPill, | ||
settings = ClusterSingletonManagerSettings(actorSystem) | ||
), | ||
name = "theHiveCortexSchemaUpdaterSingletonManager" | ||
) | ||
|
||
actorSystem.actorOf( | ||
ClusterSingletonProxy.props( | ||
singletonManagerPath = singletonManager.path.toStringWithoutAddress, | ||
settings = ClusterSingletonProxySettings(actorSystem) | ||
), | ||
name = "theHiveCortexSchemaUpdaterSingletonProxy" | ||
) | ||
} | ||
|
||
override def get(): Database = { | ||
implicit val timeout: Timeout = Timeout(5.minutes) | ||
Await.result(schemaUpdaterActor ? RequestDBStatus, timeout.duration) match { | ||
case DBStatus(status) => | ||
status.get | ||
database | ||
} | ||
} | ||
} | ||
|
||
object SchemaUpdaterActor { | ||
case object RequestDBStatus | ||
case class DBStatus(status: Try[Unit]) | ||
} | ||
|
||
class SchemaUpdaterActor @Inject() (cortexSchema: CortexSchemaDefinition, database: Database) extends Actor { | ||
import SchemaUpdaterActor._ | ||
lazy val logger: Logger = Logger(getClass) | ||
|
||
def update(): Try[Unit] = | ||
cortexSchema | ||
.update(database)(LocalUserSrv.getSystemAuthContext) | ||
.recover { | ||
case error => logger.error(s"Database with CortexSchema schema update failure", error) | ||
} | ||
|
||
override def receive: Receive = { | ||
case RequestDBStatus => | ||
val status = update() | ||
sender ! DBStatus(status) | ||
context.become(receive(status)) | ||
} | ||
|
||
def receive(status: Try[Unit]): Receive = { | ||
case RequestDBStatus => | ||
status.fold({ _ => | ||
val newStatus = update() | ||
sender ! DBStatus(newStatus) | ||
context.become(receive(newStatus)) | ||
}, _ => sender ! DBStatus(status)) | ||
} | ||
} |
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
Oops, something went wrong.