1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

policy: Accept Root config instead of SMTP for #33

This commit is contained in:
James Hillyerd
2018-04-02 19:43:05 -07:00
parent 8c66a24513
commit c2e1d58b90
4 changed files with 46 additions and 36 deletions

View File

@@ -118,7 +118,7 @@ func main() {
startupLog.Fatal().Err(err).Str("module", "storage").Msg("Fatal storage error") startupLog.Fatal().Err(err).Str("module", "storage").Msg("Fatal storage error")
} }
msgHub := msghub.New(rootCtx, conf.Web.MonitorHistory) msgHub := msghub.New(rootCtx, conf.Web.MonitorHistory)
addrPolicy := &policy.Addressing{Config: conf.SMTP} addrPolicy := &policy.Addressing{Config: conf}
mmanager := &message.StoreManager{Store: store, Hub: msgHub} mmanager := &message.StoreManager{Store: store, Hub: msgHub}
// Start Retention scanner. // Start Retention scanner.
retentionScanner := storage.NewRetentionScanner(conf.Storage, store, shutdownChan) retentionScanner := storage.NewRetentionScanner(conf.Storage, store, shutdownChan)

View File

@@ -12,7 +12,7 @@ import (
// Addressing handles email address policy. // Addressing handles email address policy.
type Addressing struct { type Addressing struct {
Config config.SMTP Config *config.Root
} }
// NewRecipient parses an address into a Recipient. // NewRecipient parses an address into a Recipient.
@@ -41,10 +41,12 @@ func (a *Addressing) NewRecipient(address string) (*Recipient, error) {
// ShouldAcceptDomain indicates if Inbucket accepts mail destined for the specified domain. // ShouldAcceptDomain indicates if Inbucket accepts mail destined for the specified domain.
func (a *Addressing) ShouldAcceptDomain(domain string) bool { func (a *Addressing) ShouldAcceptDomain(domain string) bool {
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
if a.Config.DefaultAccept && !stringutil.SliceContains(a.Config.RejectDomains, domain) { if a.Config.SMTP.DefaultAccept &&
!stringutil.SliceContains(a.Config.SMTP.RejectDomains, domain) {
return true return true
} }
if !a.Config.DefaultAccept && stringutil.SliceContains(a.Config.AcceptDomains, domain) { if !a.Config.SMTP.DefaultAccept &&
stringutil.SliceContains(a.Config.SMTP.AcceptDomains, domain) {
return true return true
} }
return false return false
@@ -53,10 +55,12 @@ func (a *Addressing) ShouldAcceptDomain(domain string) bool {
// ShouldStoreDomain indicates if Inbucket stores mail destined for the specified domain. // ShouldStoreDomain indicates if Inbucket stores mail destined for the specified domain.
func (a *Addressing) ShouldStoreDomain(domain string) bool { func (a *Addressing) ShouldStoreDomain(domain string) bool {
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
if a.Config.DefaultStore && !stringutil.SliceContains(a.Config.DiscardDomains, domain) { if a.Config.SMTP.DefaultStore &&
!stringutil.SliceContains(a.Config.SMTP.DiscardDomains, domain) {
return true return true
} }
if !a.Config.DefaultStore && stringutil.SliceContains(a.Config.StoreDomains, domain) { if !a.Config.SMTP.DefaultStore &&
stringutil.SliceContains(a.Config.SMTP.StoreDomains, domain) {
return true return true
} }
return false return false

View File

@@ -11,10 +11,12 @@ import (
func TestShouldAcceptDomain(t *testing.T) { func TestShouldAcceptDomain(t *testing.T) {
// Test with default accept. // Test with default accept.
ap := &policy.Addressing{ ap := &policy.Addressing{
Config: config.SMTP{ Config: &config.Root{
SMTP: config.SMTP{
DefaultAccept: true, DefaultAccept: true,
RejectDomains: []string{"a.deny.com", "deny.com"}, RejectDomains: []string{"a.deny.com", "deny.com"},
}, },
},
} }
testCases := []struct { testCases := []struct {
domain string domain string
@@ -36,10 +38,12 @@ func TestShouldAcceptDomain(t *testing.T) {
} }
// Test with default reject. // Test with default reject.
ap = &policy.Addressing{ ap = &policy.Addressing{
Config: config.SMTP{ Config: &config.Root{
SMTP: config.SMTP{
DefaultAccept: false, DefaultAccept: false,
AcceptDomains: []string{"a.allow.com", "allow.com"}, AcceptDomains: []string{"a.allow.com", "allow.com"},
}, },
},
} }
testCases = []struct { testCases = []struct {
domain string domain string
@@ -64,10 +68,12 @@ func TestShouldAcceptDomain(t *testing.T) {
func TestShouldStoreDomain(t *testing.T) { func TestShouldStoreDomain(t *testing.T) {
// Test with storage enabled. // Test with storage enabled.
ap := &policy.Addressing{ ap := &policy.Addressing{
Config: config.SMTP{ Config: &config.Root{
SMTP: config.SMTP{
DefaultStore: false, DefaultStore: false,
StoreDomains: []string{"store.com", "a.store.com"}, StoreDomains: []string{"store.com", "a.store.com"},
}, },
},
} }
testCases := []struct { testCases := []struct {
domain string domain string
@@ -89,10 +95,12 @@ func TestShouldStoreDomain(t *testing.T) {
} }
// Test with storage disabled. // Test with storage disabled.
ap = &policy.Addressing{ ap = &policy.Addressing{
Config: config.SMTP{ Config: &config.Root{
SMTP: config.SMTP{
DefaultStore: true, DefaultStore: true,
DiscardDomains: []string{"discard.com", "a.discard.com"}, DiscardDomains: []string{"discard.com", "a.discard.com"},
}, },
},
} }
testCases = []struct { testCases = []struct {
domain string domain string

View File

@@ -361,8 +361,8 @@ func (m *mockConn) SetReadDeadline(t time.Time) error { return nil }
func (m *mockConn) SetWriteDeadline(t time.Time) error { return nil } func (m *mockConn) SetWriteDeadline(t time.Time) error { return nil }
func setupSMTPServer(ds storage.Store) (s *Server, buf *bytes.Buffer, teardown func()) { func setupSMTPServer(ds storage.Store) (s *Server, buf *bytes.Buffer, teardown func()) {
// Test Server Config cfg := &config.Root{
cfg := config.SMTP{ SMTP: config.SMTP{
Addr: "127.0.0.1:2500", Addr: "127.0.0.1:2500",
Domain: "inbucket.local", Domain: "inbucket.local",
MaxRecipients: 5, MaxRecipients: 5,
@@ -370,32 +370,30 @@ func setupSMTPServer(ds storage.Store) (s *Server, buf *bytes.Buffer, teardown f
DefaultAccept: true, DefaultAccept: true,
RejectDomains: []string{"deny.com"}, RejectDomains: []string{"deny.com"},
Timeout: 5, Timeout: 5,
},
} }
// Capture log output.
// Capture log output
buf = new(bytes.Buffer) buf = new(bytes.Buffer)
log.SetOutput(buf) log.SetOutput(buf)
// Create a server, don't start it.
// Create a server, don't start it
shutdownChan := make(chan bool) shutdownChan := make(chan bool)
teardown = func() { teardown = func() {
close(shutdownChan) close(shutdownChan)
} }
apolicy := &policy.Addressing{Config: cfg} addrPolicy := &policy.Addressing{Config: cfg}
manager := &message.StoreManager{Store: ds} manager := &message.StoreManager{Store: ds}
s = NewServer(cfg, shutdownChan, manager, apolicy) s = NewServer(cfg.SMTP, shutdownChan, manager, addrPolicy)
return s, buf, teardown return s, buf, teardown
} }
var sessionNum int var sessionNum int
func setupSMTPSession(server *Server) net.Conn { func setupSMTPSession(server *Server) net.Conn {
// Pair of pipes to communicate // Pair of pipes to communicate.
serverConn, clientConn := net.Pipe() serverConn, clientConn := net.Pipe()
// Start the session // Start the session.
server.wg.Add(1) server.wg.Add(1)
sessionNum++ sessionNum++
go server.startSession(sessionNum, &mockConn{serverConn}) go server.startSession(sessionNum, &mockConn{serverConn})
return clientConn return clientConn
} }