| | | 1 | | using ArturRios.Mediator.Command; |
| | | 2 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 3 | | using ArturRios.Output; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Heimdall.Command.Auditing; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Wraps a command handler so every attempted write produces one audit trail entry (NFR-09), |
| | | 10 | | /// without changing the wrapped handler — plus one more for each side effect a successful |
| | | 11 | | /// command's output reports through <see cref="IAuditedSideEffects" />. Registered per-handler by |
| | | 12 | | /// <see cref="CommandHandlerRegistrationExtensions.AddAuditedCommandHandler{TCommand,TOutput,THandler}" />. |
| | | 13 | | /// </summary> |
| | 988 | 14 | | public class AuditingCommandHandler<TCommand, TOutput>( |
| | 988 | 15 | | ICommandHandlerAsync<TCommand, TOutput> inner, |
| | 988 | 16 | | IAuditLogWriter auditLogWriter, |
| | 988 | 17 | | ILogger<AuditingCommandHandler<TCommand, TOutput>> logger) |
| | | 18 | | : ICommandHandlerAsync<TCommand, TOutput> |
| | | 19 | | where TCommand : BaseCommand |
| | | 20 | | where TOutput : CommandOutput |
| | | 21 | | { |
| | | 22 | | public async Task<DataOutput<TOutput?>> HandleAsync(TCommand command, CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 988 | 24 | | var result = await inner.HandleAsync(command, cancellationToken); |
| | | 25 | | |
| | 986 | 26 | | var action = typeof(TCommand).Name; |
| | 986 | 27 | | var targetId = ResolveTargetId(result.Data); |
| | | 28 | | |
| | | 29 | | // Refusals are recorded too, and they are usually the entries worth having: a caller |
| | | 30 | | // repeatedly denied a scope they do not own, or repeatedly failing a password, leaves no |
| | | 31 | | // other trace anywhere. Only the first error is kept — enough to say why, and all of them |
| | | 32 | | // are the application's own canonical messages, so nothing a caller submitted is written |
| | | 33 | | // here. |
| | 986 | 34 | | await TryWriteAsync( |
| | 986 | 35 | | action, targetId, result.Success, result.Success ? null : result.Errors.FirstOrDefault()); |
| | | 36 | | |
| | | 37 | | // A side effect only happened if the command did, so a refusal records none. |
| | 986 | 38 | | if (result.Success && result.Data is IAuditedSideEffects sideEffects) |
| | | 39 | | { |
| | 34 | 40 | | foreach (var sideEffect in sideEffects.AuditedSideEffects()) |
| | | 41 | | { |
| | 6 | 42 | | await TryWriteAsync(sideEffect, targetId, true, null); |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | 986 | 46 | | return result; |
| | 986 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async Task TryWriteAsync(string action, Guid? targetId, bool succeeded, string? failureReason) |
| | | 50 | | { |
| | | 51 | | try |
| | | 52 | | { |
| | 992 | 53 | | await auditLogWriter.WriteAsync(action, targetId, succeeded, failureReason); |
| | 990 | 54 | | } |
| | 2 | 55 | | catch (Exception exception) |
| | | 56 | | { |
| | | 57 | | // The trail is a record of what the API did, not part of doing it: a caller whose |
| | | 58 | | // request succeeded is not told it failed because the entry could not be written. |
| | 2 | 59 | | logger.LogWarning( |
| | 2 | 60 | | exception, "Failed to write audit log entry for {Action} (target {TargetId})", |
| | 2 | 61 | | action, targetId); |
| | 2 | 62 | | } |
| | 992 | 63 | | } |
| | | 64 | | |
| | | 65 | | private static Guid? ResolveTargetId(TOutput? output) |
| | | 66 | | { |
| | 986 | 67 | | if (output is null) |
| | | 68 | | { |
| | 541 | 69 | | return null; |
| | | 70 | | } |
| | | 71 | | |
| | 445 | 72 | | var property = typeof(TOutput).GetProperty("Id") ?? typeof(TOutput).GetProperty("PublicId"); |
| | | 73 | | |
| | 445 | 74 | | return property is not null && property.PropertyType == typeof(Guid) |
| | 445 | 75 | | ? (Guid?)property.GetValue(output) |
| | 445 | 76 | | : null; |
| | | 77 | | } |
| | | 78 | | } |