hiding raise error logic to otp_version module
[akkoma] / lib / pleroma / otp_version.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.OTPVersion do
6 @type check_status() :: :ok | :undefined | {:error, String.t()}
7
8 @spec check!() :: :ok | no_return()
9 def check! do
10 case check() do
11 :ok ->
12 :ok
13
14 {:error, version} ->
15 raise "
16 !!!OTP VERSION WARNING!!!
17 You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains.
18 "
19
20 :undefined ->
21 raise "
22 !!!OTP VERSION WARNING!!!
23 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
24 "
25 end
26 end
27
28 @spec check() :: check_status()
29 def check do
30 # OTP Version https://erlang.org/doc/system_principles/versions.html#otp-version
31 [
32 Path.join(:code.root_dir(), "OTP_VERSION"),
33 Path.join([:code.root_dir(), "releases", :erlang.system_info(:otp_release), "OTP_VERSION"])
34 ]
35 |> get_version_from_files()
36 |> do_check()
37 end
38
39 @spec check([Path.t()]) :: check_status()
40 def check(paths) do
41 paths
42 |> get_version_from_files()
43 |> do_check()
44 end
45
46 defp get_version_from_files([]), do: nil
47
48 defp get_version_from_files([path | paths]) do
49 if File.exists?(path) do
50 File.read!(path)
51 else
52 get_version_from_files(paths)
53 end
54 end
55
56 defp do_check(nil), do: :undefined
57
58 defp do_check(version) do
59 version = String.replace(version, ~r/\r|\n|\s/, "")
60
61 [major, minor] =
62 version
63 |> String.split(".")
64 |> Enum.map(&String.to_integer/1)
65 |> Enum.take(2)
66
67 if (major == 22 and minor >= 2) or major > 22 do
68 :ok
69 else
70 {:error, version}
71 end
72 end
73 end