Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1030 - Refactor SMTP settings to be outside of passcode config #1121

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/knadh/koanf/providers/file"
"github.com/teamhanko/hanko/backend/ee/saml/config"
"golang.org/x/exp/slices"
zeroLogger "github.com/rs/zerolog/log"
"log"
"strings"
"time"
Expand All @@ -20,6 +21,7 @@ import (
type Config struct {
Server Server `yaml:"server" json:"server,omitempty" koanf:"server"`
Webauthn WebauthnSettings `yaml:"webauthn" json:"webauthn,omitempty" koanf:"webauthn"`
Smtp SMTP `yaml:"smtp" json:"smtp,omitempty" koanf:"smtp"`
Passcode Passcode `yaml:"passcode" json:"passcode" koanf:"passcode"`
Password Password `yaml:"password" json:"password,omitempty" koanf:"password"`
Database Database `yaml:"database" json:"database" koanf:"database"`
Expand Down Expand Up @@ -70,6 +72,8 @@ func Load(cfgFile *string) (*Config, error) {
return nil, fmt.Errorf("failed to post process config: %w", err)
}

c.arrangeSmtpSettings()

if err = c.Validate(); err != nil {
return nil, fmt.Errorf("failed to validate config: %s", err)
}
Expand All @@ -96,15 +100,18 @@ func DefaultConfig() *Config {
UserVerification: "preferred",
Timeout: 60000,
},
Smtp: SMTP{
Port: "465",
},
Passcode: Passcode{
Smtp: SMTP{
Port: "465",
},
TTL: 300,
Email: Email{
FromAddress: "[email protected]",
FromName: "Hanko",
},
Smtp: SMTP{
Port: "465",
},
},
Password: Password{
MinPasswordLength: 8,
Expand Down Expand Up @@ -162,6 +169,10 @@ func (c *Config) Validate() error {
if err != nil {
return fmt.Errorf("failed to validate webauthn settings: %w", err)
}
err = c.Smtp.Validate()
if err != nil {
return fmt.Errorf("failed to validate smtp settings: %w", err)
}
err = c.Passcode.Validate()
if err != nil {
return fmt.Errorf("failed to validate passcode settings: %w", err)
Expand Down Expand Up @@ -347,19 +358,16 @@ func (e *Email) Validate() error {

type Passcode struct {
Email Email `yaml:"email" json:"email,omitempty" koanf:"email"`
Smtp SMTP `yaml:"smtp" json:"smtp" koanf:"smtp"`
TTL int `yaml:"ttl" json:"ttl,omitempty" koanf:"ttl" jsonschema:"default=300"`
//Deprecated: Use root level Smtp instead
Smtp SMTP `yaml:"smtp" json:"smtp,omitempty" koanf:"smtp,omitempty" required:"false" envconfig:"smtp,omitempty"`
}

func (p *Passcode) Validate() error {
err := p.Email.Validate()
if err != nil {
return fmt.Errorf("failed to validate email settings: %w", err)
}
err = p.Smtp.Validate()
if err != nil {
return fmt.Errorf("failed to validate smtp settings: %w", err)
}
return nil
}

Expand Down Expand Up @@ -638,6 +646,17 @@ func (c *Config) PostProcess() error {

}

func (c *Config) arrangeSmtpSettings() {
if c.Passcode.Smtp.Validate() == nil {
if c.Smtp.Validate() == nil {
zeroLogger.Warn().Msg("Both root smtp and passcode.smtp are set. Using smtp settings from root configuration")
return
}

c.Smtp = c.Passcode.Smtp
}
}

type LoggerConfig struct {
LogHealthAndMetrics bool `yaml:"log_health_and_metrics,omitempty" json:"log_health_and_metrics" koanf:"log_health_and_metrics" jsonschema:"default=true"`
}
Expand Down
8 changes: 4 additions & 4 deletions backend/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ database:
host: localhost
port: 5432
dialect: postgres
passcode:
email:
from_address: [email protected]
smtp:
smtp:
host: smtp.example.com
user: example
password: example
passcode:
email:
from_address: [email protected]
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
Expand Down
31 changes: 29 additions & 2 deletions backend/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func TestDefaultConfigAccountParameters(t *testing.T) {
assert.Equal(t, cfg.Account.AllowSignup, true)
}

func TestDefaultConfigSmtpParameters(t *testing.T) {
cfg := DefaultConfig()
assert.Equal(t, cfg.Smtp.Port, "465")
}

func TestParseValidConfig(t *testing.T) {
configPath := "./config.yaml"
cfg, err := Load(&configPath)
Expand All @@ -32,6 +37,28 @@ func TestParseValidConfig(t *testing.T) {
}
}

func TestPasscodeSmtpSettingsCopiedToRootLevelSmtp(t *testing.T) {
configPath := "./passcode-smtp-config.yaml"
cfg, err := Load(&configPath)
if err != nil {
t.Error(err)
}
if err := cfg.Validate(); err != nil {
t.Error(err)
}

assert.Equal(t, cfg.Smtp.Port, cfg.Passcode.Smtp.Port)
assert.Equal(t, cfg.Smtp.Host, cfg.Passcode.Smtp.Host)
assert.Equal(t, cfg.Smtp.Password, cfg.Passcode.Smtp.Password)
assert.Equal(t, cfg.Smtp.User, cfg.Passcode.Smtp.User)
}

func TestRootSmtpPasscodeSmtpConflict(t *testing.T) {
configPath := "./root-passcode-smtp-config.yaml"
_, err := Load(&configPath)
assert.NoError(t, err)
}

func TestMinimalConfigValidates(t *testing.T) {
configPath := "./minimal-config.yaml"
cfg, err := Load(&configPath)
Expand Down Expand Up @@ -76,7 +103,7 @@ func TestRateLimiterConfig(t *testing.T) {
}

func TestEnvironmentVariables(t *testing.T) {
err := os.Setenv("PASSCODE_SMTP_HOST", "valueFromEnvVars")
err := os.Setenv("SMTP_HOST", "valueFromEnvVars")
require.NoError(t, err)

err = os.Setenv("WEBAUTHN_RELYING_PARTY_ORIGINS", "https://hanko.io,https://auth.hanko.io")
Expand All @@ -86,6 +113,6 @@ func TestEnvironmentVariables(t *testing.T) {
cfg, err := Load(&configPath)
require.NoError(t, err)

assert.Equal(t, "valueFromEnvVars", cfg.Passcode.Smtp.Host)
assert.Equal(t, "valueFromEnvVars", cfg.Smtp.Host)
assert.True(t, reflect.DeepEqual([]string{"https://hanko.io", "https://auth.hanko.io"}, cfg.Webauthn.RelyingParty.Origins))
}
3 changes: 1 addition & 2 deletions backend/config/minimal-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
passcode:
smtp:
smtp:
host: smtp.example.com
user: example
password: example
Expand Down
18 changes: 18 additions & 0 deletions backend/config/passcode-smtp-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
database:
user: hanko
password: hanko
host: localhost
port: 5432
dialect: postgres
passcode:
email:
from_address: [email protected]
smtp:
host: smtp.example.com
user: example
password: example
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
service:
name: Hanko Authentication Service
22 changes: 22 additions & 0 deletions backend/config/root-passcode-smtp-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
database:
user: hanko
password: hanko
host: localhost
port: 5432
dialect: postgres
smtp:
host: smtp1.example.com
user: example1
password: example1
passcode:
email:
from_address: [email protected]
smtp:
host: smtp2.example.com
user: example2
password: example2
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
service:
name: Hanko Authentication Service
4 changes: 2 additions & 2 deletions backend/handler/passcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (s *passcodeSuite) TestPasscodeHandler_Init() {

cfg := func() *config.Config {
cfg := &test.DefaultConfig
cfg.Passcode.Smtp.Host = "localhost"
cfg.Passcode.Smtp.Port = s.EmailServer.SmtpPort
cfg.Smtp.Host = s.EmailServer.SmtpHost
cfg.Smtp.Port = s.EmailServer.SmtpPort
return cfg
}

Expand Down
2 changes: 1 addition & 1 deletion backend/handler/public_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewPublicRouter(cfg *config.Config, persister persistence.Persister, promet

sessionMiddleware := hankoMiddleware.Session(cfg, sessionManager)

mailer, err := mail.NewMailer(cfg.Passcode.Smtp)
mailer, err := mail.NewMailer(cfg.Smtp)
if err != nil {
panic(fmt.Errorf("failed to create mailer: %w", err))
}
Expand Down
4 changes: 2 additions & 2 deletions backend/handler/webauthn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ var defaultConfig = config.Config{
Secrets: config.Secrets{
Keys: []string{"abcdefghijklmnop"},
},
Passcode: config.Passcode{Smtp: config.SMTP{
Smtp: config.SMTP{
Host: "localhost",
Port: "2500",
}},
},
}

type sessionManager struct {
Expand Down
8 changes: 4 additions & 4 deletions backend/test/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ var DefaultConfig = config.Config{
Secrets: config.Secrets{
Keys: []string{"abcdefghijklmnop"},
},
Smtp: config.SMTP{
Host: "localhost",
Port: "2500",
},
Passcode: config.Passcode{
Smtp: config.SMTP{
Host: "localhost",
Port: "2500",
},
Email: config.Email{
FromAddress: "[email protected]",
FromName: "Hanko Test",
Expand Down
2 changes: 2 additions & 0 deletions backend/test/mailslurper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TestMailslurper struct {
pool *dockertest.Pool
resource *dockertest.Resource
httpUrl string
SmtpHost string
SmtpPort string
}

Expand Down Expand Up @@ -74,6 +75,7 @@ func StartMailslurper() (*TestMailslurper, error) {
pool: pool,
resource: resource,
httpUrl: dsn,
SmtpHost: "localhost",
SmtpPort: smtpPort,
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions deploy/docker-compose/config-disable-signup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ database:
host: postgresd
port: 5432
dialect: postgres
smtp:
host: "mailslurper"
port: "2500"
passcode:
email:
from_address: [email protected]
smtp:
host: "mailslurper"
port: "2500"
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
Expand Down
6 changes: 3 additions & 3 deletions deploy/docker-compose/config-rate-limiting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ database:
host: postgresd
port: 5432
dialect: postgres
smtp:
host: "mailslurper"
port: "2500"
passcode:
email:
from_address: [email protected]
smtp:
host: "mailslurper"
port: "2500"
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
Expand Down
6 changes: 3 additions & 3 deletions deploy/docker-compose/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ database:
host: postgresd
port: 5432
dialect: postgres
smtp:
host: "mailslurper"
port: "2500"
passcode:
email:
from_address: [email protected]
smtp:
host: "mailslurper"
port: "2500"
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
Expand Down
8 changes: 4 additions & 4 deletions deploy/k8s/overlays/thirdparty-x-domain/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ database:
host: postgres
port: 5432
dialect: postgres
smtp:
host: "mailhog"
port: "2500"
passcode:
email:
from_address: [email protected]
smtp:
host: "mailhog"
port: "2500"
secrets:
keys:
- abcedfghijklmnopqrstuvwxyz
Expand Down Expand Up @@ -40,4 +40,4 @@ third_party:
github:
enabled: true
apple:
enabled: true
enabled: true
2 changes: 1 addition & 1 deletion e2e/.nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.15.1
v18.6.0
Loading