Adding support for Mysql (#442)

Thanks, @svigne1!

Fixes #53
pull/443/head
Jordan Wright 2016-11-19 10:37:22 -06:00 committed by GitHub
parent 8f62e77884
commit f12af50d46
23 changed files with 136 additions and 12 deletions

View File

@ -11,6 +11,7 @@
"cert_path" : "example.crt", "cert_path" : "example.crt",
"key_path": "example.key" "key_path": "example.key"
}, },
"db_name" : "sqlite3",
"db_path" : "gophish.db", "db_path" : "gophish.db",
"migrations_path" : "db/migrations/" "migrations_prefix" : "db/db_"
} }

View File

@ -26,8 +26,9 @@ type PhishServer struct {
type Config struct { type Config struct {
AdminConf AdminServer `json:"admin_server"` AdminConf AdminServer `json:"admin_server"`
PhishConf PhishServer `json:"phish_server"` PhishConf PhishServer `json:"phish_server"`
DBName string `json:"db_name"`
DBPath string `json:"db_path"` DBPath string `json:"db_path"`
MigrationsPath string `json:"migrations_path"` MigrationsPath string `json:"migrations_prefix"`
} }
// Conf contains the initialized configuration struct // Conf contains the initialized configuration struct
@ -43,4 +44,6 @@ func init() {
fmt.Printf("File error: %v\n", err) fmt.Printf("File error: %v\n", err)
} }
json.Unmarshal(config_file, &Conf) json.Unmarshal(config_file, &Conf)
// Choosing the migrations directory based on the database used.
Conf.MigrationsPath = Conf.MigrationsPath + Conf.DBName
} }

View File

@ -25,8 +25,9 @@ type ControllersSuite struct {
var as *httptest.Server = httptest.NewUnstartedServer(handlers.CombinedLoggingHandler(os.Stdout, CreateAdminRouter())) var as *httptest.Server = httptest.NewUnstartedServer(handlers.CombinedLoggingHandler(os.Stdout, CreateAdminRouter()))
func (s *ControllersSuite) SetupSuite() { func (s *ControllersSuite) SetupSuite() {
config.Conf.DBName = "sqlite3"
config.Conf.DBPath = ":memory:" config.Conf.DBPath = ":memory:"
config.Conf.MigrationsPath = "../db/migrations/" config.Conf.MigrationsPath = "../db/db_sqlite3/migrations/"
err := models.Setup() err := models.Setup()
if err != nil { if err != nil {
s.T().Fatalf("Failed creating database: %v", err) s.T().Fatalf("Failed creating database: %v", err)

5
db/db_mysql/dbconf.yml Normal file
View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -2,4 +2,4 @@ production:
driver: sqlite3 driver: sqlite3
open: gophish.db open: gophish.db
dialect: sqlite3 dialect: sqlite3
import: github.com/mattn/go-sqlite3 import: github.com/mattn/go-sqlite3

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -10,6 +10,7 @@ import (
"bitbucket.org/liamstask/goose/lib/goose" "bitbucket.org/liamstask/goose/lib/goose"
_ "github.com/go-sql-driver/mysql"
"github.com/gophish/gophish/config" "github.com/gophish/gophish/config"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3 _ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
@ -61,6 +62,23 @@ func generateSecureKey() string {
return fmt.Sprintf("%x", k) 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 // Setup initializes the Conn object
// It also populates the Gophish Config object // It also populates the Gophish Config object
func Setup() error { func Setup() error {
@ -72,12 +90,7 @@ func Setup() error {
migrateConf := &goose.DBConf{ migrateConf := &goose.DBConf{
MigrationsDir: config.Conf.MigrationsPath, MigrationsDir: config.Conf.MigrationsPath,
Env: "production", Env: "production",
Driver: goose.DBDriver{ Driver: chooseDBDriver(config.Conf.DBName, config.Conf.DBPath),
Name: "sqlite3",
OpenStr: config.Conf.DBPath,
Import: "github.com/mattn/go-sqlite3",
Dialect: &goose.Sqlite3Dialect{},
},
} }
// Get the latest possible migration // Get the latest possible migration
latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir) latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
@ -86,7 +99,7 @@ func Setup() error {
return err return err
} }
// Open our database connection // 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.LogMode(false)
db.SetLogger(Logger) db.SetLogger(Logger)
db.DB().SetMaxOpenConns(1) db.DB().SetMaxOpenConns(1)

View File

@ -18,8 +18,9 @@ type ModelsSuite struct{}
var _ = check.Suite(&ModelsSuite{}) var _ = check.Suite(&ModelsSuite{})
func (s *ModelsSuite) SetUpSuite(c *check.C) { func (s *ModelsSuite) SetUpSuite(c *check.C) {
config.Conf.DBName = "sqlite3"
config.Conf.DBPath = ":memory:" config.Conf.DBPath = ":memory:"
config.Conf.MigrationsPath = "../db/migrations/" config.Conf.MigrationsPath = "../db/db_sqlite3/migrations/"
err := Setup() err := Setup()
if err != nil { if err != nil {
c.Fatalf("Failed creating database: %v", err) c.Fatalf("Failed creating database: %v", err)