add verify tls_opts only when we open connection
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Tue, 10 Mar 2020 12:54:11 +0000 (15:54 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Tue, 10 Mar 2020 12:54:11 +0000 (15:54 +0300)
for other requests tesla will add tls_opts

lib/pleroma/gun/conn.ex
lib/pleroma/http/adapter_helper/gun.ex
lib/pleroma/http/connection.ex
test/http/adapter_helper/gun_test.exs
test/http/connection_test.exs

index 31971869049a0981da7fb274b0b87922774f06dc..57a847c30c714470998f22d002ae251c062d70a6 100644 (file)
@@ -45,6 +45,7 @@ defmodule Pleroma.Gun.Conn do
       |> Map.put_new(:retry, pool_opts[:retry] || 1)
       |> Map.put_new(:retry_timeout, pool_opts[:retry_timeout] || 1000)
       |> Map.put_new(:await_up_timeout, pool_opts[:await_up_timeout] || 5_000)
+      |> maybe_add_tls_opts(uri)
 
     key = "#{uri.scheme}:#{uri.host}:#{uri.port}"
 
@@ -70,6 +71,29 @@ defmodule Pleroma.Gun.Conn do
     end
   end
 
+  defp maybe_add_tls_opts(opts, %URI{scheme: "http"}), do: opts
+
+  defp maybe_add_tls_opts(opts, %URI{scheme: "https", host: host}) do
+    tls_opts = [
+      verify: :verify_peer,
+      cacertfile: CAStore.file_path(),
+      depth: 20,
+      reuse_sessions: false,
+      verify_fun:
+        {&:ssl_verify_hostname.verify_fun/3,
+         [check_hostname: Pleroma.HTTP.Connection.format_host(host)]}
+    ]
+
+    tls_opts =
+      if Keyword.keyword?(opts[:tls_opts]) do
+        Keyword.merge(tls_opts, opts[:tls_opts])
+      else
+        tls_opts
+      end
+
+    Map.put(opts, :tls_opts, tls_opts)
+  end
+
   defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
     connect_opts =
       uri
index 862e851c028cc50fba466e636ad608d20c44594c..55c2b192aecf451010a29f7802ab555715543428 100644 (file)
@@ -45,21 +45,11 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
 
   defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
 
-  defp add_scheme_opts(opts, %URI{scheme: "https", host: host}) do
-    adapter_opts = [
-      certificates_verification: true,
-      transport: :tls,
-      tls_opts: [
-        verify: :verify_peer,
-        cacertfile: CAStore.file_path(),
-        depth: 20,
-        reuse_sessions: false,
-        verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: format_host(host)]},
-        log_level: :warning
-      ]
-    ]
-
-    Keyword.merge(opts, adapter_opts)
+  defp add_scheme_opts(opts, %URI{scheme: "https"}) do
+    opts
+    |> Keyword.put(:certificates_verification, true)
+    |> Keyword.put(:transport, :tls)
+    |> Keyword.put(:tls_opts, log_level: :warning)
   end
 
   defp maybe_get_conn(adapter_opts, uri, connection_opts) do
@@ -93,17 +83,4 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
         |> Keyword.put(:close_conn, false)
     end
   end
-
-  @spec format_host(String.t()) :: charlist()
-  def format_host(host) do
-    host_charlist = to_charlist(host)
-
-    case :inet.parse_address(host_charlist) do
-      {:error, :einval} ->
-        :idna.encode(host_charlist)
-
-      {:ok, _ip} ->
-        host_charlist
-    end
-  end
 end
index 777e5d4c8c7ba5ee2998bfded4d15aebddf4764b..0fc88f708da48ec138ad49b4f88bcdf9ee2552b3 100644 (file)
@@ -106,4 +106,17 @@ defmodule Pleroma.HTTP.Connection do
       {:ok, ip} -> ip
     end
   end
+
+  @spec format_host(String.t()) :: charlist()
+  def format_host(host) do
+    host_charlist = to_charlist(host)
+
+    case :inet.parse_address(host_charlist) do
+      {:error, :einval} ->
+        :idna.encode(host_charlist)
+
+      {:ok, _ip} ->
+        host_charlist
+    end
+  end
 end
index 66622b60539a7a4c0c4a45958910e8a008cc4f55..6af8be15d0cdacfc98df2b655c84b912fbaa56ce 100644 (file)
@@ -38,31 +38,23 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
 
       opts = Gun.options([receive_conn: false], uri)
       assert opts[:certificates_verification]
-      refute opts[:tls_opts] == []
-
-      assert opts[:tls_opts][:verify_fun] ==
-               {&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'example.com']}
-
-      assert File.exists?(opts[:tls_opts][:cacertfile])
+      assert opts[:tls_opts][:log_level] == :warning
     end
 
     test "https ipv4 with default port" do
       uri = URI.parse("https://127.0.0.1")
 
       opts = Gun.options([receive_conn: false], uri)
-
-      assert opts[:tls_opts][:verify_fun] ==
-               {&:ssl_verify_hostname.verify_fun/3, [check_hostname: '127.0.0.1']}
+      assert opts[:certificates_verification]
+      assert opts[:tls_opts][:log_level] == :warning
     end
 
     test "https ipv6 with default port" do
       uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
 
       opts = Gun.options([receive_conn: false], uri)
-
-      assert opts[:tls_opts][:verify_fun] ==
-               {&:ssl_verify_hostname.verify_fun/3,
-                [check_hostname: '2a03:2880:f10c:83:face:b00c:0:25de']}
+      assert opts[:certificates_verification]
+      assert opts[:tls_opts][:log_level] == :warning
     end
 
     test "https url with non standart port" do
@@ -269,23 +261,4 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
              } = Connections.get_state(:gun_connections)
     end
   end
-
-  describe "format_host/1" do
-    test "with domain" do
-      assert Gun.format_host("example.com") == 'example.com'
-    end
-
-    test "with idna domain" do
-      assert Gun.format_host("ですexample.com") == 'xn--example-183fne.com'
-    end
-
-    test "with ipv4" do
-      assert Gun.format_host("127.0.0.1") == '127.0.0.1'
-    end
-
-    test "with ipv6" do
-      assert Gun.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
-               '2a03:2880:f10c:83:face:b00c:0:25de'
-    end
-  end
 end
index 25a2bac1c0b6464d7d99bb10665d1df4d8c02e60..0f62eddd26db23537ed520db87e3680befb683e1 100644 (file)
@@ -113,4 +113,23 @@ defmodule Pleroma.HTTP.ConnectionTest do
       assert opts[:proxy] == {'example.com', 4321}
     end
   end
+
+  describe "format_host/1" do
+    test "with domain" do
+      assert Connection.format_host("example.com") == 'example.com'
+    end
+
+    test "with idna domain" do
+      assert Connection.format_host("ですexample.com") == 'xn--example-183fne.com'
+    end
+
+    test "with ipv4" do
+      assert Connection.format_host("127.0.0.1") == '127.0.0.1'
+    end
+
+    test "with ipv6" do
+      assert Connection.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
+               '2a03:2880:f10c:83:face:b00c:0:25de'
+    end
+  end
 end