From 8c2953a7fcd48f366813dd4130080a5f7c2ff855 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Tue, 3 Sep 2019 18:29:25 +0200 Subject: [PATCH] Improve mail header matching, add runner watcher --- pkg/automation/mail/condition.go | 13 ++++------- pkg/automation/mail/condition_test.go | 24 ++++++++++++++++++++ system/internal/service/automation_runner.go | 2 +- system/internal/service/service.go | 4 ++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/pkg/automation/mail/condition.go b/pkg/automation/mail/condition.go index b3b4b2696..f0a2d1785 100644 --- a/pkg/automation/mail/condition.go +++ b/pkg/automation/mail/condition.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "net/mail" + "net/textproto" "regexp" "strings" @@ -155,15 +156,11 @@ func (c *Condition) CheckHeader(header mail.Header, uev userExistanceVerifier) ( // Pre-process & simplify header values: parse all addresses, // extract emails and toss away names, we do not need them for name := range header { - for i, v := range header[name] { - switch name { - case "from", - "to", - "cc", - "bcc", - "reply-to": + switch textproto.CanonicalMIMEHeaderKey(name) { + case "From", "To", "Cc", "Bcc", "Reply-To": + for i, v := range header[name] { addr, _ := mail.ParseAddress(v) - header[name][i] = addr.Address + header[name][i] = strings.Trim(addr.Address, "><") } } } diff --git a/pkg/automation/mail/condition_test.go b/pkg/automation/mail/condition_test.go index 04893449e..8f3686f92 100644 --- a/pkg/automation/mail/condition_test.go +++ b/pkg/automation/mail/condition_test.go @@ -43,6 +43,30 @@ func Test_makeMailHeaderChecker(t *testing.T) { tc: Condition{Headers: []HeaderMatcher{{Name: HeaderMatchNameSubject, Match: "SIMPLE"}}}, expecting: true, }, + { + name: "check address (brackets)", + mh: types.MailMessageHeader{Raw: map[string][]string{"From": []string{""}}}, + tc: Condition{Headers: []HeaderMatcher{{Name: HeaderMatchNameFrom, Match: "some@mail.tld"}}}, + expecting: true, + }, + { + name: "check address (bare)", + mh: types.MailMessageHeader{Raw: map[string][]string{"From": []string{"some@mail.tld"}}}, + tc: Condition{Headers: []HeaderMatcher{{Name: HeaderMatchNameFrom, Match: "some@mail.tld"}}}, + expecting: true, + }, + { + name: "check address (full, quoted)", + mh: types.MailMessageHeader{Raw: map[string][]string{"From": []string{`"John Doe" `}}}, + tc: Condition{Headers: []HeaderMatcher{{Name: HeaderMatchNameFrom, Match: "some@mail.tld"}}}, + expecting: true, + }, + { + name: "check address (full)", + mh: types.MailMessageHeader{Raw: map[string][]string{"From": []string{`John Doe `}}}, + tc: Condition{Headers: []HeaderMatcher{{Name: HeaderMatchNameFrom, Match: "some@mail.tld"}}}, + expecting: true, + }, { name: "two matchers, one matches", mh: types.MailMessageHeader{Raw: map[string][]string{"Subject": []string{"SIMPLE"}}}, diff --git a/system/internal/service/automation_runner.go b/system/internal/service/automation_runner.go index eba7c09bb..3211ca253 100644 --- a/system/internal/service/automation_runner.go +++ b/system/internal/service/automation_runner.go @@ -118,7 +118,7 @@ func (svc automationRunner) makeMailScriptRunner(ctx context.Context, mail *type MailMessage: proto.NewMailMessage(mail), } - svc.logger.Debug("executing script", zap.Any("mail", mail)) + svc.logger.Debug("preparing mail script runner", zap.Any("mail", mail)) return func(script *automation.Script) error { if svc.runner == nil { diff --git a/system/internal/service/service.go b/system/internal/service/service.go index 27d36bcdb..7b6856225 100644 --- a/system/internal/service/service.go +++ b/system/internal/service/service.go @@ -143,5 +143,9 @@ func Init(ctx context.Context, log *zap.Logger, c Config) (err error) { } func Watchers(ctx context.Context) { + // Reloading automation scripts on change + DefaultAutomationRunner.Watch(ctx) + + // Reloading permissions on change DefaultPermissions.Watch(ctx) }