fix(audit): persist config audit rows written after the final save (DEP-1, MGT-1, TPL-3, WP1 task 1.4)

IAuditService.LogAsync only stages the row; every caller that audited
after its last SaveChangesAsync, outside the configuration-command
transaction, lost the row with the DI scope (rig: zero Disable/Enable/
Delete/DeployArtifacts rows, 16 Deploy rows for 62 deployments, one
Create/Instance row for ids up to 3876).

- New IAuditService.LogAndSaveAsync: stage + save, best-effort (logs and
  swallows any failure incl. cancellation, detaches the unsaved row).
- DeploymentService (deploy/failed/reconciled/lifecycle + timeouts),
  ArtifactDeploymentService (DeployArtifacts now before the best-effort
  stamp, RetryArtifactDeployment, DeploySiteSecrets), InstanceService,
  AreaService, SiteService: post-save LogAsync -> LogAndSaveAsync.
- ManagementActor: AuditAndSaveAsync for the non-configuration handlers
  (API keys, shared schemas, ES methods, data-connection move); move now
  audits the scrubbed public shape (MGT-4) so the fix does not start
  writing protocol credentials into AuditLogEntries.
- HandleCreateInstance no longer stages a second Create row on top of
  InstanceService's own.

Tests: real-context (SQLite) tests per family read the row back through
a second context - DeploymentService, ArtifactDeploymentService,
InstanceService, ManagementActor; each fails against the old code.
Test projects gain a ConfigurationDatabase + EF Sqlite reference.
This commit is contained in:
Joseph Doherty
2026-12-03 09:59:19 -05:00
parent 5469ece012
commit 5ca998dc56
26 changed files with 829 additions and 98 deletions
@@ -14,4 +14,27 @@ public interface IAuditService
/// <param name="cancellationToken">Cancellation token for the log write.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task LogAsync(string user, string action, string entityType, string entityId, string entityName, object? afterState, CancellationToken cancellationToken = default);
/// <summary>
/// Appends an audit log entry AND persists it immediately, best-effort: any failure
/// (including the save) is logged and swallowed, and the unsaved entry is detached so it
/// cannot poison a later save. Never throws (code review 2026-12-02 DEP-1/MGT-1/TPL-3).
/// </summary>
/// <remarks>
/// Use this — not <see cref="LogAsync"/> — wherever the audit call comes AFTER the
/// operation's final <c>SaveChangesAsync</c>: <see cref="LogAsync"/> only stages the
/// row on the change tracker, so a staged row with no later save is silently discarded
/// when the DI scope is disposed. The save flushes the whole unit of work, so call this
/// only once the operation's own changes are saved; inside an ambient transaction the
/// row commits (or rolls back) with that transaction.
/// </remarks>
/// <param name="user">The authenticated username performing the action.</param>
/// <param name="action">The action performed (e.g., "Create", "Update", "Delete").</param>
/// <param name="entityType">The type name of the affected entity.</param>
/// <param name="entityId">The string representation of the entity's primary key.</param>
/// <param name="entityName">The display name of the affected entity.</param>
/// <param name="afterState">The entity state after the action; may be null for deletes.</param>
/// <param name="cancellationToken">Cancellation token for the log write.</param>
/// <returns>A task that represents the asynchronous operation; it never faults.</returns>
Task LogAndSaveAsync(string user, string action, string entityType, string entityId, string entityName, object? afterState, CancellationToken cancellationToken = default);
}