Skip to content

Commit

Permalink
remove dependency to parse_trans
Browse files Browse the repository at this point in the history
go back to our own parse transform so we can support most versions of Erlang
  • Loading branch information
benoitc committed Mar 17, 2021
1 parent 2620036 commit ffc607f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{deps, [{parse_trans, "~>3.3"} %% Only compile-time
]}.
{erl_first_files, ["src/certifi_pt.erl"]}.

{erl_opts, [deterministic %% Added since OTP 20
,{platform_define, "^2", 'OTP_20_AND_ABOVE'}
]}.
15 changes: 2 additions & 13 deletions src/certifi.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
-module(certifi).
-compile({parse_transform, ct_expand}).
-compile({parse_transform, certifi_pt}).

-export([cacertfile/0,
cacerts/0]).


%% @doc CACertFile gives the path to the file with an X.509 certificate list
%% containing the Mozilla CA Certificate that can then be used via the
%% cacertfile setting in ssl options passed to the connect function.
Expand All @@ -25,14 +24,4 @@ cacertfile() ->
%% passed to the connect function.
-spec cacerts() -> [binary(),...].
cacerts() ->
ct_expand:term(
lists:foldl(
fun ({'Certificate', Der, _}, Acc) ->
[Der | Acc]
end,
[],
(fun ({ok, Bin}) ->
public_key:pem_decode(Bin)
end)(file:read_file(cacertfile()))
)
).
ok.
19 changes: 19 additions & 0 deletions src/certifi_pt.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(certifi_pt).
-export([parse_transform/2]).

parse_transform(Forms, _Opts) ->
[replace_cacerts(Form) || Form <- Forms].

replace_cacerts({function, Ann, cacerts, 0, [_]}) ->
{ok, Binary} = file:read_file("priv/cacerts.pem"),
Pems = public_key:pem_decode(Binary),
Cacerts = [Der || {'Certificate', Der, _} <- Pems],
Body = lists:foldl(fun(Cert, Acc) ->
{cons, 0, cert_to_bin_ast(Cert), Acc}
end, {nil, 0}, Cacerts),
{function, Ann, cacerts, 0, [{clause, Ann, [], [], [Body]}]};
replace_cacerts(Other) ->
Other.

cert_to_bin_ast(Cert) ->
{bin, 0, [{bin_element, 0, {string, 0, binary_to_list(Cert)}, default, default}]}.
8 changes: 4 additions & 4 deletions test/certifi_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ reproducible_module_test() ->

cacerts_test_() ->
Certs = [Cert1, Cert2, Cert3 | _] = certifi:cacerts(),
[?_assertEqual(138, length(Certs))
,?_assertMatch(<<48,130,2,157,48,130,2,36,160,3,2,1,2,2,12,8,189,133,151,108,_/binary>>, Cert1)
,?_assertMatch(<<48,130,2,96,48,130,2,7,160,3,2,1,2,2,12,13,106,95,8,63,40,_/binary>>, Cert2)
,?_assertMatch(<<48,130,5,218,48,130,3,194,160,3,2,1,2,2,12,5,247,14,134,218, _/binary>>, Cert3)
[?_assertEqual(127, length(Certs))
,?_assertMatch(<<48,130,2,11,48,130,1,145,160,3,2,1,2,2,18,17,210,187,186,51,_/binary>>, Cert1)
,?_assertMatch(<<48,130,5,90,48,130,3,66,160,3,2,1,2,2,18,17,210,187,185,215,_/binary>>, Cert2)
,?_assertMatch(<< 48,130,2,110,48,130,1,243,160,3,2,1,2,2,16,98,246,50,108, _ / binary>>, Cert3)
,?_assertMatch(<<48,130,3,117,48,130,2,93,160,3,2,1,2,2,11,4,0,0,0,0,1,21,75,90,195,148,48,13,6,_/binary>>, lists:last(Certs))
].

0 comments on commit ffc607f

Please sign in to comment.