Skip to content

Commit

Permalink
adjusting database initialization to auto create bot account
Browse files Browse the repository at this point in the history
  • Loading branch information
its-a-feature committed Sep 17, 2024
1 parent 39fde7b commit f09c369
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.3.1-rc7] - 2024-09-09
## [3.3.1-rc9] - 2024-09-17

### Changed

- Updated some of the logging for bad messages to be clearer
- Changed the order of initializing the database so that migrations happen before initializing operators/operations

## [3.3.1-rc8] - 2024-09-13

Expand All @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added more context for GraphQL queries used by APITokens/Scripting to the event log
- Fixed a bug with exporting saved c2 profile instances that would then break imports

## [3.3.1-rc7] - 2024-09-09

### Changed

- Updated some of the logging for bad messages to be clearer

## [3.3.1-rc6] - 2024-09-04

### Changed
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.1-rc8
3.3.1-rc9
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-rc8
3.3.1-rc9
83 changes: 42 additions & 41 deletions mythic-docker/src/database/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,48 @@ 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{}
if err := DB.Select(&operators, "SELECT * FROM operator LIMIT 1"); err != nil {
if AreDatabaseErrorsEqual(err, UndefinedTable) {
Expand Down Expand Up @@ -94,47 +136,6 @@ 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)
initializeMitreAttack()
logging.LogInfo("Database Initialized")
}
Expand Down

0 comments on commit f09c369

Please sign in to comment.