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

Use tls options in sockopts for ssl connection #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion lib/bamboo/adapters/smtp_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
36 changes: 34 additions & 2 deletions test/lib/bamboo/adapters/smtp_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
})
Expand All @@ -300,16 +301,47 @@ 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]

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")

Expand Down