SR-002 — Document Service Audit Trail: System Requirements

By Engineering Team•September 24, 2026•6 min read
Metadata Details
System Name Document Service — Centralized Audit Trail Sink
Document ID SR-2026-AUDIT-001
Author Engineering Team
Status Approved

Traces back to BRD-002 — Document Service: Centralized Audit Trail.


Functional Requirements

Ingestion

  • FR-01 (Subscribe-Only Ingestion): document-service MUST consume AuditEventPayload messages from its document-audit Pub/Sub subscription only — it MUST NOT expose any gRPC or REST ingestion endpoint.
  • FR-02 (Standard Envelope): every audit event MUST carry {tenant_id, service_name, transaction_name, transaction_id, date_time, request, response} — tenant_id, service_name, transaction_name, and transaction_id together form the idempotency key.

Masking & Persistence

  • FR-03 (Mask-Once-At-Persistence): document-service MUST mask every field matching its PII_KEYS set in both request and response before the JSON is ever written to storage — phone/mobile keys receive a partial regex mask, email keys receive a partial mask, token/password/authorization keys receive full [REDACTED_SECRET] redaction, and every other PII_KEYS match receives full [REDACTED_PII] redaction. Publishers MUST NOT mask their own payloads.
  • FR-04 (Masked Storage): the masked JSON object MUST be uploaded to the configured GCS bucket under the object key {service_name}/{transaction_name}/{uuid_v4}.json — the filename MUST be a freshly generated UUID, never derived from transaction_id.
  • FR-05 (Transaction Tracking): document-service MUST record (tenant_id, service_name, transaction_name, transaction_id) → storage_path plus a status (PENDING/SUCCESS/FAILED) and an optional error_message in the document_track MongoDB collection.
  • FR-06 (Two-Phase Idempotent Write): document-service MUST insert the document_track row as PENDING before attempting the GCS upload, and MUST update it to SUCCESS or FAILED after the upload attempt completes — a duplicate delivery for an already-tracked (tenant_id, service_name, transaction_name, transaction_id) MUST be a no-op, never a duplicate row or a duplicate GCS object.

Surface

  • FR-07 (Health-Only Surface): document-service MUST expose exactly one HTTP route, a health check — no other REST or gRPC surface, per its subscribe-only design.

Publisher Contract

  • FR-08 (Fire-and-Forget Publish): a publisher MUST publish its audit event fire-and-forget (tokio::spawn in Rust, a goroutine over context.WithoutCancel(ctx) in Go) — a publish failure MUST be logged, never propagated into the RPC/webhook response that triggered it.
  • FR-09 (Raw, Unmasked Payload): a publisher MUST send the full, unmasked request/response JSON — masking is exclusively document-service’s responsibility, never the publisher’s.

Non-Functional Requirements

Reliability

  • NFR-01 (Non-Blocking Publish Latency): a publisher’s audit-event publish MUST add no observable latency to, and MUST NOT be able to fail, the RPC/webhook response path that triggered it (verified: both payment-service and shipment-service call sites are fire-and-forget).
  • NFR-02 (Redelivery / Retry on Handler Failure): a failed process_audit_event handler invocation SHOULD be retried via Pub/Sub redelivery. (Gap — the shared core-lib Pub/Sub receiver acks every message unconditionally once the handler returns, even on failure, so no redelivery ever occurs for a failed audit event. Tracked as technical debt — see Technical Debt — Document Service Audit Trail Gaps, TD-01.)

Traceability & Multi-Tenancy

  • NFR-03 (Trace Propagation): traceparent/correlation_id MUST propagate through the Pub/Sub envelope header from the publisher’s originating request through to document-service’s processing span, per SR-003 FR-08.
  • NFR-04 (Tenant Scoping): every persisted document_track row MUST carry tenant_id sourced from the audit event envelope — never invented or defaulted server-side.

Masking Integrity

  • NFR-05 (Masking Idempotency): masking MUST run exactly once, only inside document-service, at persistence time — a publisher pre-masking its own payload, or a second masking pass anywhere else, is explicitly disallowed, since masking is not guaranteed idempotent under double-application.
  • NFR-06 (Extensible Coverage): adding a new sensitive field name to masking coverage MUST require a change in exactly one place (PII_KEYS in documentaudit_masking.rs), never a change to any publisher.

Data Retention

  • NFR-07 (Retention Policy): a document_track row and its corresponding masked GCS object SHOULD be retained only as long as the tenant’s compliance/audit window requires, not indefinitely by default. (Gap — no TTL index exists on document_track, no GCS bucket lifecycle rule exists on the audit bucket, and no scheduled cleanup job exists anywhere in document-service — every row and object persists forever. Tracked as technical debt — see Technical Debt — Document Service Audit Trail Gaps, TD-02.)