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
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
backend/app/models/shipping_xfa_audit_snapshot.py
|
||||
|
||||
用途:持久化 XFA 审计完整快照密文(不可变、可校验、默认保留 3 年)。
|
||||
依赖:SQLAlchemy Base;由 audit_snapshot_service 写入/只读解密;禁止业务层直接改删。
|
||||
逻辑:普通 ShippingAuditEvent 仅保存脱敏 diff 与 snapshot_ref;完整请求/definition/
|
||||
受控原值经 envelope encryption 落入本表。ORM before_update/before_delete 与
|
||||
PostgreSQL 触发器共同保证 append-only;到期清理须 set_config
|
||||
('hxops.shipping_xfa_audit_purge','1')。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any, Optional
|
||||
|
||||
from sqlalchemy import JSON, CheckConstraint, DateTime, Index, Integer, String, Text, event
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class ShippingXfaAuditSnapshot(Base):
|
||||
"""XFA 审计完整快照密文行;禁止业务更新或删除。"""
|
||||
|
||||
__tablename__ = 'shipping_xfa_audit_snapshots'
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"purpose IN ('before', 'after', 'request', 'full', 'sample_values')",
|
||||
name='ck_shipping_xfa_audit_snapshots_purpose',
|
||||
),
|
||||
CheckConstraint(
|
||||
"encryption_alg IN ('fernet-v1')",
|
||||
name='ck_shipping_xfa_audit_snapshots_encryption_alg',
|
||||
),
|
||||
Index('ix_shipping_xfa_audit_snapshots_object', 'object_type', 'object_id', 'created_at'),
|
||||
Index('ix_shipping_xfa_audit_snapshots_retention', 'retention_expires_at', 'id'),
|
||||
Index('ix_shipping_xfa_audit_snapshots_operation', 'operation_id', 'created_at'),
|
||||
Index('ix_shipping_xfa_audit_snapshots_request', 'request_id', 'created_at'),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
# 对外稳定引用(写入审计事件 context_refs / evidence_refs),禁止猜测递增 id。
|
||||
snapshot_ref: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
|
||||
object_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
object_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
purpose: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
schema_version: Mapped[str] = mapped_column(String(32), nullable=False, default='xfa_audit_v1')
|
||||
encryption_alg: Mapped[str] = mapped_column(String(32), nullable=False, default='fernet-v1')
|
||||
key_id: Mapped[str] = mapped_column(String(64), nullable=False, default='primary')
|
||||
# Fernet 密文;静态不可读,仅敏感读 API 在权限校验后解密。
|
||||
ciphertext: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
ciphertext_sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
plaintext_sha256: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
# 脱敏摘要(可进普通导出);不得含明文订单值或 XML。
|
||||
redacted_summary_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
actor_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
request_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
operation_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
audit_event_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
retention_expires_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
|
||||
@event.listens_for(ShippingXfaAuditSnapshot, 'before_update')
|
||||
def _reject_xfa_audit_snapshot_update(_mapper, _connection, _target) -> None:
|
||||
"""拒绝 ORM 更新,保证完整快照不可篡改。"""
|
||||
raise ValidationError(
|
||||
'Shipping XFA audit snapshot is append-only',
|
||||
code='SHIPPING_NOTICE.XFA_AUDIT_SNAPSHOT_IMMUTABLE',
|
||||
)
|
||||
|
||||
|
||||
@event.listens_for(ShippingXfaAuditSnapshot, 'before_delete')
|
||||
def _reject_xfa_audit_snapshot_delete(_mapper, _connection, _target) -> None:
|
||||
"""拒绝 ORM 删除,避免业务清理抹除加密审计事实。"""
|
||||
raise ValidationError(
|
||||
'Shipping XFA audit snapshot is append-only',
|
||||
code='SHIPPING_NOTICE.XFA_AUDIT_SNAPSHOT_IMMUTABLE',
|
||||
)
|
||||
Reference in new issue
Block a user