Spaces:
Sleeping
Sleeping
Work OS validated demo ee2b491ed664 (part 2)
Browse files- LLMGateway/README.md +9 -1
- LLMGateway/credentials.py +59 -0
- LLMGateway/service.py +9 -3
- LLMGateway/tests/test_credentials.py +51 -0
LLMGateway/README.md
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
-
# LLM Gateway 工程 harness
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
本目录包含[系统设计](../docs/14_LLM_Gateway_系统设计.md)与 v0.3 实现。`routing.json` 固定三档模型;`routing.schema.json` 是配置契约;`contracts.schema.json` 定义内部 Port DTO;`examples.json` 提供正反例;`compatibility-matrix.json` 跟踪目标国产平台的待验证证据。
|
| 4 |
|
|
|
|
| 1 |
+
# LLM Gateway 工程 harness
|
| 2 |
+
|
| 3 |
+
## 指定 HF Space 的密文凭据
|
| 4 |
+
|
| 5 |
+
`dellweb3/workos-office` 使用 AES-256-GCM 信封加密:`WORKOS_BAILIAN_KEY_ENVELOPE` Secret 仅保存密文、随机96位nonce和版本;独立的 `WORKOS_BAILIAN_WRAP_KEY` Secret 保存随机256位解密密钥。认证关联数据包含固定用途和完整 Space ID,启动时必须与 HF 注入的 `SPACE_ID` 匹配。缺失、篡改、错误密钥或其他空间均拒绝解密,不降级使用旧的原始 API key。原始 `DASHSCOPE_API_KEY` 不保留在该 Space 的 Secrets、仓库、镜像或日志。
|
| 6 |
+
|
| 7 |
+
Space ID 是公开的校验上下文,不能单独当作加密密钥。解密密钥也必须留在 Secrets,不进入源码。运行时调用百炼需要在服务端内存中解密;拥有该 Space 执行权限的人仍可能读取运行时凭据,此方案不提供对 HF 宿主或 Space 管理员的隔离。本地开发继续使用被 Git 忽略的 `.env.local`。
|
| 8 |
+
|
| 9 |
+
更新时先配置成对的加密 Secrets,再发布支持信封解密的代码,验证管理端 `credential_storage=space-bound-aes256-gcm` 后移除旧的原始 Secret并重新验收。管理接口只返回存储模式和绑定 ID,不返回密文、解密密钥或 API key。参考 [AESGCM 官方说明](https://cryptography.io/en/latest/hazmat/primitives/aead/#cryptography.hazmat.primitives.ciphers.aead.AESGCM) 与 [HF Secrets 管理](https://huggingface.co/docs/huggingface_hub/guides/manage-spaces)。
|
| 10 |
|
| 11 |
本目录包含[系统设计](../docs/14_LLM_Gateway_系统设计.md)与 v0.3 实现。`routing.json` 固定三档模型;`routing.schema.json` 是配置契约;`contracts.schema.json` 定义内部 Port DTO;`examples.json` 提供正反例;`compatibility-matrix.json` 跟踪目标国产平台的待验证证据。
|
| 12 |
|
LLMGateway/credentials.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Space-bound API-key envelopes. Never log keys, envelope values, or plaintext."""
|
| 2 |
+
import base64
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
BOUND_SPACE_ID = "dellweb3/workos-office"
|
| 7 |
+
ENVELOPE_ENV = "WORKOS_BAILIAN_KEY_ENVELOPE"
|
| 8 |
+
WRAP_KEY_ENV = "WORKOS_BAILIAN_WRAP_KEY"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def _aad(space_id):
|
| 12 |
+
return ("workos/bailian-api-key/v1\0" + space_id).encode("utf-8")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def seal_key(api_key, space_id=BOUND_SPACE_ID):
|
| 16 |
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
| 17 |
+
if space_id != BOUND_SPACE_ID or not api_key or any(c.isspace() for c in api_key):
|
| 18 |
+
raise ValueError("Invalid model credential envelope input")
|
| 19 |
+
wrap_key = AESGCM.generate_key(bit_length=256)
|
| 20 |
+
nonce = os.urandom(12)
|
| 21 |
+
ciphertext = AESGCM(wrap_key).encrypt(nonce, api_key.encode("utf-8"), _aad(space_id))
|
| 22 |
+
envelope = {"version": 1, "algorithm": "AES-256-GCM", "space_id": space_id,
|
| 23 |
+
"nonce": base64.b64encode(nonce).decode("ascii"),
|
| 24 |
+
"ciphertext": base64.b64encode(ciphertext).decode("ascii")}
|
| 25 |
+
return json.dumps(envelope, separators=(",", ":")), base64.b64encode(wrap_key).decode("ascii")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def open_key(envelope_text, wrap_key_text, space_id):
|
| 29 |
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
| 30 |
+
try:
|
| 31 |
+
envelope = json.loads(envelope_text)
|
| 32 |
+
if set(envelope) != {"version", "algorithm", "space_id", "nonce", "ciphertext"}:
|
| 33 |
+
raise ValueError()
|
| 34 |
+
if envelope["version"] != 1 or envelope["algorithm"] != "AES-256-GCM":
|
| 35 |
+
raise ValueError()
|
| 36 |
+
if space_id != BOUND_SPACE_ID or envelope["space_id"] != space_id:
|
| 37 |
+
raise ValueError()
|
| 38 |
+
wrap_key = base64.b64decode(wrap_key_text, validate=True)
|
| 39 |
+
nonce = base64.b64decode(envelope["nonce"], validate=True)
|
| 40 |
+
ciphertext = base64.b64decode(envelope["ciphertext"], validate=True)
|
| 41 |
+
if len(wrap_key) != 32 or len(nonce) != 12:
|
| 42 |
+
raise ValueError()
|
| 43 |
+
value = AESGCM(wrap_key).decrypt(nonce, ciphertext, _aad(space_id)).decode("utf-8")
|
| 44 |
+
if not value or any(c.isspace() for c in value):
|
| 45 |
+
raise ValueError()
|
| 46 |
+
return value
|
| 47 |
+
except Exception:
|
| 48 |
+
raise ValueError("Encrypted model credential is invalid or belongs to another Space") from None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def model_api_key(local):
|
| 52 |
+
envelope = os.getenv(ENVELOPE_ENV)
|
| 53 |
+
wrap_key = os.getenv(WRAP_KEY_ENV)
|
| 54 |
+
if envelope is not None or wrap_key is not None:
|
| 55 |
+
# A partial or invalid envelope must fail closed, even if an old raw key exists.
|
| 56 |
+
return open_key(envelope, wrap_key, os.getenv("SPACE_ID"))
|
| 57 |
+
if os.getenv("SPACE_ID") == BOUND_SPACE_ID:
|
| 58 |
+
raise ValueError("This Space requires an encrypted model credential")
|
| 59 |
+
return os.getenv("DASHSCOPE_API_KEY") or local.get("DASHSCOPE_API_KEY", "")
|
LLMGateway/service.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
"""Three-tier server-side Qwen routing. No browser or user-supplied provider credentials."""
|
| 2 |
from __future__ import annotations
|
| 3 |
from observability import traced, span
|
| 4 |
-
from .admission import shared_admission, AdmissionError
|
|
|
|
| 5 |
|
| 6 |
import copy
|
| 7 |
import json
|
|
@@ -146,7 +147,10 @@ class LLMGateway:
|
|
| 146 |
if os.getenv("WORKOS_WORKSPACE_AUTO_SCHEMA", "1") == "1":
|
| 147 |
Base.metadata.create_all(engine)
|
| 148 |
local = local_settings() if mode == "real" else {}
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
| 150 |
self.base_url = verified_base_url(os.getenv("DASHSCOPE_BASE_URL") or local.get("DASHSCOPE_BASE_URL", "")) if mode == "real" else ""
|
| 151 |
if mode == "real" and not self.key:
|
| 152 |
raise LLMError(503, "LLM_CONFIG_INVALID", "百炼 API Key 未配置。")
|
|
@@ -164,7 +168,9 @@ class LLMGateway:
|
|
| 164 |
|
| 165 |
def settings(self, tenant):
|
| 166 |
config, version = self._policy(tenant)
|
| 167 |
-
return {"mode": self.mode, "credential_ready": bool(self.key and self.base_url), "catalog": model_catalog(),
|
|
|
|
|
|
|
| 168 |
"endpoint_host": urlsplit(self.base_url).hostname if self.base_url else None,
|
| 169 |
"version": version, "routing": config, "scheduling": self.scheduler.snapshot() if self.scheduler else {"active":0,"queued":0,"scope":"worker-process","distributed":False}}
|
| 170 |
|
|
|
|
| 1 |
"""Three-tier server-side Qwen routing. No browser or user-supplied provider credentials."""
|
| 2 |
from __future__ import annotations
|
| 3 |
from observability import traced, span
|
| 4 |
+
from .admission import shared_admission, AdmissionError
|
| 5 |
+
from .credentials import model_api_key, ENVELOPE_ENV, BOUND_SPACE_ID
|
| 6 |
|
| 7 |
import copy
|
| 8 |
import json
|
|
|
|
| 147 |
if os.getenv("WORKOS_WORKSPACE_AUTO_SCHEMA", "1") == "1":
|
| 148 |
Base.metadata.create_all(engine)
|
| 149 |
local = local_settings() if mode == "real" else {}
|
| 150 |
+
try:
|
| 151 |
+
self.key = model_api_key(local) if mode == "real" else ""
|
| 152 |
+
except ValueError:
|
| 153 |
+
raise LLMError(503, "LLM_CONFIG_INVALID", "模型加密凭据无效或与当前 Space 不匹配。") from None
|
| 154 |
self.base_url = verified_base_url(os.getenv("DASHSCOPE_BASE_URL") or local.get("DASHSCOPE_BASE_URL", "")) if mode == "real" else ""
|
| 155 |
if mode == "real" and not self.key:
|
| 156 |
raise LLMError(503, "LLM_CONFIG_INVALID", "百炼 API Key 未配置。")
|
|
|
|
| 168 |
|
| 169 |
def settings(self, tenant):
|
| 170 |
config, version = self._policy(tenant)
|
| 171 |
+
return {"mode": self.mode, "credential_ready": bool(self.key and self.base_url), "catalog": model_catalog(),
|
| 172 |
+
"credential_storage": "space-bound-aes256-gcm" if self.mode == "real" and os.getenv(ENVELOPE_ENV) else "server-side",
|
| 173 |
+
"credential_space_id": BOUND_SPACE_ID if self.mode == "real" and os.getenv(ENVELOPE_ENV) else None,
|
| 174 |
"endpoint_host": urlsplit(self.base_url).hostname if self.base_url else None,
|
| 175 |
"version": version, "routing": config, "scheduling": self.scheduler.snapshot() if self.scheduler else {"active":0,"queued":0,"scope":"worker-process","distributed":False}}
|
| 176 |
|
LLMGateway/tests/test_credentials.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import unittest
|
| 5 |
+
from unittest.mock import patch
|
| 6 |
+
from LLMGateway.credentials import seal_key, open_key, model_api_key, BOUND_SPACE_ID, ENVELOPE_ENV, WRAP_KEY_ENV
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class CredentialEnvelopeTests(unittest.TestCase):
|
| 10 |
+
def setUp(self):
|
| 11 |
+
self.raw = "synthetic-credential-only"
|
| 12 |
+
self.envelope, self.wrap = seal_key(self.raw)
|
| 13 |
+
|
| 14 |
+
def test_matching_space_decrypts_without_persisting_plaintext(self):
|
| 15 |
+
self.assertNotIn(self.raw, self.envelope)
|
| 16 |
+
self.assertEqual(open_key(self.envelope, self.wrap, BOUND_SPACE_ID), self.raw)
|
| 17 |
+
second, second_wrap = seal_key(self.raw)
|
| 18 |
+
self.assertNotEqual(self.envelope, second)
|
| 19 |
+
self.assertNotEqual(self.wrap, second_wrap)
|
| 20 |
+
|
| 21 |
+
def test_other_space_and_missing_identity_are_rejected(self):
|
| 22 |
+
for identity in [None, "another/workos-office", "dellweb3/workos-office-copy"]:
|
| 23 |
+
with self.assertRaises(ValueError):
|
| 24 |
+
open_key(self.envelope, self.wrap, identity)
|
| 25 |
+
|
| 26 |
+
def test_tampering_wrong_key_and_binding_change_fail(self):
|
| 27 |
+
other_envelope, other_wrap = seal_key(self.raw)
|
| 28 |
+
envelope=json.loads(self.envelope)
|
| 29 |
+
envelope["ciphertext"]=base64.b64encode(b"changed ciphertext").decode()
|
| 30 |
+
for value, key in [(json.dumps(envelope),self.wrap),(self.envelope,other_wrap)]:
|
| 31 |
+
with self.assertRaises(ValueError):open_key(value,key,BOUND_SPACE_ID)
|
| 32 |
+
envelope=json.loads(self.envelope);envelope['space_id']='other/space'
|
| 33 |
+
with self.assertRaises(ValueError):open_key(json.dumps(envelope),self.wrap,BOUND_SPACE_ID)
|
| 34 |
+
|
| 35 |
+
def test_invalid_envelope_does_not_fall_back_to_raw_key(self):
|
| 36 |
+
with patch.dict(os.environ, {ENVELOPE_ENV:'invalid',WRAP_KEY_ENV:self.wrap,
|
| 37 |
+
'SPACE_ID':BOUND_SPACE_ID,'DASHSCOPE_API_KEY':self.raw}):
|
| 38 |
+
with self.assertRaises(ValueError):model_api_key({})
|
| 39 |
+
|
| 40 |
+
def test_runtime_uses_envelope_over_old_raw_value(self):
|
| 41 |
+
with patch.dict(os.environ, {ENVELOPE_ENV:self.envelope,WRAP_KEY_ENV:self.wrap,
|
| 42 |
+
'SPACE_ID':BOUND_SPACE_ID,'DASHSCOPE_API_KEY':'old-synthetic'}):
|
| 43 |
+
self.assertEqual(model_api_key({}),self.raw)
|
| 44 |
+
|
| 45 |
+
def test_local_development_still_accepts_server_side_configuration(self):
|
| 46 |
+
with patch.dict(os.environ, {}, clear=True):
|
| 47 |
+
self.assertEqual(model_api_key({'DASHSCOPE_API_KEY':self.raw}),self.raw)
|
| 48 |
+
|
| 49 |
+
def test_bound_space_requires_encryption_even_if_raw_secret_exists(self):
|
| 50 |
+
with patch.dict(os.environ, {'SPACE_ID':BOUND_SPACE_ID,'DASHSCOPE_API_KEY':self.raw},clear=True):
|
| 51 |
+
with self.assertRaises(ValueError):model_api_key({})
|