|
| 1 | +# Critical Entity Soft Deletion and Audit Trail |
| 2 | + |
| 3 | +Issue #1047 protects critical control-plane records from accidental, |
| 4 | +unaudited hard deletion. |
| 5 | + |
| 6 | +## Scope |
| 7 | + |
| 8 | +The audited lifecycle service covers: |
| 9 | + |
| 10 | +- `Tenant` |
| 11 | +- persisted `ApiKey` |
| 12 | + |
| 13 | +Existing `WebhookEndpoint` deletion remains a soft delete with |
| 14 | +`deletedAt`/`deletedBy` and admin audit logging. `ScopedAdminToken` uses |
| 15 | +revocation rather than deletion. Generic Prisma hard deletes are blocked |
| 16 | +outside tests for all four entity types and for `CriticalEntityAuditEvent`. |
| 17 | + |
| 18 | +Financial records (`Transaction`, vault state, share-price snapshots, and |
| 19 | +reconciliation history) are deliberately excluded. They remain immutable and |
| 20 | +follow the regulatory retention policy rather than a reversible application |
| 21 | +delete workflow. |
| 22 | + |
| 23 | +## Invariants |
| 24 | + |
| 25 | +1. Delete and restore operations require a non-empty actor and reason. |
| 26 | +2. State changes and `CriticalEntityAuditEvent` creation occur in one database |
| 27 | + transaction. |
| 28 | +3. Conditional `updateMany` predicates make retries idempotent and prevent two |
| 29 | + concurrent requests from producing duplicate lifecycle events. |
| 30 | +4. Deleting a tenant atomically disables and soft-deletes all of its active API |
| 31 | + keys. |
| 32 | +5. Restoring a tenant does not restore credentials. Each API key requires a |
| 33 | + separate audited restore after the tenant is active. |
| 34 | +6. Authentication rejects keys whose key record or parent tenant is deleted. |
| 35 | +7. Audit metadata never includes plaintext credentials or stored key hashes. |
| 36 | +8. Audit rows are append-only. Generic Prisma `delete`/`deleteMany` operations |
| 37 | + against protected models throw outside the test environment. |
| 38 | + |
| 39 | +Raw SQL bypasses Prisma safeguards and must not be used for lifecycle changes. |
| 40 | +Retention cleanup that eventually hard-deletes records must use a dedicated, |
| 41 | +reviewed maintenance path after the documented grace period. |
| 42 | + |
| 43 | +## Service API |
| 44 | + |
| 45 | +Use `src/criticalEntityLifecycle.ts`: |
| 46 | + |
| 47 | +```ts |
| 48 | +await softDeleteTenant('tenant-id', { |
| 49 | + actor: 'admin@example.com', |
| 50 | + reason: 'Customer account closed', |
| 51 | +}); |
| 52 | + |
| 53 | +await restoreTenant('tenant-id', { |
| 54 | + actor: 'security-admin@example.com', |
| 55 | + reason: 'Closure reversed after verification', |
| 56 | +}); |
| 57 | + |
| 58 | +await softDeleteApiKey('api-key-id', { |
| 59 | + actor: 'security-admin@example.com', |
| 60 | + reason: 'Credential compromised', |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +The result status is one of: |
| 65 | + |
| 66 | +- `changed` |
| 67 | +- `not_found` |
| 68 | +- `already_deleted` |
| 69 | +- `not_deleted` |
| 70 | +- `parent_deleted` |
| 71 | + |
| 72 | +Treat statuses other than `changed` as no-op outcomes; they never create a |
| 73 | +duplicate audit event. |
| 74 | + |
| 75 | +## Audit review |
| 76 | + |
| 77 | +`listCriticalEntityAuditTrail` supports entity type, entity ID, and action |
| 78 | +filters. Results are newest-first and capped at 500 records per call. |
| 79 | + |
| 80 | +An audit record contains: |
| 81 | + |
| 82 | +- entity type and ID |
| 83 | +- `soft_delete` or `restore` |
| 84 | +- attributable actor |
| 85 | +- required reason |
| 86 | +- non-sensitive operation metadata |
| 87 | +- database-generated timestamp |
| 88 | + |
| 89 | +Retain `CriticalEntityAuditEvent` records for seven years with other security |
| 90 | +audit data. They must not be changed or removed through application CRUD. |
| 91 | + |
| 92 | +## Deployment |
| 93 | + |
| 94 | +1. Back up the database. |
| 95 | +2. Apply migration |
| 96 | + `20260729000000_add_critical_entity_soft_delete`. |
| 97 | +3. Confirm the new nullable columns and audit table exist. |
| 98 | +4. Delete a non-production API key through the lifecycle service. |
| 99 | +5. Verify authentication fails immediately. |
| 100 | +6. Verify exactly one `CriticalEntityAuditEvent` exists. |
| 101 | +7. Restore the key and verify a second audit event is appended. |
| 102 | + |
| 103 | +Rollback should restore application code first. The nullable columns and audit |
| 104 | +table are backward-compatible and should be retained until their data has been |
| 105 | +archived and a separate destructive migration is approved. |
0 commit comments