Adding ability to log to file as well as stderr. Fixes #441. Fixes #1209.

pull/1264/head
Jordan Wright 2018-10-06 15:47:31 -05:00
parent c315867cea
commit bef52d36f1
5 changed files with 110 additions and 16 deletions

View File

@ -14,5 +14,6 @@
"db_name": "sqlite3", "db_name": "sqlite3",
"db_path": "gophish.db", "db_path": "gophish.db",
"migrations_prefix": "db/db_", "migrations_prefix": "db/db_",
"contact_address": "" "contact_address": "",
"log_file": ""
} }

View File

@ -3,8 +3,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
log "github.com/gophish/gophish/logger"
) )
// AdminServer represents the Admin server configuration details // AdminServer represents the Admin server configuration details
@ -32,6 +30,7 @@ type Config struct {
MigrationsPath string `json:"migrations_prefix"` MigrationsPath string `json:"migrations_prefix"`
TestFlag bool `json:"test_flag"` TestFlag bool `json:"test_flag"`
ContactAddress string `json:"contact_address"` ContactAddress string `json:"contact_address"`
LogFile string `json:"log_file"`
} }
// Conf contains the initialized configuration struct // Conf contains the initialized configuration struct
@ -44,23 +43,19 @@ var Version = ""
const ServerName = "gophish" const ServerName = "gophish"
// LoadConfig loads the configuration from the specified filepath // LoadConfig loads the configuration from the specified filepath
func LoadConfig(filepath string) { func LoadConfig(filepath string) error {
// Get the config file // Get the config file
configFile, err := ioutil.ReadFile(filepath) configFile, err := ioutil.ReadFile(filepath)
if err != nil { if err != nil {
log.Errorf("File error: %v\n", err) return err
}
err = json.Unmarshal(configFile, &Conf)
if err != nil {
return err
} }
json.Unmarshal(configFile, &Conf)
// Choosing the migrations directory based on the database used. // Choosing the migrations directory based on the database used.
Conf.MigrationsPath = Conf.MigrationsPath + Conf.DBName Conf.MigrationsPath = Conf.MigrationsPath + Conf.DBName
// Explicitly set the TestFlag to false to prevent config.json overrides // Explicitly set the TestFlag to false to prevent config.json overrides
Conf.TestFlag = false Conf.TestFlag = false
return nil
// Print a warning if a contact address isn't provided
// (see: https://github.com/gophish/gophish/issues/1057)
if Conf.ContactAddress == "" {
log.Warnf("No contact address has been configured.")
log.Warnf("Please consider adding a contact_address entry in your config.json")
}
} }

68
config/config_test.go Normal file
View File

@ -0,0 +1,68 @@
package config
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
type ConfigSuite struct {
suite.Suite
ConfigFile *os.File
}
var validConfig = []byte(`{
"admin_server": {
"listen_url": "127.0.0.1:3333",
"use_tls": true,
"cert_path": "gophish_admin.crt",
"key_path": "gophish_admin.key"
},
"phish_server": {
"listen_url": "0.0.0.0:8080",
"use_tls": false,
"cert_path": "example.crt",
"key_path": "example.key"
},
"db_name": "sqlite3",
"db_path": "gophish.db",
"migrations_prefix": "db/db_",
"contact_address": ""
}`)
func (s *ConfigSuite) SetupTest() {
f, err := ioutil.TempFile("", "gophish-config")
s.Nil(err)
s.ConfigFile = f
}
func (s *ConfigSuite) TearDownTest() {
err := s.ConfigFile.Close()
s.Nil(err)
}
func (s *ConfigSuite) TestLoadConfig() {
_, err := s.ConfigFile.Write(validConfig)
s.Nil(err)
// Load the valid config
err = LoadConfig(s.ConfigFile.Name())
s.Nil(err)
expectedConfig := Config{}
err = json.Unmarshal(validConfig, &expectedConfig)
s.Nil(err)
expectedConfig.MigrationsPath = expectedConfig.MigrationsPath + expectedConfig.DBName
expectedConfig.TestFlag = false
s.Equal(expectedConfig, Conf)
// Load an invalid config
err = LoadConfig("bogusfile")
s.NotNil(err)
}
func TestConfigSuite(t *testing.T) {
suite.Run(t, new(ConfigSuite))
}

View File

@ -65,9 +65,22 @@ func main() {
kingpin.Parse() kingpin.Parse()
// Load the config // Load the config
config.LoadConfig(*configPath) err = config.LoadConfig(*configPath)
// Just warn if a contact address hasn't been configured
if err != nil {
log.Fatal(err)
}
if config.Conf.ContactAddress == "" {
log.Warnf("No contact address has been configured.")
log.Warnf("Please consider adding a contact_address entry in your config.json")
}
config.Version = string(version) config.Version = string(version)
err = log.Setup()
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()

View File

@ -2,7 +2,9 @@ package logger
import ( import (
"io" "io"
"os"
"github.com/gophish/gophish/config"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -12,10 +14,25 @@ var Logger *logrus.Logger
func init() { func init() {
Logger = logrus.New() Logger = logrus.New()
Logger.SetLevel(logrus.InfoLevel)
Logger.Formatter = &logrus.TextFormatter{DisableColors: true} Logger.Formatter = &logrus.TextFormatter{DisableColors: true}
} }
// Setup configures the logger based on options in the config.json.
func Setup() error {
Logger.SetLevel(logrus.InfoLevel)
// Set up logging to a file if specified in the config
logFile := config.Conf.LogFile
if logFile != "" {
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
mw := io.MultiWriter(os.Stderr, f)
Logger.Out = mw
}
return nil
}
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
Logger.Debug(args...) Logger.Debug(args...)
} }