Skip to content

Commit

Permalink
Adding support for Mysql (#442)
Browse files Browse the repository at this point in the history
Thanks, @svigne1!

Fixes #53
  • Loading branch information
jordan-wright committed Nov 19, 2016
1 parent 8f62e77 commit f12af50
Show file tree
Hide file tree
Showing 23 changed files with 136 additions and 12 deletions.
3 changes: 2 additions & 1 deletion config.json
Expand Up @@ -11,6 +11,7 @@
"cert_path" : "example.crt",
"key_path": "example.key"
},
"db_name" : "sqlite3",
"db_path" : "gophish.db",
"migrations_path" : "db/migrations/"
"migrations_prefix" : "db/db_"
}
5 changes: 4 additions & 1 deletion config/config.go
Expand Up @@ -26,8 +26,9 @@ type PhishServer struct {
type Config struct {
AdminConf AdminServer `json:"admin_server"`
PhishConf PhishServer `json:"phish_server"`
DBName string `json:"db_name"`
DBPath string `json:"db_path"`
MigrationsPath string `json:"migrations_path"`
MigrationsPath string `json:"migrations_prefix"`
}

// Conf contains the initialized configuration struct
Expand All @@ -43,4 +44,6 @@ func init() {
fmt.Printf("File error: %v\n", err)
}
json.Unmarshal(config_file, &Conf)
// Choosing the migrations directory based on the database used.
Conf.MigrationsPath = Conf.MigrationsPath + Conf.DBName
}
3 changes: 2 additions & 1 deletion controllers/api_test.go
Expand Up @@ -25,8 +25,9 @@ type ControllersSuite struct {
var as *httptest.Server = httptest.NewUnstartedServer(handlers.CombinedLoggingHandler(os.Stdout, CreateAdminRouter()))

func (s *ControllersSuite) SetupSuite() {
config.Conf.DBName = "sqlite3"
config.Conf.DBPath = ":memory:"
config.Conf.MigrationsPath = "../db/migrations/"
config.Conf.MigrationsPath = "../db/db_sqlite3/migrations/"
err := models.Setup()
if err != nil {
s.T().Fatalf("Failed creating database: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions db/db_mysql/dbconf.yml
@@ -0,0 +1,5 @@
production:
driver: mysql
open: root:@(:3307)/gophish?charset=utf8&parseTime=True&loc=Local
dialect: mysql
import: github.com/go-sql-driver/mysql
28 changes: 28 additions & 0 deletions db/db_mysql/migrations/20160118194630_init.sql
@@ -0,0 +1,28 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE IF NOT EXISTS users (id integer primary key auto_increment,username varchar(255) NOT NULL UNIQUE,hash varchar(255),api_key varchar(255) NOT NULL UNIQUE );
CREATE TABLE IF NOT EXISTS templates (id integer primary key auto_increment,user_id bigint,name varchar(255),subject varchar(255),text text,html text,modified_date datetime );
CREATE TABLE IF NOT EXISTS targets (id integer primary key auto_increment,first_name varchar(255),last_name varchar(255),email varchar(255),position varchar(255) );
CREATE TABLE IF NOT EXISTS smtp (smtp_id integer primary key auto_increment,campaign_id bigint,host varchar(255),username varchar(255),from_address varchar(255) );
CREATE TABLE IF NOT EXISTS results (id integer primary key auto_increment,campaign_id bigint,user_id bigint,r_id varchar(255),email varchar(255),first_name varchar(255),last_name varchar(255),status varchar(255) NOT NULL ,ip varchar(255),latitude real,longitude real );
CREATE TABLE IF NOT EXISTS pages (id integer primary key auto_increment,user_id bigint,name varchar(255),html text,modified_date datetime );
CREATE TABLE IF NOT EXISTS groups (id integer primary key auto_increment,user_id bigint,name varchar(255),modified_date datetime );
CREATE TABLE IF NOT EXISTS group_targets (group_id bigint,target_id bigint );
CREATE TABLE IF NOT EXISTS events (id integer primary key auto_increment,campaign_id bigint,email varchar(255),time datetime,message varchar(255) );
CREATE TABLE IF NOT EXISTS campaigns (id integer primary key auto_increment,user_id bigint,name varchar(255) NOT NULL ,created_date datetime,completed_date datetime,template_id bigint,page_id bigint,status varchar(255),url varchar(255) );
CREATE TABLE IF NOT EXISTS attachments (id integer primary key auto_increment,template_id bigint,content text,type varchar(255),name varchar(255) );

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE attachments;
DROP TABLE campaigns;
DROP TABLE events;
DROP TABLE group_targets;
DROP TABLE groups;
DROP TABLE pages;
DROP TABLE results;
DROP TABLE smtp;
DROP TABLE targets;
DROP TABLE templates;
DROP TABLE users;
@@ -0,0 +1,22 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
-- Move the relationship between campaigns and smtp to campaigns
ALTER TABLE campaigns ADD COLUMN smtp_id bigint;
-- Create a new table to store smtp records
DROP TABLE smtp;
CREATE TABLE smtp(
id integer primary key auto_increment,
user_id bigint,
interface_type varchar(255),
name varchar(255),
host varchar(255),
username varchar(255),
password varchar(255),
from_address varchar(255),
modified_date datetime,
ignore_cert_errors BOOLEAN
);
-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

@@ -0,0 +1,9 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE campaigns ADD COLUMN launch_date DATETIME;

UPDATE campaigns SET launch_date = created_date;
-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

2 changes: 1 addition & 1 deletion db/dbconf.yml → db/db_sqlite3/dbconf.yml
Expand Up @@ -2,4 +2,4 @@ production:
driver: sqlite3
open: gophish.db
dialect: sqlite3
import: github.com/mattn/go-sqlite3
import: github.com/mattn/go-sqlite3
File renamed without changes.
@@ -0,0 +1,8 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE events ADD COLUMN details BLOB;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

@@ -0,0 +1,8 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE smtp ADD COLUMN ignore_cert_errors BOOLEAN;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

@@ -0,0 +1,8 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE results ADD COLUMN position VARCHAR(255);

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

@@ -0,0 +1,9 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE pages ADD COLUMN capture_credentials BOOLEAN;
ALTER TABLE pages ADD COLUMN capture_passwords BOOLEAN;

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

8 changes: 8 additions & 0 deletions db/db_sqlite3/migrations/20160317214457_0.2_redirect_url.sql
@@ -0,0 +1,8 @@

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied
ALTER TABLE pages ADD COLUMN redirect_url VARCHAR(255);

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

27 changes: 20 additions & 7 deletions models/models.go
Expand Up @@ -10,6 +10,7 @@ import (

"bitbucket.org/liamstask/goose/lib/goose"

_ "github.com/go-sql-driver/mysql"
"github.com/gophish/gophish/config"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
Expand Down Expand Up @@ -61,6 +62,23 @@ func generateSecureKey() string {
return fmt.Sprintf("%x", k)
}

func chooseDBDriver(name, openStr string) goose.DBDriver {
d := goose.DBDriver{Name: name, OpenStr: openStr}

switch name {
case "mysql":
d.Import = "github.com/go-sql-driver/mysql"
d.Dialect = &goose.MySqlDialect{}

// Default database is sqlite3
default:
d.Import = "github.com/mattn/go-sqlite3"
d.Dialect = &goose.Sqlite3Dialect{}
}

return d
}

// Setup initializes the Conn object
// It also populates the Gophish Config object
func Setup() error {
Expand All @@ -72,12 +90,7 @@ func Setup() error {
migrateConf := &goose.DBConf{
MigrationsDir: config.Conf.MigrationsPath,
Env: "production",
Driver: goose.DBDriver{
Name: "sqlite3",
OpenStr: config.Conf.DBPath,
Import: "github.com/mattn/go-sqlite3",
Dialect: &goose.Sqlite3Dialect{},
},
Driver: chooseDBDriver(config.Conf.DBName, config.Conf.DBPath),
}
// Get the latest possible migration
latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
Expand All @@ -86,7 +99,7 @@ func Setup() error {
return err
}
// Open our database connection
db, err = gorm.Open("sqlite3", config.Conf.DBPath)
db, err = gorm.Open(config.Conf.DBName, config.Conf.DBPath)
db.LogMode(false)
db.SetLogger(Logger)
db.DB().SetMaxOpenConns(1)
Expand Down
3 changes: 2 additions & 1 deletion models/models_test.go
Expand Up @@ -18,8 +18,9 @@ type ModelsSuite struct{}
var _ = check.Suite(&ModelsSuite{})

func (s *ModelsSuite) SetUpSuite(c *check.C) {
config.Conf.DBName = "sqlite3"
config.Conf.DBPath = ":memory:"
config.Conf.MigrationsPath = "../db/migrations/"
config.Conf.MigrationsPath = "../db/db_sqlite3/migrations/"
err := Setup()
if err != nil {
c.Fatalf("Failed creating database: %v", err)
Expand Down

0 comments on commit f12af50

Please sign in to comment.