feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -2,14 +2,14 @@
|
||||
backend/app/services/shipping_notice/audit_service.py
|
||||
|
||||
用途:货代领域统一审计事件的唯一写入入口;要求操作上下文与前后快照(可为空),
|
||||
写入 Workflow 软引用与哈希链字段。
|
||||
写入 Workflow 软引用与哈希链字段;XFA 敏感变更可同事务挂接加密完整快照引用。
|
||||
依赖:ShippingAuditEvent、progress_audit_contract、audit_snapshot_service(在 append 内导入以避免环)、
|
||||
SQLAlchemy 会话。
|
||||
逻辑:Run、Task、Workflow Item、库存、门禁 Attempt 和 Chatflow 服务调用本模块写入状态与决策事实;
|
||||
写入前投影白名单快照 → 计算 diff → 解析同聚合 previous_event_hash → 计算 event_hash;
|
||||
本服务不提交事务,由调用方保证业务与审计原子性。支持 operation_id、白名单前后快照、
|
||||
证据引用、Workflow 软引用(非 session_item)、属主;actor_type 含 worker(门禁 Job);
|
||||
货代全业务审计事件永久保留(retention_expires_at=None),清理 Job 仅处理历史遗留到期行。
|
||||
逻辑:Run、Task、Workflow Item、库存、门禁 Attempt、Chatflow 与 XFA Profile 服务调用本模块;
|
||||
写入前投影白名单快照 → 可选创建 XFA 加密完整快照 → 计算 diff → 解析同聚合
|
||||
previous_event_hash → 计算 event_hash;本服务不提交事务,由调用方保证业务与审计原子性;
|
||||
审计/快照写入失败时业务不得提交。支持 operation_id、白名单前后快照、证据引用、
|
||||
Workflow 软引用、属主;actor_type 含 worker;货代统一账本永久保留(expires=None)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -210,6 +210,10 @@ def append_shipping_audit_event(
|
||||
sensitivity_level: str | None = None,
|
||||
retention_expires_at: datetime | None = None,
|
||||
persist_snapshots: bool = True,
|
||||
full_before: dict[str, Any] | None = None,
|
||||
full_after: dict[str, Any] | None = None,
|
||||
full_payload: dict[str, Any] | None = None,
|
||||
full_payload_purpose: str = 'full',
|
||||
) -> ShippingAuditEvent:
|
||||
"""
|
||||
追加一条货代审计事实(唯一写入口)。
|
||||
@@ -230,18 +234,23 @@ def append_shipping_audit_event(
|
||||
evidence_refs/artifact_refs: FileAsset ID、SHA-256、版本与证据坐标,禁止 URL/正文。
|
||||
metadata: 仅允许原因码、计数、版本等脱敏诊断事实。
|
||||
persist_snapshots: False 时只写哈希(兼容历史调用方仅传 before/after 哈希语义)。
|
||||
full_before/full_after/full_payload: XFA 完整敏感载荷;加密写入 shipping_xfa_audit_snapshots,
|
||||
引用挂到 context_refs.snapshot_*_ref;缺主密钥时失败关闭。
|
||||
full_payload_purpose: full_payload 的用途标签(默认 full)。
|
||||
|
||||
Returns:
|
||||
尚未提交的审计事件,由调用方与业务写入在同一事务提交。
|
||||
|
||||
Raises:
|
||||
ValidationError: actor 类型、元数据或快照包含正文、凭据等敏感字段。
|
||||
ServiceUnavailableError: 需要加密快照但主密钥缺失。
|
||||
"""
|
||||
# 延迟导入:audit_snapshot_service.sanitize_snapshot 会回引 project_audit_snapshot,避免模块环。
|
||||
from app.services.shipping_notice.audit_snapshot_service import (
|
||||
build_event_hash_payload,
|
||||
compute_event_hash,
|
||||
compute_snapshot_diff,
|
||||
create_xfa_audit_snapshot,
|
||||
resolve_previous_event_hash,
|
||||
)
|
||||
|
||||
@@ -256,8 +265,66 @@ def append_shipping_audit_event(
|
||||
before_snapshot = project_audit_snapshot(before) if before is not None else None
|
||||
after_snapshot = project_audit_snapshot(after) if after is not None else None
|
||||
diff_json = compute_snapshot_diff(before_snapshot, after_snapshot)
|
||||
if context_refs is not None:
|
||||
_sanitize_metadata(context_refs)
|
||||
|
||||
# [STEP 1b] XFA 完整载荷 → 加密不可变快照;失败则阻断业务提交。
|
||||
resolved_context = dict(context_refs or {}) if context_refs is not None else {}
|
||||
snapshot_refs: list[dict[str, Any]] = []
|
||||
if full_before is not None:
|
||||
before_row = create_xfa_audit_snapshot(
|
||||
payload=full_before,
|
||||
object_type=str(aggregate_type)[:64],
|
||||
object_id=str(aggregate_id)[:128],
|
||||
purpose='before',
|
||||
actor_id=actor_id,
|
||||
request_id=request_id,
|
||||
operation_id=operation_id,
|
||||
)
|
||||
resolved_context['snapshot_before_ref'] = before_row.snapshot_ref
|
||||
snapshot_refs.append({
|
||||
'kind': 'xfa_audit_snapshot',
|
||||
'purpose': 'before',
|
||||
'snapshot_ref': before_row.snapshot_ref,
|
||||
'plaintext_sha256': before_row.plaintext_sha256,
|
||||
})
|
||||
if full_after is not None:
|
||||
after_row = create_xfa_audit_snapshot(
|
||||
payload=full_after,
|
||||
object_type=str(aggregate_type)[:64],
|
||||
object_id=str(aggregate_id)[:128],
|
||||
purpose='after',
|
||||
actor_id=actor_id,
|
||||
request_id=request_id,
|
||||
operation_id=operation_id,
|
||||
)
|
||||
resolved_context['snapshot_after_ref'] = after_row.snapshot_ref
|
||||
snapshot_refs.append({
|
||||
'kind': 'xfa_audit_snapshot',
|
||||
'purpose': 'after',
|
||||
'snapshot_ref': after_row.snapshot_ref,
|
||||
'plaintext_sha256': after_row.plaintext_sha256,
|
||||
})
|
||||
if full_payload is not None:
|
||||
full_row = create_xfa_audit_snapshot(
|
||||
payload=full_payload,
|
||||
object_type=str(aggregate_type)[:64],
|
||||
object_id=str(aggregate_id)[:128],
|
||||
purpose=str(full_payload_purpose or 'full'),
|
||||
actor_id=actor_id,
|
||||
request_id=request_id,
|
||||
operation_id=operation_id,
|
||||
)
|
||||
resolved_context['snapshot_full_ref'] = full_row.snapshot_ref
|
||||
snapshot_refs.append({
|
||||
'kind': 'xfa_audit_snapshot',
|
||||
'purpose': full_row.purpose,
|
||||
'snapshot_ref': full_row.snapshot_ref,
|
||||
'plaintext_sha256': full_row.plaintext_sha256,
|
||||
})
|
||||
if resolved_context:
|
||||
_sanitize_metadata(resolved_context)
|
||||
merged_evidence = list(evidence_refs or [])
|
||||
if snapshot_refs:
|
||||
merged_evidence.extend(snapshot_refs)
|
||||
|
||||
resolved_ip_hash = client_ip_hash or hash_client_context_value(client_ip)
|
||||
resolved_ua_hash = user_agent_hash or hash_client_context_value(user_agent)
|
||||
@@ -361,13 +428,14 @@ def append_shipping_audit_event(
|
||||
previous_event_hash=previous_event_hash,
|
||||
event_hash=event_hash,
|
||||
sensitivity_level=resolved_sensitivity,
|
||||
evidence_refs_json=_sanitize_refs(evidence_refs, field_name='evidence_refs'),
|
||||
evidence_refs_json=_sanitize_refs(merged_evidence or None, field_name='evidence_refs'),
|
||||
artifact_refs_json=_sanitize_refs(artifact_refs, field_name='artifact_refs'),
|
||||
context_refs_json=dict(context_refs) if context_refs else None,
|
||||
context_refs_json=resolved_context or None,
|
||||
metadata_json=_sanitize_metadata(metadata),
|
||||
retention_expires_at=resolved_retention,
|
||||
)
|
||||
db.session.add(event)
|
||||
db.session.flush()
|
||||
return event
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user