feat: 统一 SSL 证书配置并支持用户创建时设置访问权限

- 新增 ssl_utils.py 模块,统一使用 certifi 证书
- 将所有 httpx 客户端的 verify 参数改为使用 get_ssl_context()
- 管理员创建用户时支持设置 allowed_providers/api_formats/models
- 修复 provider_ops 敏感字段列表缺失 session_cookie
This commit is contained in:
fawney19
2026-01-19 17:56:02 +08:00
parent 6bc9cdc69d
commit 0265849ed3
17 changed files with 88 additions and 35 deletions

View File

@@ -14,6 +14,7 @@ from src.clients import get_redis_client
from src.core.logger import logger
from src.models.database import User
from src.utils.auth_utils import require_admin
from src.utils.ssl_utils import get_ssl_context
router = APIRouter()
@@ -118,7 +119,7 @@ async def get_external_models(_: User = Depends(require_admin)) -> JSONResponse:
# 从 models.dev 获取数据
try:
async with httpx.AsyncClient(timeout=30.0) as client:
async with httpx.AsyncClient(timeout=30.0, verify=get_ssl_context()) as client:
response = await client.get("https://models.dev/api.json")
response.raise_for_status()
data = response.json()

View File

@@ -22,6 +22,7 @@ from src.services.model.upstream_fetcher import (
fetch_models_from_endpoints,
)
from src.utils.auth_utils import get_current_user
from src.utils.ssl_utils import get_ssl_context
router = APIRouter(prefix="/api/admin/provider-query", tags=["Provider Query"])
@@ -302,7 +303,7 @@ async def test_model(
}
# 发送测试请求
async with httpx.AsyncClient(timeout=endpoint_config["timeout"]) as client:
async with httpx.AsyncClient(timeout=endpoint_config["timeout"], verify=get_ssl_context()) as client:
# 非流式测试
logger.debug(f"[test-model] 开始非流式测试...")

View File

@@ -227,6 +227,11 @@ class AdminCreateUserAdapter(AdminApiAdapter):
else:
quota_usd = SystemConfigService.get_config(db, "default_user_quota_usd", default=10.0)
# 处理访问权限字段:空数组转为 None表示无限制
allowed_providers = request.allowed_providers if request.allowed_providers else None
allowed_api_formats = request.allowed_api_formats if request.allowed_api_formats else None
allowed_models = request.allowed_models if request.allowed_models else None
try:
user = UserService.create_user(
db=db,
@@ -235,6 +240,9 @@ class AdminCreateUserAdapter(AdminApiAdapter):
password=request.password,
role=role,
quota_usd=quota_usd,
allowed_providers=allowed_providers,
allowed_api_formats=allowed_api_formats,
allowed_models=allowed_models,
)
except ValueError as exc:
raise InvalidRequestException(str(exc))

View File

@@ -28,6 +28,7 @@ import httpx
from src.core.logger import logger
from src.core.headers import CORE_REDACT_HEADERS, merge_headers_with_protection, redact_headers_for_log
from src.utils.ssl_utils import get_ssl_context
def _redact_headers(headers: Dict[str, str]) -> Dict[str, str]:
@@ -545,7 +546,7 @@ class HttpRequestExecutor:
try:
# 使用httpx进行异步请求
async with httpx.AsyncClient(timeout=self.timeout) as client:
async with httpx.AsyncClient(timeout=self.timeout, verify=get_ssl_context()) as client:
response = await client.post(
url=request.url,
json=request.json_body,

View File

@@ -20,6 +20,7 @@ from src.database.database import get_pool_status
from src.models.database import Model, Provider
from src.services.orchestration.fallback_orchestrator import FallbackOrchestrator
from src.services.provider.transport import build_provider_url
from src.utils.ssl_utils import get_ssl_context
router = APIRouter(tags=["System Catalog"])
@@ -281,7 +282,7 @@ async def test_connection(
is_stream=False,
)
async with httpx.AsyncClient(timeout=30.0) as client:
async with httpx.AsyncClient(timeout=30.0, verify=get_ssl_context()) as client:
resp = await client.post(url, json=provider_payload, headers=provider_headers)
resp.raise_for_status()
return resp.json()