diff --git a/VERSION b/VERSION index b7b8e89a..97a7129d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.3.1-rc9 \ No newline at end of file +3.3.1-rc10 \ No newline at end of file diff --git a/mythic-docker/src/VERSION b/mythic-docker/src/VERSION index b7b8e89a..97a7129d 100644 --- a/mythic-docker/src/VERSION +++ b/mythic-docker/src/VERSION @@ -1 +1 @@ -3.3.1-rc9 \ No newline at end of file +3.3.1-rc10 \ No newline at end of file diff --git a/mythic-docker/src/database/initialize.go b/mythic-docker/src/database/initialize.go index 1f91defe..41f236d8 100644 --- a/mythic-docker/src/database/initialize.go +++ b/mythic-docker/src/database/initialize.go @@ -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 @@ -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, @@ -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) + } } @@ -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") }