(sap) fix after bot review

This commit is contained in:
Vasilisa Parshikova 2026-03-05 19:48:20 +04:00 committed by Sameer Kankute
parent 46968480c4
commit 2723c69bfe
No known key found for this signature in database
2 changed files with 14 additions and 19 deletions

View file

@ -9,9 +9,9 @@ import os
import tempfile
import httpx
from litellm import sap_service_key
from litellm.llms.custom_httpx.http_handler import _get_httpx_client, HTTPHandler
from litellm._logging import verbose_logger
import litellm
AUTH_ENDPOINT_SUFFIX = "/oauth/token"
@ -175,7 +175,7 @@ def extract_credentials(source: Source, exclude: Optional[List[str]] = None) ->
if cv.name in exclude:
continue
value = source.get(cv)
if value:
if value is not None:
credentials[cv.name] = cv.transform_fn(value) if cv.transform_fn else value
return credentials
@ -216,7 +216,7 @@ def fetch_credentials(service_key: Optional[Union[str, dict]] = None, profile: O
"""
config = init_conf(profile)
service_key = service_key or sap_service_key or _load_json_env(SERVICE_KEY_ENV_VAR)
service_key = service_key or litellm.sap_service_key or _load_json_env(SERVICE_KEY_ENV_VAR)
vcap_service = _get_vcap_service(VCAP_AICORE_SERVICE_NAME)
sources = [
@ -271,7 +271,7 @@ def validate_credentials(
]
if sum(bool(m) for m in modes) != 1:
raise ValueError(
"SAP AI Core credentials are incomplete."
"SAP AI Core credentials are incomplete. "
"Invalid credentials: provide exactly one of client_secret, "
"(cert_str & key_str), or (cert_file_path & key_file_path)."
)

View file

@ -31,35 +31,32 @@ mock_sap_vcap_service_key_dict = {
}
}]
}
def _prep_env(monkeypatch):
for var in ("AICORE_CLIENT_ID", "AICORE_CLIENT_SECRET", "AICORE_AUTH_URL",
"AICORE_BASE_URL", "AICORE_CERT_URL", "AICORE_SERVICE_KEY", "VCAP_SERVICES"):
monkeypatch.delenv(var, raising=False)
monkeypatch.setenv("AICORE_HOME", 'notexist')
def test_sap_fetch_creds_from_env_service_key(monkeypatch):
monkeypatch.setattr(sap_credentials, "sap_service_key", None)
monkeypatch.setenv("AICORE_HOME", 'notexist')
_prep_env(monkeypatch)
monkeypatch.setenv("AICORE_SERVICE_KEY", json.dumps(mock_sap_service_key_dict))
creds = sap_credentials.fetch_credentials()
assert creds == expected_creds
def test_sap_fetch_creds_from_arg_service_key(monkeypatch):
monkeypatch.setattr(sap_credentials, "sap_service_key", None)
monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False)
monkeypatch.setenv("AICORE_HOME", 'notexist')
_prep_env(monkeypatch)
creds = sap_credentials.fetch_credentials(service_key=json.dumps(mock_sap_service_key_dict))
assert creds == expected_creds
def test_fetch_creds_from_env_vcap_service(monkeypatch):
monkeypatch.setattr(sap_credentials, "sap_service_key", None)
monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False)
monkeypatch.setenv("AICORE_HOME", 'notexist')
_prep_env(monkeypatch)
monkeypatch.setenv("VCAP_SERVICES", json.dumps(mock_sap_vcap_service_key_dict))
creds = sap_credentials.fetch_credentials()
assert creds['client_id'] == "vcap-clientid"
assert creds['client_secret'] == "vcap-clientsecret"
def test_fetch_creds_from_env(monkeypatch):
monkeypatch.setattr(sap_credentials, "sap_service_key", None)
monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False)
monkeypatch.delenv("VCAP_SERVICES", raising=False)
monkeypatch.setenv("AICORE_HOME", 'notexist')
_prep_env(monkeypatch)
monkeypatch.setenv("AICORE_CLIENT_ID", "env-client-id")
monkeypatch.setenv("AICORE_CLIENT_SECRET", "env-client-secret")
monkeypatch.setenv("AICORE_AUTH_URL", "env-auth-url")
@ -75,9 +72,7 @@ def test_fetch_creds_from_env(monkeypatch):
assert creds['resource_group'] == "env-resource-group"
def test_creds_priority_order(monkeypatch):
monkeypatch.setattr(sap_credentials, "sap_service_key", None)
monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False)
monkeypatch.setenv("AICORE_HOME", 'notexist')
_prep_env(monkeypatch)
monkeypatch.setenv("AICORE_CLIENT_ID", "env-client-id")
monkeypatch.setenv("AICORE_CLIENT_SECRET", "env-client-secret")
monkeypatch.setenv("AICORE_AUTH_URL", "env-auth-url")