File 1541-Allow-setting-certificates-via-application-config.patch of Package erlang
From 13d6a3522f77b0d18b837982f1be929d7ba604bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@dashbit.co>
Date: Tue, 1 Oct 2024 09:13:02 +0200
Subject: [PATCH] Allow setting certificates via application config
---
lib/public_key/src/pubkey_os_cacerts.erl | 10 +++++++++-
lib/public_key/src/public_key.erl | 15 ++++++++++++++-
lib/public_key/test/public_key_SUITE.erl | 8 +++++++-
3 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/lib/public_key/src/pubkey_os_cacerts.erl b/lib/public_key/src/pubkey_os_cacerts.erl
index f3659cda5c..17e76b0084 100644
--- a/lib/public_key/src/pubkey_os_cacerts.erl
+++ b/lib/public_key/src/pubkey_os_cacerts.erl
@@ -37,7 +37,15 @@
get() ->
case persistent_term:get(?MODULE, not_loaded) of
not_loaded ->
- case load() of
+ _ = application:load(public_key),
+
+ Result =
+ case application:get_env(public_key, cacerts_path) of
+ {ok, EnvVar} -> load([EnvVar]);
+ undefined -> load()
+ end,
+
+ case Result of
ok ->
persistent_term:get(?MODULE);
{error, Reason} ->
diff --git a/lib/public_key/test/public_key_SUITE.erl b/lib/public_key/test/public_key_SUITE.erl
index 3db9c6ae37..869176ba3b 100644
--- a/lib/public_key/test/public_key_SUITE.erl
+++ b/lib/public_key/test/public_key_SUITE.erl
@@ -1607,6 +1607,12 @@ cacerts_load(Config) ->
ok
end,
+ %% Load from application environment
+ application:set_env(public_key, cacerts_path, filename:join(Datadir, "cacerts.pem")),
+ 2 = length(public_key:cacerts_get()),
+ application:unset_env(public_key, cacerts_path),
+ true = public_key:cacerts_clear(),
+
%% Load default OS certs
%% there is no default installed OS certs on netbsd
%% can be installed with 'pkgin install mozilla-rootcerts'
@@ -1624,7 +1630,7 @@ cacerts_load(Config) ->
ok = public_key:cacerts_load(filename:join(Datadir, "cacerts.pem")),
[_TestCert1, _TestCert2] = public_key:cacerts_get(),
- %% Re-Load default OS certs
+ %% Reload default OS certs
try
process_flag(trap_exit, true),
flush(),
--
2.43.0