feat(integration-connector): 绑定参数映射与凭据 DB 加密落库模式

- 绑定新增 param_mapping_json:域参数→接口参数声明式映射(map_response 规范),空对象同名直通
- 凭据引用新增 db 维护模式:secret_ciphertext Fernet 加密落库,主密钥 INTEGRATION_CREDENTIAL_KEY 仅环境注入,与 env 引用模式并存
- 新增 credential_types 凭据形态注册表:代码注册各类型字段清单,UI 动态渲染与 API 组装校验的唯一事实来源
- 移除绑定 context_source_code 独立列,路由条件统一 context_selector_json
- 管理端表单 Modal 重构为 Drawer,新增 JSON 编辑字段组件与页面级测试
- 迁移 rd1/re1 重挂当前唯一 head dd22ee33ff44,维持单 head 链
- settings 移除 IN_TRANSIT_TRANSFER_ERP_* 配置,外部端点/凭据移交连接器客户端实例管理
This commit is contained in:
hugo committed 2026-09-23 00:44:56 +08:00
1 parent 5df204fdea
commit 31591e663e
35 files changed
+4345 -750

No files matched your search

@@ -1,18 +1,32 @@
"""services/integration_connector/security.py
用途:凭据引用解析(fail-closed)与敏感字段脱敏。
依赖:IntegrationCredentialReference 模型、current_app.config、errors.py。
逻辑:凭据只存 secret_ref 引用;解析时读当前环境配置,缺失即抛 CredentialUnresolvedError;
sanitize 递归脱敏敏感键;credential_summary 只回元数据,绝不回显明文。
依赖:IntegrationCredentialReference 模型、core.credential_crypto、current_app.config、errors.py。
逻辑:db 模式解密 secret_ciphertext(主密钥 INTEGRATION_CREDENTIAL_KEY 环境注入);
env 模式按 secret_ref 读当前环境配置,缺失即抛 CredentialUnresolvedError;
sanitize 递归脱敏敏感键;credential_summary 只回元数据,绝不回显明文/密文。
"""
from __future__ import annotations
import json
from typing import Any
from app.core.credential_crypto import CredentialCryptoErrorCodes, decrypt_secret
from app.core.errors import AppError
from app.models import IntegrationCredentialReference
from app.services.integration_connector.errors import CredentialUnresolvedError
# 连接器凭据密文的 Fernet 主密钥配置键与错误码。
CREDENTIAL_KEY_CONFIG_NAME = 'INTEGRATION_CREDENTIAL_KEY'
_DB_SECRET_CODES = CredentialCryptoErrorCodes(
key_missing='INTEGRATION.CREDENTIAL_KEY_MISSING',
key_invalid='INTEGRATION.CREDENTIAL_KEY_INVALID',
secret_empty='INTEGRATION.CREDENTIAL_SECRET_EMPTY',
secret_missing='INTEGRATION.CREDENTIAL_SECRET_MISSING',
secret_invalid='INTEGRATION.CREDENTIAL_SECRET_INVALID',
)
DEFAULT_SENSITIVE_KEYS = frozenset({
'password', 'token', 'secret', 'authorization', 'cookie', 'access_key',
'secret_key', 'credential', 'api_key', 'apikey', 'credentials',
@@ -20,10 +34,10 @@ DEFAULT_SENSITIVE_KEYS = frozenset({
def resolve_credential(credential_ref_code: str) -> dict[str, Any]:
"""按凭据引用解析当前环境真实凭据(从 current_app.config[secret_ref] 读取)。
"""按凭据引用解析真实凭据(db 解密 / env 配置键),fail-closed。
Raises:
CredentialUnresolvedError: 引用不存在或 secret_ref 对应配置缺失/为空。
CredentialUnresolvedError: 引用不存在、密文缺失、主密钥缺失/无效或 env 键缺失。
"""
from flask import current_app
@@ -32,13 +46,29 @@ def resolve_credential(credential_ref_code: str) -> dict[str, Any]:
).first()
if cred is None:
raise CredentialUnresolvedError(ref=credential_ref_code)
if cred.secret_provider == 'db':
if not cred.secret_ciphertext:
raise CredentialUnresolvedError(ref=credential_ref_code)
try:
value = decrypt_secret(
cred.secret_ciphertext,
key_config_name=CREDENTIAL_KEY_CONFIG_NAME,
codes=_DB_SECRET_CODES,
)
except AppError as exc:
raise CredentialUnresolvedError(message=str(exc.message)) from exc
try:
parsed = json.loads(value)
except (ValueError, TypeError):
parsed = None
return dict(parsed) if isinstance(parsed, dict) else {'value': value}
secret_ref = cred.secret_ref
value = current_app.config.get(secret_ref) if has_app_ctx() else None
if value is None or value == '':
config_value = current_app.config.get(secret_ref) if has_app_ctx() else None
if config_value is None or config_value == '':
raise CredentialUnresolvedError(ref=secret_ref)
if isinstance(value, dict):
return dict(value)
return {'value': value}
if isinstance(config_value, dict):
return dict(config_value)
return {'value': config_value}
def has_app_ctx() -> bool: