Skip to content

Commit

Permalink
feat(BUX-515): removed PaymailConfig.Enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Jan 24, 2024
1 parent c6b5b82 commit 956afbf
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 52 deletions.
3 changes: 1 addition & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down
1 change: 0 additions & 1 deletion config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ paymail:
domain_validation_enabled: false
domains:
- localhost
enabled: true
sender_validation_enabled: false
request_logging: true
server_config:
Expand Down
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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.
Expand Down
1 change: 0 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func getPaymailDefaults() *PaymailConfig {
DefaultNote: "bux Address Resolution",
Domains: []string{"localhost"},
DomainValidationEnabled: true,
Enabled: true,
SenderValidationEnabled: false,
}
}
Expand Down
28 changes: 10 additions & 18 deletions config/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
34 changes: 18 additions & 16 deletions config/validate_paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 23 additions & 6 deletions config/validate_paymail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
})
}
6 changes: 1 addition & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 956afbf

Please sign in to comment.