From b798c309e0ee963c710553206d16a55a711ecddf Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:05:34 +0200 Subject: [PATCH 01/11] Add path wrapper --- .../IPathWrapper.cs | 8 +++ .../PathMaskingOperator.cs | 52 +++++++++++++++++++ .../PathWrapper.cs | 19 +++++++ 3 files changed, 79 insertions(+) create mode 100644 src/Serilog.Enrichers.Sensitive/IPathWrapper.cs create mode 100644 src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs create mode 100644 src/Serilog.Enrichers.Sensitive/PathWrapper.cs diff --git a/src/Serilog.Enrichers.Sensitive/IPathWrapper.cs b/src/Serilog.Enrichers.Sensitive/IPathWrapper.cs new file mode 100644 index 0000000..ad4fea9 --- /dev/null +++ b/src/Serilog.Enrichers.Sensitive/IPathWrapper.cs @@ -0,0 +1,8 @@ +namespace Serilog.Enrichers.Sensitive; + +public interface IPathWrapper +{ + bool IsDirectory(string path); + string GetFileName(string path); + string GetDirectoryName(string path); +} \ No newline at end of file diff --git a/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs b/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs new file mode 100644 index 0000000..ff377ec --- /dev/null +++ b/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs @@ -0,0 +1,52 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.RegularExpressions; + +namespace Serilog.Enrichers.Sensitive; + +/// +/// Represents a masking operator for path names. +/// Supports path names for Windows, Linux, and macOS. +/// +public class PathMaskingOperator : RegexMaskingOperator +{ + private readonly IPathWrapper _pathWrapper; + private readonly bool _combineMaskWithPath; + private const string PathPattern = + @"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$"; + + /// + /// Initializes a new instance of the class. + /// + /// The path wrapper. + /// This means if set to then the mask will combine with the file name or its directory name. + public PathMaskingOperator(IPathWrapper pathWrapper, bool combineMaskWithPath = true) : base(PathPattern) + { + _pathWrapper = pathWrapper; + _combineMaskWithPath = combineMaskWithPath; + } + /// + /// Initializes a new instance of the class, with default . + /// + /// This means if set to then the mask will combine with the file name or its directory name. + public PathMaskingOperator(bool combineMaskWithPath = true) : this(new PathWrapper(), combineMaskWithPath) + { + } + /// + /// Initializes a new instance of the class. + /// + public PathMaskingOperator() : this(combineMaskWithPath: true) + { + } + [SuppressMessage("ReSharper", "InvertIf")] + protected override string PreprocessMask(string mask, Match match) + { + if (_combineMaskWithPath) + { + var value = match.Value; + return _pathWrapper.IsDirectory(value) + ? mask + _pathWrapper.GetDirectoryName(value) + : mask + _pathWrapper.GetFileName(value); + } + return mask; + } +} \ No newline at end of file diff --git a/src/Serilog.Enrichers.Sensitive/PathWrapper.cs b/src/Serilog.Enrichers.Sensitive/PathWrapper.cs new file mode 100644 index 0000000..de8be98 --- /dev/null +++ b/src/Serilog.Enrichers.Sensitive/PathWrapper.cs @@ -0,0 +1,19 @@ +using System.IO; + +namespace Serilog.Enrichers.Sensitive; + +public class PathWrapper : IPathWrapper +{ + public bool IsDirectory(string path) + { + return Path.GetExtension(path) == string.Empty; + } + public string GetFileName(string path) + { + return Path.GetFileName(path); + } + public string GetDirectoryName(string path) + { + return new DirectoryInfo(path).Name; + } +} \ No newline at end of file From 9fc309f250d6be814555f9579e752235017ed17a Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:05:57 +0200 Subject: [PATCH 02/11] Add tests --- .../WhenMaskingPaths.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingPaths.cs diff --git a/test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingPaths.cs b/test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingPaths.cs new file mode 100644 index 0000000..4380acd --- /dev/null +++ b/test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingPaths.cs @@ -0,0 +1,39 @@ +using FluentAssertions; +using Xunit; + +namespace Serilog.Enrichers.Sensitive.Tests.Unit; + +public class WhenMaskingPaths +{ + private const string Mask = @"***\"; + + [Theory] + [InlineData(@"C:\Users\Admin\Secret\File.dll", @"***\", false)] + [InlineData(@"C:\Users\Admin\Secret\File.dll", @"***\File.dll", true)] + [InlineData(@"C:\Users\Admin\Secret\Hidden\File.dll", @"***\File.dll", true)] + [InlineData(@"C:\Users\Admin\Secret\Hidden", @"***\", false)] + [InlineData(@"C:\Users\Admin\Secret\Hidden", @"***\Hidden", true)] + [InlineData(@"C:\Users\Admin\Secret", @"***\Secret", true)] + [InlineData(@"C:\Users\", @"***\Users", true)] + [InlineData(@"/home/i_use_arch_linux_btw", @"***\i_use_arch_linux_btw", true)] + [InlineData(@"/home/i_use_arch_linux_btw", @"***\", false)] + [InlineData(@"C:\", @"***\C:\", true)] + [InlineData(@"C:\", @"***\", false)] + [InlineData("File.txt", "File.txt", false)] + [InlineData(@"This is not a path", "This is not a path", false)] + public void GivenPaths_ReturnsExpectedResult(string path, string result, bool combineMaskWithPath) + { + TheMaskedResultOf(path, combineMaskWithPath) + .Should() + .Be(result); + } + + private static string TheMaskedResultOf(string input, bool combineMaskWithPath) + { + var maskingResult = new PathMaskingOperator(combineMaskWithPath) + .Mask(input, Mask); + return maskingResult.Match + ? maskingResult.Result + : input; + } +} \ No newline at end of file From 7ec4223b4f381d2b15c3b866c710c45debdce584 Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:06:16 +0200 Subject: [PATCH 03/11] Add demo --- src/Serilog.Enrichers.Sensitive.Demo/Program.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Serilog.Enrichers.Sensitive.Demo/Program.cs b/src/Serilog.Enrichers.Sensitive.Demo/Program.cs index fcafa5a..b97b7dd 100644 --- a/src/Serilog.Enrichers.Sensitive.Demo/Program.cs +++ b/src/Serilog.Enrichers.Sensitive.Demo/Program.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Threading.Tasks; using Serilog.Core; @@ -13,7 +14,8 @@ static async Task Main(string[] args) { new EmailAddressMaskingOperator(), new IbanMaskingOperator(), - new CreditCardMaskingOperator(false) + new CreditCardMaskingOperator(false), + new PathMaskingOperator() }) .WriteTo.Console() .CreateLogger(); @@ -43,16 +45,24 @@ static async Task Main(string[] args) Key = 12345, XmlValue = "4111111111111111" }; logger.Information("Object dump with embedded credit card: {x}", x); + + // Path to the file is also masked + logger.Information("This is path to the file: {0}", + @"E:\SuperSecretPath\test.txt"); + + // Path to the directory is also masked + logger.Information("This is path to the directory: {0}", @"C:\Admin\"); } // But outside the sensitive area nothing is masked logger.Information("Felix can be reached at felix@cia.gov"); + logger.Information("This is your path to the file: {file}", new FileInfo("test.txt").FullName); // Now, show that this works for async contexts too logger.Information("Now, show the Async works"); - + var t1 = LogAsSensitiveAsync(logger); var t2 = LogAsUnsensitiveAsync(logger); From 50c80db0f293a0ad42882625c35d09a067f1cbf8 Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:31:38 +0200 Subject: [PATCH 04/11] Add benchmark --- .../PathMaskingOperatorBenchmarks.cs | 37 +++++++++++++++++++ .../Program.cs | 1 + 2 files changed, 38 insertions(+) create mode 100644 test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs diff --git a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs new file mode 100644 index 0000000..ae6534e --- /dev/null +++ b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs @@ -0,0 +1,37 @@ +using System.Text.RegularExpressions; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Engines; + +namespace Serilog.Enrichers.Sensitive.Tests.Benchmark; + +[SimpleJob(RunStrategy.Throughput, warmupCount: 1)] +public class PathMaskingOperatorBenchmarks +{ + private const string PathInput = "/home/me"; + private const string MaskValue = "***MASKED***"; + + private readonly Regex PathRegex = + new(@"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$"); + + private readonly Regex PathCompiledRegex = + new(@"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$", + RegexOptions.Compiled); + + [Benchmark] + public string PathRegexReplace() + { + string result = null; + for (var i = 0; i < 10000; i++) + result = PathRegex.Replace(PathInput, MaskValue); + return result; + } + + [Benchmark] + public string PathRegexCompiledReplace() + { + string result = null; + for (var i = 0; i < 10000; i++) + result = PathCompiledRegex.Replace(PathInput, MaskValue); + return result; + } +} \ No newline at end of file diff --git a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs index a995081..ff8f835 100644 --- a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs +++ b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs @@ -9,6 +9,7 @@ private static void Main(string[] args) BenchmarkRunner.Run(); BenchmarkRunner.Run(); BenchmarkRunner.Run(); + BenchmarkRunner.Run(); } } } From 144b637784ef0beb8f6c02d773d8f8bfbe6d71b5 Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:32:48 +0200 Subject: [PATCH 05/11] Update version to 0.2.0.0 --- .../Serilog.Enrichers.Sensitive.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj b/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj index e7e4b39..0817a30 100644 --- a/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj +++ b/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj @@ -8,11 +8,11 @@ true - 0.1.0.0 + 0.2.0.0 Serilog enricher to mask sensitive data An enricher to be used for masking sensitive (PII) data using Serilog 2021 Sander van Vliet - Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman + Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman, sunnamed434 https://github.com/serilog-contrib/Serilog.Enrichers.Sensitive/README.md MIT https://github.com/serilog-contrib/Serilog.Enrichers.Sensitive/ From 9c9b50621570cdbb76ac3a236dbf141defc3dbaf Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:33:38 +0200 Subject: [PATCH 06/11] Update README and add new PathMaskingOperator --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7220f52..fd1c449 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ By default the enricher uses the following masking operators: - EmailAddressMaskingOperator - IbanMaskingOperator - CreditCardMaskingOperator +- PathMaskingOperator It's good practice to only configure the masking operators that are applicable for your application. For example: From d4c2ac8e764d87a28c11905dfb94881364127b80 Mon Sep 17 00:00:00 2001 From: sunnamed434 <65300126+sunnamed434@users.noreply.github.com> Date: Mon, 20 Feb 2023 12:17:44 +0200 Subject: [PATCH 07/11] Update version correctly --- Directory.Build.props | 4 ++-- .../Serilog.Enrichers.Sensitive.csproj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 66f8453..56c36b9 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.7.1.0 - Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman + 1.8.0.0 + Sander van Vliet, Huibert Jan Nieuwkamer, Scott Toberman, sunnamed434 Codenizer BV 2023 Sander van Vliet diff --git a/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj b/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj index 0817a30..0e59a01 100644 --- a/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj +++ b/src/Serilog.Enrichers.Sensitive/Serilog.Enrichers.Sensitive.csproj @@ -8,7 +8,7 @@ true - 0.2.0.0 + 0.1.0.0 Serilog enricher to mask sensitive data An enricher to be used for masking sensitive (PII) data using Serilog 2021 Sander van Vliet From 2296354a3bad93da6d3e08149002bfd279c93b48 Mon Sep 17 00:00:00 2001 From: sunnamed434 Date: Sat, 4 Mar 2023 18:57:50 +0200 Subject: [PATCH 08/11] Remove benchmark --- .../PathMaskingOperatorBenchmarks.cs | 37 ------------------- .../Program.cs | 1 - 2 files changed, 38 deletions(-) delete mode 100644 test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs diff --git a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs deleted file mode 100644 index ae6534e..0000000 --- a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/PathMaskingOperatorBenchmarks.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Text.RegularExpressions; -using BenchmarkDotNet.Attributes; -using BenchmarkDotNet.Engines; - -namespace Serilog.Enrichers.Sensitive.Tests.Benchmark; - -[SimpleJob(RunStrategy.Throughput, warmupCount: 1)] -public class PathMaskingOperatorBenchmarks -{ - private const string PathInput = "/home/me"; - private const string MaskValue = "***MASKED***"; - - private readonly Regex PathRegex = - new(@"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$"); - - private readonly Regex PathCompiledRegex = - new(@"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$", - RegexOptions.Compiled); - - [Benchmark] - public string PathRegexReplace() - { - string result = null; - for (var i = 0; i < 10000; i++) - result = PathRegex.Replace(PathInput, MaskValue); - return result; - } - - [Benchmark] - public string PathRegexCompiledReplace() - { - string result = null; - for (var i = 0; i < 10000; i++) - result = PathCompiledRegex.Replace(PathInput, MaskValue); - return result; - } -} \ No newline at end of file diff --git a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs index ff8f835..a995081 100644 --- a/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs +++ b/test/Serilog.Enrichers.Sensitive.Tests.Benchmark/Program.cs @@ -9,7 +9,6 @@ private static void Main(string[] args) BenchmarkRunner.Run(); BenchmarkRunner.Run(); BenchmarkRunner.Run(); - BenchmarkRunner.Run(); } } } From 968e9b9a91c81357ef5d3004187be9012dcf1edc Mon Sep 17 00:00:00 2001 From: sunnamed434 Date: Sat, 4 Mar 2023 18:58:03 +0200 Subject: [PATCH 09/11] Add PathMaskingOperator as new default masking operator --- src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs b/src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs index 6c37b5e..b1b71d6 100644 --- a/src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs +++ b/src/Serilog.Enrichers.Sensitive/SensitiveDataEnricher.cs @@ -220,7 +220,8 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { new EmailAddressMaskingOperator(), new IbanMaskingOperator(), - new CreditCardMaskingOperator() + new CreditCardMaskingOperator(), + new PathMaskingOperator() }; } From 163a2156be85370dd0951dd5aee5bd5550108ab2 Mon Sep 17 00:00:00 2001 From: sunnamed434 Date: Sat, 4 Mar 2023 18:58:40 +0200 Subject: [PATCH 10/11] Code refactoring --- .../PathMaskingOperator.cs | 22 ++++++++----------- .../PathWrapper.cs | 2 ++ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs b/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs index ff377ec..0c2814a 100644 --- a/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs +++ b/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs @@ -10,7 +10,7 @@ namespace Serilog.Enrichers.Sensitive; public class PathMaskingOperator : RegexMaskingOperator { private readonly IPathWrapper _pathWrapper; - private readonly bool _combineMaskWithPath; + private readonly bool _keepLastPartOfPath; private const string PathPattern = @"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$"; @@ -18,29 +18,25 @@ public class PathMaskingOperator : RegexMaskingOperator /// Initializes a new instance of the class. /// /// The path wrapper. - /// This means if set to then the mask will combine with the file name or its directory name. - public PathMaskingOperator(IPathWrapper pathWrapper, bool combineMaskWithPath = true) : base(PathPattern) + /// This means if set to then the mask will combine with the file name or its directory name. + // ReSharper disable once MemberCanBePrivate.Global + public PathMaskingOperator(IPathWrapper pathWrapper, bool keepLastPartOfPath = true) : base(PathPattern) { _pathWrapper = pathWrapper; - _combineMaskWithPath = combineMaskWithPath; + _keepLastPartOfPath = keepLastPartOfPath; } /// /// Initializes a new instance of the class, with default . /// - /// This means if set to then the mask will combine with the file name or its directory name. - public PathMaskingOperator(bool combineMaskWithPath = true) : this(new PathWrapper(), combineMaskWithPath) - { - } - /// - /// Initializes a new instance of the class. - /// - public PathMaskingOperator() : this(combineMaskWithPath: true) + /// This means if set to then the mask will combine with the file name or its directory name. + public PathMaskingOperator(bool keepLastPartOfPath = true) : this(PathWrapper.Instance, keepLastPartOfPath) { } + [SuppressMessage("ReSharper", "InvertIf")] protected override string PreprocessMask(string mask, Match match) { - if (_combineMaskWithPath) + if (_keepLastPartOfPath) { var value = match.Value; return _pathWrapper.IsDirectory(value) diff --git a/src/Serilog.Enrichers.Sensitive/PathWrapper.cs b/src/Serilog.Enrichers.Sensitive/PathWrapper.cs index de8be98..cf0591f 100644 --- a/src/Serilog.Enrichers.Sensitive/PathWrapper.cs +++ b/src/Serilog.Enrichers.Sensitive/PathWrapper.cs @@ -4,6 +4,8 @@ namespace Serilog.Enrichers.Sensitive; public class PathWrapper : IPathWrapper { + public static readonly PathWrapper Instance = new(); + public bool IsDirectory(string path) { return Path.GetExtension(path) == string.Empty; From 245da54c81171072e0e427ed9b35ab34b3acb067 Mon Sep 17 00:00:00 2001 From: sunnamed434 Date: Sun, 5 Mar 2023 12:33:29 +0200 Subject: [PATCH 11/11] Remove path wrapper --- .../IPathWrapper.cs | 8 ------- .../PathMaskingOperator.cs | 22 +++++-------------- .../PathWrapper.cs | 21 ------------------ 3 files changed, 6 insertions(+), 45 deletions(-) delete mode 100644 src/Serilog.Enrichers.Sensitive/IPathWrapper.cs delete mode 100644 src/Serilog.Enrichers.Sensitive/PathWrapper.cs diff --git a/src/Serilog.Enrichers.Sensitive/IPathWrapper.cs b/src/Serilog.Enrichers.Sensitive/IPathWrapper.cs deleted file mode 100644 index ad4fea9..0000000 --- a/src/Serilog.Enrichers.Sensitive/IPathWrapper.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Serilog.Enrichers.Sensitive; - -public interface IPathWrapper -{ - bool IsDirectory(string path); - string GetFileName(string path); - string GetDirectoryName(string path); -} \ No newline at end of file diff --git a/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs b/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs index 0c2814a..ff23b91 100644 --- a/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs +++ b/src/Serilog.Enrichers.Sensitive/PathMaskingOperator.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Text.RegularExpressions; namespace Serilog.Enrichers.Sensitive; @@ -9,7 +10,6 @@ namespace Serilog.Enrichers.Sensitive; /// public class PathMaskingOperator : RegexMaskingOperator { - private readonly IPathWrapper _pathWrapper; private readonly bool _keepLastPartOfPath; private const string PathPattern = @"^(?:[a-zA-Z]\:|\\\\[\w-]+\\[\w-]+\$?|[\/][^\/\0]+)+(\\[^\\/:*?""<>|]*)*(\\?)?$"; @@ -17,21 +17,11 @@ public class PathMaskingOperator : RegexMaskingOperator /// /// Initializes a new instance of the class. /// - /// The path wrapper. - /// This means if set to then the mask will combine with the file name or its directory name. - // ReSharper disable once MemberCanBePrivate.Global - public PathMaskingOperator(IPathWrapper pathWrapper, bool keepLastPartOfPath = true) : base(PathPattern) + /// If set to then the mask will keep together with file name or its directory name. + public PathMaskingOperator(bool keepLastPartOfPath = true) : base(PathPattern) { - _pathWrapper = pathWrapper; _keepLastPartOfPath = keepLastPartOfPath; } - /// - /// Initializes a new instance of the class, with default . - /// - /// This means if set to then the mask will combine with the file name or its directory name. - public PathMaskingOperator(bool keepLastPartOfPath = true) : this(PathWrapper.Instance, keepLastPartOfPath) - { - } [SuppressMessage("ReSharper", "InvertIf")] protected override string PreprocessMask(string mask, Match match) @@ -39,9 +29,9 @@ protected override string PreprocessMask(string mask, Match match) if (_keepLastPartOfPath) { var value = match.Value; - return _pathWrapper.IsDirectory(value) - ? mask + _pathWrapper.GetDirectoryName(value) - : mask + _pathWrapper.GetFileName(value); + return Path.GetExtension(value) == string.Empty + ? mask + new DirectoryInfo(value).Name + : mask + Path.GetFileName(value); } return mask; } diff --git a/src/Serilog.Enrichers.Sensitive/PathWrapper.cs b/src/Serilog.Enrichers.Sensitive/PathWrapper.cs deleted file mode 100644 index cf0591f..0000000 --- a/src/Serilog.Enrichers.Sensitive/PathWrapper.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.IO; - -namespace Serilog.Enrichers.Sensitive; - -public class PathWrapper : IPathWrapper -{ - public static readonly PathWrapper Instance = new(); - - public bool IsDirectory(string path) - { - return Path.GetExtension(path) == string.Empty; - } - public string GetFileName(string path) - { - return Path.GetFileName(path); - } - public string GetDirectoryName(string path) - { - return new DirectoryInfo(path).Name; - } -} \ No newline at end of file