feat(shipping-notice): 增强发货通知与审计功能,支持敏感数据控制与哈希链校验
- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全 - 引入审计事件的哈希链校验机制,支持数据完整性验证 - 增强订单抽取与审计功能,支持敏感快照的访问控制 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 完善文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
1 parent
95cdfe66b0
commit
d0fe084999
225 files changed
+25923
-6653
No files matched your search
@@ -1,11 +1,15 @@
|
||||
"""
|
||||
backend/app/services/shipping_notice/audit_service.py
|
||||
|
||||
用途:集中追加货代领域审计事件并拒绝含敏感业务载荷的元数据与快照。
|
||||
依赖:ShippingAuditEvent、progress_audit_contract、SQLAlchemy 会话。
|
||||
逻辑:Run、Task、会话 PDF 工作项、库存和 Chatflow 服务调用本模块写入状态与决策事实;
|
||||
用途:货代领域统一审计事件的唯一写入入口;要求操作上下文与前后快照(可为空),
|
||||
写入 Workflow 软引用与哈希链字段。
|
||||
依赖:ShippingAuditEvent、progress_audit_contract、audit_snapshot_service(在 append 内导入以避免环)、
|
||||
SQLAlchemy 会话。
|
||||
逻辑:Run、Task、Workflow Item、库存和 Chatflow 服务调用本模块写入状态与决策事实;
|
||||
写入前投影白名单快照 → 计算 diff → 解析同聚合 previous_event_hash → 计算 event_hash;
|
||||
本服务不提交事务,由调用方保证业务与审计原子性。支持 operation_id、白名单前后快照、
|
||||
证据引用、会话工作项关联、属主与三年保留到期;IP / User-Agent 仅持久化哈希。
|
||||
证据引用、Workflow 软引用(非 session_item)、属主;货代全业务审计事件永久保留
|
||||
(retention_expires_at=None),清理 Job 仅处理历史遗留的到期行。IP / User-Agent 仅持久化哈希。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -22,6 +26,7 @@ from app.services.shipping_notice.progress_audit_contract import (
|
||||
AUDIT_FORBIDDEN_KEYS,
|
||||
AUDIT_SNAPSHOT_ALLOWED_KEYS,
|
||||
is_forbidden_audit_key,
|
||||
is_permanent_audit_event_type,
|
||||
)
|
||||
from app.services.shipping_notice.progress_feature_flags import audit_retention_days
|
||||
|
||||
@@ -172,6 +177,7 @@ def append_shipping_audit_event(
|
||||
actor_type: str = 'system',
|
||||
actor_id: str | int | None = None,
|
||||
actor_role: str | None = None,
|
||||
actor_display: str | None = None,
|
||||
request_id: str | None = None,
|
||||
operation_id: str | None = None,
|
||||
correlation_id: str | None = None,
|
||||
@@ -182,7 +188,10 @@ def append_shipping_audit_event(
|
||||
unit_id: str | int | None = None,
|
||||
document_id: str | int | None = None,
|
||||
review_case_no: str | None = None,
|
||||
session_item_id: str | None = None,
|
||||
workflow_id: str | None = None,
|
||||
workflow_item_id: str | None = None,
|
||||
workflow_item_version_id: str | None = None,
|
||||
version_no: int | None = None,
|
||||
owner_user_id: str | int | None = None,
|
||||
client_ip: str | None = None,
|
||||
user_agent: str | None = None,
|
||||
@@ -198,20 +207,26 @@ def append_shipping_audit_event(
|
||||
artifact_refs: list[dict[str, Any]] | None = None,
|
||||
context_refs: dict[str, Any] | None = None,
|
||||
metadata: dict[str, Any] | None = None,
|
||||
sensitivity_level: str | None = None,
|
||||
retention_expires_at: datetime | None = None,
|
||||
persist_snapshots: bool = True,
|
||||
) -> ShippingAuditEvent:
|
||||
"""
|
||||
追加一条货代审计事实。
|
||||
追加一条货代审计事实(唯一写入口)。
|
||||
|
||||
Args:
|
||||
event_type: 稳定业务事件名,例如 ``session_work_item_created``;禁止传入用户自由文本。
|
||||
event_type: 稳定业务事件名,例如 ``workflow_item_created``;禁止传入用户自由文本。
|
||||
aggregate_type/aggregate_id: 业务聚合类型与稳定编号,不使用可删除记录的外键。
|
||||
operation_id: 同一次用户/系统操作的关联键,与 request_id 共同贯通事件链。
|
||||
session_item_id: 会话 PDF 工作项 item_no;软引用。
|
||||
workflow_id: Workflow.workflow_no 软引用。
|
||||
workflow_item_id: WorkflowItem.item_no 软引用。
|
||||
workflow_item_version_id: ItemVersion 主键或稳定编号的字符串软引用。
|
||||
version_no: 当前版本号(可选)。
|
||||
actor_display: 操作者展示名(可选)。
|
||||
sensitivity_level: ``normal`` / ``sensitive`` 等分级标签。
|
||||
owner_user_id: 业务属主,与 actor_id(实际操作者)区分,支撑代操作审计。
|
||||
client_ip/user_agent: 原始客户端上下文,仅哈希后入库;也可直接传 *_hash。
|
||||
before/after: 可复现的受控状态投影;持久化白名单快照及其哈希。
|
||||
before/after: 可复现的受控状态投影;持久化白名单快照及其哈希,并计算 diff_json。
|
||||
evidence_refs/artifact_refs: FileAsset ID、SHA-256、版本与证据坐标,禁止 URL/正文。
|
||||
metadata: 仅允许原因码、计数、版本等脱敏诊断事实。
|
||||
persist_snapshots: False 时只写哈希(兼容历史调用方仅传 before/after 哈希语义)。
|
||||
@@ -222,47 +237,118 @@ def append_shipping_audit_event(
|
||||
Raises:
|
||||
ValidationError: actor 类型、元数据或快照包含正文、凭据等敏感字段。
|
||||
"""
|
||||
# 延迟导入: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,
|
||||
resolve_previous_event_hash,
|
||||
)
|
||||
|
||||
allowed_actor_types = {'user', 'system', 'rpa', 'chatflow', 'admin'}
|
||||
if actor_type not in allowed_actor_types:
|
||||
raise ValidationError('Invalid shipping audit actor type', code='SHIPPING_NOTICE.AUDIT_ACTOR_INVALID')
|
||||
if result is not None and result not in {'success', 'failure', 'rejected', 'ignored'}:
|
||||
raise ValidationError('Invalid shipping audit result', code='SHIPPING_NOTICE.AUDIT_RESULT_INVALID')
|
||||
|
||||
# [STEP 1] 投影白名单快照并计算 diff。
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
resolved_workflow_id = str(workflow_id)[:64] if workflow_id else None
|
||||
resolved_workflow_item_id = str(workflow_item_id)[:64] if workflow_item_id else None
|
||||
resolved_workflow_item_version_id = (
|
||||
str(workflow_item_version_id)[:64] if workflow_item_version_id else None
|
||||
)
|
||||
resolved_version_no = int(version_no) if version_no is not None else None
|
||||
resolved_actor_id = str(actor_id)[:128] if actor_id is not None else None
|
||||
resolved_operation_id = str(operation_id)[:64] if operation_id else None
|
||||
resolved_request_id = str(request_id)[:64] if request_id else None
|
||||
resolved_run_no = str(run_no)[:64] if run_no else None
|
||||
resolved_task_no = str(task_no)[:64] if task_no else None
|
||||
resolved_document_id = str(document_id)[:64] if document_id is not None else None
|
||||
resolved_review_case_no = str(review_case_no)[:64] if review_case_no else None
|
||||
resolved_action = str(action)[:64] if action else None
|
||||
resolved_reason_code = str(reason_code)[:128] if reason_code else None
|
||||
resolved_sensitivity = str(sensitivity_level)[:16] if sensitivity_level else None
|
||||
|
||||
# [STEP 2] flush 同事务先前事件,保证哈希链前驱可查。
|
||||
db.session.flush()
|
||||
previous_event_hash = resolve_previous_event_hash(
|
||||
aggregate_type=str(aggregate_type)[:64],
|
||||
aggregate_id=str(aggregate_id)[:128],
|
||||
)
|
||||
hash_payload = build_event_hash_payload(
|
||||
event_type=str(event_type)[:64],
|
||||
aggregate_type=str(aggregate_type)[:64],
|
||||
aggregate_id=str(aggregate_id)[:128],
|
||||
action=resolved_action,
|
||||
result=result,
|
||||
reason_code=resolved_reason_code,
|
||||
workflow_id=resolved_workflow_id,
|
||||
workflow_item_id=resolved_workflow_item_id,
|
||||
workflow_item_version_id=resolved_workflow_item_version_id,
|
||||
version_no=resolved_version_no,
|
||||
run_no=resolved_run_no,
|
||||
document_id=resolved_document_id,
|
||||
review_case_no=resolved_review_case_no,
|
||||
task_no=resolved_task_no,
|
||||
operation_id=resolved_operation_id,
|
||||
request_id=resolved_request_id,
|
||||
actor_type=actor_type,
|
||||
actor_id=resolved_actor_id,
|
||||
before_snapshot=before_snapshot if persist_snapshots else None,
|
||||
after_snapshot=after_snapshot if persist_snapshots else None,
|
||||
diff_json=diff_json,
|
||||
previous_event_hash=previous_event_hash,
|
||||
sensitivity_level=resolved_sensitivity,
|
||||
)
|
||||
event_hash = compute_event_hash(hash_payload)
|
||||
|
||||
# [STEP 3] 货代统一审计账本:全业务节点永久保留,忽略调用方传入的到期时间。
|
||||
# default_retention_expires_at 仅保留给遗留清理 Job / 运维工具兼容,新写入一律为 None。
|
||||
resolved_retention: datetime | None = None
|
||||
if not is_permanent_audit_event_type(str(event_type)) and retention_expires_at is not None:
|
||||
resolved_retention = retention_expires_at
|
||||
elif not is_permanent_audit_event_type(str(event_type)):
|
||||
resolved_retention = default_retention_expires_at()
|
||||
event = ShippingAuditEvent(
|
||||
event_type=str(event_type)[:64],
|
||||
aggregate_type=str(aggregate_type)[:64],
|
||||
aggregate_id=str(aggregate_id)[:128],
|
||||
run_no=str(run_no)[:64] if run_no else None,
|
||||
task_no=str(task_no)[:64] if task_no else None,
|
||||
run_no=resolved_run_no,
|
||||
task_no=resolved_task_no,
|
||||
intake_no=str(intake_no)[:64] if intake_no else None,
|
||||
actor_type=actor_type,
|
||||
actor_id=str(actor_id)[:128] if actor_id is not None else None,
|
||||
actor_id=resolved_actor_id,
|
||||
actor_role=str(actor_role)[:64] if actor_role else None,
|
||||
request_id=str(request_id)[:64] if request_id else None,
|
||||
operation_id=str(operation_id)[:64] if operation_id else None,
|
||||
actor_display=str(actor_display)[:128] if actor_display else None,
|
||||
request_id=resolved_request_id,
|
||||
operation_id=resolved_operation_id,
|
||||
correlation_id=str(correlation_id)[:64] if correlation_id else None,
|
||||
source=str(source)[:32] if source else None,
|
||||
result=result,
|
||||
error_code=str(error_code)[:64] if error_code else None,
|
||||
conversation_id=str(conversation_id)[:100] if conversation_id else None,
|
||||
unit_id=str(unit_id)[:64] if unit_id is not None else None,
|
||||
document_id=str(document_id)[:64] if document_id is not None else None,
|
||||
review_case_no=str(review_case_no)[:64] if review_case_no else None,
|
||||
session_item_id=str(session_item_id)[:64] if session_item_id else None,
|
||||
document_id=resolved_document_id,
|
||||
review_case_no=resolved_review_case_no,
|
||||
workflow_id=resolved_workflow_id,
|
||||
workflow_item_id=resolved_workflow_item_id,
|
||||
workflow_item_version_id=resolved_workflow_item_version_id,
|
||||
version_no=resolved_version_no,
|
||||
owner_user_id=str(owner_user_id)[:64] if owner_user_id is not None else None,
|
||||
client_ip_hash=str(resolved_ip_hash)[:64] if resolved_ip_hash else None,
|
||||
user_agent_hash=str(resolved_ua_hash)[:64] if resolved_ua_hash else None,
|
||||
attempt_no=int(attempt_no) if attempt_no is not None else None,
|
||||
action=str(action)[:64] if action else None,
|
||||
reason_code=str(reason_code)[:128] if reason_code else None,
|
||||
action=resolved_action,
|
||||
reason_code=resolved_reason_code,
|
||||
reason=str(reason)[:2000] if reason else None,
|
||||
before_hash=_stable_hash(before_snapshot if before_snapshot is not None else before)
|
||||
if before is not None else None,
|
||||
@@ -270,11 +356,15 @@ def append_shipping_audit_event(
|
||||
if after is not None else None,
|
||||
before_snapshot_json=before_snapshot if persist_snapshots else None,
|
||||
after_snapshot_json=after_snapshot if persist_snapshots else None,
|
||||
diff_json=diff_json,
|
||||
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'),
|
||||
artifact_refs_json=_sanitize_refs(artifact_refs, field_name='artifact_refs'),
|
||||
context_refs_json=dict(context_refs) if context_refs else None,
|
||||
metadata_json=_sanitize_metadata(metadata),
|
||||
retention_expires_at=retention_expires_at or default_retention_expires_at(),
|
||||
retention_expires_at=resolved_retention,
|
||||
)
|
||||
db.session.add(event)
|
||||
return event
|
||||
|
||||
Reference in new issue
Block a user