-
Notifications
You must be signed in to change notification settings - Fork 976
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
courier: Implement message templates and SMTP delivery (#146)
This patch adds a message templates (with override capabilities) and SMTP delivery. Integration tests using MailHog test fault resilience and e2e email delivery. This system is designed to be extended for SMS and other use cases. Closes #99
- Loading branch information
Showing
42 changed files
with
769 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,12 +34,17 @@ jobs: | |
image: circleci/golang:1.13 | ||
environment: | ||
- GO111MODULE=on | ||
- TEST_MAILHOG_SMTP=smtp://test:[email protected]:1025 | ||
- TEST_MAILHOG_API=smtp://127.0.0.1:8025 | ||
- TEST_SELFSERVICE_OIDC_HYDRA_ADMIN=http://127.0.0.1:4445 | ||
- TEST_SELFSERVICE_OIDC_HYDRA_PUBLIC=http://127.0.0.1:4444 | ||
- TEST_SELFSERVICE_OIDC_HYDRA_INTEGRATION_ADDR=127.0.0.1:4499 | ||
- TEST_DATABASE_POSTGRESQL=postgres://test:test@localhost:5432/postgres?sslmode=disable | ||
- TEST_DATABASE_MYSQL=mysql://root:test@(localhost:3306)/mysql?parseTime=true | ||
# - TEST_DATABASE_COCKROACHDB=cockroach://root@localhost:26257/defaultdb?sslmode=disable | ||
- | ||
image: mailhog/mailhog:v1.0.0 | ||
command: MailHog -invite-jim -jim-linkspeed-affect=0.25 -jim-reject-auth=0.25 -jim-reject-recipient=0.25 -jim-reject-sender=0.25 -jim-disconnect=0.25 -jim-linkspeed-min=1250 -jim-linkspeed-max=12500 | ||
- | ||
image: postgres:9.6 | ||
environment: | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
contrib/sql/migrations/20191100000005_identities.mysql.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE identity_credential_identifiers MODIFY COLUMN identifier VARCHAR(255); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
drop_table("courier_messages") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
create_table("courier_messages") { | ||
t.Column("id", "uuid", {primary: true}) | ||
|
||
t.Column("type", "int") | ||
t.Column("status", "int") | ||
|
||
t.Column("body", "string") | ||
t.Column("subject", "string") | ||
t.Column("recipient", "string") | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package courier | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff" | ||
"github.com/gofrs/uuid" | ||
"github.com/pkg/errors" | ||
"gopkg.in/gomail.v2" | ||
|
||
"github.com/ory/kratos/driver/configuration" | ||
"github.com/ory/kratos/x" | ||
) | ||
|
||
type ( | ||
smtpDependencies interface { | ||
PersistenceProvider | ||
x.LoggingProvider | ||
} | ||
Courier struct { | ||
dialer *gomail.Dialer | ||
d smtpDependencies | ||
c configuration.Provider | ||
} | ||
Provider interface { | ||
Courier() *Courier | ||
} | ||
) | ||
|
||
func NewSMTP(d smtpDependencies, c configuration.Provider) *Courier { | ||
uri := c.CourierSMTPURL() | ||
sslSkipVerify, _ := strconv.ParseBool(uri.Query().Get("skip_ssl_verify")) | ||
password, _ := uri.User.Password() | ||
port, _ := strconv.ParseInt(uri.Port(), 10, 64) | ||
return &Courier{ | ||
d: d, | ||
c: c, | ||
dialer: &gomail.Dialer{ | ||
Host: uri.Hostname(), | ||
Port: int(port), | ||
Username: uri.User.Username(), | ||
Password: password, | ||
SSL: uri.Scheme == "smtps", | ||
TLSConfig: &tls.Config{InsecureSkipVerify: sslSkipVerify}, | ||
}, | ||
} | ||
} | ||
|
||
func (m *Courier) SendEmail(ctx context.Context, t EmailTemplate) (uuid.UUID, error) { | ||
body, err := t.EmailBody() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
subject, err := t.EmailSubject() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
recipient, err := t.EmailRecipient() | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
message := &Message{ | ||
Status: MessageStatusQueued, | ||
Type: MessageTypeEmail, | ||
Body: body, | ||
Subject: subject, | ||
Recipient: recipient, | ||
} | ||
if err := m.d.CourierPersister().AddMessage(ctx, message); err != nil { | ||
return uuid.Nil, err | ||
} | ||
return message.ID, nil | ||
} | ||
|
||
func (m *Courier) Work(ctx context.Context) error { | ||
errChan := make(chan error) | ||
defer close(errChan) | ||
|
||
go m.watchMessages(ctx, errChan) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case err := <-errChan: | ||
return err | ||
} | ||
} | ||
|
||
func (m *Courier) watchMessages(ctx context.Context, errChan chan error) { | ||
for { | ||
if err := backoff.Retry(func() error { | ||
messages, err := m.d.CourierPersister().NextMessages(ctx, 10) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k := range messages { | ||
var msg Message = messages[k] | ||
|
||
switch msg.Type { | ||
case MessageTypeEmail: | ||
from := m.c.CourierSMTPFrom() | ||
gm := gomail.NewMessage() | ||
gm.SetHeader("From", from) | ||
gm.SetHeader("To", msg.Recipient) | ||
gm.SetHeader("Subject", msg.Subject) | ||
gm.SetBody("text/plain", msg.Body) | ||
gm.AddAlternative("text/html", msg.Body) | ||
|
||
if err := m.dialer.DialAndSend(gm); err != nil { | ||
m.d.Logger(). | ||
WithError(err). | ||
WithField("smtp_server", fmt.Sprintf("smtp(s)://%s:%d", m.dialer.Host, m.dialer.Port)). | ||
WithField("email_to", msg.Recipient).WithField("email_from", from). | ||
Error("Unable to send email using SMTP connection.") | ||
continue | ||
} | ||
|
||
if err := m.d.CourierPersister().SetMessageStatus(ctx, msg.ID, MessageStatusSent); err != nil { | ||
m.d.Logger(). | ||
WithError(err). | ||
WithField("message_id", msg.ID). | ||
Error(`Unable to set the message status to "sent".`) | ||
return err | ||
} | ||
default: | ||
return errors.Errorf("received unexpected message type: %d", msg.Type) | ||
} | ||
} | ||
|
||
return nil | ||
}, backoff.NewExponentialBackOff()); err != nil { | ||
errChan <- err | ||
return | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} |
Oops, something went wrong.