From afa00e2a9c82c5456f3b8ce02c584d739fce16ba Mon Sep 17 00:00:00 2001 From: Stuart Small Date: Wed, 1 Jul 2020 21:06:31 -0600 Subject: [PATCH] Add ability to provide initial API key on service standup (#1883) --- models/models.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/models/models.go b/models/models.go index bc1fe3a4..29a68151 100644 --- a/models/models.go +++ b/models/models.go @@ -34,6 +34,11 @@ const DefaultAdminUsername = "admin" // randomly const InitialAdminPassword = "GOPHISH_INITIAL_ADMIN_PASSWORD" +// InitialAdminApiToken is the environment variable that specifies the +// API token to seed the initial root login instead of generating one +// randomly +const InitialAdminApiToken = "GOPHISH_INITIAL_ADMIN_API_TOKEN" + const ( CampaignInProgress string = "In progress" CampaignQueued string = "Queued" @@ -208,7 +213,13 @@ func Setup(c *config.Config) error { RoleID: adminRole.ID, PasswordChangeRequired: true, } - adminUser.ApiKey = auth.GenerateSecureKey(auth.APIKeyLength) + + if envToken := os.Getenv(InitialAdminApiToken); envToken != "" { + adminUser.ApiKey = envToken + } else { + adminUser.ApiKey = auth.GenerateSecureKey(auth.APIKeyLength) + } + err = db.Save(&adminUser).Error if err != nil { log.Error(err)