OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
fix: validate Azure OpenAI Foundry keys before saving (#2373)
Edwin Jose5cf69da52cac
diff --git a/frontend/app/settings/_components/provider-settings-dialog.tsx b/frontend/app/settings/_components/provider-settings-dialog.tsxindex 552e33713..b04e24ec8 100644--- a/frontend/app/settings/_components/provider-settings-dialog.tsx+++ b/frontend/app/settings/_components/provider-settings-dialog.tsx@@ -40,10 +40,9 @@ const EMPTY_FIELDS: CatalogCredentialField[] = [];* generic `provider_credentials` payload, so adding a provider row to* `config/model_providers.yaml` is enough to make it configurable here.*- * There is no live key check on save — OpenRAG has no generic- * "list this provider's models" endpoint to probe with. The credentials are- * validated the first time a model from this provider is selected in Agent or- * Ingestion settings, which reports the provider's own error.+ * Most generic providers have no credential-only check on save; they are+ * validated when a model is selected in Agent or Ingestion settings. Azure+ * OpenAI is an exception: the backend probes its key before persisting it.*/const ProviderSettingsDialog = ({provider,diff --git a/src/api/provider_validation.py b/src/api/provider_validation.pyindex abad24f2b..3f72852b8 100644--- a/src/api/provider_validation.py+++ b/src/api/provider_validation.py@@ -990,8 +990,8 @@ async def _test_azure_lightweight_health(credentials: dict[str, str]) -> None:"""Validate Azure OpenAI credentials without selecting or billing a deployment.Azure deployment enumeration is an Azure Resource Manager operation, not an- Azure OpenAI data-plane operation. The data plane does provide ``models``;- use that endpoint so a valid resource is not rejected with ResourceNotFound.+ Azure OpenAI data-plane operation. Both Azure OpenAI Service and Foundry+ resource roots expose authenticated, read-only model listing routes."""api_base = credentials.get("api_base")api_version = credentials.get("api_version")@@ -1030,11 +1030,19 @@ async def _test_azure_lightweight_health(credentials: dict[str, str]) -> None:headers["Authorization"] = f"Bearer {access_token}"else:headers["api-key"] = api_key or ""+ if is_azure_ai_foundry_endpoint(api_base):+ # Azure OpenAI deployments on Foundry resource roots use the OpenAI v1+ # route. The older /models/info route excludes Azure OpenAI endpoints.+ url = f"{api_base.rstrip('/')}/openai/v1/models"+ params = None+ else:+ url = f"{api_base.rstrip('/')}/openai/models"+ params = {"api-version": api_version or "2024-10-21"}response = await _http_request_with_retry("GET",- f"{api_base.rstrip('/')}/openai/models",+ url,headers=headers,- params={"api-version": api_version or "2024-10-21"},+ params=params,timeout=30.0,)if response.status_code != 200:diff --git a/src/api/settings/endpoints.py b/src/api/settings/endpoints.pyindex 212f1459d..773e6dd37 100644--- a/src/api/settings/endpoints.py+++ b/src/api/settings/endpoints.py@@ -14,7 +14,6 @@ from fastapi import Depends, HTTPException, Requestfrom fastapi.responses import JSONResponsefrom api.provider_validation import (- is_azure_ai_foundry_endpoint,sanitize_provider_error_content,validate_provider_setup,)@@ -462,10 +461,8 @@ async def update_settings(logger.info("Running provider validation before modifying config")# A generic provider save normally has no selected model to- # probe. Native Azure OpenAI is the exception: its models route- # validates the endpoint and API key without consuming tokens.- # Foundry resource URLs instead follow LiteLLM's model route,- # preserving the behavior they had on main.+ # probe. Azure is the exception: both OpenAI Service and+ # Foundry resource endpoints have a read-only auth probe.for provider, submitted in (body.provider_credentials or {}).items():provider_key = _provider_key(provider)credentials = current_config.providers.pending_credentials(@@ -486,14 +483,12 @@ async def update_settings()if missing:raise ValueError(f"{', '.join(missing)} is required for Azure OpenAI")- if not is_azure_ai_foundry_endpoint(credentials.get("api_base")):- await validate_provider_setup(- provider=provider_key,- api_key=credentials.get("api_key")- or credentials.get("azure_ad_token"),- endpoint=credentials.get("api_base"),- credentials=credentials,- )+ await validate_provider_setup(+ provider=provider_key,+ api_key=credentials.get("api_key") or credentials.get("azure_ad_token"),+ endpoint=credentials.get("api_base"),+ credentials=credentials,+ )# Validate LLM provider if being changedif body.llm_provider is not None or body.llm_model is not None:diff --git a/tests/unit/api/test_settings_endpoints.py b/tests/unit/api/test_settings_endpoints.pyindex 169d4565e..2702be8f0 100644--- a/tests/unit/api/test_settings_endpoints.py+++ b/tests/unit/api/test_settings_endpoints.py@@ -5,6 +5,7 @@ Validates error handling in update_docling_preset endpoint.from unittest.mock import AsyncMock, MagicMock, patch+import httpximport pytestfrom fastapi import HTTPExceptionfrom pydantic import ValidationError@@ -258,6 +259,51 @@ async def test_update_settings_validates_azure_credentials_before_saving():save.assert_not_called()+async def test_update_settings_rejects_invalid_azure_foundry_key_before_saving():+ """The Azure OpenAI form must probe Foundry resource keys on Save too."""+ from api.settings.endpoints import update_settings+ from config.config_manager import OpenRAGConfig++ config = OpenRAGConfig.from_dict({})+ config.edited = True+ body = SettingsUpdateBody(+ provider_credentials={+ "azure": {+ "api_key": "wrong-key",+ "api_base": "https://example.services.ai.azure.com",+ }+ },+ provider_auth_methods={"azure": "api_key"},+ )+ rbac = MagicMock()+ rbac.has_permission = AsyncMock(return_value=True)++ with (+ patch("api.settings.endpoints.get_openrag_config", return_value=config),+ patch(+ "api.provider_validation._http_request_with_retry",+ new_callable=AsyncMock,+ return_value=httpx.Response(+ 401, json={"error": {"message": "Invalid subscription key"}}+ ),+ ) as request,+ patch("api.settings.endpoints.config_manager.save_config_file") as save,+ ):+ response = await update_settings(+ body=body,+ session_manager=AsyncMock(),+ user=MagicMock(spec=User),+ models_service=MagicMock(),+ rbac=rbac,+ )++ assert response.status_code == 400+ assert b"Invalid subscription key" in response.body+ request.assert_awaited_once()+ save.assert_not_called()++@pytest.mark.parametrize("provider",["openai", "watsonx", "anthropic", "local", "ollama", "azure", "azure_ai", "openai_like"],diff --git a/tests/unit/test_openai_provider_check_retry.py b/tests/unit/test_openai_provider_check_retry.pyindex 340694f3e..4ce0e6b6c 100644--- a/tests/unit/test_openai_provider_check_retry.py+++ b/tests/unit/test_openai_provider_check_retry.py@@ -156,6 +156,29 @@ async def test_azure_lightweight_health_checks_endpoint_and_key(monkeypatch):assert captured["params"] == {"api-version": "2024-10-21"}+async def test_azure_foundry_lightweight_health_checks_endpoint_and_key(monkeypatch):+ captured = {}++ async def fake_retry(method, url, **kwargs):+ captured.update(method=method, url=url, **kwargs)+ return httpx.Response(200, json={"data": [{"id": "test-model"}]})++ monkeypatch.setattr("api.provider_validation._http_request_with_retry", fake_retry)++ await _test_azure_lightweight_health(+ {+ "api_key": "azure-key",+ "api_base": "https://example.services.ai.azure.com/",+ }+ )++ assert captured["method"] == "GET"+ assert captured["url"] == "https://example.services.ai.azure.com/openai/v1/models"+ assert captured["headers"]["api-key"] == "azure-key"+ assert captured["params"] is None++@pytest.mark.asyncioasync def test_azure_lightweight_health_rejects_missing_credentials():with pytest.raises(ValueError, match="Azure credentials are required"):