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