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

feat: For otp 25+ use public_key:cacerts_get for the default certificate list. #114

Merged
merged 4 commits into from
Jan 2, 2024
Merged
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
22 changes: 21 additions & 1 deletion src/ldclient_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,27 @@ get_event_schema() ->
%% @end
-spec tls_basic_options() -> [ssl:tls_client_option()].
tls_basic_options() ->
tls_basic_options(filelib:is_regular(?HTTP_DEFAULT_LINUX_CASTORE)).
case erlang:list_to_integer(erlang:system_info(otp_release)) >= 25 of
true -> tls_basic_erlef_options();
false -> tls_basic_options(filelib:is_regular(?HTTP_DEFAULT_LINUX_CASTORE))
end.

%% The public_key:cacerts_get function does not exist prior to OTP 25, so we
%% need to ignore the warning when building code that will not be using it.
-dialyzer({no_missing_calls, tls_basic_erlef_options/0}).

%% @doc Provide basic options for using TLS with the default OTP 25+.
%% Follows the recommendations from the Erlang Security Working Group.
%% https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/ssl
%%
%% @end
-spec tls_basic_erlef_options() -> [ssl:tls_client_option()].
tls_basic_erlef_options() ->
CaCerts = public_key:cacerts_get(),
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the certs are empty, what is the final behavior? (hopefully that no TLS negotiation succeeds).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:ssl.connect/3 would fail, as the verify: :verify_peer mode requires a CA trust store.

If both :cacertfile and :cacerts are blank (empty list/charlist) the options are invalid and :ssl.connect/3 returns an error without even attempting the connection.

{cacerts, CaCerts}
| tls_base_options()
].

%% @doc Provide basic options for using TLS with the default linux store.
%% This will try to use the a certificate store located at
Expand Down
48 changes: 30 additions & 18 deletions test/ldclient_config_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,36 @@ get_http_options_multiple_options(_) ->
} = maps:get(http_options, Settings, undefined).

tls_basic_options(_) ->
BasicOptions = ldclient_config:tls_basic_options(),
case os:type() of
{unix, linux} ->
[
{cacertfile, "/etc/ssl/certs/ca-certificates.crt"},
{verify, verify_peer},
{ciphers, Ciphers},
{depth, 3},
{customize_hostname_check, _}] = BasicOptions,
true = (length(Ciphers) =/= 0);
{_, _} ->
[
{cacerts, _},
{verify, verify_peer},
{ciphers, Ciphers},
{depth, 3},
{customize_hostname_check, _}] = BasicOptions,
true = (length(Ciphers) =/= 0)
case erlang:list_to_integer(erlang:system_info(otp_release)) >= 25 of
true ->
BasicOptions = ldclient_config:tls_basic_options(),
CaCerts = public_key:cacerts_get(),
[{cacerts, CaCerts},
{verify, verify_peer},
{ciphers, Ciphers},
{depth, 3},
{customize_hostname_check, _}] = BasicOptions,
true = (length(Ciphers) =/= 0);
false ->
BasicOptions = ldclient_config:tls_basic_options(),
case os:type() of
{unix, linux} ->
[
{cacertfile, "/etc/ssl/certs/ca-certificates.crt"},
{verify, verify_peer},
{ciphers, Ciphers},
{depth, 3},
{customize_hostname_check, _}] = BasicOptions,
true = (length(Ciphers) =/= 0);
{_, _} ->
[
{cacerts, _},
{verify, verify_peer},
{ciphers, Ciphers},
{depth, 3},
{customize_hostname_check, _}] = BasicOptions,
true = (length(Ciphers) =/= 0)
end
end.

tls_with_ca_certfile_options(_) ->
Expand Down