fix(shipping-notice): 修复抽取续抽与 STP 入队并发死锁并幂等草稿校验
- 抽取 Job 续抽交接时 defer_stp_enqueue,STP 入队推迟到 run 收口统一触发,消除与续抽 Job 互抢行锁的死锁(2026-09-22 生产事故) - 草稿显式 validate 在内容未变时幂等短路:不递增版本、不写事件、不重复触发下游,防连点「保存并校验」产生版本/Job 风暴(事故根因) - STP 自动建单入队前 supersede 同文档过期排队 Job,防止旧版本 Job 抢跑 - 抽取 Job 异常收口识别 PostgreSQL 瞬时锁冲突(40P01/55P03)标记可重试,不再终态失败要求人工重新识别 - 订单凭据加密抽离为 app/core/credential_crypto 通用模块,shipping_notice 委托调用并保留原错误码 - 同步复核视图与测试:新增草稿校验幂等契约测试,更新 STP 入队与 Worker 重试用例
This commit is contained in:
1 parent
13c33d063c
commit
5df204fdea
12 files changed
+865
-87
No files matched your search
@@ -1,44 +1,26 @@
|
||||
"""
|
||||
services/shipping_notice/credential_crypto.py
|
||||
|
||||
用途:加密和解密库存 RPA Profile 的登录凭据。
|
||||
依赖:Flask current_app、cryptography.fernet;调用方仅保存密文,不将凭据写入日志或 API 响应。
|
||||
逻辑:管理服务写入 Profile 前调用 encrypt_credential;RPA 配置读取服务在受控内存中调用 decrypt_credential。
|
||||
用途:加密和解密库存 RPA Profile 的登录凭据(委托通用 core 模块,仅固定本域密钥键与错误码)。
|
||||
依赖:app.core.credential_crypto(Fernet 加解密);调用方仅保存密文,不将凭据写入日志或 API 响应。
|
||||
逻辑:管理服务写入 Profile 前调用 encrypt_credential;RPA 配置读取服务在受控内存中调用
|
||||
decrypt_credential。主密钥键 FREIGHT_RPA_CREDENTIAL_KEY(2026-09-18 RPA 命名空间)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from cryptography.fernet import Fernet, InvalidToken
|
||||
from flask import current_app
|
||||
from app.core.credential_crypto import CredentialCryptoErrorCodes, decrypt_secret, encrypt_secret
|
||||
|
||||
from app.core.errors import ServiceUnavailableError, ValidationError
|
||||
# 本域历史错误码原样保留,调用方(管理服务/测试)不受重构影响。
|
||||
_RPA_CODES = CredentialCryptoErrorCodes(
|
||||
key_missing='SHIPPING.RPA_CREDENTIAL_KEY_MISSING',
|
||||
key_invalid='SHIPPING.RPA_CREDENTIAL_KEY_INVALID',
|
||||
secret_empty='SHIPPING.RPA_CREDENTIAL_EMPTY',
|
||||
secret_missing='SHIPPING.RPA_CREDENTIAL_MISSING',
|
||||
secret_invalid='SHIPPING.RPA_CREDENTIAL_INVALID',
|
||||
)
|
||||
|
||||
|
||||
def _fernet() -> Fernet:
|
||||
"""
|
||||
构造当前环境的凭据加密器。
|
||||
|
||||
核心逻辑:主密钥只从运行环境读取,缺失或格式无效时拒绝继续,避免写入无法恢复的密文。
|
||||
|
||||
Returns:
|
||||
Fernet: 可执行对称加密的实例。
|
||||
|
||||
Raises:
|
||||
ServiceUnavailableError: 未配置或配置了无效的主密钥。
|
||||
"""
|
||||
key = str(current_app.config.get('FREIGHT_RPA_CREDENTIAL_KEY') or '').strip()
|
||||
if not key:
|
||||
raise ServiceUnavailableError(
|
||||
'FREIGHT_RPA_CREDENTIAL_KEY is not configured',
|
||||
code='SHIPPING.RPA_CREDENTIAL_KEY_MISSING',
|
||||
)
|
||||
try:
|
||||
return Fernet(key.encode('ascii'))
|
||||
except (ValueError, UnicodeEncodeError) as exc:
|
||||
raise ServiceUnavailableError(
|
||||
'FREIGHT_RPA_CREDENTIAL_KEY is invalid',
|
||||
code='SHIPPING.RPA_CREDENTIAL_KEY_INVALID',
|
||||
) from exc
|
||||
_KEY_CONFIG_NAME = 'FREIGHT_RPA_CREDENTIAL_KEY'
|
||||
|
||||
|
||||
def encrypt_credential(value: str) -> str:
|
||||
@@ -52,12 +34,10 @@ def encrypt_credential(value: str) -> str:
|
||||
str: 可安全保存到数据库的 Fernet 密文。
|
||||
|
||||
Raises:
|
||||
ServiceUnavailableError: 未配置或配置了无效的主密钥。
|
||||
ValidationError: 传入空凭据。
|
||||
"""
|
||||
plaintext = str(value or '')
|
||||
if not plaintext:
|
||||
raise ValidationError('RPA credential must not be empty', code='SHIPPING.RPA_CREDENTIAL_EMPTY')
|
||||
return _fernet().encrypt(plaintext.encode('utf-8')).decode('ascii')
|
||||
return encrypt_secret(value, key_config_name=_KEY_CONFIG_NAME, codes=_RPA_CODES)
|
||||
|
||||
|
||||
def decrypt_credential(ciphertext: str) -> str:
|
||||
@@ -71,12 +51,7 @@ def decrypt_credential(ciphertext: str) -> str:
|
||||
str: 仅供 RPA 执行内存使用的明文。
|
||||
|
||||
Raises:
|
||||
ServiceUnavailableError: 未配置或配置了无效的主密钥。
|
||||
ValidationError: 密文为空、已损坏或使用了错误主密钥。
|
||||
"""
|
||||
value = str(ciphertext or '').strip()
|
||||
if not value:
|
||||
raise ValidationError('RPA credential ciphertext is missing', code='SHIPPING.RPA_CREDENTIAL_MISSING')
|
||||
try:
|
||||
return _fernet().decrypt(value.encode('ascii')).decode('utf-8')
|
||||
except (InvalidToken, UnicodeDecodeError) as exc:
|
||||
raise ValidationError('RPA credential ciphertext is invalid', code='SHIPPING.RPA_CREDENTIAL_INVALID') from exc
|
||||
return decrypt_secret(ciphertext, key_config_name=_KEY_CONFIG_NAME, codes=_RPA_CODES)
|
||||
Reference in new issue
Block a user