mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-01-28 10:18:51 +00:00
Implemet Outbound emails (#576)
* add mailer * setup configs * only setup mailer if from_email is present * Do not assume OUTBOUND_EMAIL_FROM exists * update docs * add usage of mailer as tests * address comments
This commit is contained in:
35
apps/fz_http/lib/fz_http/mailer.ex
Normal file
35
apps/fz_http/lib/fz_http/mailer.ex
Normal file
@@ -0,0 +1,35 @@
|
||||
defmodule FzHttp.Mailer do
|
||||
@moduledoc """
|
||||
Outbound Email Sender.
|
||||
"""
|
||||
|
||||
use Swoosh.Mailer, otp_app: :fz_http
|
||||
|
||||
alias Swoosh.{Adapters, Email}
|
||||
|
||||
@provider_mapping %{
|
||||
"smtp" => Adapters.SMTP,
|
||||
"mailgun" => Adapters.Mailgun,
|
||||
"mandrill" => Adapters.Mandrill,
|
||||
"sendgrid" => Adapters.Sendgrid,
|
||||
"post_mark" => Adapters.Postmark,
|
||||
"sendmail" => Adapters.Sendmail
|
||||
}
|
||||
|
||||
def default_email do
|
||||
Email.new()
|
||||
|> Email.from(Application.fetch_env!(:fz_http, FzHttp.Mailer)[:from_email])
|
||||
end
|
||||
|
||||
def configs_for(provider) do
|
||||
adapter = Map.fetch!(@provider_mapping, provider)
|
||||
|
||||
mailer_configs =
|
||||
System.fetch_env!("OUTBOUND_EMAIL_CONFIGS")
|
||||
|> Jason.decode!()
|
||||
|> Map.fetch!(provider)
|
||||
|> Enum.map(fn {k, v} -> {String.to_atom(k), v} end)
|
||||
|
||||
[adapter: adapter] ++ mailer_configs
|
||||
end
|
||||
end
|
||||
@@ -134,4 +134,12 @@ defmodule FzHttpWeb.Router do
|
||||
live "/diagnostics/connectivity_checks", ConnectivityCheckLive.Index, :index
|
||||
end
|
||||
end
|
||||
|
||||
if Mix.env() == :dev do
|
||||
scope "/dev" do
|
||||
pipe_through [:browser]
|
||||
|
||||
forward "/mailbox", Plug.Swoosh.MailboxPreview
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -86,6 +86,8 @@ defmodule FzHttp.MixProject do
|
||||
{:phoenix_live_view, "~> 0.17.5"},
|
||||
{:gettext, "~> 0.18"},
|
||||
{:jason, "~> 1.2"},
|
||||
{:phoenix_swoosh, "~> 1.0"},
|
||||
{:gen_smtp, "~> 1.0"},
|
||||
# XXX: Change this when hex package is updated
|
||||
{:cidr, github: "firezone/cidr-elixir"},
|
||||
{:telemetry, "~> 0.4.3"},
|
||||
|
||||
66
apps/fz_http/test/fz_http/mailer_test.exs
Normal file
66
apps/fz_http/test/fz_http/mailer_test.exs
Normal file
@@ -0,0 +1,66 @@
|
||||
defmodule FzHttp.MailerTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias FzHttp.Mailer
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
test "default_email contains from_email" do
|
||||
assert Mailer.default_email().from == {"", "test@firez.one"}
|
||||
end
|
||||
|
||||
test "configs_for provider" do
|
||||
System.put_env(
|
||||
"OUTBOUND_EMAIL_CONFIGS",
|
||||
Jason.encode!(%{"smtp" => %{"config_key" => "config_value"}})
|
||||
)
|
||||
|
||||
assert Mailer.configs_for("smtp") == [
|
||||
adapter: Swoosh.Adapters.SMTP,
|
||||
config_key: "config_value"
|
||||
]
|
||||
end
|
||||
|
||||
describe "with templates" do
|
||||
defmodule SampleEmail do
|
||||
use Phoenix.Swoosh,
|
||||
template_root: "test/fz_http/mailer_test",
|
||||
template_path: "sample_email"
|
||||
|
||||
def test_heex(number) do
|
||||
Mailer.default_email()
|
||||
|> subject("testing")
|
||||
|> to("test@localhost")
|
||||
|> render_body("test_heex.html", %{title: "Testing!", number: number})
|
||||
end
|
||||
|
||||
def test_multipart do
|
||||
Mailer.default_email()
|
||||
|> subject("testing")
|
||||
|> to("test@localhost")
|
||||
|> render_body(:test_multipart, %{})
|
||||
end
|
||||
end
|
||||
|
||||
test "heex" do
|
||||
email = SampleEmail.test_heex(123)
|
||||
assert email.html_body == ~s|<h1 data-number="123">Testing!</h1>|
|
||||
end
|
||||
|
||||
test "multipart" do
|
||||
email = SampleEmail.test_multipart()
|
||||
assert email.text_body == ~s|Welcome to TEXT\n|
|
||||
assert email.html_body == ~s|<h1>Welcome to HTML</h1>\n|
|
||||
end
|
||||
|
||||
test "delivery" do
|
||||
SampleEmail.test_heex(0) |> Mailer.deliver!()
|
||||
|
||||
assert_email_sent(
|
||||
subject: "testing",
|
||||
from: {"", "test@firez.one"},
|
||||
to: [{"", "test@localhost"}],
|
||||
html_body: ~s|<h1 data-number="0">Testing!</h1>|
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
<h1 data-number={@number}><%= @title %></h1>
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Welcome to HTML</h1>
|
||||
@@ -0,0 +1 @@
|
||||
Welcome to TEXT
|
||||
@@ -153,3 +153,5 @@ config :fz_http,
|
||||
local_auth_enabled: local_auth_enabled,
|
||||
okta_auth_enabled: google_auth_enabled,
|
||||
google_auth_enabled: okta_auth_enabled
|
||||
|
||||
config :fz_http, FzHttp.Mailer, adapter: Swoosh.Adapters.Local, from_email: "dev@firez.one"
|
||||
|
||||
@@ -60,3 +60,5 @@ config :ueberauth, Ueberauth,
|
||||
{:okta, {Ueberauth.Strategy.Okta, []}},
|
||||
{:google, {Ueberauth.Strategy.Google, []}}
|
||||
]
|
||||
|
||||
config :swoosh, local: false
|
||||
|
||||
@@ -65,6 +65,17 @@ if config_env() == :prod do
|
||||
allow_unprivileged_device_management =
|
||||
FzString.to_boolean(System.fetch_env!("ALLOW_UNPRIVILEGED_DEVICE_MANAGEMENT"))
|
||||
|
||||
# Outbound Email
|
||||
from_email = System.get_env("OUTBOUND_EMAIL_FROM")
|
||||
|
||||
if from_email do
|
||||
provider = System.get_env("OUTBOUND_EMAIL_PROVIDER", "sendmail")
|
||||
|
||||
config :fz_http,
|
||||
FzHttp.Mailer,
|
||||
[from_email: from_email] ++ FzHttp.Mailer.configs_for(provider)
|
||||
end
|
||||
|
||||
# Local auth
|
||||
local_auth_enabled = FzString.to_boolean(System.fetch_env!("LOCAL_AUTH_ENABLED"))
|
||||
|
||||
|
||||
@@ -57,3 +57,5 @@ config :ueberauth, Ueberauth,
|
||||
{:okta, {Ueberauth.Strategy.Okta, []}},
|
||||
{:google, {Ueberauth.Strategy.Google, []}}
|
||||
]
|
||||
|
||||
config :fz_http, FzHttp.Mailer, adapter: Swoosh.Adapters.Test, from_email: "test@firez.one"
|
||||
|
||||
@@ -164,11 +164,9 @@ Shown below is a complete listing of the configuration options available in
|
||||
| `default['firezone']['ssl']['session_timeout']` | SSL session timeout. | `'5m'` |
|
||||
| `default['firezone']['robots_allow']` | nginx robots allow. | `'/'` |
|
||||
| `default['firezone']['robots_disallow']` | nginx robots disallow. | `nil` |
|
||||
| `default['firezone']['from_email']` | Outbound email from address. | `nil` |
|
||||
| `default['firezone']['smtp_address']` | Outbound email SMTP server address. | `nil` |
|
||||
| `default['firezone']['smtp_password']` | Outbound email SMTP password. | `nil` |
|
||||
| `default['firezone']['smtp_port']` | Outbound email SMTP port. | `nil` |
|
||||
| `default['firezone']['smtp_user_name']` | Outbound email SMTP username. | `nil` |
|
||||
| `default['firezone']['outbound_email']['from']` | Outbound email from address. | `nil` |
|
||||
| `default['firezone']['outbound_email']['provider']` | Outbound email service provider. | `nil` |
|
||||
| `default['firezone']['outbound_email']['configs']` | Outbound email provider configs. | see `omnibus/cookbooks/firezone/attributes/default.rb` |
|
||||
| `default['firezone']['telemetry']['enabled']` | Enable or disable anonymized product telemetry. | `true` |
|
||||
| `default['firezone']['connectivity_checks']['enabled']` | Enable or disable the Firezone connectivity checks service. | `true` |
|
||||
| `default['firezone']['connectivity_checks']['interval']` | Interval between connectivity checks in seconds. | `3_600` |
|
||||
|
||||
3
mix.lock
3
mix.lock
@@ -24,6 +24,7 @@
|
||||
"excoveralls": {:hex, :excoveralls, "0.14.4", "295498f1ae47bdc6dce59af9a585c381e1aefc63298d48172efaaa90c3d251db", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e3ab02f2df4c1c7a519728a6f0a747e71d7d6e846020aae338173619217931c1"},
|
||||
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
|
||||
"floki": {:hex, :floki, "0.32.1", "dfe3b8db3b793939c264e6f785bca01753d17318d144bd44b407fb3493acaa87", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "d4b91c713e4a784a3f7b1e3cc016eefc619f6b1c3898464222867cafd3c681a3"},
|
||||
"gen_smtp": {:hex, :gen_smtp, "1.2.0", "9cfc75c72a8821588b9b9fe947ae5ab2aed95a052b81237e0928633a13276fd3", [:rebar3], [{:ranch, ">= 1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "5ee0375680bca8f20c4d85f58c2894441443a743355430ff33a783fe03296779"},
|
||||
"gettext": {:hex, :gettext, "0.19.1", "564953fd21f29358e68b91634799d9d26989f8d039d7512622efb3c3b1c97892", [:mix], [], "hexpm", "10c656c0912b8299adba9b061c06947511e3f109ab0d18b44a866a4498e77222"},
|
||||
"guardian": {:hex, :guardian, "2.2.3", "23fca9ed3583f3d517d17a33b179814167d987e7127cf6a30a191945e2ae7d6b", [:mix], [{:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "048bb7bafecd692208b01b4c0a919dd6865937a631332a6236674b9b4e898c63"},
|
||||
"guardian_db": {:hex, :guardian_db, "2.1.0", "ec95a9d99cdd1e550555d09a7bb4a340d8887aad0697f594590c2fd74be02426", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.1", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:guardian, "~> 1.0 or ~> 2.0", [hex: :guardian, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "f8e7d543ac92c395f3a7fd5acbe6829faeade57d688f7562e2f0fca8f94a0d70"},
|
||||
@@ -53,6 +54,7 @@
|
||||
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.3.3", "3a53772a6118d5679bf50fc1670505a290e32a1d195df9e069d8c53ab040c054", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "766796676e5f558dbae5d1bdb066849673e956005e3730dfd5affd7a6da4abac"},
|
||||
"phoenix_live_view": {:hex, :phoenix_live_view, "0.17.9", "36b5aa812bc3ccd64c9630f6b3234d9ea21105493237e927aae19d0ba758f0db", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f7ebc3e0ba0c5f6b6996ed6c901ddbfdaba59a6d09b569e7cb2f2f7d693b4455"},
|
||||
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.1", "ba04e489ef03763bf28a17eb2eaddc2c20c6d217e2150a61e3298b0f4c2012b5", [:mix], [], "hexpm", "81367c6d1eea5878ad726be80808eb5a787a23dee699f96e72b1109c57cdd8d9"},
|
||||
"phoenix_swoosh": {:hex, :phoenix_swoosh, "1.0.1", "0db6eb6405a6b06cae4fdf4144659b3f4fee4553e2856fe8a53ba12e9fb21a74", [:mix], [{:finch, "~> 0.8", [hex: :finch, repo: "hexpm", optional: true]}, {:hackney, "~> 1.10", [hex: :hackney, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:swoosh, "~> 1.5", [hex: :swoosh, repo: "hexpm", optional: false]}], "hexpm", "e34890004baec08f0fa12bd8c77bf64bfb4156b84a07fb79da9322fa94bc3781"},
|
||||
"phoenix_view": {:hex, :phoenix_view, "1.1.2", "1b82764a065fb41051637872c7bd07ed2fdb6f5c3bd89684d4dca6e10115c95a", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "7ae90ad27b09091266f6adbb61e1d2516a7c3d7062c6789d46a7554ec40f3a56"},
|
||||
"plug": {:hex, :plug, "1.12.1", "645678c800601d8d9f27ad1aebba1fdb9ce5b2623ddb961a074da0b96c35187d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d57e799a777bc20494b784966dc5fbda91eb4a09f571f76545b72a634ce0d30b"},
|
||||
"plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"},
|
||||
@@ -62,6 +64,7 @@
|
||||
"posthog": {:hex, :posthog, "0.1.0", "0abe2af719c0c30fe6a24569a8947a19f0edfa63f6fed61685a219a8d5655786", [:mix], [{:hackney, "~> 1.10", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "ee0426999bd35edf3dfa84141bbd3de17ae07e04d62d269fd5ee581925f1c222"},
|
||||
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
|
||||
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
|
||||
"swoosh": {:hex, :swoosh, "1.6.6", "6018c6f4659ac0b4f30684982993b7812b2bb97436d39f76fcfa8c9e3ae74f85", [:mix], [{:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e92c7206efd442f08484993676ab072afab2f2bb1e87e604230bb1183c5980de"},
|
||||
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
|
||||
"ueberauth": {:hex, :ueberauth, "0.7.0", "9c44f41798b5fa27f872561b6f7d2bb0f10f03fdd22b90f454232d7b087f4b75", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "2efad9022e949834f16cc52cd935165049d81fa9e925690f91035c2e4b58d905"},
|
||||
"ueberauth_google": {:hex, :ueberauth_google, "0.10.1", "db7bd2d99d2ff38e7449042a08d9560741b0dcaf1c31191729b97188b025465e", [:mix], [{:oauth2, "~> 1.0 or ~> 2.0", [hex: :oauth2, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.7.0", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "b799f547d279bb836e1f7039fc9fbb3a9d008a695e2a25bd06bffe591a168ba1"},
|
||||
|
||||
@@ -381,17 +381,47 @@ default['firezone']['robots_allow'] = '/'
|
||||
default['firezone']['robots_disallow'] = nil
|
||||
|
||||
# ### Outbound Email Settings
|
||||
#
|
||||
# If none of these are set, the :sendmail delivery method will be used. Using
|
||||
# If from_email not set, the outbound email feature will be disabled
|
||||
default['firezone']['outbound_email']['from'] = nil
|
||||
# If provider not set, the :sendmail delivery method will be used. Using
|
||||
# the sendmail delivery method requires that a working mail transfer agent
|
||||
# (usually set up with a relay host) be configured on this machine.
|
||||
#
|
||||
# SMTP will use the 'plain' authentication method.
|
||||
default['firezone']['from_email'] = nil
|
||||
default['firezone']['smtp_address'] = nil
|
||||
default['firezone']['smtp_password'] = nil
|
||||
default['firezone']['smtp_port'] = nil
|
||||
default['firezone']['smtp_user_name'] = nil
|
||||
default['firezone']['outbound_email']['provider'] = nil
|
||||
# Configure one or more providers below.
|
||||
# See the Swoosh library documentation for more information on configuring adapters:
|
||||
# https://github.com/swoosh/swoosh#adapters
|
||||
default['firezone']['outbound_email']['configs'] = {
|
||||
smtp: {
|
||||
# only relay is required, but you will need some combination of the rest
|
||||
relay: 'smtp.example.com',
|
||||
port: 587, # integer
|
||||
username: '', # needs to be string if present
|
||||
password: '', # needs to be string if present
|
||||
ssl: true, # boolean
|
||||
tls: :always, # always / never / if_available
|
||||
auth: :always, # always / never / if_available
|
||||
no_mx_lookup: false, # boolean
|
||||
retries: 2 # integer
|
||||
},
|
||||
mailgun: {
|
||||
# both are required
|
||||
apikey: nil,
|
||||
domain: nil # example.com
|
||||
},
|
||||
mandrill: {
|
||||
api_key: nil
|
||||
},
|
||||
sendgrid: {
|
||||
api_key: nil
|
||||
},
|
||||
post_mark: {
|
||||
api_key: nil
|
||||
},
|
||||
sendmail: {
|
||||
cmd_path: '/usr/bin/sendmail',
|
||||
cmd_args: '-N delay,failure,success'
|
||||
}
|
||||
}
|
||||
|
||||
# ## Telemetry
|
||||
#
|
||||
|
||||
@@ -251,6 +251,11 @@ class Firezone
|
||||
'CONNECTIVITY_CHECKS_ENABLED' => attributes['connectivity_checks']['enabled'].to_s,
|
||||
'CONNECTIVITY_CHECKS_INTERVAL' => attributes['connectivity_checks']['interval'].to_s,
|
||||
|
||||
# Outbound Emails
|
||||
'OUTBOUND_EMAIL_PROVIDER' => attributes['outbound_email']['provider'],
|
||||
'OUTBOUND_EMAIL_CONFIGS' => attributes['outbound_email']['configs'].to_json,
|
||||
'OUTBOUND_EMAIL_FROM' => attributes['outbound_email']['from'],
|
||||
|
||||
# Auth
|
||||
'LOCAL_AUTH_ENABLED' => attributes['authentication']['local']['enabled'].to_s,
|
||||
'OKTA_AUTH_ENABLED' => attributes['authentication']['okta']['enabled'].to_s,
|
||||
|
||||
Reference in New Issue
Block a user