fix: separate voice credentials and refresh feature toggle

This commit is contained in:
2026-08-11 17:46:56 +08:00
parent fd2da26d10
commit 74213a363f
10 changed files with 97 additions and 11 deletions

View File

@@ -59,4 +59,6 @@ BOOTSTRAP_ADMIN_USERNAME=admin
BOOTSTRAP_ADMIN_PASSWORD=admin123456
BOOTSTRAP_ADMIN_NAME=系统管理员
ALIYUN_NLS_APP_KEY=
ALIYUN_NLS_ACCESS_KEY_ID=
ALIYUN_NLS_ACCESS_KEY_SECRET=
ALIYUN_NLS_ENDPOINT=https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/asr

View File

@@ -49,6 +49,8 @@ class Settings(BaseSettings):
aliyun_sms_template_param_key: str = "code"
aliyun_sms_endpoint: str = "dysmsapi.aliyuncs.com"
aliyun_nls_app_key: str = ""
aliyun_nls_access_key_id: str = ""
aliyun_nls_access_key_secret: str = ""
aliyun_nls_endpoint: str = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/asr"
mock_rag_enabled: bool = False
mock_model_enabled: bool = True

View File

@@ -10,7 +10,11 @@ from app.core.config import get_settings
ENCRYPTED_PREFIX = "enc:v1:"
MASKED_SECRET = "******"
SENSITIVE_CONFIG_KEYS = {"aliyun_sms_access_key_secret", "feishu_app_secret"}
SENSITIVE_CONFIG_KEYS = {
"aliyun_sms_access_key_secret",
"aliyun_nls_access_key_secret",
"feishu_app_secret",
}
class SecretService:

View File

@@ -105,15 +105,15 @@ def load_voice_config(db: Session) -> VoiceInputConfig:
values = {row.config_key: row.config_value for row in rows}
enabled = _bool(values.get("voice_input_enabled"), settings.voice_input_enabled)
duration = _int(values.get("voice_max_duration_seconds"), settings.voice_max_duration_seconds, 5, 60)
access_key_id = str(values.get("aliyun_sms_access_key_id") or settings.aliyun_sms_access_key_id).strip()
encrypted_secret = str(values.get("aliyun_sms_access_key_secret") or settings.aliyun_sms_access_key_secret).strip()
access_key_id = str(values.get("aliyun_nls_access_key_id") or settings.aliyun_nls_access_key_id).strip()
encrypted_secret = str(values.get("aliyun_nls_access_key_secret") or settings.aliyun_nls_access_key_secret).strip()
return VoiceInputConfig(
enabled=enabled,
max_duration_seconds=duration,
app_key=settings.aliyun_nls_app_key.strip(),
app_key=str(values.get("aliyun_nls_app_key") or settings.aliyun_nls_app_key).strip(),
access_key_id=access_key_id,
access_key_secret=SecretService.decrypt(encrypted_secret),
endpoint=settings.aliyun_nls_endpoint.strip(),
endpoint=str(values.get("aliyun_nls_endpoint") or settings.aliyun_nls_endpoint).strip(),
)

View File

@@ -24,9 +24,9 @@ def settings(**overrides):
"voice_input_enabled": False,
"voice_max_duration_seconds": 60,
"aliyun_nls_app_key": "app-key",
"aliyun_nls_access_key_id": "voice-access-id",
"aliyun_nls_access_key_secret": "voice-access-secret",
"aliyun_nls_endpoint": "https://nls.example/asr",
"aliyun_sms_access_key_id": "access-id",
"aliyun_sms_access_key_secret": "access-secret",
}
values.update(overrides)
return SimpleNamespace(**values)
@@ -50,6 +50,22 @@ def test_transcription_endpoint_is_rejected_when_switch_is_off(monkeypatch):
assert exc.value.status_code == 404
def test_voice_credentials_are_loaded_independently_from_sms(monkeypatch):
monkeypatch.setattr("app.services.voice_input_service.get_settings", lambda: settings())
with database() as db:
db.add_all([
SystemConfig(config_key="aliyun_sms_access_key_id", config_value="sms-id"),
SystemConfig(config_key="aliyun_nls_app_key", config_value="db-app-key"),
SystemConfig(config_key="aliyun_nls_access_key_id", config_value="db-voice-id"),
SystemConfig(config_key="aliyun_nls_access_key_secret", config_value="db-voice-secret"),
])
db.commit()
config = load_voice_config(db)
assert config.app_key == "db-app-key"
assert config.access_key_id == "db-voice-id"
assert config.access_key_secret == "db-voice-secret"
def test_transcription_returns_provider_text(monkeypatch):
monkeypatch.setattr("app.services.voice_input_service.get_settings", lambda: settings(voice_input_enabled=True))
monkeypatch.setattr("app.services.voice_input_service._normalize_audio", lambda *_: b"wav")