Skip to content

Commit 72976c3

Browse files
authored
improvement: handle SSL configuration for other openai compat config. (#6907)
1. handle SSL configuration for other openai compat config. 2. Update `ca_bundle_path` to use the `SSL_CERT_FILE` environment variable as a fallback if not provided in the configuration.
1 parent 4255117 commit 72976c3

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

marimo/_server/ai/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,16 @@ def _for_openai_like(
124124
ai_config, name, fallback_key=fallback_key, require_key=require_key
125125
)
126126

127+
# Use SSL_CERT_FILE environment variable as fallback for ca_bundle_path
128+
ca_bundle_path = ai_config.get("ca_bundle_path") or cls.os_key(
129+
"SSL_CERT_FILE"
130+
)
131+
127132
kwargs: dict[str, Any] = {
128133
"base_url": _get_base_url(ai_config) or fallback_base_url,
129134
"api_key": key,
130135
"ssl_verify": ai_config.get("ssl_verify", True),
131-
"ca_bundle_path": ai_config.get("ca_bundle_path", None),
136+
"ca_bundle_path": ca_bundle_path,
132137
"client_pem": ai_config.get("client_pem", None),
133138
"extra_headers": ai_config.get("extra_headers", None),
134139
"tools": _get_tools(config.get("mode", "manual")),

tests/_server/ai/test_ai_config.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,77 @@ def test_for_model_with_autocomplete_model(self) -> None:
974974
assert provider_config.tools is None
975975

976976

977+
class TestSSLConfiguration:
978+
"""Tests for SSL configuration across all OpenAI-like providers."""
979+
980+
@pytest.mark.parametrize(
981+
("provider_name", "provider_method", "api_key_config"),
982+
[
983+
("openai", "for_openai", {"open_ai": {"api_key": "test-key"}}),
984+
("github", "for_github", {"github": {"api_key": "test-key"}}),
985+
("ollama", "for_ollama", {"ollama": {"api_key": "test-key"}}),
986+
],
987+
)
988+
def test_ssl_config_from_provider_config(
989+
self,
990+
provider_name: str,
991+
provider_method: str,
992+
api_key_config: AiConfig,
993+
) -> None:
994+
"""Test SSL configuration is read from provider config."""
995+
# Get the provider key from api_key_config
996+
provider_key = next(iter(api_key_config.keys()))
997+
998+
config: AiConfig = {
999+
**api_key_config,
1000+
}
1001+
config[provider_key]["ssl_verify"] = False
1002+
config[provider_key]["ca_bundle_path"] = "/custom/path/to/ca.pem"
1003+
config[provider_key]["client_pem"] = "/custom/path/to/client.pem"
1004+
config[provider_key]["extra_headers"] = {"X-Custom": "header"}
1005+
1006+
method = getattr(AnyProviderConfig, provider_method)
1007+
provider_config = method(config)
1008+
1009+
assert provider_config.ssl_verify is False, (
1010+
f"{provider_name}: ssl_verify should be False"
1011+
)
1012+
assert provider_config.ca_bundle_path == "/custom/path/to/ca.pem", (
1013+
f"{provider_name}: ca_bundle_path should match"
1014+
)
1015+
assert provider_config.client_pem == "/custom/path/to/client.pem", (
1016+
f"{provider_name}: client_pem should match"
1017+
)
1018+
assert provider_config.extra_headers == {"X-Custom": "header"}, (
1019+
f"{provider_name}: extra_headers should match"
1020+
)
1021+
1022+
@pytest.mark.parametrize(
1023+
("provider_name", "provider_method", "api_key_config"),
1024+
[
1025+
("openai", "for_openai", {"open_ai": {"api_key": "test-key"}}),
1026+
("github", "for_github", {"github": {"api_key": "test-key"}}),
1027+
("ollama", "for_ollama", {"ollama": {"api_key": "test-key"}}),
1028+
],
1029+
)
1030+
@patch.dict(os.environ, {"SSL_CERT_FILE": "/env/path/to/ca.pem"})
1031+
def test_ssl_cert_file_fallback(
1032+
self,
1033+
provider_name: str,
1034+
provider_method: str,
1035+
api_key_config: AiConfig,
1036+
) -> None:
1037+
"""Test SSL_CERT_FILE environment variable is used as fallback."""
1038+
config: AiConfig = {**api_key_config}
1039+
1040+
method = getattr(AnyProviderConfig, provider_method)
1041+
provider_config = method(config)
1042+
1043+
assert provider_config.ca_bundle_path == "/env/path/to/ca.pem", (
1044+
f"{provider_name}: should use SSL_CERT_FILE env var as fallback"
1045+
)
1046+
1047+
9771048
class TestEdgeCases:
9781049
"""Tests for edge cases and error conditions."""
9791050

0 commit comments

Comments
 (0)