Adds environment variable to set the initial admin password

This change adds a `GOPHISH_INITIAL_ADMIN_PASSWORD` environment variable so that system administrators can set the initial admin password rather than having it randomly generated. This is especially useful in automated deployment scenarios, or scenarios using Docker (ref #1876, #1874)
pull/1883/head
Jordan Wright 2020-06-25 08:31:28 -05:00
parent bb7de8df3e
commit bf76f86ea4
1 changed files with 14 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"time"
"bitbucket.org/liamstask/goose/lib/goose"
@ -28,6 +29,11 @@ const MaxDatabaseConnectionAttempts int = 10
// DefaultAdminUsername is the default username for the administrative user
const DefaultAdminUsername = "admin"
// InitialAdminPassword is the environment variable that specifies which
// password to use for the initial root login instead of generating one
// randomly
const InitialAdminPassword = "GOPHISH_INITIAL_ADMIN_PASSWORD"
const (
CampaignInProgress string = "In progress"
CampaignQueued string = "Queued"
@ -88,9 +94,14 @@ func chooseDBDriver(name, openStr string) goose.DBDriver {
}
func createTemporaryPassword(u *User) error {
var temporaryPassword string
if envPassword := os.Getenv(InitialAdminPassword); envPassword != "" {
temporaryPassword = envPassword
} else {
// This will result in a 16 character password which could be viewed as an
// inconvenience, but it should be ok for now.
temporaryPassword := auth.GenerateSecureKey(auth.MinPasswordLength)
temporaryPassword = auth.GenerateSecureKey(auth.MinPasswordLength)
}
hash, err := auth.GeneratePasswordHash(temporaryPassword)
if err != nil {
return err