Files
syops/backend/app/services/shipping_notice/credential_crypto.py
T
hugo 5df204fdea 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 重试用例
2026-09-23 00:41:32 +08:00

58 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
services/shipping_notice/credential_crypto.py
用途:加密和解密库存 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 app.core.credential_crypto import CredentialCryptoErrorCodes, decrypt_secret, encrypt_secret
# 本域历史错误码原样保留,调用方(管理服务/测试)不受重构影响。
_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',
)
_KEY_CONFIG_NAME = 'FREIGHT_RPA_CREDENTIAL_KEY'
def encrypt_credential(value: str) -> str:
"""
加密单个 RPA 凭据字段以便持久化。
Args:
value: 非空明文凭据;调用方不得记录此值。
Returns:
str: 可安全保存到数据库的 Fernet 密文。
Raises:
ServiceUnavailableError: 未配置或配置了无效的主密钥。
ValidationError: 传入空凭据。
"""
return encrypt_secret(value, key_config_name=_KEY_CONFIG_NAME, codes=_RPA_CODES)
def decrypt_credential(ciphertext: str) -> str:
"""
在受控运行时解密 RPA 凭据。
Args:
ciphertext: 数据库中保存的 Fernet 密文。
Returns:
str: 仅供 RPA 执行内存使用的明文。
Raises:
ServiceUnavailableError: 未配置或配置了无效的主密钥。
ValidationError: 密文为空、已损坏或使用了错误主密钥。
"""
return decrypt_secret(ciphertext, key_config_name=_KEY_CONFIG_NAME, codes=_RPA_CODES)