From 4872765581e455f9990cc153c9ed3d1dce92702b Mon Sep 17 00:00:00 2001 From: Andreas Lappe Date: Mon, 20 Jun 2022 22:00:44 +0200 Subject: [PATCH] Use tls options in sockopts for ssl connection --- lib/bamboo/adapters/smtp_adapter.ex | 25 ++++++++++++- .../lib/bamboo/adapters/smtp_adapter_test.exs | 36 +++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/bamboo/adapters/smtp_adapter.ex b/lib/bamboo/adapters/smtp_adapter.ex index 6dc57ea..c570c13 100644 --- a/lib/bamboo/adapters/smtp_adapter.ex +++ b/lib/bamboo/adapters/smtp_adapter.ex @@ -75,10 +75,21 @@ defmodule Bamboo.SMTPAdapter do end def deliver(email, config) do - gen_smtp_config = + tmp_gen_smtp_config = config |> to_gen_smtp_server_config + gen_smtp_config = + if Keyword.get(tmp_gen_smtp_config, :ssl) == true do + tls_options = Keyword.get(tmp_gen_smtp_config, :tls_options, []) + + tmp_gen_smtp_config + |> Keyword.put(:sockopts, tls_options) + |> Keyword.delete(:tls_options) + else + tmp_gen_smtp_config + end + response = try do email @@ -493,13 +504,25 @@ defmodule Bamboo.SMTPAdapter do defp to_gen_smtp_server_config({:tls_cacertfile, value}, config) when is_binary(value) do + value = String.to_charlist(value) + Keyword.update(config, :tls_options, [{:cacertfile, value}], fn c -> [{:cacertfile, value} | c] end) end + defp to_gen_smtp_server_config({:tls_server_name_indication, name}, config) + when is_binary(name) do + name = String.to_charlist(name) + + Keyword.update(config, :tls_options, [{:server_name_indication, name}], fn c -> + [{:server_name_indication, name} | c] + end) + end + defp to_gen_smtp_server_config({:tls_cacerts, value}, config) when is_binary(value) do + value = String.to_charlist(value) Keyword.update(config, :tls_options, [{:cacerts, value}], fn c -> [{:cacerts, value} | c] end) end diff --git a/test/lib/bamboo/adapters/smtp_adapter_test.exs b/test/lib/bamboo/adapters/smtp_adapter_test.exs index 04d3107..73c9296 100644 --- a/test/lib/bamboo/adapters/smtp_adapter_test.exs +++ b/test/lib/bamboo/adapters/smtp_adapter_test.exs @@ -291,6 +291,7 @@ defmodule Bamboo.SMTPAdapterTest do tls_cacertfile: "somewhere", tls_cacerts: "…", tls_depth: 99, + tls_server_name_indication: "example.com", tls_verify_fun: {&:ssl_verify_hostname.verify_fun/3, check_hostname: "example.com"}, allowed_tls_versions: [:tlsv1, :"tlsv1.2"] }) @@ -300,9 +301,10 @@ defmodule Bamboo.SMTPAdapterTest do [{{_from, _to, _raw_email}, gen_smtp_config}] = FakeGenSMTP.fetch_sent_emails() assert :verify_peer == gen_smtp_config[:tls_options][:verify] - assert "somewhere" == gen_smtp_config[:tls_options][:cacertfile] - assert "…" == gen_smtp_config[:tls_options][:cacerts] + assert 'somewhere' == gen_smtp_config[:tls_options][:cacertfile] + assert '…' == gen_smtp_config[:tls_options][:cacerts] assert 99 == gen_smtp_config[:tls_options][:depth] + assert 'example.com' == gen_smtp_config[:tls_options][:server_name_indication] assert {&:ssl_verify_hostname.verify_fun/3, [check_hostname: "example.com"]} == gen_smtp_config[:tls_options][:verify_fun] @@ -310,6 +312,36 @@ defmodule Bamboo.SMTPAdapterTest do assert [:tlsv1, :"tlsv1.2"] == gen_smtp_config[:tls_options][:versions] end + test "sets tls options as socket options when ssl is specified" do + config = + SMTPAdapter.handle_config( + configuration(%{ + ssl: true, + tls_verify: :verify_peer, + tls_cacertfile: "somewhere", + tls_cacerts: "…", + tls_depth: 99, + tls_server_name_indication: "example.com", + tls_verify_fun: {&:ssl_verify_hostname.verify_fun/3, check_hostname: "example.com"}, + allowed_tls_versions: [:tlsv1, :"tlsv1.2"] + }) + ) + + {:ok, "200 Ok 1234567890"} = SMTPAdapter.deliver(new_email(), config) + [{{_from, _to, _raw_email}, gen_smtp_config}] = FakeGenSMTP.fetch_sent_emails() + + assert :verify_peer == gen_smtp_config[:sockopts][:verify] + assert 'somewhere' == gen_smtp_config[:sockopts][:cacertfile] + assert '…' == gen_smtp_config[:sockopts][:cacerts] + assert 99 == gen_smtp_config[:sockopts][:depth] + assert 'example.com' == gen_smtp_config[:sockopts][:server_name_indication] + + assert {&:ssl_verify_hostname.verify_fun/3, [check_hostname: "example.com"]} == + gen_smtp_config[:sockopts][:verify_fun] + + assert [:tlsv1, :"tlsv1.2"] == gen_smtp_config[:sockopts][:versions] + end + test "sets no_mx_lookups false from System when specified" do System.put_env("NO_MX_LOOKUPS", "false")