| | | 1 | | using ArturRios.Data.Relational.Core.DependencyInjection; |
| | | 2 | | using ArturRios.Heimdall.Command.Auditing; |
| | | 3 | | using ArturRios.Heimdall.Command.Handlers; |
| | | 4 | | using ArturRios.Heimdall.Command.Input; |
| | | 5 | | using ArturRios.Heimdall.Command.Output; |
| | | 6 | | using ArturRios.Heimdall.Shared.Retention; |
| | | 7 | | using ArturRios.Heimdall.WebApi.Monitoring; |
| | | 8 | | using ArturRios.Heimdall.WebApi.Retention; |
| | | 9 | | using ArturRios.Util.WebApi.Configuration; |
| | | 10 | | using ArturRios.Util.WebApi.Security.Extensions; |
| | | 11 | | using Serilog; |
| | | 12 | | |
| | | 13 | | namespace ArturRios.Heimdall.WebApi; |
| | | 14 | | |
| | | 15 | | public static class DataRetentionRegistrationExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Registers the retention schedule (NFR-19) and the pass that enforces it. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// The options are a singleton because they are a deployment-wide policy read once at |
| | | 22 | | /// start-up, and the purge is registered as a hosted service unless it has been switched |
| | | 23 | | /// off — which is a deployment decision an operator has to take deliberately, since keeping |
| | | 24 | | /// personal data past its retention period is the thing NFR-19 forbids. The handler is |
| | | 25 | | /// registered audited like every other command: the entry recording that the schedule was |
| | | 26 | | /// enforced is as much the point as the deletion is. |
| | | 27 | | /// </remarks> |
| | | 28 | | public static IServiceCollection AddDataRetention(this IServiceCollection services) |
| | | 29 | | { |
| | 973 | 30 | | var retention = DataRetentionOptions.FromEnvironment(); |
| | | 31 | | |
| | 973 | 32 | | if (retention.InvalidVariables.Count > 0) |
| | | 33 | | { |
| | 0 | 34 | | Log.Warning( |
| | 0 | 35 | | "Ignoring unusable retention settings and applying the documented defaults: {Variables}", |
| | 0 | 36 | | retention.InvalidVariables); |
| | | 37 | | } |
| | | 38 | | |
| | 973 | 39 | | services.AddSingleton(retention); |
| | 973 | 40 | | services |
| | 973 | 41 | | .AddAuditedCommandHandler<PurgeExpiredTokensCommand, PurgeExpiredTokensCommandOutput, |
| | 973 | 42 | | PurgeExpiredTokensCommandHandler>(); |
| | 973 | 43 | | services |
| | 973 | 44 | | .AddAuditedCommandHandler<AnonymiseExpiredDeletionsCommand, AnonymiseExpiredDeletionsCommandOutput, |
| | 973 | 45 | | AnonymiseExpiredDeletionsCommandHandler>(); |
| | 973 | 46 | | services |
| | 973 | 47 | | .AddAuditedCommandHandler<PseudonymiseAuditActorsCommand, PseudonymiseAuditActorsCommandOutput, |
| | 973 | 48 | | PseudonymiseAuditActorsCommandHandler>(); |
| | | 49 | | |
| | 973 | 50 | | if (retention.PurgeEnabled) |
| | | 51 | | { |
| | 973 | 52 | | services.AddHostedService<TokenRetentionService>(); |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 0 | 56 | | Log.Warning( |
| | 0 | 57 | | "The token retention purge is switched off by {Variable}; expired tokens will not be removed", |
| | 0 | 58 | | DataRetentionOptions.PurgeEnabledVariable); |
| | | 59 | | } |
| | | 60 | | |
| | 973 | 61 | | if (retention.AnonymisationEnabled) |
| | | 62 | | { |
| | 973 | 63 | | services.AddHostedService<IdentityAnonymisationService>(); |
| | | 64 | | } |
| | | 65 | | else |
| | | 66 | | { |
| | 0 | 67 | | Log.Warning( |
| | 0 | 68 | | "Identity anonymisation is switched off by {Variable}; logically deleted identities " + |
| | 0 | 69 | | "will be kept past their retention window", |
| | 0 | 70 | | DataRetentionOptions.AnonymisationEnabledVariable); |
| | | 71 | | } |
| | | 72 | | |
| | 973 | 73 | | if (retention.SecurityMonitoringEnabled) |
| | | 74 | | { |
| | 973 | 75 | | services.AddHostedService<SecurityMonitoringService>(); |
| | | 76 | | } |
| | | 77 | | else |
| | | 78 | | { |
| | 0 | 79 | | Log.Warning( |
| | 0 | 80 | | "Security signal detection is switched off by {Variable}; the audit trail and lockout " + |
| | 0 | 81 | | "counters will keep recording what happens and nobody will be told about it", |
| | 0 | 82 | | DataRetentionOptions.SecurityMonitoringEnabledVariable); |
| | | 83 | | } |
| | | 84 | | |
| | 973 | 85 | | if (retention.AuditPseudonymisationEnabled) |
| | | 86 | | { |
| | 973 | 87 | | services.AddHostedService<AuditRetentionService>(); |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | 0 | 91 | | Log.Warning( |
| | 0 | 92 | | "Audit attribution pseudonymisation is switched off by {Variable}; the trail will " + |
| | 0 | 93 | | "keep naming people past its attribution period, including erased ones", |
| | 0 | 94 | | DataRetentionOptions.AuditPseudonymisationEnabledVariable); |
| | | 95 | | } |
| | | 96 | | |
| | 973 | 97 | | return services; |
| | | 98 | | } |
| | | 99 | | } |