Skip to content

Commit

Permalink
fix: smtp config not always enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Feb 19, 2025
1 parent af5f928 commit 7ba8b22
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
2 changes: 1 addition & 1 deletion internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ EOF
fmt.Sprintf("GOTRUE_MFA_MAX_ENROLLED_FACTORS=%v", utils.Config.Auth.MFA.MaxEnrolledFactors),
}

if utils.Config.Auth.Email.Smtp != nil && utils.Config.Auth.Email.Smtp.IsEnabled() {
if utils.Config.Auth.Email.Smtp != nil && utils.Config.Auth.Email.Smtp.Enabled {
env = append(env,
fmt.Sprintf("GOTRUE_SMTP_HOST=%s", utils.Config.Auth.Email.Smtp.Host),
fmt.Sprintf("GOTRUE_SMTP_PORT=%d", utils.Config.Auth.Email.Smtp.Port),
Expand Down
46 changes: 19 additions & 27 deletions pkg/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type (
}

smtp struct {
Enabled *bool `toml:"enabled"`
Enabled bool `toml:"enabled"`
Host string `toml:"host"`
Port uint16 `toml:"port"`
User string `toml:"user"`
Expand Down Expand Up @@ -495,10 +495,7 @@ func (e *email) fromAuthConfig(remoteConfig v1API.AuthConfigResponse) {
e.OtpExpiry = cast.IntToUint(remoteConfig.MailerOtpExp)
e.SecurePasswordChange = cast.Val(remoteConfig.SecurityUpdatePasswordRequireReauthentication, false)
e.MaxFrequency = time.Duration(cast.Val(remoteConfig.SmtpMaxFrequency, 0)) * time.Second
// When local config is not set, we assume platform defaults should not change
if e.Smtp != nil {
e.Smtp.fromAuthConfig(remoteConfig)
}
e.Smtp.fromAuthConfig(remoteConfig)
if len(e.Template) == 0 {
return
}
Expand Down Expand Up @@ -558,13 +555,8 @@ func (e *email) fromAuthConfig(remoteConfig v1API.AuthConfigResponse) {
}
}

func (s smtp) IsEnabled() bool {
// If Enabled is not defined, or defined and set to true
return cast.Val(s.Enabled, true)
}

func (s smtp) toAuthConfigBody(body *v1API.UpdateAuthConfigBody) {
if !s.IsEnabled() {
if !s.Enabled {
// Setting a single empty string disables SMTP
body.SmtpHost = cast.Ptr("")
return
Expand All @@ -580,25 +572,25 @@ func (s smtp) toAuthConfigBody(body *v1API.UpdateAuthConfigBody) {
}

func (s *smtp) fromAuthConfig(remoteConfig v1API.AuthConfigResponse) {
showDiff := s.IsEnabled()
// Api resets all values when SMTP is disabled
if enabled := remoteConfig.SmtpHost != nil; s.Enabled != nil {
*s.Enabled = enabled
}
if !showDiff {
// When local config is not set, we assume platform defaults should not change
if s == nil {
return
}
s.Host = cast.Val(remoteConfig.SmtpHost, "")
s.User = cast.Val(remoteConfig.SmtpUser, "")
if len(s.Pass.SHA256) > 0 {
s.Pass.SHA256 = cast.Val(remoteConfig.SmtpPass, "")
}
s.AdminEmail = cast.Val(remoteConfig.SmtpAdminEmail, "")
s.SenderName = cast.Val(remoteConfig.SmtpSenderName, "")
portStr := cast.Val(remoteConfig.SmtpPort, "0")
if port, err := strconv.ParseUint(portStr, 10, 16); err == nil {
s.Port = uint16(port)
if s.Enabled {
s.Host = cast.Val(remoteConfig.SmtpHost, "")
s.User = cast.Val(remoteConfig.SmtpUser, "")
if len(s.Pass.SHA256) > 0 {
s.Pass.SHA256 = cast.Val(remoteConfig.SmtpPass, "")
}
s.AdminEmail = cast.Val(remoteConfig.SmtpAdminEmail, "")
s.SenderName = cast.Val(remoteConfig.SmtpSenderName, "")
portStr := cast.Val(remoteConfig.SmtpPort, "0")
if port, err := strconv.ParseUint(portStr, 10, 16); err == nil {
s.Port = uint16(port)
}
}
// Api resets all values when SMTP is disabled
s.Enabled = remoteConfig.SmtpHost != nil
}

func (s sms) toAuthConfigBody(body *v1API.UpdateAuthConfigBody) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/config/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ func TestEmailDiff(t *testing.T) {
},
},
Smtp: &smtp{
Enabled: cast.Ptr(true),
Enabled: true,
Host: "smtp.sendgrid.net",
Port: 587,
User: "apikey",
Expand Down Expand Up @@ -602,9 +602,10 @@ func TestEmailDiff(t *testing.T) {
},
},
Smtp: &smtp{
Host: "smtp.sendgrid.net",
Port: 587,
User: "apikey",
Enabled: true,
Host: "smtp.sendgrid.net",
Port: 587,
User: "apikey",
Pass: Secret{
Value: "test-key",
SHA256: "ed64b7695a606bc6ab4fcb41fe815b5ddf1063ccbc87afe1fa89756635db520e",
Expand Down Expand Up @@ -699,7 +700,7 @@ func TestEmailDiff(t *testing.T) {
"reauthentication": {},
},
Smtp: &smtp{
Enabled: cast.Ptr(false),
Enabled: false,
Host: "smtp.sendgrid.net",
Port: 587,
User: "apikey",
Expand Down
8 changes: 7 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ func (c *config) loadFromReader(v *viper.Viper, r io.Reader) error {
v.Set(k, true)
}
}
// Set default values when [auth.email.smtp] is defined
if smtp := v.GetStringMap("auth.email.smtp"); len(smtp) > 0 {
if _, exists := smtp["enabled"]; !exists {
v.Set("auth.email.smtp.enabled", true)
}
}
if err := v.UnmarshalExact(c, func(dc *mapstructure.DecoderConfig) {
dc.TagName = "toml"
dc.Squash = true
Expand Down Expand Up @@ -916,7 +922,7 @@ func (e *email) validate(fsys fs.FS) (err error) {
}
e.Template[name] = tmpl
}
if e.Smtp != nil && e.Smtp.IsEnabled() {
if e.Smtp != nil && e.Smtp.Enabled {
if len(e.Smtp.Host) == 0 {
return errors.New("Missing required field in config: auth.email.smtp.host")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
diff remote[auth] local[auth]
--- remote[auth]
+++ local[auth]
@@ -31,35 +31,43 @@
@@ -31,36 +31,44 @@
inactivity_timeout = "0s"

[email]
Expand Down Expand Up @@ -43,12 +43,14 @@ diff remote[auth] local[auth]
+content = "recovery-content"
content_path = ""
[email.smtp]
-enabled = false
-host = ""
-port = 0
-user = ""
-pass = ""
-admin_email = ""
-sender_name = ""
+enabled = true
+host = "smtp.sendgrid.net"
+port = 587
+user = "apikey"
Expand Down

0 comments on commit 7ba8b22

Please # to comment.