Skip to content

Commit 091170f

Browse files
committed
fix: Resolve ca-certificates installed in the local environment
Signed-off-by: Julien Jerphanion <[email protected]>
1 parent 6f11ca2 commit 091170f

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

libmamba/src/download/downloader.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "mamba/core/invoke.hpp"
99
#include "mamba/core/thread_utils.hpp"
1010
#include "mamba/core/util.hpp"
11+
#include "mamba/core/util_os.hpp"
1112
#include "mamba/core/util_scope.hpp"
1213
#include "mamba/download/downloader.hpp"
1314
#include "mamba/util/build.hpp"
@@ -84,19 +85,41 @@ namespace mamba::download
8485
// from `conda-forge::ca-certificates` and the system CA certificates.
8586
else if (remote_fetch_params.ssl_verify == "<system>")
8687
{
87-
// Use the CA certificates from `conda-forge::ca-certificates` installed in the
88-
// root prefix or the system CA certificates if the certificate is not present.
89-
fs::u8path root_prefix = detail::get_root_prefix();
90-
fs::u8path env_prefix_conda_cert = root_prefix / "ssl" / "cacert.pem";
88+
fs::u8path libmamba_path = get_libmamba_path();
89+
// Find the supposed environment prefix of libmamba.
90+
// `libmamba` is installed at:
91+
// - `${PREFIX}/lib/libmamba${SHLIB_EXT}` on Unix
92+
// - `${PREFIX}/Library/bin/libmamba$.dll` on Windows
93+
fs::u8path libmamba_env_prefix = fs::weakly_canonical(
94+
util::on_win ? libmamba_path.parent_path().parent_path().parent_path()
95+
: libmamba_path.parent_path().parent_path()
96+
);
97+
fs::u8path env_prefix_conda_cert = libmamba_env_prefix / "ssl" / "cacert.pem";
9198

92-
LOG_INFO << "Checking for CA certificates at the root prefix: "
99+
LOG_INFO << "Checking for CA certificates in the same environment as the libmamba installation: "
93100
<< env_prefix_conda_cert;
94101

95102
if (fs::exists(env_prefix_conda_cert))
96103
{
97-
LOG_INFO << "Using CA certificates from `conda-forge::ca-certificates` installed in the root prefix "
104+
LOG_INFO << "Using CA certificates from the same prefix as the libmamba installation "
98105
<< "(i.e " << env_prefix_conda_cert << ")";
99106
remote_fetch_params.ssl_verify = env_prefix_conda_cert;
107+
return;
108+
}
109+
110+
// Try to use the CA certificates from `conda-forge::ca-certificates` installed
111+
// in the root prefix.
112+
fs::u8path root_prefix = detail::get_root_prefix();
113+
fs::u8path root_prefix_conda_cert = root_prefix / "ssl" / "cacert.pem";
114+
115+
LOG_INFO << "Checking for CA certificates at the root prefix: "
116+
<< root_prefix_conda_cert;
117+
118+
if (fs::exists(root_prefix_conda_cert))
119+
{
120+
LOG_INFO << "Using CA certificates from `conda-forge::ca-certificates` installed in the root prefix "
121+
<< "(i.e " << root_prefix_conda_cert << ")";
122+
remote_fetch_params.ssl_verify = root_prefix_conda_cert;
100123
remote_fetch_params.curl_initialized = true;
101124
return;
102125
}

micromamba/tests/test_env.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,18 @@ def test_env_export_with_pip(tmp_path, json_flag):
525525
# Check that `requests` and `urllib3` (pulled dependency) are exported
526526
assert "requests==2.32.3" in pip_section_vals
527527
assert any(pkg.startswith("urllib3==") for pkg in pip_section_vals)
528+
529+
530+
def test_env_export_with_ca_certificates(tmp_path):
531+
# CA certificates in the same environment as `libmamba` installation are used by default.
532+
tmp_env_prefix = tmp_path / "env-export-with-ca-certificates"
533+
534+
helpers.create("-p", tmp_env_prefix, "ca-certificates", no_dry_run=True)
535+
536+
# Copy the `micromamba` executable in this prefix `bin` subdirectory
537+
shutil.copy(helpers.get_umamba(), tmp_env_prefix / "bin" / "micromamba")
538+
539+
# Run a command using mamba in verbose mode and check that the logs contain the
540+
# message "Using CA certificates from the same prefix as the libmamba installation"
541+
output = helpers.umamba_run("-p", tmp_env_prefix, "micromamba", "search", "python", "-vvv")
542+
assert "Using CA certificates from the same prefix as the libmamba installation" in output

0 commit comments

Comments
 (0)