-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Audit.MongoClient: New extension to audit
MongoClient
instances.
Generate Audit Logs by adding a Command Event Subscriber into the configuration of the MongoDB Driver (#640, #641)
- Loading branch information
thepirat000
authored and
thepirat000
committed
Dec 9, 2023
1 parent
a293e25
commit bc755e6
Showing
44 changed files
with
1,457 additions
and
205 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,49 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<Description>Generate detailed Audit Logs for operations executed within MongoDB.</Description> | ||
<Copyright>Copyright 2023</Copyright> | ||
<AssemblyTitle>Audit.MongoClient</AssemblyTitle> | ||
<Authors>Federico Colombo</Authors> | ||
<TargetFrameworks>net472;netstandard2.0;net5.0</TargetFrameworks> | ||
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants> | ||
<NoWarn>$(NoWarn);1591</NoWarn> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AssemblyName>Audit.MongoClient</AssemblyName> | ||
<PackageId>Audit.MongoClient</PackageId> | ||
<PackageTags>Audit;Trail;Log;MongoDB;Client</PackageTags> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<PackageProjectUrl>https://github.com/thepirat000/Audit.NET</PackageProjectUrl> | ||
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard2.0' ">2.0.3</NetStandardImplicitPackageVersion> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<RepositoryUrl>https://github.com/thepirat000/Audit.NET</RepositoryUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\LICENSE" Pack="true" PackagePath="LICENSE" /> | ||
<None Include="README.md" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MongoDB.Driver" Version="2.22.0" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition=" '$(TargetFramework)' != 'net5.0' "> | ||
<DefineConstants>$(DefineConstants);IS_NK_JSON</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(TargetFramework)' == 'net5.0' "> | ||
<DefineConstants>$(DefineConstants);IS_TEXT_JSON</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Audit.NET\Audit.NET.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="images\icon.png" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
</Project> |
This file contains 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,15 @@ | ||
using Audit.Core; | ||
|
||
namespace Audit.MongoClient | ||
{ | ||
/// <summary> | ||
/// Represents the output of the audit process for a Mongo command event | ||
/// </summary> | ||
public class AuditEventMongoCommand : AuditEvent | ||
{ | ||
/// <summary> | ||
/// Gets or sets the Mongo Command details. | ||
/// </summary> | ||
public MongoCommandEvent Command { get; set; } | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Audit.MongoClient/ConfigurationApi/AuditMongoConfigurator.cs
This file contains 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,72 @@ | ||
using System; | ||
using Audit.Core; | ||
using MongoDB.Driver.Core.Events; | ||
|
||
namespace Audit.MongoClient.ConfigurationApi | ||
{ | ||
public class AuditMongoConfigurator : IAuditMongoConfigurator | ||
{ | ||
internal Func<CommandSucceededEvent, bool> _includeReplyPredicate; | ||
internal Func<CommandStartedEvent, bool> _commandFilter; | ||
internal Func<CommandStartedEvent, string> _eventTypePredicate; | ||
internal EventCreationPolicy? _eventCreationPolicy; | ||
internal AuditDataProvider _auditDataProvider; | ||
internal IAuditScopeFactory _auditScopeFactory; | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator IncludeReply(Func<CommandSucceededEvent, bool> includeReplyPredicate) | ||
{ | ||
_includeReplyPredicate = includeReplyPredicate; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator IncludeReply(bool include = true) | ||
{ | ||
_includeReplyPredicate = _ => include; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator EventType(Func<CommandStartedEvent, string> eventTypePredicate) | ||
{ | ||
_eventTypePredicate = eventTypePredicate; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator EventType(string eventType) | ||
{ | ||
_eventTypePredicate = _ => eventType; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator CommandFilter(Func<CommandStartedEvent, bool> commandFilter) | ||
{ | ||
_commandFilter = commandFilter; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator AuditScopeFactory(IAuditScopeFactory auditScopeFactory) | ||
{ | ||
_auditScopeFactory = auditScopeFactory; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator CreationPolicy(EventCreationPolicy eventCreationPolicy) | ||
{ | ||
_eventCreationPolicy = eventCreationPolicy; | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IAuditMongoConfigurator AuditDataProvider(AuditDataProvider auditDataProvider) | ||
{ | ||
_auditDataProvider = auditDataProvider; | ||
return this; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Audit.MongoClient/ConfigurationApi/ClusterBuilderExtensions.cs
This file contains 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,20 @@ | ||
using System; | ||
using Audit.MongoClient.ConfigurationApi; | ||
using MongoDB.Driver.Core.Configuration; | ||
|
||
namespace Audit.MongoClient | ||
{ | ||
public static class ClusterBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds an Event Subscriber to the MongoDB ClusterBuilder, using the given audit configuration. | ||
/// </summary> | ||
/// <param name="clusterBuilder">The cluster builder</param> | ||
/// <param name="config">The audit configuration. Null to use the default configuration</param> | ||
public static ClusterBuilder AddAuditSubscriber(this ClusterBuilder clusterBuilder, Action<IAuditMongoConfigurator> config = null) | ||
{ | ||
clusterBuilder.Subscribe(new MongoAuditEventSubscriber(config)); | ||
return clusterBuilder; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Audit.MongoClient/ConfigurationApi/IAuditMongoConfigurator.cs
This file contains 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,57 @@ | ||
using System; | ||
using Audit.Core; | ||
using MongoDB.Driver.Core.Events; | ||
|
||
namespace Audit.MongoClient.ConfigurationApi | ||
{ | ||
public interface IAuditMongoConfigurator | ||
{ | ||
/// <summary> | ||
/// Specifies a predicate to determine whether the audit event should include the server reply. The reply is not included by default. | ||
/// </summary> | ||
/// <param name="includeReplyPredicate">A function of the Command Succeeded Event to determine whether to include the the server reply in the audit event</param> | ||
IAuditMongoConfigurator IncludeReply(Func<CommandSucceededEvent, bool> includeReplyPredicate); | ||
|
||
/// <summary> | ||
/// Specifies whether the audit event should include the server reply. The reply is not included by default. | ||
/// </summary> | ||
/// <param name="include">True to include the server reply, false otherwise</param> | ||
IAuditMongoConfigurator IncludeReply(bool include = true); | ||
|
||
/// <summary> | ||
/// Specifies a predicate to determine the event type name on the audit output. | ||
/// </summary> | ||
/// <param name="eventTypeNamePredicate">A function of the Command Start Event to determine the event type name. The following placeholders can be used as part of the string: | ||
/// - {command}: replaced with the command name. | ||
/// </param> | ||
IAuditMongoConfigurator EventType(Func<CommandStartedEvent, string> eventTypeNamePredicate); | ||
/// <summary> | ||
/// Specifies the event type name to use in the audit output. | ||
/// </summary> | ||
/// <param name="eventTypeName">The event type name to use. The following placeholders can be used as part of the string: | ||
/// - {command}: replaced with the command name. | ||
/// </param> | ||
IAuditMongoConfigurator EventType(string eventTypeName); | ||
|
||
/// <summary> | ||
/// Sets a filter function to determine the events to log as a function of the command. By default all commands are logged. | ||
/// </summary> | ||
IAuditMongoConfigurator CommandFilter(Func<CommandStartedEvent, bool> commandFilter); | ||
|
||
/// <summary> | ||
/// Specifies the event creation policy to use for this interception. Default is NULL to use the globally configured creation policy. | ||
/// </summary> | ||
/// <param name="eventCreationPolicy">The creation policy to use</param> | ||
IAuditMongoConfigurator CreationPolicy(EventCreationPolicy eventCreationPolicy); | ||
|
||
/// <summary> | ||
/// Specifies the audit data provider to use. Default is NULL to use the globally configured data provider. | ||
/// </summary> | ||
IAuditMongoConfigurator AuditDataProvider(AuditDataProvider auditDataProvider); | ||
|
||
/// <summary> | ||
/// Specifies the Audit Scope factory to use. Default is NULL to use the default AuditScopeFactory. | ||
/// </summary> | ||
IAuditMongoConfigurator AuditScopeFactory(IAuditScopeFactory auditScopeFactory); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Audit.MongoClient/ConfigurationApi/MongoClientSettingsExtensions.cs
This file contains 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,28 @@ | ||
using System; | ||
using Audit.MongoClient.ConfigurationApi; | ||
using MongoDB.Driver; | ||
|
||
namespace Audit.MongoClient | ||
{ | ||
public static class MongoClientSettingsExtensions | ||
{ | ||
/// <summary> | ||
/// Adds an Event Subscriber to the MongoClientSetting's ClusterBuilder, using the given audit configuration. | ||
/// </summary> | ||
/// <param name="clientSettings">The client settings instance</param> | ||
/// <param name="config">The audit configuration. Null to use the default configuration</param> | ||
public static MongoClientSettings AddAuditSubscriber(this MongoClientSettings clientSettings, Action<IAuditMongoConfigurator> config = null) | ||
{ | ||
|
||
if (clientSettings.ClusterConfigurator == null) | ||
{ | ||
clientSettings.ClusterConfigurator = cc => cc.AddAuditSubscriber(config); | ||
} | ||
else | ||
{ | ||
throw new ArgumentException("Adding an Audit Subscriber to MongoClientSettings is not possible due to an existing ClusterConfigurator. Instead, utilize the AddAuditSubscriber function provided by the ClusterBuilder."); | ||
} | ||
return clientSettings; | ||
} | ||
} | ||
} |
Oops, something went wrong.