Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mailer logging #1805

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (a *API) isValidAuthorizedEmail(w http.ResponseWriter, req *http.Request) (
}
}

return ctx, badRequestError(ErrorCodeEmailAddressNotAuthorized, "Email address %q cannot be used as it is not authorized", email)
return ctx, badRequestError(ErrorCodeEmailAddressNotAuthorized, "Email address %q is not a member of this organizations team.", email)
}
return ctx, nil
}
Expand Down
60 changes: 60 additions & 0 deletions internal/mailer/mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/auth/internal/conf"
)

var urlRegexp = regexp.MustCompile(`^https?://[^/]+`)
Expand Down Expand Up @@ -85,3 +87,61 @@ func TestRelativeURL(t *testing.T) {
assert.Equal(t, c.Expected, res, c.URL)
}
}

func TestGetProjectRefFromHeaders(t *testing.T) {
cases := []struct {
from string
exp string
ok bool
}{
{
from: `{"x-supabase-project-ref": ["abcjrhohrqmvcpjpsyzc"]}`,
exp: "abcjrhohrqmvcpjpsyzc",
ok: true,
},
{
from: `{"X-Supabase-Project-Ref": ["abcjrhohrqmvcpjpsyzc"]}`,
exp: "abcjrhohrqmvcpjpsyzc",
ok: true,
},
{
from: `{"X-Other-Headers": ["somevalue"]}`,
exp: "",
ok: false,
},
{
from: `{"x-supabase-project-ref": [""]}`,
exp: "",
ok: false,
},
{
from: ``,
exp: "",
ok: false,
},
{
from: `{"x-supabase-project-ref": null}`,
exp: "",
ok: false,
},
}
for _, tc := range cases {
mailer := TemplateMailer{
Config: &conf.GlobalConfiguration{
SMTP: conf.SMTPConfiguration{
Headers: tc.from,
},
},
}
require.NoError(t, mailer.Config.SMTP.Validate())

hdrs := mailer.Headers(tc.from)
ref, ok := getProjectRefFromHeaders(hdrs)
if exp, got := tc.ok, ok; exp != got {
t.Fatalf("exp %v; got %v", exp, got)
}
if exp, got := tc.exp, ref; exp != got {
t.Fatalf("exp %v; got %v", exp, got)
}
}
}
43 changes: 41 additions & 2 deletions internal/mailer/mailme.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ func (m *MailmeMailer) Mail(to, subjectTemplate, templateURL, defaultTemplate st
if err != nil {
return err
}

body, err := m.MailBody(templateURL, defaultTemplate, templateData)
if err != nil {
return err
}

subjectStr := subject.String()
mail := gomail.NewMessage()
mail.SetHeader("From", m.From)
mail.SetHeader("To", to)
mail.SetHeader("Subject", subject.String())
mail.SetHeader("Subject", subjectStr)

for k, v := range headers {
if v != nil {
Expand All @@ -82,8 +84,45 @@ func (m *MailmeMailer) Mail(to, subjectTemplate, templateURL, defaultTemplate st
if m.LocalName != "" {
dial.LocalName = m.LocalName
}
return dial.DialAndSend(mail)

defer func() {
projectRef, ok := getProjectRefFromHeaders(headers)
if !ok {
return
}

fields := logrus.Fields{
"event": "mail.send",
"project_ref": projectRef,
"mail_from": m.From,
"mail_to": to,
"mail_subject": subject,
"mail_template": subjectTemplate,
}
m.Logger.WithFields(fields).Info("mail.send")
}()
if err := dial.DialAndSend(mail); err != nil {
return err
}
return nil
}

const (
mailSupabaseProjectRefHeader = `x-supabase-project-ref`
)

func getProjectRefFromHeaders(headers map[string][]string) (string, bool) {
for key, val := range headers {
if len(val) == 0 || val[0] == "" {
continue
}

hdr := strings.ToLower(key)
if hdr == mailSupabaseProjectRefHeader {
return val[0], true
}
}
return "", false
}

type MailTemplate struct {
Expand Down
63 changes: 49 additions & 14 deletions internal/mailer/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,58 @@ import (
)

func TestTemplateHeaders(t *testing.T) {
mailer := TemplateMailer{
Config: &conf.GlobalConfiguration{
SMTP: conf.SMTPConfiguration{
Headers: `{"X-Test-A": ["test-a", "test-b"], "X-Test-B": ["test-c", "abc $messageType"]}`,
cases := []struct {
from string
typ string
exp map[string][]string
}{
{
from: `{"x-supabase-project-ref": ["abcjrhohrqmvcpjpsyzc"]}`,
typ: "OTHER-TYPE",
exp: map[string][]string{
"x-supabase-project-ref": {"abcjrhohrqmvcpjpsyzc"},
},
},
}

require.NoError(t, mailer.Config.SMTP.Validate())
{
from: `{"X-Test-A": ["test-a", "test-b"], "X-Test-B": ["test-c", "abc $messageType"]}`,
typ: "TEST-MESSAGE-TYPE",
exp: map[string][]string{
"X-Test-A": {"test-a", "test-b"},
"X-Test-B": {"test-c", "abc TEST-MESSAGE-TYPE"},
},
},

require.Equal(t, mailer.Headers("TEST-MESSAGE-TYPE"), map[string][]string{
"X-Test-A": {"test-a", "test-b"},
"X-Test-B": {"test-c", "abc TEST-MESSAGE-TYPE"},
})
{
from: `{"X-Test-A": ["test-a", "test-b"], "X-Test-B": ["test-c", "abc $messageType"]}`,
typ: "OTHER-TYPE",
exp: map[string][]string{
"X-Test-A": {"test-a", "test-b"},
"X-Test-B": {"test-c", "abc OTHER-TYPE"},
},
},

require.Equal(t, mailer.Headers("OTHER-TYPE"), map[string][]string{
"X-Test-A": {"test-a", "test-b"},
"X-Test-B": {"test-c", "abc OTHER-TYPE"},
})
{
from: `{"X-Test-A": ["test-a", "test-b"], "X-Test-B": ["test-c", "abc $messageType"], "x-supabase-project-ref": ["abcjrhohrqmvcpjpsyzc"]}`,
typ: "OTHER-TYPE",
exp: map[string][]string{
"X-Test-A": {"test-a", "test-b"},
"X-Test-B": {"test-c", "abc OTHER-TYPE"},
"x-supabase-project-ref": {"abcjrhohrqmvcpjpsyzc"},
},
},
}
for _, tc := range cases {
mailer := TemplateMailer{
Config: &conf.GlobalConfiguration{
SMTP: conf.SMTPConfiguration{
Headers: tc.from,
},
},
}
require.NoError(t, mailer.Config.SMTP.Validate())

hdrs := mailer.Headers(tc.typ)
require.Equal(t, hdrs, tc.exp)
}
}
Loading