-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dc043f
commit dfcfa19
Showing
6 changed files
with
260 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Serilog.Enrichers.Sensitive; | ||
|
||
public class MaskOptions | ||
{ | ||
public static readonly MaskOptions Default= new(); | ||
public const int NotSet = -1; | ||
public int ShowFirst { get; set; } = NotSet; | ||
public int ShowLast { get; set; } = NotSet; | ||
public bool PreserveLength { get; set; } = true; | ||
|
||
} |
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,35 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Serilog.Enrichers.Sensitive; | ||
|
||
public class MaskPropertyCollection : List<MaskProperty> | ||
{ | ||
private readonly Dictionary<string, MaskOptions> _properties = new(); | ||
|
||
public void Add(string propertyName) | ||
{ | ||
_properties.Add(propertyName.ToLower(), MaskOptions.Default); | ||
} | ||
|
||
public void Add(string propertyName, MaskOptions maskOptions) | ||
{ | ||
_properties.Add(propertyName.ToLower(), maskOptions); | ||
} | ||
|
||
public bool TryGetProperty(string propertyName, out MaskOptions options) | ||
{ | ||
return _properties.TryGetValue(propertyName.ToLower(), out options); | ||
} | ||
|
||
public static MaskPropertyCollection From(IEnumerable<string> enricherOptionsMaskProperties) | ||
{ | ||
var collection = new MaskPropertyCollection(); | ||
|
||
foreach (var x in enricherOptionsMaskProperties) | ||
{ | ||
collection.Add(x, MaskOptions.Default); | ||
} | ||
|
||
return collection; | ||
} | ||
} |
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,9 @@ | ||
namespace Serilog.Enrichers.Sensitive; | ||
|
||
public class UriMaskOptions : MaskOptions | ||
{ | ||
public bool ShowScheme { get; set; } = true; | ||
public bool ShowHost { get; set; } = true; | ||
public bool ShowPath { get; set; } = false; | ||
public bool ShowQueryString { get; set; } = false; | ||
} |
75 changes: 75 additions & 0 deletions
75
test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingWithOptions.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,75 @@ | ||
using System; | ||
using Serilog.Sinks.InMemory; | ||
using Serilog.Sinks.InMemory.Assertions; | ||
using Xunit; | ||
|
||
namespace Serilog.Enrichers.Sensitive.Tests.Unit | ||
{ | ||
public class WhenMaskingWithOptions | ||
{ | ||
[Theory] | ||
[InlineData("1234567890", 2, 2, true, "12******90")] | ||
[InlineData("1234567890", 2, 2, false, "12***90")] | ||
[InlineData("1234567890", 2, -5, false, "12***")] | ||
[InlineData("1234567890", 2, -5, true, "12********")] | ||
[InlineData("1234567890", -5, 2, false, "***90")] | ||
[InlineData("1234567890", -5, 2, true, "********90")] | ||
[InlineData("1234", 2, 2, true, "1***4")] | ||
[InlineData("1234", 2, 3, true, "1***4")] | ||
[InlineData("124", 2, 2, true, "1***")] | ||
[InlineData("12", 2, 2, true, SensitiveDataEnricher.DefaultMaskValue)] | ||
[InlineData("1234", 5, MaskOptions.NotSet, true, "1***")] | ||
[InlineData("1234", MaskOptions.NotSet, 5, true, "***4")] | ||
public void GivenMaskOptionsWithInputShorterThanNumberOfCharactersThatShouldBeShown(string input, int showFirst, int showLast, bool preserveLength, string expectedValue) | ||
{ | ||
var inMemorySink = new InMemorySink(); | ||
var logger = new LoggerConfiguration() | ||
.Enrich.WithSensitiveDataMasking( | ||
options => options.MaskProperties.Add("Prop", new MaskOptions{ ShowFirst = showFirst, ShowLast = showLast, PreserveLength = preserveLength})) | ||
.WriteTo.Sink(inMemorySink) | ||
.CreateLogger(); | ||
|
||
logger.Information("{Prop}", input); | ||
|
||
inMemorySink | ||
.Should() | ||
.HaveMessage("{Prop}") | ||
.Appearing().Once() | ||
.WithProperty("Prop") | ||
.WithValue(expectedValue); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, false ,false, false, "https://***/***?***")] | ||
[InlineData(true, true ,false, false, "https://example.com/***?***")] | ||
[InlineData(true, true,true, false, "https://example.com/some/sensitive/path?***")] | ||
[InlineData(true, false,true, true, "https://***/some/sensitive/path?foo=bar")] | ||
[InlineData(false, false,true, true, "***://***/some/sensitive/path?foo=bar")] | ||
[InlineData(false, false,true, false, "***://***/some/sensitive/path?***")] | ||
public void GivenUriMaskOptions(bool showScheme, bool showHost, bool showPath, bool showQuery, | ||
string expectedValue) | ||
{ | ||
var inMemorySink = new InMemorySink(); | ||
var logger = new LoggerConfiguration() | ||
.Enrich.WithSensitiveDataMasking( | ||
options => options.MaskProperties.Add("Prop", new UriMaskOptions | ||
{ | ||
ShowScheme = showScheme, | ||
ShowHost = showHost, | ||
ShowPath = showPath, | ||
ShowQueryString = showQuery | ||
})) | ||
.WriteTo.Sink(inMemorySink) | ||
.CreateLogger(); | ||
|
||
logger.Information("{Prop}", new Uri("https://example.com/some/sensitive/path?foo=bar")); | ||
|
||
inMemorySink | ||
.Should() | ||
.HaveMessage("{Prop}") | ||
.Appearing().Once() | ||
.WithProperty("Prop") | ||
.WithValue(expectedValue); | ||
} | ||
} | ||
} |