Skip to content

Commit 97bac5d

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

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

libmamba/src/download/downloader.cpp

Lines changed: 30 additions & 7 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";
91-
92-
LOG_INFO << "Checking for CA certificates at the root prefix: "
88+
const fs::u8path executable_path = get_self_exe_path();
89+
// Find the supposed environment prefix.
90+
// `mamba` or `micromamba` is installed at:
91+
// - `${PREFIX}/bin/{mamba,micromamba}` on Unix
92+
// - `${PREFIX}/Library/bin/{mamba,micromamba}.exe` on Windows
93+
const fs::u8path env_prefix
94+
= (util::on_win ? executable_path.parent_path().parent_path().parent_path()
95+
: executable_path.parent_path().parent_path());
96+
97+
const fs::u8path env_prefix_conda_cert = env_prefix / "ssl" / "cacert.pem";
98+
99+
LOG_INFO << "Checking for CA certificates in the same environment as the executable 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 executable 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+
const fs::u8path root_prefix = detail::get_root_prefix();
113+
const 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import shutil
4+
import subprocess
45

56
from packaging.version import Version
67
from pathlib import Path
@@ -525,3 +526,30 @@ def test_env_export_with_pip(tmp_path, json_flag):
525526
# Check that `requests` and `urllib3` (pulled dependency) are exported
526527
assert "requests==2.32.3" in pip_section_vals
527528
assert any(pkg.startswith("urllib3==") for pkg in pip_section_vals)
529+
530+
531+
def test_env_export_with_ca_certificates(tmp_path):
532+
# CA certificates in the same environment as `mamba` or `micromamba`
533+
# executable installation are used by default.
534+
tmp_env_prefix = tmp_path / "env-export-with-ca-certificates"
535+
536+
helpers.create("-p", tmp_env_prefix, "ca-certificates", no_dry_run=True)
537+
538+
# Copy the `micromamba` executable in this prefix `bin` subdirectory
539+
(tmp_env_prefix / "bin").mkdir(parents=True, exist_ok=True)
540+
tmp_env_micromamba = tmp_env_prefix / "bin" / "micromamba"
541+
shutil.copy(helpers.get_umamba(), tmp_env_micromamba)
542+
543+
# Run a command using mamba in verbose mode and check that the ca-certificates file
544+
# from the same environment as the executable is used by default.
545+
p = subprocess.run(
546+
[tmp_env_micromamba, "search", "xtensor", "-v"],
547+
capture_output=True,
548+
check=True,
549+
)
550+
stderr = p.stderr.decode()
551+
assert (
552+
"Checking for CA certificates in the same environment as the executable installation"
553+
in stderr
554+
)
555+
assert "Using CA certificates from the same prefix as the executable installation" in stderr

0 commit comments

Comments
 (0)