-
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
5 changed files
with
139 additions
and
152 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
14 changes: 2 additions & 12 deletions
14
cortex/connector/src/main/scala/org/thp/thehive/connector/cortex/models/SchemaUpdater.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 |
---|---|---|
@@ -1,20 +1,10 @@ | ||
package org.thp.thehive.connector.cortex.models | ||
|
||
import play.api.Logger | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import org.thp.scalligraph.auth.UserSrv | ||
import org.thp.scalligraph.models.Database | ||
import org.thp.thehive.models.{SchemaUpdater => TheHiveSchemaUpdater} | ||
|
||
@Singleton | ||
class SchemaUpdater @Inject() (thehiveSchemaUpdater: TheHiveSchemaUpdater, cortexSchema: CortexSchema, db: Database, userSrv: UserSrv) { | ||
val latestVersion: Int = 1 | ||
|
||
val currentVersion: Int = db.version("thehive-cortex") | ||
if (currentVersion < latestVersion) { | ||
Logger(getClass).info(s"Cortex database schema is outdated ($currentVersion). Upgrading to version $latestVersion ...") | ||
db.createSchemaFrom(cortexSchema)(userSrv.getSystemAuthContext) | ||
db.setVersion("thehive-cortex", latestVersion) | ||
} | ||
class SchemaUpdater @Inject() (cortexSchema: CortexSchema, db: Database, userSrv: UserSrv) { | ||
cortexSchema.update(db)(userSrv.getSystemAuthContext).get | ||
} |
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 |
---|---|---|
@@ -1,64 +1,15 @@ | ||
package org.thp.thehive.models | ||
|
||
import gremlin.scala._ | ||
import javax.inject.{Inject, Singleton} | ||
import org.janusgraph.core.schema.ConsistencyModifier | ||
import org.thp.scalligraph.auth.UserSrv | ||
import org.thp.scalligraph.janus.JanusDatabase | ||
import org.thp.scalligraph.models.{Database, IndexType, Operations} | ||
import org.thp.scalligraph.steps.StepsOps._ | ||
import play.api.Logger | ||
import org.thp.scalligraph.models.Database | ||
import play.api.inject.ApplicationLifecycle | ||
|
||
import scala.concurrent.Future | ||
import scala.util.{Success, Try} | ||
|
||
@Singleton | ||
class SchemaUpdater @Inject() (theHiveSchema: TheHiveSchema, db: Database, userSrv: UserSrv, applicationLifeCycle: ApplicationLifecycle) { | ||
lazy val logger: Logger = Logger(getClass) | ||
|
||
applicationLifeCycle.addStopHook(() => Future.successful(db.close())) | ||
|
||
Operations("thehive", theHiveSchema) | ||
.addProperty[Option[Boolean]]("Observable", "seen") | ||
.updateGraph("Add manageConfig permission to org-admin profile", "Profile") { traversal => | ||
Try(traversal.has("name", "org-admin").raw.property(Key("permissions") -> "manageConfig").iterate()) | ||
Success(()) | ||
} | ||
.updateGraph("Remove duplicate custom fields", "CustomField") { traversal => | ||
traversal.toIterator.foldLeft(Set.empty[String]) { (names, vertex) => | ||
val name = vertex.value[String]("name") | ||
if (names.contains(name)) { | ||
vertex.remove() | ||
names | ||
} else | ||
names + name | ||
} | ||
Success(()) | ||
} | ||
.addIndex("CustomField", IndexType.unique, "name") | ||
.dbOperation[JanusDatabase]("Remove locks") { db => | ||
def removePropertyLock(name: String) = | ||
db.managementTransaction { mgmt => | ||
Try(mgmt.setConsistency(mgmt.getPropertyKey(name), ConsistencyModifier.DEFAULT)) | ||
.recover { | ||
case error => logger.warn(s"Unable to remove lock on property $name: $error") | ||
} | ||
} | ||
def removeIndexLock(name: String) = | ||
db.managementTransaction { mgmt => | ||
Try(mgmt.setConsistency(mgmt.getGraphIndex(name), ConsistencyModifier.DEFAULT)) | ||
.recover { | ||
case error => logger.warn(s"Unable to remove lock on index $name: $error") | ||
} | ||
} | ||
|
||
removeIndexLock("CaseNumber") | ||
removePropertyLock("number") | ||
removeIndexLock("DataData") | ||
removePropertyLock("data") | ||
} | ||
.addIndex("Tag", IndexType.tryUnique, "namespace", "predicate", "value") | ||
.dbOperation[JanusDatabase]("Enable indexes")(_.enableIndexes()) | ||
.execute(db)(userSrv.getSystemAuthContext) | ||
applicationLifeCycle | ||
.addStopHook(() => Future.successful(db.close())) | ||
theHiveSchema.operations.execute(db, theHiveSchema)(userSrv.getSystemAuthContext).get | ||
} |
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