Skip to content

Commit

Permalink
#1785 Enforce database initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Mar 9, 2021
1 parent 7c1922f commit 923997d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ object Migrate extends App with MigrationOps {
ConfigFactory
.parseResources("play/reference-overrides.conf")
.withFallback(ConfigFactory.defaultReference())
.withFallback(ConfigFactory.parseFile(new File("migration/src/main/resources/reference.conf")))
.resolve()
// import scala.collection.JavaConverters._
// Thread.currentThread().getContextClassLoader().getResources("reference.conf").asScala.foreach(println)
OParser.parse(argParser, args, defaultConfig).foreach { config =>
implicit val actorSystem: ActorSystem = ActorSystem("TheHiveMigration", config)
implicit val ec: ExecutionContext = actorSystem.dispatcher
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
package org.thp.thehive.migration.th4

import akka.actor.ActorSystem
import org.janusgraph.core.JanusGraph
import org.thp.scalligraph.SingleInstance
import org.thp.scalligraph.janus.JanusDatabase
import org.thp.scalligraph.models.{Database, UpdatableSchema}
import play.api.Configuration

import javax.inject.{Inject, Provider, Singleton}
import scala.collection.JavaConverters._
import scala.collection.immutable

@Singleton
class JanusDatabaseProvider @Inject() (configuration: Configuration, system: ActorSystem, schemas: immutable.Set[UpdatableSchema])
extends Provider[Database] {

def dropOtherConnections(db: JanusGraph): Unit = {
val mgmt = db.openManagement()
mgmt
.getOpenInstances
.asScala
.filterNot(_.endsWith("(current)"))
.foreach(mgmt.forceCloseInstance)
mgmt.commit()
}

override lazy val get: Database = {
val janusDatabase = JanusDatabase.openDatabase(configuration, system)
dropOtherConnections(janusDatabase)
val db = new JanusDatabase(
JanusDatabase.openDatabase(configuration, system),
janusDatabase,
configuration,
system,
new SingleInstance(true)
)
schemas.foreach(schema => db.createSchemaFrom(schema)(schema.authContext))
schemas.toTry(schema => schema.update(db)).get
db
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,7 @@ class Output @Inject() (
| ${tags.size} tags""".stripMargin)
}

def startMigration(): Try[Unit] =
// db match {
// case jdb: JanusDatabase => jdb.dropOtherConnections.recover { case error => logger.error(s"Fail to remove other connection", error) }
// case _ =>
// }
if (db.version("thehive") == 0)
db.createSchemaFrom(theHiveSchema)(LocalUserSrv.getSystemAuthContext)
.flatMap(_ => db.setVersion(theHiveSchema.name, theHiveSchema.operations.lastVersion))
.flatMap(_ => db.createSchemaFrom(cortexSchema)(LocalUserSrv.getSystemAuthContext))
.flatMap(_ => db.setVersion(cortexSchema.name, cortexSchema.operations.lastVersion))
.map(_ => retrieveExistingData())
else
theHiveSchema
.update(db)
.flatMap(_ => cortexSchema.update(db))
.map { _ =>
retrieveExistingData()
db.rebuildIndexes()
}
def startMigration(): Try[Unit] = Success(retrieveExistingData())

def endMigration(): Try[Unit] = {
db.addSchemaIndexes(theHiveSchema)
Expand Down

0 comments on commit 923997d

Please sign in to comment.