From 730fa245c54c27d7b5c87c41a0440f92123fc6b2 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 09:51:48 +0100 Subject: [PATCH 1/8] Trust CA file in addition to default CA bundle A CA file in 'ssl_verify' (or 'AYON_CA_FILE') replaced the default CA bundle of 'requests' and 'websocket', so a server signed by a private CA was trusted but nothing else. Sessions now use an adapter with SSL context trusting both, and websockets get the same context. Co-Authored-By: Claude Opus 5.5 --- ayon_api/server_api.py | 94 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 6 deletions(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index 5cc23cc3e..c5b3977b5 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -141,6 +141,81 @@ def _get_requests_ca_bundle() -> str | None: return None +def _create_ssl_context(ca_path: str) -> ssl.SSLContext: + """SSL context trusting default CA bundle and CA certificates from path. + + A CA file passed to 'requests' or 'websocket' replaces their default + CA bundle, so a server signed by a private CA would be trusted but + nothing else (e.g. redirects to cloud storage). The context trusts + both instead. + + Args: + ca_path (str): Path to CA file or directory with CA certificates. + + Returns: + ssl.SSLContext: SSL context for client connections. + + """ + default_bundle = _get_requests_ca_bundle() + if default_bundle and os.path.isdir(default_bundle): + context = ssl.create_default_context(capath=default_bundle) + elif default_bundle: + context = ssl.create_default_context(cafile=default_bundle) + else: + context = ssl.create_default_context() + + if os.path.isdir(ca_path): + context.load_verify_locations(capath=ca_path) + else: + context.load_verify_locations(cafile=ca_path) + return context + + +class _CAFileAdapter(requests.adapters.HTTPAdapter): + """HTTP adapter trusting additional CA certificates. + + Used when 'ssl_verify' is a path. The session still has 'verify' set + to the path, which 'urllib3' loads to the context too. + + Args: + ca_path (str): Path to CA file or directory with CA certificates. + + """ + def __init__(self, ca_path: str, **kwargs) -> None: + ssl_context = _create_ssl_context(ca_path) + # 'urllib3' matches hostname on its own, same as with its default + # context. Python's check fails for IP addresses on 'urllib3' 1.x, + # which does not send server hostname for them. + ssl_context.check_hostname = False + self._ssl_context = ssl_context + super().__init__(**kwargs) + + def init_poolmanager(self, *args, **kwargs) -> None: + kwargs["ssl_context"] = self._ssl_context + super().init_poolmanager(*args, **kwargs) + + def proxy_manager_for(self, proxy, **proxy_kwargs): + proxy_kwargs["ssl_context"] = self._ssl_context + return super().proxy_manager_for(proxy, **proxy_kwargs) + + +def _apply_ssl_verify( + session: requests.Session, ssl_verify: bool | str | None +) -> None: + """Set 'verify' of session and mount adapter for CA path.""" + session.verify = ssl_verify + current = getattr(session, "adapters", {}).get("https://") + if isinstance(ssl_verify, str): + adapter = _CAFileAdapter(ssl_verify) + elif isinstance(current, _CAFileAdapter): + adapter = requests.adapters.HTTPAdapter() + else: + return + session.mount("https://", adapter) + if current is not None: + current.close() + + def _disable_websocket_ipv6() -> None: """Skip IPv6 addresses in 'websocket' when python does not support IPv6. @@ -342,7 +417,8 @@ class ServerAPI( propagated into events. ssl_verify (bool | str | None): Verify SSL certificate Looks for env variable value ``AYON_CA_FILE`` by default. If not - available then 'True' is used. + available then 'True' is used. A path to CA file or directory + is trusted in addition to the default CA bundle. cert (str | None): Path to certificate file. Looks for env variable value ``AYON_CERT_FILE`` by default. create_session (bool): Create session for connection if @@ -504,7 +580,7 @@ def set_ssl_verify( self._ssl_verify = ssl_verify for session in (self._session, self._validation_session): if session is not None: - session.verify = ssl_verify + _apply_ssl_verify(session, ssl_verify) def get_cert(self) -> str | None: """Current cert file used for connection to server. @@ -1003,8 +1079,8 @@ def create_session( self._validation_session = None if session is None: session = requests.Session() - session.cert = self._cert - session.verify = self._ssl_verify + session.cert = self._cert + _apply_ssl_verify(session, self._ssl_verify) session.headers.update(self.get_headers()) self._session_functions_mapping = { @@ -1044,7 +1120,7 @@ def _get_validation_session(self) -> requests.Session: if self._validation_session is None: session = requests.Session() session.cert = self._cert - session.verify = self._ssl_verify + _apply_ssl_verify(session, self._ssl_verify) self._validation_session = session return self._validation_session @@ -1086,7 +1162,13 @@ def create_websocket( # module, which might be missing (e.g. python on macOS), so # REST would work but websocket would always fail. ca_bundle = self._ssl_verify - if not isinstance(ca_bundle, str): + if isinstance(ca_bundle, str): + # Trust default CA bundle too, same as REST requests do + if "context" not in prepared_sslopt: + prepared_sslopt["context"] = _create_ssl_context( + ca_bundle + ) + else: ca_bundle = _get_requests_ca_bundle() if ca_bundle: key = "ca_certs" From 49f5261a363acfd1606aa8751e4eef14a12ad468 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:05:17 +0100 Subject: [PATCH 2/8] Use CA file for requests made without session Requests of 'ServerAPI' without authorized session (server version, login, downloads and uploads) used plain 'requests' functions without 'ssl_verify' and 'cert'. They now use the validation session. Also 'login_to_server' and 'logout_from_server' use 'AYON_CA_FILE' and 'AYON_CERT_FILE'. Login in AYON launcher failed with only 'AYON_CA_FILE' set because of it. Co-Authored-By: Claude Opus 5.5 --- ayon_api/server_api.py | 43 ++++++++++++++++++++++++++++++++++++------ ayon_api/utils.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index c5b3977b5..90445b058 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -6,6 +6,7 @@ from __future__ import annotations import copy +import functools from dataclasses import dataclass import os import re @@ -503,12 +504,19 @@ def __init__( # token validation, see '_get_validation_session' self._validation_session: requests.Session | None = None + # Requests made without authorized session use validation session, + # so they respect 'ssl_verify' and 'cert' self._base_functions_mapping = { - RequestTypes.get: requests.get, - RequestTypes.post: requests.post, - RequestTypes.put: requests.put, - RequestTypes.patch: requests.patch, - RequestTypes.delete: requests.delete + request_type: functools.partial( + self._validation_session_request, request_type + ) + for request_type in ( + RequestTypes.get, + RequestTypes.post, + RequestTypes.put, + RequestTypes.patch, + RequestTypes.delete, + ) } self._session_functions_mapping = {} @@ -1124,6 +1132,24 @@ def _get_validation_session(self) -> requests.Session: self._validation_session = session return self._validation_session + def _validation_session_request( + self, request_type: RequestType, url: str, **kwargs + ) -> requests.Response: + """Send request without authorized session. + + Args: + request_type (RequestType): Type of request. + url (str): Request url. + **kwargs: Keyword arguments for 'requests'. + + Returns: + requests.Response: Response from server. + + """ + session = self._get_validation_session() + func = getattr(session, request_type.name.lower()) + return func(url, **kwargs) + def create_websocket( self, endpoint: str, @@ -1849,7 +1875,12 @@ def _endpoint_to_url( def _logout(self) -> None: if self._token_info.is_valid: - logout_from_server(self._base_url, self._token_info.token) + logout_from_server( + self._base_url, + self._token_info.token, + verify=self._ssl_verify, + cert=self._cert, + ) def _get_server_info(self) -> dict[str, Any]: """Get server info without a session.""" diff --git a/ayon_api/utils.py b/ayon_api/utils.py index aeaabb2f9..6954fa251 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -635,6 +635,8 @@ def login_to_server( username: str, password: str, timeout: float | None = None, + verify: str | bool | None = None, + cert: str | None = None, ) -> str | None: """Use login to the server to receive token. @@ -644,6 +646,10 @@ def login_to_server( password (str): User's password. timeout (float | None): Timeout for request. Value from 'get_default_timeout' is used if not specified. + verify (str | bool | None): SSL verification for request. Value from + 'AYON_CA_FILE' environment variable is used if not specified. + cert (str | None): SSL certificate for request. Value from + 'AYON_CERT_FILE' environment variable is used if not specified. Returns: str | None: User's token if login was successfull. @@ -652,6 +658,13 @@ def login_to_server( """ if timeout is None: timeout = get_default_timeout() + + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + headers = {"Content-Type": "application/json"} response = requests.post( f"{url}/api/auth/login", @@ -661,6 +674,8 @@ def login_to_server( "password": password }, timeout=timeout, + verify=verify, + cert=cert, ) token = None # 200 - success @@ -676,6 +691,8 @@ def logout_from_server( url: str, token: str, timeout: float | None = None, + verify: str | bool | None = None, + cert: str | None = None, ) -> None: """Logout from server and throw token away. @@ -684,10 +701,21 @@ def logout_from_server( token (str): Token which should be used to log out. timeout (float | None): Timeout for request. Value from 'get_default_timeout' is used if not specified. + verify (str | bool | None): SSL verification for request. Value from + 'AYON_CA_FILE' environment variable is used if not specified. + cert (str | None): SSL certificate for request. Value from + 'AYON_CERT_FILE' environment variable is used if not specified. """ if timeout is None: timeout = get_default_timeout() + + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + headers = { "Content-Type": "application/json", "Authorization": f"Bearer {token}", @@ -696,6 +724,8 @@ def logout_from_server( f"{url}/api/auth/logout", headers=headers, timeout=timeout, + verify=verify, + cert=cert, ) From c99951757852c71208e414a853b84aba764a480e Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:27:43 +0100 Subject: [PATCH 3/8] Use CA file for web action requests 'take_web_action_event' and 'abort_web_action_event' did not use 'AYON_CA_FILE' and 'AYON_CERT_FILE', so web actions in AYON launcher failed with server signed by private CA. Co-Authored-By: Claude Opus 5.5 --- ayon_api/utils.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 6954fa251..8ed7d691f 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -1395,7 +1395,9 @@ def get_media_mime_type(filepath: str) -> str | None: def take_web_action_event( server_url: str, - action_token: str + action_token: str, + verify: str | bool | None = None, + cert: str | None = None, ) -> dict[str, Any]: """Take web action event using action token. @@ -1404,13 +1406,25 @@ def take_web_action_event( Args: server_url (str): AYON server url. action_token (str): Action token. + verify (str | bool | None): SSL verification for request. Value from + 'AYON_CA_FILE' environment variable is used if not specified. + cert (str | None): SSL certificate for request. Value from + 'AYON_CERT_FILE' environment variable is used if not specified. Returns: dict[str, Any]: Web action event. """ + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + response = requests.get( - f"{server_url}/api/actions/take/{action_token}" + f"{server_url}/api/actions/take/{action_token}", + verify=verify, + cert=cert, ) response.raise_for_status() return response.json() @@ -1419,7 +1433,9 @@ def take_web_action_event( def abort_web_action_event( server_url: str, action_token: str, - reason: str + reason: str, + verify: str | bool | None = None, + cert: str | None = None, ) -> requests.Response: """Abort web action event using action token. @@ -1429,14 +1445,26 @@ def abort_web_action_event( server_url (str): AYON server url. action_token (str): Action token. reason (str): Reason why webaction event was aborted. + verify (str | bool | None): SSL verification for request. Value from + 'AYON_CA_FILE' environment variable is used if not specified. + cert (str | None): SSL certificate for request. Value from + 'AYON_CERT_FILE' environment variable is used if not specified. Returns: requests.Response: Response from server. """ + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + response = requests.post( f"{server_url}/api/actions/abort/{action_token}", json={"message": reason}, + verify=verify, + cert=cert, ) response.raise_for_status() return response From ec3a903f1d1de29ec5f9b6220cb12e717170a288 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:36:12 +0100 Subject: [PATCH 4/8] Add fake 'requests.adapters' to automated api script 'server_api' subclasses 'requests.adapters.HTTPAdapter' on import. Co-Authored-By: Claude Opus 5.5 --- automated_api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/automated_api.py b/automated_api.py index 761f15b51..92f99dc3d 100644 --- a/automated_api.py +++ b/automated_api.py @@ -24,6 +24,11 @@ requests.__dict__["Response"] = type( "Response", (), {"__module__": "requests"} ) +requests_adapters = type(sys)("requests.adapters") +requests_adapters.__dict__["HTTPAdapter"] = type( + "HTTPAdapter", (), {"__module__": "requests.adapters"} +) +requests.__dict__["adapters"] = requests_adapters websocket = type(sys)("websocket") websocket.__dict__.update({ name: type(name, (), {"__module__": "websocket"}) From 0a827ab5384dc76c7a95e2bd35ab2c424e80ffe8 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 11:11:27 +0100 Subject: [PATCH 5/8] Trust default CA bundle in request helpers too Helpers in 'utils' and token check of 'EventHub' passed the CA file to plain 'requests', which replaces the default CA bundle. With only a studio CA in 'AYON_CA_FILE', a server with public certificate failed in server check, login and web actions, while 'ServerAPI' worked. SSL helpers moved to 'utils' and the helpers use '_send_request', which sends the request through a session with the CA file adapter. Co-Authored-By: Claude Opus 5.5 --- ayon_api/events.py | 5 +- ayon_api/server_api.py | 98 +-------------------- ayon_api/utils.py | 187 +++++++++++++++++++++++++++++++++-------- 3 files changed, 157 insertions(+), 133 deletions(-) diff --git a/ayon_api/events.py b/ayon_api/events.py index 5162b73f4..6847c5e70 100644 --- a/ayon_api/events.py +++ b/ayon_api/events.py @@ -13,10 +13,10 @@ from typing import Any, Callable import weakref -import requests from websocket import ABNF, WebSocketTimeoutException from .exceptions import UnauthorizedError +from .utils import RequestTypes, _send_request if typing.TYPE_CHECKING: from websocket import WebSocket @@ -892,7 +892,8 @@ def _check_token(self) -> bool | None: {"X-Api-Key": token}, ): try: - response = requests.get( + response = _send_request( + RequestTypes.get, url, headers=headers, verify=ssl_verify, diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index 90445b058..832e299d2 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -77,6 +77,9 @@ fill_own_attribs, UserInfo, get_user_info_by_token, + _get_requests_ca_bundle, + _create_ssl_context, + _apply_ssl_verify, ) from ._api_helpers import ( InstallersAPI, @@ -122,101 +125,6 @@ ) -def _get_requests_ca_bundle() -> str | None: - """CA bundle used by 'requests' for ssl verification. - - Returns: - str | None: Path to CA bundle file or directory. - - """ - # Same order as 'requests' uses when 'trust_env' is enabled - for env_key in ("REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE"): - path = os.environ.get(env_key) - if path: - return path - try: - from requests.certs import where - - return where() - except Exception: - return None - - -def _create_ssl_context(ca_path: str) -> ssl.SSLContext: - """SSL context trusting default CA bundle and CA certificates from path. - - A CA file passed to 'requests' or 'websocket' replaces their default - CA bundle, so a server signed by a private CA would be trusted but - nothing else (e.g. redirects to cloud storage). The context trusts - both instead. - - Args: - ca_path (str): Path to CA file or directory with CA certificates. - - Returns: - ssl.SSLContext: SSL context for client connections. - - """ - default_bundle = _get_requests_ca_bundle() - if default_bundle and os.path.isdir(default_bundle): - context = ssl.create_default_context(capath=default_bundle) - elif default_bundle: - context = ssl.create_default_context(cafile=default_bundle) - else: - context = ssl.create_default_context() - - if os.path.isdir(ca_path): - context.load_verify_locations(capath=ca_path) - else: - context.load_verify_locations(cafile=ca_path) - return context - - -class _CAFileAdapter(requests.adapters.HTTPAdapter): - """HTTP adapter trusting additional CA certificates. - - Used when 'ssl_verify' is a path. The session still has 'verify' set - to the path, which 'urllib3' loads to the context too. - - Args: - ca_path (str): Path to CA file or directory with CA certificates. - - """ - def __init__(self, ca_path: str, **kwargs) -> None: - ssl_context = _create_ssl_context(ca_path) - # 'urllib3' matches hostname on its own, same as with its default - # context. Python's check fails for IP addresses on 'urllib3' 1.x, - # which does not send server hostname for them. - ssl_context.check_hostname = False - self._ssl_context = ssl_context - super().__init__(**kwargs) - - def init_poolmanager(self, *args, **kwargs) -> None: - kwargs["ssl_context"] = self._ssl_context - super().init_poolmanager(*args, **kwargs) - - def proxy_manager_for(self, proxy, **proxy_kwargs): - proxy_kwargs["ssl_context"] = self._ssl_context - return super().proxy_manager_for(proxy, **proxy_kwargs) - - -def _apply_ssl_verify( - session: requests.Session, ssl_verify: bool | str | None -) -> None: - """Set 'verify' of session and mount adapter for CA path.""" - session.verify = ssl_verify - current = getattr(session, "adapters", {}).get("https://") - if isinstance(ssl_verify, str): - adapter = _CAFileAdapter(ssl_verify) - elif isinstance(current, _CAFileAdapter): - adapter = requests.adapters.HTTPAdapter() - else: - return - session.mount("https://", adapter) - if current is not None: - current.close() - - def _disable_websocket_ipv6() -> None: """Skip IPv6 addresses in 'websocket' when python does not support IPv6. diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 8ed7d691f..67cbf2d03 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -8,6 +8,7 @@ import copy import logging import json +import ssl import uuid import string import platform @@ -586,6 +587,142 @@ def prepare_entity_changes( return changes +def _get_requests_ca_bundle() -> str | None: + """CA bundle used by 'requests' for ssl verification. + + Returns: + str | None: Path to CA bundle file or directory. + + """ + # Same order as 'requests' uses when 'trust_env' is enabled + for env_key in ("REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE"): + path = os.environ.get(env_key) + if path: + return path + try: + from requests.certs import where + + return where() + except Exception: + return None + + +def _create_ssl_context(ca_path: str) -> ssl.SSLContext: + """SSL context trusting default CA bundle and CA certificates from path. + + A CA file passed to 'requests' or 'websocket' replaces their default + CA bundle, so a server signed by a private CA would be trusted but + nothing else (e.g. redirects to cloud storage). The context trusts + both instead. + + Args: + ca_path (str): Path to CA file or directory with CA certificates. + + Returns: + ssl.SSLContext: SSL context for client connections. + + """ + default_bundle = _get_requests_ca_bundle() + if default_bundle and os.path.isdir(default_bundle): + context = ssl.create_default_context(capath=default_bundle) + elif default_bundle: + context = ssl.create_default_context(cafile=default_bundle) + else: + context = ssl.create_default_context() + + if os.path.isdir(ca_path): + context.load_verify_locations(capath=ca_path) + else: + context.load_verify_locations(cafile=ca_path) + return context + + +class _CAFileAdapter(requests.adapters.HTTPAdapter): + """HTTP adapter trusting additional CA certificates. + + Used when 'ssl_verify' is a path. The session still has 'verify' set + to the path, which 'urllib3' loads to the context too. + + Args: + ca_path (str): Path to CA file or directory with CA certificates. + + """ + def __init__(self, ca_path: str, **kwargs) -> None: + ssl_context = _create_ssl_context(ca_path) + # 'urllib3' matches hostname on its own, same as with its default + # context. Python's check fails for IP addresses on 'urllib3' 1.x, + # which does not send server hostname for them. + ssl_context.check_hostname = False + self._ssl_context = ssl_context + super().__init__(**kwargs) + + def init_poolmanager(self, *args, **kwargs) -> None: + kwargs["ssl_context"] = self._ssl_context + super().init_poolmanager(*args, **kwargs) + + def proxy_manager_for(self, proxy, **proxy_kwargs): + proxy_kwargs["ssl_context"] = self._ssl_context + return super().proxy_manager_for(proxy, **proxy_kwargs) + + +def _apply_ssl_verify( + session: requests.Session, ssl_verify: bool | str | None +) -> None: + """Set 'verify' of session and mount adapter for CA path.""" + session.verify = ssl_verify + current = getattr(session, "adapters", {}).get("https://") + if isinstance(ssl_verify, str): + adapter = _CAFileAdapter(ssl_verify) + elif isinstance(current, _CAFileAdapter): + adapter = requests.adapters.HTTPAdapter() + else: + return + session.mount("https://", adapter) + if current is not None: + current.close() + + +def _send_request( + request_type: RequestType, + url: str, + verify: str | bool | None = None, + cert: str | None = None, + **kwargs, +) -> requests.Response: + """Send request without existing session. + + Same as 'requests.get' and others, but CA file in 'verify' is trusted + in addition to default CA bundle, same as in 'ServerAPI'. + + Args: + request_type (RequestType): Type of request. + url (str): Request url. + verify (str | bool | None): SSL verification for request. Value from + 'AYON_CA_FILE' environment variable is used if not specified. + cert (str | None): SSL certificate for request. Value from + 'AYON_CERT_FILE' environment variable is used if not specified. + **kwargs: Keyword arguments for 'requests'. + + Returns: + requests.Response: Response from server. + + """ + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + + session = requests.Session() + try: + session.cert = cert + _apply_ssl_verify(session, verify) + func = getattr(session, request_type.name.lower()) + return func(url, **kwargs) + finally: + session.close() + + def _try_parse_url(url: str) -> ParseResult | None: try: return urlparse(url) @@ -602,16 +739,11 @@ def _try_connect_to_server( if timeout is None: timeout = get_default_timeout() - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None - try: # TODO add validation if the url lead to AYON server # - this won't validate if the url lead to 'google.com' - response = requests.get( + response = _send_request( + RequestTypes.get, f"{url}/api/info", timeout=timeout, verify=verify, @@ -659,14 +791,9 @@ def login_to_server( if timeout is None: timeout = get_default_timeout() - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None - headers = {"Content-Type": "application/json"} - response = requests.post( + response = _send_request( + RequestTypes.post, f"{url}/api/auth/login", headers=headers, json={ @@ -710,17 +837,12 @@ def logout_from_server( if timeout is None: timeout = get_default_timeout() - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None - headers = { "Content-Type": "application/json", "Authorization": f"Bearer {token}", } - requests.post( + _send_request( + RequestTypes.post, f"{url}/api/auth/logout", headers=headers, timeout=timeout, @@ -792,7 +914,10 @@ def get_user_info_by_token( else: auth_headers.append(user_item) - request_func = requests.get if session is None else session.get + if session is None: + request_func = functools.partial(_send_request, RequestTypes.get) + else: + request_func = session.get for header_value, is_service in auth_headers: headers = base_headers.copy() headers.update(header_value) @@ -1415,13 +1540,8 @@ def take_web_action_event( dict[str, Any]: Web action event. """ - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None - - response = requests.get( + response = _send_request( + RequestTypes.get, f"{server_url}/api/actions/take/{action_token}", verify=verify, cert=cert, @@ -1454,13 +1574,8 @@ def abort_web_action_event( requests.Response: Response from server. """ - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None - - response = requests.post( + response = _send_request( + RequestTypes.post, f"{server_url}/api/actions/abort/{action_token}", json={"message": reason}, verify=verify, From a6a9b8a5e13e28083d94f6d95d5d1beb87082bf6 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 11:15:13 +0100 Subject: [PATCH 6/8] Make SSL helpers used by other modules public Co-Authored-By: Claude Opus 5.5 --- ayon_api/events.py | 4 ++-- ayon_api/server_api.py | 16 ++++++++-------- ayon_api/utils.py | 26 +++++++++++++------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ayon_api/events.py b/ayon_api/events.py index 6847c5e70..318e828a8 100644 --- a/ayon_api/events.py +++ b/ayon_api/events.py @@ -16,7 +16,7 @@ from websocket import ABNF, WebSocketTimeoutException from .exceptions import UnauthorizedError -from .utils import RequestTypes, _send_request +from .utils import RequestTypes, send_request if typing.TYPE_CHECKING: from websocket import WebSocket @@ -892,7 +892,7 @@ def _check_token(self) -> bool | None: {"X-Api-Key": token}, ): try: - response = _send_request( + response = send_request( RequestTypes.get, url, headers=headers, diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index 832e299d2..0b0059b8d 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -77,9 +77,9 @@ fill_own_attribs, UserInfo, get_user_info_by_token, - _get_requests_ca_bundle, - _create_ssl_context, - _apply_ssl_verify, + get_requests_ca_bundle, + create_ssl_context, + apply_ssl_verify, ) from ._api_helpers import ( InstallersAPI, @@ -496,7 +496,7 @@ def set_ssl_verify( self._ssl_verify = ssl_verify for session in (self._session, self._validation_session): if session is not None: - _apply_ssl_verify(session, ssl_verify) + apply_ssl_verify(session, ssl_verify) def get_cert(self) -> str | None: """Current cert file used for connection to server. @@ -996,7 +996,7 @@ def create_session( if session is None: session = requests.Session() session.cert = self._cert - _apply_ssl_verify(session, self._ssl_verify) + apply_ssl_verify(session, self._ssl_verify) session.headers.update(self.get_headers()) self._session_functions_mapping = { @@ -1036,7 +1036,7 @@ def _get_validation_session(self) -> requests.Session: if self._validation_session is None: session = requests.Session() session.cert = self._cert - _apply_ssl_verify(session, self._ssl_verify) + apply_ssl_verify(session, self._ssl_verify) self._validation_session = session return self._validation_session @@ -1099,11 +1099,11 @@ def create_websocket( if isinstance(ca_bundle, str): # Trust default CA bundle too, same as REST requests do if "context" not in prepared_sslopt: - prepared_sslopt["context"] = _create_ssl_context( + prepared_sslopt["context"] = create_ssl_context( ca_bundle ) else: - ca_bundle = _get_requests_ca_bundle() + ca_bundle = get_requests_ca_bundle() if ca_bundle: key = "ca_certs" if os.path.isdir(ca_bundle): diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 67cbf2d03..b05557958 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -587,7 +587,7 @@ def prepare_entity_changes( return changes -def _get_requests_ca_bundle() -> str | None: +def get_requests_ca_bundle() -> str | None: """CA bundle used by 'requests' for ssl verification. Returns: @@ -607,7 +607,7 @@ def _get_requests_ca_bundle() -> str | None: return None -def _create_ssl_context(ca_path: str) -> ssl.SSLContext: +def create_ssl_context(ca_path: str) -> ssl.SSLContext: """SSL context trusting default CA bundle and CA certificates from path. A CA file passed to 'requests' or 'websocket' replaces their default @@ -622,7 +622,7 @@ def _create_ssl_context(ca_path: str) -> ssl.SSLContext: ssl.SSLContext: SSL context for client connections. """ - default_bundle = _get_requests_ca_bundle() + default_bundle = get_requests_ca_bundle() if default_bundle and os.path.isdir(default_bundle): context = ssl.create_default_context(capath=default_bundle) elif default_bundle: @@ -648,7 +648,7 @@ class _CAFileAdapter(requests.adapters.HTTPAdapter): """ def __init__(self, ca_path: str, **kwargs) -> None: - ssl_context = _create_ssl_context(ca_path) + ssl_context = create_ssl_context(ca_path) # 'urllib3' matches hostname on its own, same as with its default # context. Python's check fails for IP addresses on 'urllib3' 1.x, # which does not send server hostname for them. @@ -665,7 +665,7 @@ def proxy_manager_for(self, proxy, **proxy_kwargs): return super().proxy_manager_for(proxy, **proxy_kwargs) -def _apply_ssl_verify( +def apply_ssl_verify( session: requests.Session, ssl_verify: bool | str | None ) -> None: """Set 'verify' of session and mount adapter for CA path.""" @@ -682,7 +682,7 @@ def _apply_ssl_verify( current.close() -def _send_request( +def send_request( request_type: RequestType, url: str, verify: str | bool | None = None, @@ -716,7 +716,7 @@ def _send_request( session = requests.Session() try: session.cert = cert - _apply_ssl_verify(session, verify) + apply_ssl_verify(session, verify) func = getattr(session, request_type.name.lower()) return func(url, **kwargs) finally: @@ -742,7 +742,7 @@ def _try_connect_to_server( try: # TODO add validation if the url lead to AYON server # - this won't validate if the url lead to 'google.com' - response = _send_request( + response = send_request( RequestTypes.get, f"{url}/api/info", timeout=timeout, @@ -792,7 +792,7 @@ def login_to_server( timeout = get_default_timeout() headers = {"Content-Type": "application/json"} - response = _send_request( + response = send_request( RequestTypes.post, f"{url}/api/auth/login", headers=headers, @@ -841,7 +841,7 @@ def logout_from_server( "Content-Type": "application/json", "Authorization": f"Bearer {token}", } - _send_request( + send_request( RequestTypes.post, f"{url}/api/auth/logout", headers=headers, @@ -915,7 +915,7 @@ def get_user_info_by_token( auth_headers.append(user_item) if session is None: - request_func = functools.partial(_send_request, RequestTypes.get) + request_func = functools.partial(send_request, RequestTypes.get) else: request_func = session.get for header_value, is_service in auth_headers: @@ -1540,7 +1540,7 @@ def take_web_action_event( dict[str, Any]: Web action event. """ - response = _send_request( + response = send_request( RequestTypes.get, f"{server_url}/api/actions/take/{action_token}", verify=verify, @@ -1574,7 +1574,7 @@ def abort_web_action_event( requests.Response: Response from server. """ - response = _send_request( + response = send_request( RequestTypes.post, f"{server_url}/api/actions/abort/{action_token}", json={"message": reason}, From 94689d80699975939f3563d9c40b347311ed223c Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 9 Nov 2026 11:51:32 +0100 Subject: [PATCH 7/8] Resolve SSL arguments in one function 'resolve_ssl_args' fills 'None' values from 'AYON_CA_FILE' and 'AYON_CERT_FILE' and documents that 'verify=True' and 'cert=""' skip them, e.g. for a process connected to multiple servers. Co-Authored-By: Claude Opus 5.5 --- ayon_api/server_api.py | 21 +++++------------ ayon_api/utils.py | 52 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index 0b0059b8d..dbd937cb0 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -80,6 +80,7 @@ get_requests_ca_bundle, create_ssl_context, apply_ssl_verify, + resolve_ssl_args, ) from ._api_helpers import ( InstallersAPI, @@ -324,12 +325,10 @@ class ServerAPI( sender (str | None): Sender of requests, more specific than sender type (e.g. machine name). Used in server logs and propagated into events. - ssl_verify (bool | str | None): Verify SSL certificate - Looks for env variable value ``AYON_CA_FILE`` by default. If not - available then 'True' is used. A path to CA file or directory - is trusted in addition to the default CA bundle. - cert (str | None): Path to certificate file. Looks for env - variable value ``AYON_CERT_FILE`` by default. + ssl_verify (bool | str | None): Verify SSL certificate, see + 'resolve_ssl_args' for possible values. + cert (str | None): Path to client certificate file, see + 'resolve_ssl_args' for possible values. create_session (bool): Create session for connection if token is available. Default is True. timeout (float | None): Timeout for requests. @@ -385,15 +384,7 @@ def __init__( self.set_timeout(timeout) self.set_max_retries(max_retries) - if ssl_verify is None: - # Custom AYON env variable for CA file or 'True' - # - that should cover most default behaviors in 'requests' - # with 'certifi' - ssl_verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") - + ssl_verify, cert = resolve_ssl_args(ssl_verify, cert) self._ssl_verify = ssl_verify self._cert = cert diff --git a/ayon_api/utils.py b/ayon_api/utils.py index b05557958..6e4f831cd 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -682,6 +682,46 @@ def apply_ssl_verify( current.close() +def resolve_ssl_args( + verify: str | bool | None, + cert: str | None, +) -> tuple[str | bool, str | None]: + """Resolve SSL verification and client certificate for requests. + + 'None' values are filled from environment variables: + - 'verify' from 'AYON_CA_FILE', 'True' if it is not set. + - 'cert' from 'AYON_CERT_FILE', no client certificate if it is not set. + + Other values skip the environment variables. That is useful e.g. for + a process connected to multiple servers, where only some of them + use the CA file: + - 'verify=True' uses only the default CA bundle. + - 'verify=False' disables verification. + - 'verify=""' trusts the CA file (or directory) in addition to + the default CA bundle. + - 'cert=""' does not use any client certificate. + - 'cert=""' uses the client certificate. + + Empty string 'cert' is kept, so passing the result to other functions + does not fill it from the environment again. + + Args: + verify (str | bool | None): SSL verification. + cert (str | None): Path to client certificate file. + + Returns: + tuple[str | bool, str | None]: SSL verification and client + certificate. + + """ + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + return verify, cert + + def send_request( request_type: RequestType, url: str, @@ -707,11 +747,7 @@ def send_request( requests.Response: Response from server. """ - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None + verify, cert = resolve_ssl_args(verify, cert) session = requests.Session() try: @@ -895,11 +931,7 @@ def get_user_info_by_token( if timeout is None: timeout = get_default_timeout() - if verify is None: - verify = os.environ.get("AYON_CA_FILE") or True - - if cert is None: - cert = os.environ.get("AYON_CERT_FILE") or None + verify, cert = resolve_ssl_args(verify, cert) base_headers = { "Content-Type": "application/json", From ebd1876397594841792f77c34ca7cbd863c16c42 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:37:12 +0100 Subject: [PATCH 8/8] Fix client certificate and hostname checks with CA file - Websocket loads client certificate to the custom SSL context, because 'websocket' ignores 'certfile' when context is passed. - 'set_cert' recreates CA file adapter, 'urllib3' loads client certificate to its shared SSL context so the previous one was sent. - SSL context does not match 'commonName' of certificates without 'subjectAltName', same as 'urllib3' 2 default context. - Error of CA file that can't be loaded contains its path. Co-Authored-By: Claude Opus 5.5 --- ayon_api/server_api.py | 11 ++++++++--- ayon_api/utils.py | 27 +++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index dbd937cb0..947c6b864 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -511,6 +511,9 @@ def set_cert(self, cert: str | None) -> None: for session in (self._session, self._validation_session): if session is not None: session.cert = cert + # 'urllib3' loads client cert to SSL context of CA file + # adapter, new adapter does not send the previous one + apply_ssl_verify(session, self._ssl_verify) ssl_verify = property(get_ssl_verify, set_ssl_verify) cert = property(get_cert, set_cert) @@ -1090,9 +1093,11 @@ def create_websocket( if isinstance(ca_bundle, str): # Trust default CA bundle too, same as REST requests do if "context" not in prepared_sslopt: - prepared_sslopt["context"] = create_ssl_context( - ca_bundle - ) + context = create_ssl_context(ca_bundle) + # 'websocket' ignores 'certfile' with custom context + if self._cert: + context.load_cert_chain(self._cert) + prepared_sslopt["context"] = context else: ca_bundle = get_requests_ca_bundle() if ca_bundle: diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 6e4f831cd..36be18bce 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -621,6 +621,9 @@ def create_ssl_context(ca_path: str) -> ssl.SSLContext: Returns: ssl.SSLContext: SSL context for client connections. + Raises: + OSError: CA certificates could not be loaded from the path. + """ default_bundle = get_requests_ca_bundle() if default_bundle and os.path.isdir(default_bundle): @@ -629,11 +632,20 @@ def create_ssl_context(ca_path: str) -> ssl.SSLContext: context = ssl.create_default_context(cafile=default_bundle) else: context = ssl.create_default_context() + # Same as 'urllib3' 2 default context, certificate without + # 'subjectAltName' is not matched by its 'commonName' + context.hostname_checks_common_name = False - if os.path.isdir(ca_path): - context.load_verify_locations(capath=ca_path) - else: - context.load_verify_locations(cafile=ca_path) + try: + if os.path.isdir(ca_path): + context.load_verify_locations(capath=ca_path) + else: + context.load_verify_locations(cafile=ca_path) + except OSError as exc: + # Errors of 'ssl' do not contain the path + raise OSError( + f"Could not load CA certificates from '{ca_path}': {exc}" + ) from exc return context @@ -643,6 +655,13 @@ class _CAFileAdapter(requests.adapters.HTTPAdapter): Used when 'ssl_verify' is a path. The session still has 'verify' set to the path, which 'urllib3' loads to the context too. + All connections of the adapter share one SSL context, and 'urllib3' + loads the client certificate and 'verify' path of each request + into it. Create a new adapter when the client certificate changes + ('apply_ssl_verify'). Passing 'verify' per request does not + restrict trust to it, and other path adds CA to the context for + all following connections. + Args: ca_path (str): Path to CA file or directory with CA certificates.