monkrb 0.16.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +91 -0
- data/README.md +5 -3
- data/exe/monk +58 -5
- data/lib/monk/auth.rb +3 -3
- data/lib/monk/jobs/adapters/pg.rb +272 -0
- data/lib/monk/jobs/args.rb +43 -0
- data/lib/monk/jobs/claim.rb +7 -0
- data/lib/monk/jobs/errors.rb +29 -0
- data/lib/monk/jobs/job.rb +117 -0
- data/lib/monk/jobs/runtime.rb +239 -0
- data/lib/monk/jobs/worker.rb +94 -0
- data/lib/monk/jobs.rb +229 -0
- data/lib/monk/mail/address.rb +41 -0
- data/lib/monk/mail/errors.rb +35 -0
- data/lib/monk/mail/later.rb +53 -0
- data/lib/monk/mail/message.rb +66 -0
- data/lib/monk/mail/mime.rb +124 -0
- data/lib/monk/mail/transports.rb +115 -0
- data/lib/monk/mail/url.rb +76 -0
- data/lib/monk/mail.rb +121 -0
- data/lib/monk/persistence/pg.rb +21 -1
- data/lib/monk/scaffold.rb +316 -18
- data/lib/monk/templates/auth/config/auth.rb +18 -8
- data/lib/monk/templates/auth/views/mail/magic_link.erb +3 -0
- data/lib/monk/templates/jobs/bin/jobs +26 -0
- data/lib/monk/templates/jobs/config/jobs.rb +17 -0
- data/lib/monk/templates/jobs/db/migrate/00000000000002_create_jobs_tables.down.sql +3 -0
- data/lib/monk/templates/jobs/db/migrate/00000000000002_create_jobs_tables.up.sql +48 -0
- data/lib/monk/templates/jobs/jobs/hello_job.rb +9 -0
- data/lib/monk/templates/jobs/jobs/send_login_link.rb +27 -0
- data/lib/monk/templates/mail/Gemfile.extra +1 -0
- data/lib/monk/templates/mail/config/mail.rb +19 -0
- data/lib/monk/templates/postgres/Dockerfile +3 -1
- data/lib/monk/version.rb +1 -1
- metadata +40 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Monk
|
|
2
|
+
module Mail
|
|
3
|
+
# Just enough address handling for what Monk::Mail sends: split
|
|
4
|
+
# "Name <addr>" into its parts, and render one back into a header
|
|
5
|
+
# value. Not an RFC 5322 parser -- groups, comments and quoted local
|
|
6
|
+
# parts are out of scope (docs/adr/0012-minimal-built-in-mailer.md).
|
|
7
|
+
module Address
|
|
8
|
+
ANGLE = /\A\s*(.*?)\s*<([^<>]*)>\s*\z/
|
|
9
|
+
# A display name made only of these goes out as-is; anything else
|
|
10
|
+
# ASCII gets quoted, so a comma in "Doe, John" can't split one
|
|
11
|
+
# recipient into two.
|
|
12
|
+
PLAIN_PHRASE = %r{\A[A-Za-z0-9!#$%&'*+\-/=?^_`{|}~ ]+\z}
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# "App <a@app.test>" -> ["App", "a@app.test"]; "a@app.test" -> [nil, "a@app.test"]
|
|
17
|
+
def split(value)
|
|
18
|
+
match = ANGLE.match(value)
|
|
19
|
+
return [nil, value.strip] unless match
|
|
20
|
+
|
|
21
|
+
name = match[1]
|
|
22
|
+
[name.empty? ? nil : name, match[2].strip]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def bare(value) = split(value).last
|
|
26
|
+
|
|
27
|
+
def header(value)
|
|
28
|
+
name, address = split(value)
|
|
29
|
+
name ? "#{phrase(name)} <#{address}>" : address
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def phrase(name)
|
|
33
|
+
return name if name.length >= 2 && name.start_with?('"') && name.end_with?('"')
|
|
34
|
+
return MIME.encode_words(name) unless name.ascii_only?
|
|
35
|
+
return name if name.match?(PLAIN_PHRASE)
|
|
36
|
+
|
|
37
|
+
%("#{name.gsub(/["\\]/) { |char| "\\#{char}" }}")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Monk
|
|
2
|
+
module Mail
|
|
3
|
+
# An ArgumentError: it's always the caller's input that's wrong, never
|
|
4
|
+
# the transport or the network.
|
|
5
|
+
class InvalidMessageError < ArgumentError
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class InvalidMailUrlError < StandardError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class MissingMailUrlError < StandardError
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class MissingDependencyError < StandardError
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class ViewsNotFrozenError < StandardError
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class NotConfiguredError < StandardError
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A send that failed on the way out -- network, TLS, or the server
|
|
24
|
+
# refusing it. The transport's own exception is the #cause.
|
|
25
|
+
class DeliveryError < StandardError
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# A DeliveryError the server made final: an SMTP 5xx (no such mailbox,
|
|
29
|
+
# relaying refused) or refused credentials. Raised by
|
|
30
|
+
# Monk::Mail::DeliveryJob (require "monk/mail/later") so the job isn't
|
|
31
|
+
# retried; still a DeliveryError, so a rescue of that catches it too.
|
|
32
|
+
class PermanentDeliveryError < DeliveryError
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require_relative "../mail"
|
|
2
|
+
require_relative "../jobs"
|
|
3
|
+
|
|
4
|
+
module Monk
|
|
5
|
+
module Mail
|
|
6
|
+
# Sends one message from a job process (docs/adr/0014-mail-from-jobs-and-login-links-created-in-the-job.md).
|
|
7
|
+
# Enqueued by Monk::Mail.deliver_later, never directly: its argument
|
|
8
|
+
# is the message's fields, already checked and with from: resolved.
|
|
9
|
+
class DeliveryJob < Monk::Job
|
|
10
|
+
queue "mailers"
|
|
11
|
+
never_retry PermanentDeliveryError
|
|
12
|
+
|
|
13
|
+
# The SMTP failures that are final, by class name so this file never
|
|
14
|
+
# loads net/smtp itself (the app's Gemfile decides that, ADR 0012):
|
|
15
|
+
# a 5xx reply, a malformed command, refused credentials. Everything
|
|
16
|
+
# else -- a 4xx "try later", a refused connection, TLS, a timeout --
|
|
17
|
+
# is worth retrying.
|
|
18
|
+
PERMANENT_SMTP_ERRORS = %w[Net::SMTPFatalError Net::SMTPSyntaxError Net::SMTPAuthenticationError].freeze
|
|
19
|
+
|
|
20
|
+
def self.perform(fields)
|
|
21
|
+
Monk::Mail.deliver(**fields.transform_keys(&:to_sym))
|
|
22
|
+
rescue DeliveryError => e
|
|
23
|
+
raise unless PERMANENT_SMTP_ERRORS.include?(e.cause&.class&.name)
|
|
24
|
+
|
|
25
|
+
raise PermanentDeliveryError, e.message
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
# deliver, later: the same arguments, plus enqueue's wait:, at: and
|
|
31
|
+
# conn: (the last one enqueues inside the caller's transaction, so the
|
|
32
|
+
# email commits or rolls back with the app's own writes). Returns the
|
|
33
|
+
# job's id. The message is built -- and so checked -- here, before
|
|
34
|
+
# anything is enqueued: bad input raises InvalidMessageError in the
|
|
35
|
+
# caller, exactly as deliver would, rather than failing later in the
|
|
36
|
+
# job process. Render HTML before calling (html: Monk::Mail.render(...)):
|
|
37
|
+
# the job stores the finished message and only sends it.
|
|
38
|
+
#
|
|
39
|
+
# Monk::Mail.deliver_later(to: order[:email], subject: "Your receipt", text: receipt_text)
|
|
40
|
+
def deliver_later(to:, subject:, text: nil, html: nil, reply_to: nil, from: nil, wait: nil, at: nil, conn: nil)
|
|
41
|
+
raise NotConfiguredError, "call Monk::Mail.configure(url:, from:) before Monk::Mail.deliver_later" unless config
|
|
42
|
+
|
|
43
|
+
from ||= config[:from]
|
|
44
|
+
if from.nil?
|
|
45
|
+
raise InvalidMessageError, "no from: given, and Monk::Mail.configure has no default from: (MAIL_FROM)"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
message = Message.new(from: from, to: to, subject: subject, text: text, html: html, reply_to: reply_to)
|
|
49
|
+
DeliveryJob.enqueue(message.to_h.transform_keys(&:to_s), wait: wait, at: at, conn: conn)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module Monk
|
|
2
|
+
module Mail
|
|
3
|
+
# One email, as a frozen value: built per request inside whichever
|
|
4
|
+
# worker Ractor serves it, then handed to a transport without a copy
|
|
5
|
+
# (docs/adr/0012-minimal-built-in-mailer.md). Deeply frozen on
|
|
6
|
+
# construction, so it's Ractor.shareable? even when the caller passed
|
|
7
|
+
# mutable Strings or Arrays -- and later mutating those can't change it.
|
|
8
|
+
#
|
|
9
|
+
# Addresses are kept as given ("Name <addr>" or a bare "addr"); nothing
|
|
10
|
+
# here parses them. `to` is always an Array, however it was passed.
|
|
11
|
+
Message = Data.define(:from, :to, :subject, :text, :html, :reply_to) do
|
|
12
|
+
def initialize(from:, to:, subject:, text: nil, html: nil, reply_to: nil)
|
|
13
|
+
to = Array(to) unless to.nil?
|
|
14
|
+
to&.each { |address| Fields.header!(:to, address) }
|
|
15
|
+
raise InvalidMessageError, "to: needs at least one recipient" if to.nil? || to.empty?
|
|
16
|
+
|
|
17
|
+
Fields.header!(:from, from)
|
|
18
|
+
Fields.header!(:subject, subject)
|
|
19
|
+
Fields.header!(:reply_to, reply_to) unless reply_to.nil?
|
|
20
|
+
Fields.body!(:text, text)
|
|
21
|
+
Fields.body!(:html, html)
|
|
22
|
+
raise InvalidMessageError, "a message needs a text: or html: body (or both)" if text.nil? && html.nil?
|
|
23
|
+
|
|
24
|
+
super(**Ractor.make_shareable(
|
|
25
|
+
{ from: from, to: to, subject: subject, text: text, html: html, reply_to: reply_to },
|
|
26
|
+
copy: true,
|
|
27
|
+
))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# RFC 5322 wire format, ready for SMTP DATA or a raw-MIME API. date:,
|
|
31
|
+
# message_id: and boundary: are generated when omitted; tests pin them.
|
|
32
|
+
def to_mime(**) = MIME.build(self, **)
|
|
33
|
+
|
|
34
|
+
# The bare addresses, for the SMTP envelope (MAIL FROM / RCPT TO).
|
|
35
|
+
def envelope_from = Address.bare(from)
|
|
36
|
+
def envelope_to = to.map { |address| Address.bare(address) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Field checks shared by Message and Monk::Mail.configure(from:).
|
|
40
|
+
module Fields
|
|
41
|
+
module_function
|
|
42
|
+
|
|
43
|
+
# A CR or LF in a header value would let whoever controls it (say, a
|
|
44
|
+
# user typing their email into a login form) inject headers of their
|
|
45
|
+
# own -- a Bcc: to anyone. Rejected outright rather than stripped:
|
|
46
|
+
# silently rewriting a recipient is worse than refusing it.
|
|
47
|
+
def header!(field, value)
|
|
48
|
+
string!(field, value)
|
|
49
|
+
raise InvalidMessageError, "#{field}: can't be blank" if value.strip.empty?
|
|
50
|
+
return unless value.match?(/[\r\n]/)
|
|
51
|
+
|
|
52
|
+
raise InvalidMessageError, "#{field}: can't contain a line break (#{value.inspect})"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def body!(field, value)
|
|
56
|
+
string!(field, value) unless value.nil?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def string!(field, value)
|
|
60
|
+
return if value.is_a?(String)
|
|
61
|
+
|
|
62
|
+
raise InvalidMessageError, "#{field}: must be a String, got #{value.inspect}"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
require "securerandom"
|
|
2
|
+
|
|
3
|
+
module Monk
|
|
4
|
+
module Mail
|
|
5
|
+
# Renders a Message to RFC 5322 wire format -- what the `mail` gem
|
|
6
|
+
# would do, cut down to Monk::Mail's scope: a text part, an HTML part,
|
|
7
|
+
# or both as multipart/alternative. Bodies are UTF-8 and always base64
|
|
8
|
+
# (never 7bit/quoted-printable), so any content survives any relay and
|
|
9
|
+
# no line ever gets near the 998-octet limit. Header values that
|
|
10
|
+
# aren't plain ASCII are RFC 2047 encoded words. Plain module
|
|
11
|
+
# functions over frozen input, so it runs in any Ractor.
|
|
12
|
+
module MIME
|
|
13
|
+
CRLF = "\r\n".freeze
|
|
14
|
+
MAX_LINE = 78
|
|
15
|
+
# 42 bytes base64-encode to 56 chars; with the "=?UTF-8?B?" / "?="
|
|
16
|
+
# wrapper that's a 68-char encoded word, under RFC 2047's 75, and
|
|
17
|
+
# "Subject: " plus one of them still fits a 78-char line.
|
|
18
|
+
WORD_BYTES = 42
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
def build(message, date: Time.now, message_id: nil, boundary: nil)
|
|
23
|
+
message_id ||= "<#{SecureRandom.uuid}@#{domain(message.envelope_from)}>"
|
|
24
|
+
headers = [
|
|
25
|
+
"From: #{address_list([message.from], "From")}",
|
|
26
|
+
"To: #{address_list(message.to, "To")}",
|
|
27
|
+
("Reply-To: #{address_list([message.reply_to], "Reply-To")}" if message.reply_to),
|
|
28
|
+
"Subject: #{unstructured(message.subject, "Subject")}",
|
|
29
|
+
"Date: #{date.strftime("%a, %d %b %Y %H:%M:%S %z")}",
|
|
30
|
+
"Message-ID: #{message_id}",
|
|
31
|
+
"MIME-Version: 1.0",
|
|
32
|
+
].compact
|
|
33
|
+
|
|
34
|
+
parts = [
|
|
35
|
+
(["text/plain", message.text] if message.text),
|
|
36
|
+
(["text/html", message.html] if message.html),
|
|
37
|
+
].compact
|
|
38
|
+
|
|
39
|
+
if parts.size == 1
|
|
40
|
+
(headers + part(*parts.first)).join(CRLF)
|
|
41
|
+
else
|
|
42
|
+
boundary ||= "monk-#{SecureRandom.hex(16)}"
|
|
43
|
+
head = headers + [%(Content-Type: multipart/alternative; boundary="#{boundary}"), "", ""]
|
|
44
|
+
body = parts.map { |type, content| "--#{boundary}#{CRLF}#{part(type, content).join(CRLF)}" }
|
|
45
|
+
head.join(CRLF) + body.join + "--#{boundary}--#{CRLF}"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# A part's own headers, a blank line, then its base64 body (which
|
|
50
|
+
# ends in CRLF).
|
|
51
|
+
def part(type, content)
|
|
52
|
+
body = utf8(content, type).gsub(/\r?\n/, CRLF)
|
|
53
|
+
[
|
|
54
|
+
"Content-Type: #{type}; charset=UTF-8",
|
|
55
|
+
"Content-Transfer-Encoding: base64",
|
|
56
|
+
"",
|
|
57
|
+
[body].pack("m").gsub("\n", CRLF),
|
|
58
|
+
]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def address_list(addresses, name)
|
|
62
|
+
rendered = addresses.map { |address| Address.header(utf8(address, name)) }
|
|
63
|
+
one_line = rendered.join(", ")
|
|
64
|
+
name.length + 2 + one_line.length <= MAX_LINE ? one_line : rendered.join(",#{CRLF} ")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# ASCII stays readable (folded at spaces when long); anything else,
|
|
68
|
+
# or anything that could be mistaken for an encoded word, is encoded.
|
|
69
|
+
def unstructured(value, name)
|
|
70
|
+
value = utf8(value, name)
|
|
71
|
+
return encode_words(value) if !value.ascii_only? || value.include?("=?")
|
|
72
|
+
|
|
73
|
+
fold(value, name.length + 2)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# RFC 2047 "B" encoded words, split so none exceeds WORD_BYTES of
|
|
77
|
+
# input -- and never inside a multibyte character, which would leave
|
|
78
|
+
# both halves undecodable. Joined by folding whitespace, which
|
|
79
|
+
# decoders drop between adjacent encoded words.
|
|
80
|
+
def encode_words(value)
|
|
81
|
+
chunks = value.each_char.with_object([+""]) do |char, acc|
|
|
82
|
+
acc << +"" if acc.last.bytesize + char.bytesize > WORD_BYTES
|
|
83
|
+
acc.last << char
|
|
84
|
+
end
|
|
85
|
+
chunks.map { |chunk| "=?UTF-8?B?#{[chunk].pack("m0")}?=" }.join("#{CRLF} ")
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Greedy wrap at spaces; a continuation line starts with one space.
|
|
89
|
+
# A value that already fits is returned untouched, spacing and all.
|
|
90
|
+
def fold(value, offset)
|
|
91
|
+
return value if offset + value.length <= MAX_LINE
|
|
92
|
+
|
|
93
|
+
lines = []
|
|
94
|
+
current = nil
|
|
95
|
+
value.split.each do |word|
|
|
96
|
+
width = lines.empty? ? offset : 1
|
|
97
|
+
if current && width + current.length + 1 + word.length > MAX_LINE
|
|
98
|
+
lines << current
|
|
99
|
+
current = word
|
|
100
|
+
else
|
|
101
|
+
current = current ? "#{current} #{word}" : word
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
lines << current if current
|
|
105
|
+
lines.join("#{CRLF} ")
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def domain(address)
|
|
109
|
+
address.include?("@") ? address.split("@").last : "localhost"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Every piece of text goes out as UTF-8. A binary String is assumed
|
|
113
|
+
# to be UTF-8 bytes; any other encoding is transcoded.
|
|
114
|
+
def utf8(value, name)
|
|
115
|
+
string = value.encoding == Encoding::BINARY ? value.dup.force_encoding(Encoding::UTF_8) : value.encode(Encoding::UTF_8)
|
|
116
|
+
return string if string.valid_encoding?
|
|
117
|
+
|
|
118
|
+
raise InvalidMessageError, "#{name} isn't valid UTF-8"
|
|
119
|
+
rescue EncodingError
|
|
120
|
+
raise InvalidMessageError, "#{name} can't be converted to UTF-8"
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
module Monk
|
|
2
|
+
module Mail
|
|
3
|
+
# Where a Message goes. Each transport is a frozen Data value holding
|
|
4
|
+
# only its settings -- never a connection or client object -- so it
|
|
5
|
+
# can sit in Monk::Mail's boot-frozen config and be read from any
|
|
6
|
+
# worker Ractor; the client is built fresh on every send, inside the
|
|
7
|
+
# Ractor doing the sending, the same pattern as PG::Connection
|
|
8
|
+
# (docs/adr/0012-minimal-built-in-mailer.md).
|
|
9
|
+
module Transports
|
|
10
|
+
# starttls: :always (fail if the server doesn't offer it), :auto (use
|
|
11
|
+
# it if offered) or :never. tls: true is implicit TLS from the first
|
|
12
|
+
# byte (smtps://, usually port 465), where STARTTLS doesn't apply.
|
|
13
|
+
#
|
|
14
|
+
# net-smtp is the app's dependency, not Monk's (a bundled gem since
|
|
15
|
+
# Ruby 3.1, like pg/redis are opt-in): required when an smtp:// URL
|
|
16
|
+
# is configured, so a missing gem fails the boot, not the first send.
|
|
17
|
+
SMTP = Data.define(:host, :port, :user, :password, :tls, :starttls, :open_timeout, :read_timeout) do
|
|
18
|
+
def self.require_library!
|
|
19
|
+
require "net/smtp"
|
|
20
|
+
rescue LoadError
|
|
21
|
+
raise MissingDependencyError,
|
|
22
|
+
"MAIL_URL is smtp:// or smtps://, which needs the net-smtp gem -- a bundled, not default, " \
|
|
23
|
+
"gem since Ruby 3.1. Add `gem \"net-smtp\"` to your Gemfile."
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Called at boot, in the main Ractor, from Monk::Mail.freeze_registry!.
|
|
27
|
+
# Net::SMTP keeps its AUTH mechanisms ({PLAIN: AuthPlain, ...}) in a
|
|
28
|
+
# class-level Hash, filled once when net/smtp is required; unfrozen,
|
|
29
|
+
# a worker Ractor reading it raises Ractor::IsolationError on every
|
|
30
|
+
# authenticated send. Sealing it is the same move as
|
|
31
|
+
# Monk::Auth.freeze_rqrcode!. The cost: an auth mechanism registered
|
|
32
|
+
# after boot would raise FrozenError -- nothing in net-smtp does that.
|
|
33
|
+
def self.prepare!
|
|
34
|
+
require_library!
|
|
35
|
+
Ractor.make_shareable(Net::SMTP::Authenticator.auth_classes)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initialize(host:, port:, user: nil, password: nil, tls: false, starttls: :auto, open_timeout: 5,
|
|
39
|
+
read_timeout: 10)
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# One connection per message, built here, inside whichever Ractor
|
|
44
|
+
# is sending. Any network, TLS or SMTP-level failure becomes a
|
|
45
|
+
# Monk::Mail::DeliveryError (the original is its #cause), so a
|
|
46
|
+
# caller rescues one class whatever went wrong.
|
|
47
|
+
def deliver(message)
|
|
48
|
+
smtp = Net::SMTP.new(host, port, tls: tls, starttls: starttls == :never ? false : starttls)
|
|
49
|
+
smtp.open_timeout = open_timeout
|
|
50
|
+
smtp.read_timeout = read_timeout
|
|
51
|
+
smtp.start(helo: MIME.domain(message.envelope_from), user: user, secret: password) do |session|
|
|
52
|
+
session.send_message(message.to_mime, message.envelope_from, *message.envelope_to)
|
|
53
|
+
end
|
|
54
|
+
message
|
|
55
|
+
rescue Net::SMTPError, IOError, SystemCallError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError => e
|
|
56
|
+
raise DeliveryError, "SMTP delivery via #{host}:#{port} failed: #{e.class}: #{e.message.strip}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Never print the password -- a transport ends up in logs and
|
|
60
|
+
# exception messages more easily than anyone intends.
|
|
61
|
+
def inspect
|
|
62
|
+
"#<#{self.class.name} #{tls ? "smtps" : "smtp"}://#{"#{user}:***@" if user}#{host}:#{port} " \
|
|
63
|
+
"starttls=#{starttls}>"
|
|
64
|
+
end
|
|
65
|
+
alias_method :to_s, :inspect
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Writes each message to Monk::Log instead of sending it: the
|
|
69
|
+
# development default when MAIL_URL is unset, and an explicit
|
|
70
|
+
# log:// anywhere else. One line per message in log/<env>.log (the
|
|
71
|
+
# text body escaped with #inspect so it stays one line; the HTML
|
|
72
|
+
# only by size), plus, in development only, the whole readable
|
|
73
|
+
# message on $stdout -- where a developer is looking, and where a
|
|
74
|
+
# magic link gets clicked. Same stdout posture as Base#log_request
|
|
75
|
+
# and Monk::Auth.log_dev_link.
|
|
76
|
+
Log = Data.define do
|
|
77
|
+
def deliver(message)
|
|
78
|
+
Monk::Log.info("[mail] #{summary(message)}")
|
|
79
|
+
if Monk.env.development?
|
|
80
|
+
$stdout.puts(readable(message))
|
|
81
|
+
$stdout.flush
|
|
82
|
+
end
|
|
83
|
+
message
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def summary(message)
|
|
89
|
+
fields = [
|
|
90
|
+
"from=#{message.from.inspect}",
|
|
91
|
+
"to=#{message.to.join(", ").inspect}",
|
|
92
|
+
("reply_to=#{message.reply_to.inspect}" if message.reply_to),
|
|
93
|
+
"subject=#{message.subject.inspect}",
|
|
94
|
+
("text=#{message.text.inspect}" if message.text),
|
|
95
|
+
("html=#{message.html.bytesize} bytes" if message.html),
|
|
96
|
+
]
|
|
97
|
+
fields.compact.join(" ")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def readable(message)
|
|
101
|
+
lines = [
|
|
102
|
+
"[mail] ----------------------------------------",
|
|
103
|
+
"From: #{message.from}",
|
|
104
|
+
"To: #{message.to.join(", ")}",
|
|
105
|
+
("Reply-To: #{message.reply_to}" if message.reply_to),
|
|
106
|
+
"Subject: #{message.subject}",
|
|
107
|
+
]
|
|
108
|
+
lines += ["", "--- text ---", message.text] if message.text
|
|
109
|
+
lines += ["", "--- html ---", message.html] if message.html
|
|
110
|
+
(lines.compact + ["[mail] ----------------------------------------"]).join("\n")
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require "uri"
|
|
2
|
+
|
|
3
|
+
module Monk
|
|
4
|
+
module Mail
|
|
5
|
+
# MAIL_URL -> a transport value. Runs once, from Monk::Mail.configure
|
|
6
|
+
# in the main Ractor at boot, so a malformed URL fails the boot rather
|
|
7
|
+
# than the first send (docs/adr/0003-boot-time-fail-fast-shareability.md's
|
|
8
|
+
# posture). Error messages redact the password: the URL is a secret.
|
|
9
|
+
module URL
|
|
10
|
+
SUPPORTED = "smtp://, smtps://, log://".freeze
|
|
11
|
+
STARTTLS_MODES = %w[auto always never].freeze
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def parse(url)
|
|
16
|
+
uri = URI.parse(url)
|
|
17
|
+
case uri.scheme&.downcase
|
|
18
|
+
when "log" then Transports::Log.new
|
|
19
|
+
when "smtp" then smtp(uri, url, tls: false)
|
|
20
|
+
when "smtps" then smtp(uri, url, tls: true)
|
|
21
|
+
else invalid!(url, "unsupported scheme #{uri.scheme.inspect}; supported: #{SUPPORTED}")
|
|
22
|
+
end
|
|
23
|
+
rescue URI::InvalidURIError
|
|
24
|
+
invalid!(url, "not a valid URL; expected e.g. smtp://user:pass@smtp.example.com:587")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def smtp(uri, url, tls:)
|
|
28
|
+
invalid!(url, "missing host") if uri.host.nil? || uri.host.empty?
|
|
29
|
+
|
|
30
|
+
Transports::SMTP.require_library!
|
|
31
|
+
user = decode(uri.user)
|
|
32
|
+
Transports::SMTP.new(
|
|
33
|
+
host: uri.host,
|
|
34
|
+
port: uri.port || (tls ? 465 : 587),
|
|
35
|
+
user: user,
|
|
36
|
+
password: decode(uri.password),
|
|
37
|
+
tls: tls,
|
|
38
|
+
starttls: starttls(uri, url, tls: tls, credentials: !user.nil?),
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Default :always with credentials (a stripped STARTTLS would
|
|
43
|
+
# otherwise send the password in clear), :auto without (a local
|
|
44
|
+
# relay usually has no TLS set up).
|
|
45
|
+
def starttls(uri, url, tls:, credentials:)
|
|
46
|
+
options = URI.decode_www_form(uri.query || "").to_h
|
|
47
|
+
unknown = options.keys - ["starttls"]
|
|
48
|
+
invalid!(url, "unknown option(s) #{unknown.join(", ")}; the only one is starttls=") if unknown.any?
|
|
49
|
+
|
|
50
|
+
mode = options["starttls"]
|
|
51
|
+
if tls
|
|
52
|
+
invalid!(url, "starttls= doesn't apply to smtps://, which is TLS from the start") if mode
|
|
53
|
+
return :never
|
|
54
|
+
end
|
|
55
|
+
return credentials ? :always : :auto if mode.nil?
|
|
56
|
+
|
|
57
|
+
invalid!(url, "starttls= must be one of #{STARTTLS_MODES.join(", ")}") unless STARTTLS_MODES.include?(mode)
|
|
58
|
+
mode.to_sym
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def decode(component)
|
|
62
|
+
return nil if component.nil? || component.empty?
|
|
63
|
+
|
|
64
|
+
URI.decode_uri_component(component)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def invalid!(url, reason)
|
|
68
|
+
raise InvalidMailUrlError, "MAIL_URL #{redact(url).inspect}: #{reason}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def redact(url)
|
|
72
|
+
url.to_s.sub(%r{(://[^:/@]*:)[^@/]*@}, '\1***@')
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
data/lib/monk/mail.rb
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
require_relative "freeze_hooks"
|
|
2
|
+
require_relative "environment"
|
|
3
|
+
require_relative "log"
|
|
4
|
+
require_relative "views"
|
|
5
|
+
require_relative "context"
|
|
6
|
+
require_relative "mail/errors"
|
|
7
|
+
require_relative "mail/address"
|
|
8
|
+
require_relative "mail/mime"
|
|
9
|
+
require_relative "mail/message"
|
|
10
|
+
require_relative "mail/transports"
|
|
11
|
+
require_relative "mail/url"
|
|
12
|
+
|
|
13
|
+
module Monk
|
|
14
|
+
# Sending email. Opt-in: require "monk/mail" explicitly -- `require
|
|
15
|
+
# "monk"` alone does not load this. Text and/or HTML messages only, MIME
|
|
16
|
+
# built by Monk itself, since the `mail` gem can't run inside a worker
|
|
17
|
+
# Ractor (docs/adr/0012-minimal-built-in-mailer.md).
|
|
18
|
+
module Mail
|
|
19
|
+
class << self
|
|
20
|
+
# Typically, in config/mail.rb:
|
|
21
|
+
#
|
|
22
|
+
# Monk::Mail.configure(url: ENV["MAIL_URL"], from: ENV["MAIL_FROM"])
|
|
23
|
+
#
|
|
24
|
+
# url: picks the transport (smtp://, smtps://, log://) and is parsed
|
|
25
|
+
# here, so a bad one fails the boot. Unset or empty means log:// in
|
|
26
|
+
# development and raises everywhere else -- same posture as
|
|
27
|
+
# Monk::Auth.deliver_link: an app that forgot to wire mail should
|
|
28
|
+
# find out at boot, not when a user never gets their login link.
|
|
29
|
+
# from: is the default sender, optional since each send can pass its own.
|
|
30
|
+
def configure(url:, from: nil)
|
|
31
|
+
Fields.header!(:from, from) unless from.nil?
|
|
32
|
+
|
|
33
|
+
@config = { transport: transport_for(url), from: from }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
attr_reader :config
|
|
37
|
+
|
|
38
|
+
# Builds a Message and sends it through the configured transport,
|
|
39
|
+
# synchronously -- the calling worker Ractor waits for the send
|
|
40
|
+
# (docs/adr/0012-minimal-built-in-mailer.md accepts that cost; a
|
|
41
|
+
# local relay or a fast HTTPS provider keeps it short). Returns the
|
|
42
|
+
# Message. Invalid input raises InvalidMessageError before anything
|
|
43
|
+
# is sent; a failed send raises DeliveryError.
|
|
44
|
+
#
|
|
45
|
+
# Monk::Mail.deliver(to: email, subject: "Your login link", text: "Log in: #{link}")
|
|
46
|
+
def deliver(to:, subject:, text: nil, html: nil, reply_to: nil, from: nil)
|
|
47
|
+
raise NotConfiguredError, "call Monk::Mail.configure(url:, from:) before Monk::Mail.deliver" unless config
|
|
48
|
+
|
|
49
|
+
from ||= config[:from]
|
|
50
|
+
if from.nil?
|
|
51
|
+
raise InvalidMessageError, "no from: given, and Monk::Mail.configure has no default from: (MAIL_FROM)"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
message = Message.new(from: from, to: to, subject: subject, text: text, html: html, reply_to: reply_to)
|
|
55
|
+
config[:transport].deliver(message)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Renders a Monk::Views template to an HTML String for deliver's
|
|
59
|
+
# html: -- e.g. views/mail/magic_link.erb:
|
|
60
|
+
#
|
|
61
|
+
# html: Monk::Mail.render("mail/magic_link", link: link)
|
|
62
|
+
#
|
|
63
|
+
# The same detached-Context render Monk::Live::Renderer does: no
|
|
64
|
+
# request behind it, so the template sees only its locals, never
|
|
65
|
+
# params or the session. The app's page layout is skipped (layout:
|
|
66
|
+
# false); pass layout: "mail/layout" for an email-specific one.
|
|
67
|
+
# `<%= %>` escapes as usual, which is right for HTML and wrong for a
|
|
68
|
+
# plain-text body -- build text: as a Ruby String instead.
|
|
69
|
+
def render(template, layout: false, **locals)
|
|
70
|
+
ensure_views_frozen!
|
|
71
|
+
String.new(Monk::Context.new({}).render(template, layout: layout, **locals)).freeze
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Called from Base#freeze! (Seam B), via Monk.freeze_hooks. Freezes
|
|
75
|
+
# the value, not the module, same as Monk::Auth.freeze_registry! --
|
|
76
|
+
# an unfrozen config Hash can't be read from a worker Ractor at all.
|
|
77
|
+
def freeze_registry!
|
|
78
|
+
transport = @config&.fetch(:transport)
|
|
79
|
+
transport.class.prepare! if transport.class.respond_to?(:prepare!)
|
|
80
|
+
@config = Ractor.make_shareable(@config)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Test-only: drops the current config. Not part of the app-facing API.
|
|
84
|
+
def reset!
|
|
85
|
+
@config = nil
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
# Unfrozen, a render from a worker Ractor dies with an opaque
|
|
91
|
+
# Ractor::IsolationError inside Monk::Views, and one from the main
|
|
92
|
+
# Ractor works by accident and then fails in production -- so both
|
|
93
|
+
# fail here, naming the fix (same check as Monk::Live::Renderer).
|
|
94
|
+
def ensure_views_frozen!
|
|
95
|
+
return if Ractor.shareable?(Monk::Views.registry)
|
|
96
|
+
|
|
97
|
+
raise_views_not_frozen
|
|
98
|
+
rescue Ractor::IsolationError
|
|
99
|
+
raise_views_not_frozen
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def raise_views_not_frozen
|
|
103
|
+
raise ViewsNotFrozenError,
|
|
104
|
+
"Monk::Mail.render can't render before views are compiled and frozen -- call Monk.boot(app) " \
|
|
105
|
+
"(or Monk.freeze!) in the main Ractor first"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def transport_for(url)
|
|
109
|
+
return URL.parse(url) unless url.nil? || url.empty?
|
|
110
|
+
return Transports::Log.new if Monk.env.development?
|
|
111
|
+
|
|
112
|
+
raise MissingMailUrlError,
|
|
113
|
+
"Monk::Mail.configure(url:) is empty and this isn't development (MONK_ENV=#{Monk.env}) -- " \
|
|
114
|
+
"no email would ever be sent. Set MAIL_URL, e.g. smtp://user:pass@smtp.example.com:587, " \
|
|
115
|
+
"or log:// if logging messages instead of sending them is truly intended here."
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
Monk.freeze_hooks << self
|
|
120
|
+
end
|
|
121
|
+
end
|
data/lib/monk/persistence/pg.rb
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "json"
|
|
1
2
|
require "pg"
|
|
2
3
|
|
|
3
4
|
require_relative "../persistence"
|
|
@@ -16,12 +17,31 @@ module Monk
|
|
|
16
17
|
module Pg
|
|
17
18
|
extend Monk::Persistence::Registry
|
|
18
19
|
|
|
20
|
+
# json/jsonb columns, decoded with plain JSON.parse. pg's own
|
|
21
|
+
# PG::TextDecoder::JSON (1.6.3) calls JSON.parse(string, quirks_mode:
|
|
22
|
+
# true), and json 3 removed that keyword, so with pg's default every
|
|
23
|
+
# json/jsonb read raised ArgumentError. json 3 parses a bare scalar
|
|
24
|
+
# ("7", "\"text\"") without it.
|
|
25
|
+
class JsonDecoder < PG::SimpleDecoder
|
|
26
|
+
def decode(string, _tuple = nil, _field = nil)
|
|
27
|
+
JSON.parse(string)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# pg's default result types, with JsonDecoder for json and jsonb.
|
|
32
|
+
# Built once and made shareable, so every Ractor's #connect can use
|
|
33
|
+
# it rather than building its own.
|
|
34
|
+
RESULT_TYPES = Ractor.make_shareable(
|
|
35
|
+
PG::BasicTypeRegistry.new.register_default_types
|
|
36
|
+
.tap { |types| %w[json jsonb].each { |name| types.register_type(0, name, nil, JsonDecoder) } },
|
|
37
|
+
)
|
|
38
|
+
|
|
19
39
|
class << self
|
|
20
40
|
private
|
|
21
41
|
|
|
22
42
|
def connect(**opts)
|
|
23
43
|
conn = PG.connect(**opts)
|
|
24
|
-
conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn)
|
|
44
|
+
conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: RESULT_TYPES)
|
|
25
45
|
conn
|
|
26
46
|
end
|
|
27
47
|
|