From 956afbfc6bd2c5e5fe777c49d7f61f392f2a6692 Mon Sep 17 00:00:00 2001 From: Krzysztof Tomecki <152964795+chris-4chain@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:47:24 +0100 Subject: [PATCH] feat(BUX-515): removed PaymailConfig.Enabled --- cmd/server/main.go | 3 +-- config.example.yaml | 1 - config/config.go | 6 ++++-- config/config_test.go | 1 - config/defaults.go | 1 - config/services.go | 28 ++++++++++----------------- config/validate_paymail.go | 34 +++++++++++++++++---------------- config/validate_paymail_test.go | 29 ++++++++++++++++++++++------ server/server.go | 6 +----- 9 files changed, 57 insertions(+), 52 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 71356f326..0e342e6b7 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -51,12 +51,11 @@ func main() { // (debugging: show services that are enabled or not) if appConfig.Debug { services.Logger.Debug().Msgf( - "datastore: %s | cachestore: %s | taskmanager: %s | new_relic: %t | paymail: %t | graphql: %t", + "datastore: %s | cachestore: %s | taskmanager: %s | new_relic: %t | graphql: %t", appConfig.Db.Datastore.Engine.String(), appConfig.Cache.Engine.String(), appConfig.TaskManager.Factory.String(), appConfig.NewRelic.Enabled, - appConfig.Paymail.Enabled, appConfig.GraphQL.Enabled, ) } diff --git a/config.example.yaml b/config.example.yaml index 7545fcd01..3b5331956 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -100,7 +100,6 @@ paymail: domain_validation_enabled: false domains: - localhost - enabled: true sender_validation_enabled: false request_logging: true server_config: diff --git a/config/config.go b/config/config.go index 8f9f1c2a8..807385c47 100644 --- a/config/config.go +++ b/config/config.go @@ -202,8 +202,6 @@ type PaymailConfig struct { Domains []string `json:"domains" mapstructure:"domains"` // DomainValidationEnabled should be turned off if hosted domain is not paymail related. DomainValidationEnabled bool `json:"domain_validation_enabled" mapstructure:"domain_validation_enabled"` - // Enabled is a flag for enabling the Paymail Server Service. - Enabled bool `json:"enabled" mapstructure:"enabled"` // SenderValidationEnabled should be turned on for extra security. SenderValidationEnabled bool `json:"sender_validation_enabled" mapstructure:"sender_validation_enabled"` } @@ -218,6 +216,10 @@ type BeefConfig struct { PulseAuthToken string `json:"pulse_auth_token" mapstructure:"pulse_auth_token"` } +func (b *BeefConfig) enabled() bool { + return b != nil && b.UseBeef +} + // TaskManagerConfig is a configuration for the taskmanager type TaskManagerConfig struct { // Factory is the Task Manager factory, memory or redis. diff --git a/config/config_test.go b/config/config_test.go index fcd9ba67d..95f81ba2f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -100,7 +100,6 @@ func TestAppConfig_Validate(t *testing.T) { t.Run("paymail - no domains", func(t *testing.T) { app, _ := baseTestConfig(t) - app.Paymail.Enabled = true app.Paymail.Domains = nil err := app.Validate() assert.Error(t, err) diff --git a/config/defaults.go b/config/defaults.go index f7cc98589..3369a0ca3 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -146,7 +146,6 @@ func getPaymailDefaults() *PaymailConfig { DefaultNote: "bux Address Resolution", Domains: []string{"localhost"}, DomainValidationEnabled: true, - Enabled: true, SenderValidationEnabled: false, } } diff --git a/config/services.go b/config/services.go index bacc8260b..55d3ce2bd 100644 --- a/config/services.go +++ b/config/services.go @@ -171,14 +171,6 @@ func (s *AppServices) loadBux(ctx context.Context, appConfig *AppConfig, testMod options = loadPaymail(appConfig, options) - if appConfig.Paymail.Beef.UseBeef { - if appConfig.Paymail.Beef.PulseHeaderValidationURL == "" { - err = errors.New("pulse is required for BEEF to work") - return err - } - options = append(options, bux.WithPaymailBeefSupport(appConfig.Paymail.Beef.PulseHeaderValidationURL, appConfig.Paymail.Beef.PulseAuthToken)) - } - options = loadTaskManager(appConfig, options) if appConfig.Notifications != nil && appConfig.Notifications.Enabled { @@ -270,16 +262,16 @@ func loadCluster(appConfig *AppConfig, options []bux.ClientOps) ([]bux.ClientOps } func loadPaymail(appConfig *AppConfig, options []bux.ClientOps) []bux.ClientOps { - // Set the Paymail server if enabled - if appConfig.Paymail.Enabled { - // Append the server config - options = append(options, bux.WithPaymailSupport( - appConfig.Paymail.Domains, - appConfig.Paymail.DefaultFromPaymail, - appConfig.Paymail.DefaultNote, - appConfig.Paymail.DomainValidationEnabled, - appConfig.Paymail.SenderValidationEnabled, - )) + pm := appConfig.Paymail + options = append(options, bux.WithPaymailSupport( + pm.Domains, + pm.DefaultFromPaymail, + pm.DefaultNote, + pm.DomainValidationEnabled, + pm.SenderValidationEnabled, + )) + if pm.Beef.enabled() { + options = append(options, bux.WithPaymailBeefSupport(pm.Beef.PulseHeaderValidationURL, pm.Beef.PulseAuthToken)) } return options } diff --git a/config/validate_paymail.go b/config/validate_paymail.go index 00fccaa05..7492e5fba 100644 --- a/config/validate_paymail.go +++ b/config/validate_paymail.go @@ -9,26 +9,28 @@ import ( // Validate checks the configuration for specific rules func (p *PaymailConfig) Validate() error { + if p == nil { + return errors.New("paymail config is required") + } + if p.Beef.enabled() && p.Beef.PulseHeaderValidationURL == "" { + return errors.New("beef_url is required for beef") + } + if len(p.Domains) == 0 { + return errors.New("at least one domain is required for paymail") + } - // Only if enabled - if p.Enabled { - if len(p.Domains) == 0 { - return errors.New("domain is required for paymail") + var err error + for _, domain := range p.Domains { + domain, err = sanitize.Domain(domain, false, true) + if err != nil { + return err } - - var err error - for _, domain := range p.Domains { - domain, err = sanitize.Domain(domain, false, true) - if err != nil { - return err - } - if !validate.IsValidHost(domain) { - return errors.New("domain [" + domain + "] is not a valid hostname") - } + if !validate.IsValidHost(domain) { + return errors.New("domain [" + domain + "] is not a valid hostname") } - - // Todo: validate the default_from_paymail and default_note values } + // Todo: validate the default_from_paymail and default_note values + return nil } diff --git a/config/validate_paymail_test.go b/config/validate_paymail_test.go index c911f2aa8..1482b9f74 100644 --- a/config/validate_paymail_test.go +++ b/config/validate_paymail_test.go @@ -13,7 +13,6 @@ func TestPaymailConfig_Validate(t *testing.T) { t.Run("no domains", func(t *testing.T) { p := PaymailConfig{ Domains: nil, - Enabled: true, } err := p.Validate() require.Error(t, err) @@ -22,7 +21,6 @@ func TestPaymailConfig_Validate(t *testing.T) { t.Run("zero domains", func(t *testing.T) { p := PaymailConfig{ Domains: []string{}, - Enabled: true, } err := p.Validate() require.Error(t, err) @@ -31,7 +29,6 @@ func TestPaymailConfig_Validate(t *testing.T) { t.Run("empty domains", func(t *testing.T) { p := PaymailConfig{ Domains: []string{""}, - Enabled: true, } err := p.Validate() require.Error(t, err) @@ -40,7 +37,6 @@ func TestPaymailConfig_Validate(t *testing.T) { t.Run("invalid hostname", func(t *testing.T) { p := PaymailConfig{ Domains: []string{"..."}, - Enabled: true, } err := p.Validate() require.Error(t, err) @@ -49,7 +45,6 @@ func TestPaymailConfig_Validate(t *testing.T) { t.Run("spaces in hostname", func(t *testing.T) { p := PaymailConfig{ Domains: []string{"spaces in domain"}, - Enabled: true, } err := p.Validate() require.Error(t, err) @@ -58,10 +53,32 @@ func TestPaymailConfig_Validate(t *testing.T) { t.Run("valid domains", func(t *testing.T) { p := PaymailConfig{ Domains: []string{"test.com", "domain.com"}, - Enabled: true, } err := p.Validate() require.NoError(t, err) }) + t.Run("invalid beef", func(t *testing.T) { + p := PaymailConfig{ + Domains: []string{"test.com", "domain.com"}, + Beef: &BeefConfig{ + UseBeef: true, + PulseHeaderValidationURL: "", + }, + } + err := p.Validate() + require.Error(t, err) + }) + + t.Run("valid beef", func(t *testing.T) { + p := PaymailConfig{ + Domains: []string{"test.com", "domain.com"}, + Beef: &BeefConfig{ + UseBeef: true, + PulseHeaderValidationURL: "http://localhost:8080/api/v1/chain/merkleroot/verify", + }, + } + err := p.Validate() + require.NoError(t, err) + }) } diff --git a/server/server.go b/server/server.go index f0e32f4da..cbcc12545 100644 --- a/server/server.go +++ b/server/server.go @@ -40,7 +40,6 @@ func NewServer(appConfig *config.AppConfig, services *config.AppServices) *Serve // Serve will load a server and start serving func (s *Server) Serve() { - // Load the server defaults s.WebServer = &http.Server{ Addr: ":" + strconv.Itoa(s.AppConfig.Server.Port), @@ -82,7 +81,6 @@ func (s *Server) Shutdown(ctx context.Context) error { // Handlers will return handlers func (s *Server) Handlers() *nrhttprouter.Router { - // Start a transaction for loading handlers txn := s.Services.NewRelic.StartTransaction("load_handlers") defer txn.End() @@ -119,9 +117,7 @@ func (s *Server) Handlers() *nrhttprouter.Router { } // Load Paymail - if s.AppConfig.Paymail.Enabled { - pmail.RegisterRoutes(s.Router, s.AppConfig, s.Services) - } + pmail.RegisterRoutes(s.Router, s.AppConfig, s.Services) // Return the router return s.Router.HTTPRouter