Skip to content

Commit

Permalink
fixing initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
its-a-feature committed Sep 17, 2024
1 parent e83bd2e commit 0b539df
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.1-rc9
3.3.1-rc10
2 changes: 1 addition & 1 deletion mythic-docker/src/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.1-rc9
3.3.1-rc10
91 changes: 47 additions & 44 deletions mythic-docker/src/database/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,8 @@ var currentMigrationVersion int64 = 3003002
func Initialize() {
DB = getNewDbConnection()
logging.LogInfo("Successfully connected to database, initializing...")

// background process to reconnect to the database if we lose connection
go checkDBConnection()
migrations := &migrate.FileMigrationSource{
Dir: "database/migrations",
}
migrationList, err := migrations.FindMigrations()
if err != nil {
logging.LogFatalError(err, "Failed to find migrations")
}
currentMigrationVersionID := ""
for i, _ := range migrationList {
logging.LogInfo("migration info", "NumberPrefixMatches", migrationList[i].NumberPrefixMatches(),
"version", migrationList[i].VersionInt(), "id", migrationList[i].Id)
if migrationList[i].VersionInt() == currentMigrationVersion {
currentMigrationVersionID = migrationList[i].Id
}
}
if currentMigrationVersionID == "" {
logging.LogFatalError(nil, "Current migration version set to a non-existing file", "version", currentMigrationVersion)
}
// Migrations generated via https://github.com/djrobstep/migra/blob/master/docs/options.md
migrate.SetSchema("public")
migrate.SetTable("mythic_server_migration_tracking")
n, err := migrate.ExecVersion(DB.DB, "postgres", migrations, migrate.Up, currentMigrationVersion)
if err != nil {
//logging.LogError(err, "Error from migrate.ExecVersion")
appliedMigrations := []migrate.MigrationRecord{}
if err2 := DB.Select(&appliedMigrations, `SELECT * FROM mythic_server_migration_tracking`); err2 != nil {
logging.LogFatalError(err2, "Failed to get applied migrations from database")
}
successfullyAppliedMigrations := false
for i, _ := range appliedMigrations {
if appliedMigrations[i].Id == currentMigrationVersionID && !appliedMigrations[i].AppliedAt.IsZero() {
successfullyAppliedMigrations = true
}
}
if !successfullyAppliedMigrations {
logging.LogFatalError(err, "Failed to apply all necessary migrations for specified version", "version", currentMigrationVersion)
}
}
logging.LogInfo("Applied migrations up to current version", "version", currentMigrationVersion, "applied", n)
operators := []databaseStructs.Operator{}
var newOperation databaseStructs.Operation
if err := DB.Select(&operators, "SELECT * FROM operator LIMIT 1"); err != nil {
if AreDatabaseErrorsEqual(err, UndefinedTable) {
// need to setup the database
Expand All @@ -83,7 +42,7 @@ func Initialize() {
Admin: true,
Active: true,
}
newOperation := databaseStructs.Operation{
newOperation = databaseStructs.Operation{
Name: utils.MythicConfig.DefaultOperationName,
Webhook: utils.MythicConfig.DefaultOperationWebhook,
Channel: utils.MythicConfig.DefaultOperationChannel,
Expand Down Expand Up @@ -127,7 +86,7 @@ func Initialize() {
(:operator_id, :operation_id, :view_mode)`, operatorOperation); err != nil {
logging.LogError(err, "Failed to create operator operation mapping for new operator and new operation")
}
CreateOperationBotForOperation(newOperation)

}
}

Expand All @@ -136,6 +95,50 @@ func Initialize() {
logging.LogFatalError(err, "pq error", GetDatabaseErrorString(err))
}
}
// background process to reconnect to the database if we lose connection
go checkDBConnection()
migrations := &migrate.FileMigrationSource{
Dir: "database/migrations",
}
migrationList, err := migrations.FindMigrations()
if err != nil {
logging.LogFatalError(err, "Failed to find migrations")
}
currentMigrationVersionID := ""
for i, _ := range migrationList {
logging.LogInfo("migration info", "NumberPrefixMatches", migrationList[i].NumberPrefixMatches(),
"version", migrationList[i].VersionInt(), "id", migrationList[i].Id)
if migrationList[i].VersionInt() == currentMigrationVersion {
currentMigrationVersionID = migrationList[i].Id
}
}
if currentMigrationVersionID == "" {
logging.LogFatalError(nil, "Current migration version set to a non-existing file", "version", currentMigrationVersion)
}
// Migrations generated via https://github.com/djrobstep/migra/blob/master/docs/options.md
migrate.SetSchema("public")
migrate.SetTable("mythic_server_migration_tracking")
n, err := migrate.ExecVersion(DB.DB, "postgres", migrations, migrate.Up, currentMigrationVersion)
if err != nil {
//logging.LogError(err, "Error from migrate.ExecVersion")
appliedMigrations := []migrate.MigrationRecord{}
if err2 := DB.Select(&appliedMigrations, `SELECT * FROM mythic_server_migration_tracking`); err2 != nil {
logging.LogFatalError(err2, "Failed to get applied migrations from database")
}
successfullyAppliedMigrations := false
for i, _ := range appliedMigrations {
if appliedMigrations[i].Id == currentMigrationVersionID && !appliedMigrations[i].AppliedAt.IsZero() {
successfullyAppliedMigrations = true
}
}
if !successfullyAppliedMigrations {
logging.LogFatalError(err, "Failed to apply all necessary migrations for specified version", "version", currentMigrationVersion)
}
}
logging.LogInfo("Applied migrations up to current version", "version", currentMigrationVersion, "applied", n)
if newOperation.ID > 0 {
CreateOperationBotForOperation(newOperation)
}
initializeMitreAttack()
logging.LogInfo("Database Initialized")
}
Expand Down

0 comments on commit 0b539df

Please sign in to comment.