Skip to content

Commit 69a8b19

Browse files
committed
chore: refactor env loading logic
1 parent 7e2d2c7 commit 69a8b19

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

internal/start/start.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ EOF
360360
fmt.Sprintf("GOTRUE_SECURITY_REFRESH_TOKEN_REUSE_INTERVAL=%v", utils.Config.Auth.RefreshTokenReuseInterval),
361361
}
362362

363-
if utils.Config.Auth.Sms.Twilio != nil {
363+
if utils.Config.Auth.Sms.Twilio.Enabled {
364364
env = append(
365365
env,
366366
"GOTRUE_SMS_PROVIDER=twilio",
@@ -369,23 +369,23 @@ EOF
369369
"GOTRUE_SMS_TWILIO_MESSAGE_SERVICE_SID="+utils.Config.Auth.Sms.Twilio.MessageServiceSid,
370370
)
371371
}
372-
if utils.Config.Auth.Sms.Messagebird != nil {
372+
if utils.Config.Auth.Sms.Messagebird.Enabled {
373373
env = append(
374374
env,
375375
"GOTRUE_SMS_PROVIDER=messagebird",
376376
"GOTRUE_SMS_MESSAGEBIRD_ACCESS_KEY="+utils.Config.Auth.Sms.Messagebird.AccessKey,
377377
"GOTRUE_SMS_MESSAGEBIRD_ORIGINATOR="+utils.Config.Auth.Sms.Messagebird.Originator,
378378
)
379379
}
380-
if utils.Config.Auth.Sms.Textlocal != nil {
380+
if utils.Config.Auth.Sms.Textlocal.Enabled {
381381
env = append(
382382
env,
383383
"GOTRUE_SMS_PROVIDER=textlocal",
384384
"GOTRUE_SMS_TEXTLOCAL_API_KEY="+utils.Config.Auth.Sms.Textlocal.ApiKey,
385385
"GOTRUE_SMS_TEXTLOCAL_SENDER="+utils.Config.Auth.Sms.Textlocal.Sender,
386386
)
387387
}
388-
if utils.Config.Auth.Sms.Vonage != nil {
388+
if utils.Config.Auth.Sms.Vonage.Enabled {
389389
env = append(
390390
env,
391391
"GOTRUE_SMS_PROVIDER=vonage",

internal/utils/config.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var (
6868
initConfigEmbed string
6969
initConfigTemplate = template.Must(template.New("initConfig").Parse(initConfigEmbed))
7070
invalidProjectId = regexp.MustCompile("[^a-zA-Z0-9_.-]+")
71+
envPattern = regexp.MustCompile(`^env\((.*)\)$`)
7172
)
7273

7374
// Type for turning human-friendly bytes string ("5MB", "32kB") into an int64 during toml decoding.
@@ -164,31 +165,35 @@ type (
164165
}
165166

166167
sms struct {
167-
EnableSignup bool `toml:"enable_signup"`
168-
EnableConfirmations bool `toml:"enable_confirmations"`
169-
Twilio *twilioConfig `toml:"twilio" mapstructure:"twilio"`
170-
Messagebird *messagebirdConfig `toml:"messagebird" mapstructure:"messagebird"`
171-
Textlocal *textlocalConfig `toml:"textlocal" mapstructure:"textlocal"`
172-
Vonage *vonageConfig `toml:"vonage" mapstructure:"vonage"`
168+
EnableSignup bool `toml:"enable_signup"`
169+
EnableConfirmations bool `toml:"enable_confirmations"`
170+
Twilio twilioConfig `toml:"twilio" mapstructure:"twilio"`
171+
Messagebird messagebirdConfig `toml:"messagebird" mapstructure:"messagebird"`
172+
Textlocal textlocalConfig `toml:"textlocal" mapstructure:"textlocal"`
173+
Vonage vonageConfig `toml:"vonage" mapstructure:"vonage"`
173174
}
174175

175176
twilioConfig struct {
177+
Enabled bool `toml:"enabled"`
176178
AccountSid string `toml:"account_sid"`
177179
MessageServiceSid string `toml:"message_service_sid"`
178180
AuthToken string `toml:"auth_token" mapstructure:"auth_token"`
179181
}
180182

181183
messagebirdConfig struct {
184+
Enabled bool `toml:"enabled"`
182185
Originator string `toml:"originator"`
183186
AccessKey string `toml:"access_key" mapstructure:"access_key"`
184187
}
185188

186189
textlocalConfig struct {
187-
Sender string `toml:"sender"`
188-
ApiKey string `toml:"api_key" mapstructure:"api_key"`
190+
Enabled bool `toml:"enabled"`
191+
Sender string `toml:"sender"`
192+
ApiKey string `toml:"api_key" mapstructure:"api_key"`
189193
}
190194

191195
vonageConfig struct {
196+
Enabled bool `toml:"enabled"`
192197
From string `toml:"from"`
193198
ApiKey string `toml:"api_key" mapstructure:"api_key"`
194199
ApiSecret string `toml:"api_secret" mapstructure:"api_secret"`
@@ -315,7 +320,7 @@ func LoadConfigFS(fsys afero.Fs) error {
315320
Config.Auth.ServiceRoleKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU"
316321
}
317322

318-
if Config.Auth.Sms.Twilio != nil {
323+
if Config.Auth.Sms.Twilio.Enabled {
319324
if len(Config.Auth.Sms.Twilio.AccountSid) == 0 {
320325
return errors.New("Missing required field in config: auth.sms.twilio.account_sid")
321326
}
@@ -326,23 +331,23 @@ func LoadConfigFS(fsys afero.Fs) error {
326331
return errors.New("Missing required field in config: auth.sms.twilio.auth_token")
327332
}
328333
}
329-
if Config.Auth.Sms.Messagebird != nil {
334+
if Config.Auth.Sms.Messagebird.Enabled {
330335
if len(Config.Auth.Sms.Messagebird.Originator) == 0 {
331336
return errors.New("Missing required field in config: auth.sms.messagebird.originator")
332337
}
333338
if len(Config.Auth.Sms.Messagebird.AccessKey) == 0 {
334339
return errors.New("Missing required field in config: auth.sms.messagebird.access_key")
335340
}
336341
}
337-
if Config.Auth.Sms.Textlocal != nil {
342+
if Config.Auth.Sms.Textlocal.Enabled {
338343
if len(Config.Auth.Sms.Textlocal.Sender) == 0 {
339344
return errors.New("Missing required field in config: auth.sms.textlocal.sender")
340345
}
341346
if len(Config.Auth.Sms.Textlocal.ApiKey) == 0 {
342347
return errors.New("Missing required field in config: auth.sms.textlocal.api_key")
343348
}
344349
}
345-
if Config.Auth.Sms.Vonage != nil {
350+
if Config.Auth.Sms.Vonage.Enabled {
346351
if len(Config.Auth.Sms.Vonage.From) == 0 {
347352
return errors.New("Missing required field in config: auth.sms.vonage.from")
348353
}
@@ -365,21 +370,6 @@ func LoadConfigFS(fsys afero.Fs) error {
365370
Secret: "",
366371
}
367372
} else if Config.Auth.External[ext].Enabled {
368-
maybeLoadEnv := func(s string) (string, error) {
369-
matches := regexp.MustCompile(`^env\((.*)\)$`).FindStringSubmatch(s)
370-
if len(matches) == 0 {
371-
return s, nil
372-
}
373-
374-
envName := matches[1]
375-
value := os.Getenv(envName)
376-
if value == "" {
377-
return "", errors.New(`Error evaluating "env(` + envName + `)": environment variable ` + envName + " is unset.")
378-
}
379-
380-
return value, nil
381-
}
382-
383373
var clientId, secret, redirectUri, url string
384374

385375
if Config.Auth.External[ext].ClientId == "" {
@@ -400,15 +390,13 @@ func LoadConfigFS(fsys afero.Fs) error {
400390
}
401391
secret = v
402392
}
403-
404393
if Config.Auth.External[ext].RedirectUri != "" {
405394
v, err := maybeLoadEnv(Config.Auth.External[ext].RedirectUri)
406395
if err != nil {
407396
return err
408397
}
409398
redirectUri = v
410399
}
411-
412400
if Config.Auth.External[ext].Url != "" {
413401
v, err := maybeLoadEnv(Config.Auth.External[ext].Url)
414402
if err != nil {
@@ -463,6 +451,20 @@ func LoadConfigFS(fsys afero.Fs) error {
463451
return nil
464452
}
465453

454+
func maybeLoadEnv(s string) (string, error) {
455+
matches := envPattern.FindStringSubmatch(s)
456+
if len(matches) == 0 {
457+
return s, nil
458+
}
459+
460+
envName := matches[1]
461+
if value := os.Getenv(envName); value != "" {
462+
return value, nil
463+
}
464+
465+
return "", fmt.Errorf(`Error evaluating "env(%s)": environment variable %s is unset.`, s, envName)
466+
}
467+
466468
func sanitizeProjectId(src string) string {
467469
// A valid project ID must only contain alphanumeric and special characters _.-
468470
sanitized := invalidProjectId.ReplaceAllString(src, "_")

internal/utils/templates/init_config.test.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ enable_signup = true
7777
enable_confirmations = false
7878

7979
# Configure one of the supported SMS providers: `twilio`, `messagebird`, `textlocal`, `vonage`.
80-
# [auth.sms.twilio]
81-
# account_sid = ""
82-
# message_service_sid = ""
80+
[auth.sms.twilio]
81+
enabled = false
82+
account_sid = ""
83+
message_service_sid = ""
8384
# DO NOT commit your Twilio auth token to git. You can leave this field blank and define the
8485
# following environment variable instead: SUPABASE_AUTH_SMS_TWILIO_AUTH_TOKEN
85-
# auth_token = ""
86+
auth_token = ""
8687

8788
# Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`,
8889
# `discord`, `facebook`, `github`, `gitlab`, `google`, `keycloak`, `linkedin`, `notion`, `twitch`,

internal/utils/templates/init_config.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ enable_signup = true
7777
enable_confirmations = false
7878

7979
# Configure one of the supported SMS providers: `twilio`, `messagebird`, `textlocal`, `vonage`.
80-
# [auth.sms.twilio]
81-
# account_sid = ""
82-
# message_service_sid = ""
80+
[auth.sms.twilio]
81+
enabled = false
82+
account_sid = ""
83+
message_service_sid = ""
8384
# DO NOT commit your Twilio auth token to git. You can leave this field blank and define the
8485
# following environment variable instead: SUPABASE_AUTH_SMS_TWILIO_AUTH_TOKEN
85-
# auth_token = ""
86+
auth_token = ""
8687

8788
# Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`,
8889
# `discord`, `facebook`, `github`, `gitlab`, `google`, `keycloak`, `linkedin`, `notion`, `twitch`,

0 commit comments

Comments
 (0)