From 662164c7ffe05d2113ea6fa9aa4ed3e02db87c6c Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Wed, 1 Mar 2023 18:31:02 +0100 Subject: [PATCH] smtp: require templates only if a server is configured or in service mode This regression was introduced after recent changes to allow setting the SMTP settings from the WebAdmin UI. Fixes #1217 Signed-off-by: Nicola Murino --- go.mod | 12 +++---- go.sum | 25 +++++++------ internal/cmd/smtptest.go | 2 +- internal/cmd/startsubsys.go | 2 +- internal/common/dataretention_test.go | 10 +++--- internal/common/protocol_test.go | 52 +++++++++++++-------------- internal/httpd/httpd_test.go | 30 ++++++++-------- internal/service/service.go | 2 +- internal/smtp/smtp.go | 51 ++++++++++++++++++-------- 9 files changed, 104 insertions(+), 82 deletions(-) diff --git a/go.mod b/go.mod index 20278b38..37e0b282 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,7 @@ require ( github.com/lithammer/shortuuid/v3 v3.0.7 github.com/mattn/go-sqlite3 v1.14.16 github.com/mhale/smtpd v0.8.0 - github.com/minio/sio v0.3.0 + github.com/minio/sio v0.3.1 github.com/otiai10/copy v1.9.0 github.com/pires/go-proxyproto v0.6.2 github.com/pkg/sftp v1.13.6-0.20230213180117-971c283182b6 @@ -53,7 +53,7 @@ require ( github.com/rs/xid v1.4.0 github.com/rs/zerolog v1.29.0 github.com/sftpgo/sdk v0.1.3-0.20230213182959-2d89540f8810 - github.com/shirou/gopsutil/v3 v3.23.1 + github.com/shirou/gopsutil/v3 v3.23.2 github.com/spf13/afero v1.9.4 github.com/spf13/cobra v1.6.1 github.com/spf13/viper v1.15.0 @@ -73,7 +73,7 @@ require ( golang.org/x/sys v0.5.0 golang.org/x/term v0.5.0 golang.org/x/time v0.3.0 - google.golang.org/api v0.110.0 + google.golang.org/api v0.111.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) @@ -138,11 +138,11 @@ require ( github.com/mitchellh/go-testing-interface v1.14.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/oklog/run v1.1.0 // indirect - github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/pelletier/go-toml/v2 v2.0.7 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/prometheus/client_model v0.3.0 // indirect - github.com/prometheus/common v0.39.0 // indirect + github.com/prometheus/common v0.41.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/spf13/cast v1.5.0 // indirect @@ -157,7 +157,7 @@ require ( golang.org/x/tools v0.6.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20230227214838-9b19f0bdc514 // indirect + google.golang.org/genproto v0.0.0-20230301171018-9ab4bdc49ad5 // indirect google.golang.org/grpc v1.53.0 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index 1aa05394..2b885162 100644 --- a/go.sum +++ b/go.sum @@ -1522,8 +1522,8 @@ github.com/miekg/dns v1.1.51/go.mod h1:2Z9d3CP1LQWihRZUf29mQ19yDThaI4DAYzte2CaQW github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= -github.com/minio/sio v0.3.0 h1:syEFBewzOMOYVzSTFpp1MqpSZk8rUNbz8VIIc+PNzus= -github.com/minio/sio v0.3.0/go.mod h1:8b0yPp2avGThviy/+OCJBI6OMpvxoUuiLvE6F1lebhw= +github.com/minio/sio v0.3.1 h1:d59r5RTHb1OsQaSl1EaTWurzMMDRLA5fgNmjzD4eVu4= +github.com/minio/sio v0.3.1/go.mod h1:S0ovgVgc+sTlQyhiXA1ppBLv7REM7TYi5yyq2qL/Y6o= github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= @@ -1668,8 +1668,8 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9 github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= -github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= +github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us= +github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc= @@ -1741,8 +1741,8 @@ github.com/prometheus/common v0.30.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+ github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/common v0.34.0/go.mod h1:gB3sOl7P0TvJabZpLY5uQMpUqRCPPCyRLCZYc7JZTNE= github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA= -github.com/prometheus/common v0.39.0 h1:oOyhkDq05hPZKItWVBkJ6g6AtGxi+fy7F4JvUV8uhsI= -github.com/prometheus/common v0.39.0/go.mod h1:6XBZ7lYdLCbkAVhwRsWTZn+IN5AB9F/NXd5w0BbEX0Y= +github.com/prometheus/common v0.41.0 h1:npo01n6vUlRViIj5fgwiK8vlNIh8bnoxqh3gypKsyAw= +github.com/prometheus/common v0.41.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc= github.com/prometheus/common/assets v0.1.0/go.mod h1:D17UVUE12bHbim7HzwUvtqm6gwBEaDQ0F+hIGbFbccI= github.com/prometheus/common/assets v0.2.0/go.mod h1:D17UVUE12bHbim7HzwUvtqm6gwBEaDQ0F+hIGbFbccI= github.com/prometheus/common/sigv4 v0.1.0/go.mod h1:2Jkxxk9yYvCkE5G1sQT7GuEXm57JrvHu9k5YwTjsNtI= @@ -1804,8 +1804,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod github.com/secsy/goftp v0.0.0-20200609142545-aa2de14babf4 h1:PT+ElG/UUFMfqy5HrxJxNzj3QBOf7dZwupeVC+mG1Lo= github.com/sftpgo/sdk v0.1.3-0.20230213182959-2d89540f8810 h1:9K/1RGoZcWiv2ue1JvAnKwerOzJsCAUqCR2/BnibT8s= github.com/sftpgo/sdk v0.1.3-0.20230213182959-2d89540f8810/go.mod h1:B1lPGb05WtvvrX5IuhHrSjWdRT867qBaoxlS2Q9+1bA= -github.com/shirou/gopsutil/v3 v3.23.1 h1:a9KKO+kGLKEvcPIs4W62v0nu3sciVDOOOPUD0Hz7z/4= -github.com/shirou/gopsutil/v3 v3.23.1/go.mod h1:NN6mnm5/0k8jw4cBfCnJtr5L7ErOTg18tMNpgFkn0hA= +github.com/shirou/gopsutil/v3 v3.23.2 h1:PAWSuiAszn7IhPMBtXsbSCafej7PqUOvY6YywlQUExU= +github.com/shirou/gopsutil/v3 v3.23.2/go.mod h1:gv0aQw33GLo3pG8SiWKiQrbDzbRY1K80RyZJ7V4Th1M= github.com/shoenig/test v0.4.3/go.mod h1:xYtyGBC5Q3kzCNyJg/SjgNpfAa2kvmgA0i5+lQso8x0= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= @@ -2390,7 +2390,6 @@ golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -2584,8 +2583,8 @@ google.golang.org/api v0.99.0/go.mod h1:1YOf74vkVndF7pG6hIHuINsM7eWwpVTAfNMNiL91 google.golang.org/api v0.100.0/go.mod h1:ZE3Z2+ZOr87Rx7dqFsdRQkRBk36kDtp/h+QpHbB7a70= google.golang.org/api v0.102.0/go.mod h1:3VFl6/fzoA+qNuS1N1/VfXY4LjoXN/wzeIp7TweWwGo= google.golang.org/api v0.103.0/go.mod h1:hGtW6nK1AC+d9si/UBhw8Xli+QMOf6xyNAyJw4qU9w0= -google.golang.org/api v0.110.0 h1:l+rh0KYUooe9JGbGVx71tbFo4SMbMTXK3I3ia2QSEeU= -google.golang.org/api v0.110.0/go.mod h1:7FC4Vvx1Mooxh8C5HWjzZHcavuS2f6pmJpZx60ca7iI= +google.golang.org/api v0.111.0 h1:bwKi+z2BsdwYFRKrqwutM+axAlYLz83gt5pDSXCJT+0= +google.golang.org/api v0.111.0/go.mod h1:qtFHvU9mhgTJegR31csQ+rwxyUTHOKFqCKWp1J0fdw0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -2719,8 +2718,8 @@ google.golang.org/genproto v0.0.0-20221109142239-94d6d90a7d66/go.mod h1:rZS5c/ZV google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg= google.golang.org/genproto v0.0.0-20221201164419-0e50fba7f41c/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg= google.golang.org/genproto v0.0.0-20221201204527-e3fa12d562f3/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg= -google.golang.org/genproto v0.0.0-20230227214838-9b19f0bdc514 h1:rtNKfB++wz5mtDY2t5C8TXlU5y52ojSu7tZo0z7u8eQ= -google.golang.org/genproto v0.0.0-20230227214838-9b19f0bdc514/go.mod h1:TvhZT5f700eVlTNwND1xoEZQeWTB2RY/65kplwl/bFA= +google.golang.org/genproto v0.0.0-20230301171018-9ab4bdc49ad5 h1:/cadn7taPtPlCgiWNetEPsle7jgnlad2R7gR5MXB6dM= +google.golang.org/genproto v0.0.0-20230301171018-9ab4bdc49ad5/go.mod h1:TvhZT5f700eVlTNwND1xoEZQeWTB2RY/65kplwl/bFA= google.golang.org/grpc v0.0.0-20160317175043-d3ddb4469d5a/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= diff --git a/internal/cmd/smtptest.go b/internal/cmd/smtptest.go index 2943929b..876b5635 100644 --- a/internal/cmd/smtptest.go +++ b/internal/cmd/smtptest.go @@ -50,7 +50,7 @@ If the SMTP configuration is correct you should receive this email.`, os.Exit(1) } smtpConfig := config.GetSMTPConfig() - err = smtpConfig.Initialize(configDir) + err = smtpConfig.Initialize(configDir, false) if err != nil { logger.ErrorToConsole("unable to initialize SMTP configuration: %v", err) os.Exit(1) diff --git a/internal/cmd/startsubsys.go b/internal/cmd/startsubsys.go index e5021b70..4e1ea36b 100644 --- a/internal/cmd/startsubsys.go +++ b/internal/cmd/startsubsys.go @@ -114,7 +114,7 @@ Command-line flags should be specified in the Subsystem declaration. os.Exit(1) } smtpConfig := config.GetSMTPConfig() - err = smtpConfig.Initialize(configDir) + err = smtpConfig.Initialize(configDir, false) if err != nil { logger.Error(logSender, connectionID, "unable to initialize SMTP configuration: %v", err) os.Exit(1) diff --git a/internal/common/dataretention_test.go b/internal/common/dataretention_test.go index e861e585..a5717b79 100644 --- a/internal/common/dataretention_test.go +++ b/internal/common/dataretention_test.go @@ -87,7 +87,7 @@ func TestRetentionValidation(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) err = check.Validate() @@ -99,7 +99,7 @@ func TestRetentionValidation(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) check.Notifications = []RetentionCheckNotification{RetentionCheckNotificationHook} @@ -120,7 +120,7 @@ func TestRetentionEmailNotifications(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) user := dataprovider.User{ @@ -160,7 +160,7 @@ func TestRetentionEmailNotifications(t *testing.T) { } smtpCfg.Port = 2626 - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) err = check.sendEmailNotification(nil) assert.Error(t, err) @@ -175,7 +175,7 @@ func TestRetentionEmailNotifications(t *testing.T) { } smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) err = check.sendEmailNotification(nil) assert.Error(t, err) diff --git a/internal/common/protocol_test.go b/internal/common/protocol_test.go index 5cc07782..945e83d7 100644 --- a/internal/common/protocol_test.go +++ b/internal/common/protocol_test.go @@ -3492,7 +3492,7 @@ func TestEventRule(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ @@ -3825,7 +3825,7 @@ func TestEventRule(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -3839,7 +3839,7 @@ func TestEventRuleProviderEvents(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) saveObjectScriptPath := filepath.Join(os.TempDir(), "provider.sh") @@ -3982,7 +3982,7 @@ func TestEventRuleProviderEvents(t *testing.T) { lastReceivedEmail.reset() // generate an error for the failure action smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) _, _, err = httpdtest.UpdateFolder(folder, http.StatusOK) assert.NoError(t, err) @@ -4608,7 +4608,7 @@ func TestEventFsActionsGroupFilters(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ @@ -4728,7 +4728,7 @@ func TestEventFsActionsGroupFilters(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -4739,7 +4739,7 @@ func TestBackupAsAttachment(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ @@ -4812,7 +4812,7 @@ func TestBackupAsAttachment(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -5057,7 +5057,7 @@ func TestEventActionCompressQuotaErrors(t *testing.T) { From: "notify@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) testDir := "archiveDir" @@ -5215,7 +5215,7 @@ func TestEventActionCompressQuotaErrors(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -5463,7 +5463,7 @@ func TestEventActionEmailAttachments(t *testing.T) { From: "notify@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ @@ -5569,7 +5569,7 @@ func TestEventActionEmailAttachments(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -5580,7 +5580,7 @@ func TestEventActionsRetentionReports(t *testing.T) { From: "notify@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) testDir := "/d" @@ -5817,7 +5817,7 @@ func TestEventActionsRetentionReports(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -5828,7 +5828,7 @@ func TestEventRuleFirstUploadDownloadActions(t *testing.T) { From: "notify@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ Name: "action1", @@ -5948,7 +5948,7 @@ func TestEventRuleFirstUploadDownloadActions(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -5959,7 +5959,7 @@ func TestEventRuleRenameEvent(t *testing.T) { From: "notify@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ @@ -6029,7 +6029,7 @@ func TestEventRuleRenameEvent(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -6040,7 +6040,7 @@ func TestEventRuleCertificate(t *testing.T) { From: "notify@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) lastReceivedEmail.reset() @@ -6156,7 +6156,7 @@ func TestEventRuleCertificate(t *testing.T) { }) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -6177,7 +6177,7 @@ func TestEventRuleIPBlocked(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) a1 := dataprovider.BaseEventAction{ @@ -6278,7 +6278,7 @@ func TestEventRuleIPBlocked(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) err = common.Initialize(oldConfig, 0) @@ -6292,7 +6292,7 @@ func TestEventRulePasswordExpiration(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated) @@ -6485,7 +6485,7 @@ func TestEventRulePasswordExpiration(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -7080,7 +7080,7 @@ func TestSFTPLoopError(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) user1 := getTestUser() @@ -7210,7 +7210,7 @@ func TestSFTPLoopError(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } diff --git a/internal/httpd/httpd_test.go b/internal/httpd/httpd_test.go index 629a0bb4..c3dfc852 100644 --- a/internal/httpd/httpd_test.go +++ b/internal/httpd/httpd_test.go @@ -6285,7 +6285,7 @@ func TestNamingRules(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) err = dataprovider.Close() assert.NoError(t, err) @@ -6514,7 +6514,7 @@ func TestNamingRules(t *testing.T) { assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -9108,7 +9108,7 @@ func TestSMTPConfig(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) smtpTestURL := path.Join(webConfigsPath, "smtp", "test") @@ -9175,7 +9175,7 @@ func TestSMTPConfig(t *testing.T) { err = dataprovider.UpdateConfigs(nil, "", "", "") assert.NoError(t, err) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) } @@ -12219,7 +12219,7 @@ func TestMaxSessions(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) assert.NoError(t, err) csrfToken, err := getCSRFToken(httpBaseURL + webLoginPath) @@ -12248,7 +12248,7 @@ func TestMaxSessions(t *testing.T) { assert.Contains(t, rr.Body.String(), "Password reset successfully but unable to login") smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) common.Connections.Remove(connection.GetID()) @@ -12517,7 +12517,7 @@ func TestSFTPLoopError(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) assert.NoError(t, err) csrfToken, err := getCSRFToken(httpBaseURL + webLoginPath) @@ -12546,7 +12546,7 @@ func TestSFTPLoopError(t *testing.T) { assert.Contains(t, rr.Body.String(), "Password reset successfully but unable to login") smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) _, err = httpdtest.RemoveUser(user1, http.StatusOK) @@ -22850,7 +22850,7 @@ func TestAdminForgotPassword(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a := getTestAdmin() @@ -22956,7 +22956,7 @@ func TestAdminForgotPassword(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) form = make(url.Values) @@ -22971,7 +22971,7 @@ func TestAdminForgotPassword(t *testing.T) { assert.Contains(t, rr.Body.String(), "Unable to send confirmation code via email") smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) form.Set("username", altAdminUsername) @@ -23005,7 +23005,7 @@ func TestUserForgotPassword(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) u := getTestUser() @@ -23118,7 +23118,7 @@ func TestUserForgotPassword(t *testing.T) { assert.GreaterOrEqual(t, len(lastResetCode), 20) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) req, err = http.NewRequest(http.MethodGet, webClientForgotPwdPath, nil) @@ -23156,7 +23156,7 @@ func TestAPIForgotPassword(t *testing.T) { From: "notification@example.com", TemplatesPath: "templates", } - err := smtpCfg.Initialize(configDir) + err := smtpCfg.Initialize(configDir, true) require.NoError(t, err) a := getTestAdmin() @@ -23301,7 +23301,7 @@ func TestAPIForgotPassword(t *testing.T) { assert.GreaterOrEqual(t, len(lastResetCode), 20) smtpCfg = smtp.Config{} - err = smtpCfg.Initialize(configDir) + err = smtpCfg.Initialize(configDir, true) require.NoError(t, err) // without an smtp configuration reset password is not available diff --git a/internal/service/service.go b/internal/service/service.go index e4893c6e..d16ddf99 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -148,7 +148,7 @@ func (s *Service) initializeServices(disableAWSInstallationCode bool) error { return err } smtpConfig := config.GetSMTPConfig() - err = smtpConfig.Initialize(s.ConfigDir) + err = smtpConfig.Initialize(s.ConfigDir, s.PortableMode != 1) if err != nil { logger.Error(logSender, "", "unable to initialize SMTP configuration: %v", err) logger.ErrorToConsole("unable to initialize SMTP configuration: %v", err) diff --git a/internal/smtp/smtp.go b/internal/smtp/smtp.go index e3943c67..0b1f2d32 100644 --- a/internal/smtp/smtp.go +++ b/internal/smtp/smtp.go @@ -196,20 +196,7 @@ func (c *Config) isEqual(other *Config) bool { return true } -// Initialize initialized and validates the SMTP configuration -func (c *Config) Initialize(configDir string) error { - if c.TemplatesPath == "" { - logger.Debug(logSender, "", "templates path empty, using default") - c.TemplatesPath = "templates" - } - templatesPath := util.FindSharedDataPath(c.TemplatesPath, configDir) - if templatesPath == "" { - return fmt.Errorf("smtp: invalid templates path %q", templatesPath) - } - loadTemplates(filepath.Join(templatesPath, templateEmailDir)) - if c.Host == "" { - return loadConfigFromProvider() - } +func (c *Config) validate() error { if c.Port <= 0 || c.Port > 65535 { return fmt.Errorf("smtp: invalid port %d", c.Port) } @@ -222,6 +209,42 @@ func (c *Config) Initialize(configDir string) error { if c.From == "" && c.User == "" { return fmt.Errorf(`smtp: from address and user cannot both be empty`) } + return nil +} + +func (c *Config) loadTemplates(configDir string) error { + if c.TemplatesPath == "" { + logger.Debug(logSender, "", "templates path empty, using default") + c.TemplatesPath = "templates" + } + templatesPath := util.FindSharedDataPath(c.TemplatesPath, configDir) + if templatesPath == "" { + return fmt.Errorf("smtp: invalid templates path %q", templatesPath) + } + loadTemplates(filepath.Join(templatesPath, templateEmailDir)) + return nil +} + +// Initialize initialized and validates the SMTP configuration +func (c *Config) Initialize(configDir string, isService bool) error { + if !isService && c.Host == "" { + if err := loadConfigFromProvider(); err != nil { + return err + } + if !config.isEnabled() { + return nil + } + return c.loadTemplates(configDir) + } + if err := c.loadTemplates(configDir); err != nil { + return err + } + if c.Host == "" { + return loadConfigFromProvider() + } + if err := c.validate(); err != nil { + return err + } initialConfig = c config.Set(nil) logger.Debug(logSender, "", "configuration successfully initialized, host: %q, port: %d, username: %q, auth: %d, encryption: %d, helo: %q",