-
Notifications
You must be signed in to change notification settings - Fork 2
feat: audit trail #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: audit trail #288
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
| using Microsoft.EntityFrameworkCore.Metadata; | ||
| using SW.Bitween.Domain; | ||
| using SW.Bitween.Domain.Accounts; | ||
| using SW.Bitween.Domain.Gateway; | ||
| using SW.Bitween.Services; | ||
| using SW.EfCoreExtensions; | ||
|
|
||
| namespace SW.Bitween; | ||
|
|
||
| /// <summary> | ||
| /// What the audit trail records, and what it must never record. Owned by C# in the same way as the | ||
| /// permission catalog, so the rules sit in one readable list rather than being spread across the | ||
| /// handlers that happen to write each entity. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// <b>Default deny.</b> An entity absent from <see cref="Audited"/> produces no audit rows at all. | ||
| /// That is deliberate: everything in Bitween shares one <see cref="BitweenDbContext"/>, so an | ||
| /// allow-everything policy would write several rows per message processed and bury the | ||
| /// configuration changes the trail exists to show. It also means <see cref="AuditEntry"/> cannot | ||
| /// audit itself. | ||
| /// </para> | ||
| /// <para> | ||
| /// <b>Credentials.</b> <see cref="Redacted"/> names the properties that are never captured in any | ||
| /// entity state. An audit table is read by more people than the screens that mask these values, so | ||
| /// a property that is hidden in the UI and recorded here in full would be a leak wearing a | ||
| /// different hat. | ||
| /// </para> | ||
| /// </remarks> | ||
| public static class AuditPolicy | ||
| { | ||
| /// <summary> | ||
| /// The configuration entities, their owned children, and the account/role records. Runtime | ||
| /// traffic — every <c>Xchange</c>, result, delivery, receive attempt, retry usage counter and | ||
| /// refresh token — is deliberately absent. | ||
| /// </summary> | ||
| private static readonly HashSet<Type> Audited = | ||
| [ | ||
| typeof(Subscription), | ||
| typeof(Schedule), // owned by Subscription | ||
| typeof(SubscriptionCategory), | ||
| typeof(Partner), | ||
| typeof(ApiCredential), // owned by Partner | ||
| typeof(Document), | ||
| typeof(ApiGateway), | ||
| typeof(ApiGatewayPartner), | ||
| typeof(BusGateway), | ||
| typeof(BusGatewayRoute), | ||
| typeof(WorkGroup), | ||
| typeof(RetryPolicy), | ||
| typeof(RetryAlertOverride), | ||
| typeof(Notifier), | ||
| typeof(GlobalAdapterValuesSet), | ||
| typeof(Setting), | ||
| typeof(Account), | ||
| typeof(Role), | ||
| typeof(AccountRoleLink) | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// Properties whose values never enter the trail. The adapter property bags are here because | ||
| /// each is stored as a single JSON column, so the change tracker sees one property rather than | ||
| /// the individual keys — there is no way to keep <c>Host</c> and drop <c>Password</c> without | ||
| /// cracking the JSON open and asking the adapter which of its keys are <c>[Secure]</c>, which | ||
| /// would mean starting a serverless adapter in the middle of a save. Recording that a | ||
| /// subscription was edited without saying which adapter key changed is the deliberate trade. | ||
| /// </summary> | ||
| private static readonly Dictionary<Type, HashSet<string>> Redacted = new() | ||
| { | ||
| [typeof(Subscription)] = | ||
| [ | ||
| nameof(Subscription.HandlerProperties), | ||
| nameof(Subscription.MapperProperties), | ||
| nameof(Subscription.ReceiverProperties), | ||
| nameof(Subscription.ValidatorProperties) | ||
| ], | ||
| [typeof(Partner)] = [nameof(Partner.AdapterProperties)], | ||
| [typeof(Notifier)] = [nameof(Notifier.HandlerProperties)], | ||
| [typeof(RetryPolicy)] = [nameof(RetryPolicy.AlertHandlerProperties)], | ||
| [typeof(RetryAlertOverride)] = [nameof(RetryAlertOverride.AlertHandlerProperties)], | ||
|
|
||
| // Free-form values an adapter reads by name. Nothing marks which of them is a credential, | ||
| // so there is no safe subset to keep. | ||
| [typeof(GlobalAdapterValuesSet)] = [nameof(GlobalAdapterValuesSet.Values)], | ||
|
|
||
| // The key itself is the credential. Name survives, so adding or revoking one is visible. | ||
| [typeof(ApiCredential)] = [nameof(ApiCredential.Key)], | ||
|
|
||
| [typeof(Account)] = [nameof(Account.Password)] | ||
| }; | ||
|
|
||
| public static readonly AuditOptions Options = new() | ||
| { | ||
| ShouldAuditEntity = entry => Audited.Contains(entry.Metadata.ClrType), | ||
| ShouldAuditProperty = ShouldAuditProperty | ||
| }; | ||
|
|
||
| private static bool ShouldAuditProperty(EntityEntry entry, IProperty property) | ||
| { | ||
| if (Redacted.TryGetValue(entry.Metadata.ClrType, out var redacted) && | ||
| redacted.Contains(property.Name)) | ||
| return false; | ||
|
|
||
| // A setting's value is only sometimes a secret, and unlike an adapter's properties the | ||
| // catalog says which — a static lookup, no adapter to start — so the theme colour stays | ||
| // readable in the trail while the licence key never enters it. | ||
| if (entry.Entity is Setting setting && property.Name == nameof(Setting.Value)) | ||
| return SettingsCatalog.Find(setting.Id)?.Secret != true; | ||
|
|
||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Newtonsoft.Json; | ||
| using SW.EfCoreExtensions; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.Domain; | ||
|
|
||
| /// <summary> | ||
| /// One configuration change, as the change tracker saw it: which entity, which properties, from what | ||
| /// to what, by whom. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Rows are written by <see cref="BitweenDbContext.SaveChangesAsync"/> for the entities | ||
| /// <see cref="AuditPolicy"/> admits, in the same transaction as the change itself — so an entry | ||
| /// exists for every audited change that committed, and for no change that didn't. | ||
| /// </para> | ||
| /// <para> | ||
| /// Nothing edits or deletes an entry. That is the point of the table, and it is why the setters are | ||
| /// private and there is no update path anywhere in the code. | ||
| /// </para> | ||
| /// </remarks> | ||
| public class AuditEntry : BaseEntity<string> | ||
| { | ||
| private AuditEntry() | ||
| { | ||
| } | ||
|
|
||
| public AuditEntry(GenericAuditDiffJson diff) | ||
| { | ||
| Id = Guid.NewGuid().ToString("N"); | ||
| CorrelationId = diff.CorrelationId; | ||
| Sequence = diff.Sequence; | ||
| // The library hands back a DateTimeOffset. Every other timestamp in Bitween is a UTC | ||
| // DateTime, and MySQL has no offset type to store one in, so it is flattened here rather | ||
| // than becoming the one column that behaves differently on one of the three providers. | ||
| OccurredOn = diff.Timestamp.UtcDateTime; | ||
| UserId = diff.UserId; | ||
| EntityName = diff.EntityName; | ||
| EntityKey = FlattenKey(diff.PrimaryKey); | ||
| State = diff.State; | ||
| Changes = JsonConvert.SerializeObject(diff.Changes); | ||
| } | ||
|
|
||
| /// <summary>Shared by every entry written by the same save, so one request reads as one change.</summary> | ||
| public string CorrelationId { get; private set; } | ||
|
|
||
| /// <summary>Position within that save, 1-based. Only meaningful alongside <see cref="CorrelationId"/>.</summary> | ||
| public int Sequence { get; private set; } | ||
|
|
||
| public DateTime OccurredOn { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// The account id behind the change, or null for a save with no signed-in user — a bus consumer | ||
| /// or a scheduled job. Kept as the id rather than a name so that renaming an account doesn't | ||
| /// rewrite history. | ||
| /// </summary> | ||
| public string UserId { get; private set; } | ||
|
|
||
| /// <summary>The entity's display name, e.g. <c>Subscription</c>.</summary> | ||
| public string EntityName { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// The primary key as a single value, so the history of one row is an indexed lookup rather than | ||
| /// a JSON scan. Composite keys are joined with <c>|</c> in property-name order. | ||
| /// </summary> | ||
| public string EntityKey { get; private set; } | ||
|
|
||
| /// <summary><c>Added</c>, <c>Modified</c> or <c>Deleted</c>.</summary> | ||
| public string State { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// The per-property before and after values, as a JSON object of | ||
| /// <c>{ "PropertyName": { "Old": …, "New": … } }</c>. Properties excluded by | ||
| /// <see cref="AuditPolicy"/> never reach it. | ||
| /// </summary> | ||
| public string Changes { get; private set; } | ||
|
|
||
| private static string FlattenKey(object primaryKey) => | ||
| primaryKey is IDictionary<string, object> values | ||
| ? string.Join("|", values.OrderBy(kv => kv.Key).Select(kv => kv.Value?.ToString())) | ||
| : primaryKey?.ToString(); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.