Skip to content

Commit ae3685b

Browse files
committed
Use mergin.client.set_trusted_certificates() instead of patching cert.pem
Replace the macOS-specific cert.pem file patching with a call to mergin.client.set_trusted_certificates(cafile), a new API to be added to python-api-client. The mergin client module stores the extra CA file path and loads it alongside its default bundle (system CAs on Linux/Windows, bundled cert.pem on macOS) whenever a MerginClient is instantiated. This avoids mutating files inside the installed plugin directory and ensures CAs are always up to date (the PEM file is rewritten on every call). A hasattr guard keeps the code compatible with older py-client versions that don't have the new function yet. Remove the sys import that was only needed for the platform check.
1 parent 485b843 commit ae3685b

1 file changed

Lines changed: 19 additions & 27 deletions

File tree

Mergin/utils_auth.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import hashlib
55
import os
66
import re
7-
import sys
87
import typing
98
import uuid
109
import json
@@ -559,15 +558,15 @@ def url_reachable(url: str) -> bool:
559558

560559
def setup_qgis_ssl_for_mergin_client() -> None:
561560
"""
562-
Export QGIS trusted CA certificates so that MerginClient's SSL context
563-
can verify servers using custom/internal CAs configured in QGIS.
564-
565-
This does two things:
566-
1. Writes all QGIS trusted CAs to a PEM file and sets SSL_CERT_FILE
567-
so that Python's default SSL context (used by MerginClient on
568-
Linux/Windows) picks them up.
569-
2. On macOS, appends the QGIS CAs to MerginClient's bundled cert.pem
570-
so that the macOS fallback code path also trusts them.
561+
Register QGIS trusted CA certificates with the mergin client module so that
562+
all subsequent MerginClient instances trust servers signed by CAs configured
563+
in QGIS.
564+
565+
Writes the QGIS trusted CAs to a PEM file, then passes that path to
566+
mergin.client.set_trusted_certificates() so MerginClient loads those CAs
567+
in addition to its default bundle (system CAs on Linux/Windows, bundled
568+
cert.pem on macOS). Also sets SSL_CERT_FILE as a fallback for code paths
569+
that use Python's default SSL context directly.
571570
"""
572571
qgis_ca_pem = QgsApplication.authManager().trustedCaCertsPemText()
573572
if hasattr(qgis_ca_pem, "data"):
@@ -576,29 +575,22 @@ def setup_qgis_ssl_for_mergin_client() -> None:
576575
if not qgis_ca_pem:
577576
return
578577

579-
# 1. Write to a PEM file and set SSL_CERT_FILE
580578
settings_dir = QgsApplication.qgisSettingsDirPath()
581579
ca_file_path = os.path.join(settings_dir, "mergin-trusted-cas.pem")
582580
with open(ca_file_path, "w") as f:
583581
f.write(qgis_ca_pem)
582+
583+
# Fallback: SSL_CERT_FILE is respected by Python's default SSL context.
584584
os.environ["SSL_CERT_FILE"] = ca_file_path
585585

586-
# 2. On macOS: patch MerginClient's bundled cert.pem so the fallback
587-
# code path (which ignores SSL_CERT_FILE) also trusts QGIS CAs.
588-
if sys.platform == "darwin":
589-
plugin_dir = os.path.dirname(os.path.realpath(__file__))
590-
bundled_cert = os.path.join(plugin_dir, "mergin", "cert.pem")
591-
if os.path.exists(bundled_cert):
592-
marker = "# --- QGIS trusted CAs ---"
593-
with open(bundled_cert, "r") as f:
594-
existing = f.read()
595-
# Always replace the QGIS section so stale CAs get refreshed
596-
if marker in existing:
597-
base_content = existing[: existing.index(marker)].rstrip()
598-
else:
599-
base_content = existing.rstrip()
600-
with open(bundled_cert, "w") as f:
601-
f.write(base_content + f"\n\n{marker}\n" + qgis_ca_pem)
586+
# Primary path: register the CA file with the mergin client module so that
587+
# every MerginClient instance (on all platforms) loads it alongside its
588+
# default CA bundle. Requires mergin.client.set_trusted_certificates()
589+
# from python-api-client >= <next release>.
590+
from .mergin import client as mergin_client
591+
592+
if hasattr(mergin_client, "set_trusted_certificates"):
593+
mergin_client.set_trusted_certificates(ca_file_path)
602594

603595

604596
def qgis_support_sso() -> bool:

0 commit comments

Comments
 (0)