-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support masking properties that have a Uri as the value
- Loading branch information
1 parent
40445e6
commit fd8ee21
Showing
4 changed files
with
116 additions
and
1 deletion.
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
96 changes: 96 additions & 0 deletions
96
test/Serilog.Enrichers.Sensitive.Tests.Unit/WhenMaskingLogEventWithNonStringScalarValue.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,96 @@ | ||
using Serilog.Core; | ||
using Serilog.Sinks.InMemory; | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Serilog.Sinks.InMemory.Assertions; | ||
using Xunit; | ||
|
||
namespace Serilog.Enrichers.Sensitive.Tests.Unit | ||
{ | ||
public class WhenMaskingLogEventWithNonStringScalarValue | ||
{ | ||
private readonly InMemorySink _inMemorySnk; | ||
private readonly Logger _logger; | ||
|
||
[Fact] | ||
public void GivenPropertyValueIsUri_ValueIsMasked() | ||
{ | ||
_logger.Information("Message {Prop}", new Uri("https://tempuri.org?someparam=SENSITIVE")); | ||
|
||
_inMemorySnk | ||
.Snapshot() | ||
.Should() | ||
.HaveMessage("Message {Prop}") | ||
.Appearing() | ||
.Once() | ||
.WithProperty("Prop") | ||
.WithValue(new Uri("https://tempuri.org?***MASKED***")); | ||
} | ||
|
||
[Fact] | ||
public void GivenPropertyValueIsTypeWithToStringOverride_ValueIsMasked() | ||
{ | ||
_logger.Information("Message {Prop}", new TypeWithToStringOverride("my SECRET message")); | ||
|
||
_inMemorySnk | ||
.Snapshot() | ||
.Should() | ||
.HaveMessage("Message {Prop}") | ||
.Appearing() | ||
.Once() | ||
.WithProperty("Prop") | ||
.WithValue("my ***MASKED*** message"); | ||
} | ||
|
||
public WhenMaskingLogEventWithNonStringScalarValue() | ||
{ | ||
_inMemorySnk = new InMemorySink(); | ||
|
||
_logger = new LoggerConfiguration() | ||
.Enrich.WithSensitiveDataMasking(options => | ||
{ | ||
options.MaskingOperators.Add(new UriMaskingOperator()); | ||
options.MaskingOperators.Add(new TestRegexMaskingOperator()); | ||
}) | ||
.WriteTo.Sink(_inMemorySnk) | ||
.CreateLogger(); | ||
} | ||
} | ||
|
||
public class TestRegexMaskingOperator : RegexMaskingOperator | ||
{ | ||
public TestRegexMaskingOperator() : base("SECRET", RegexOptions.Compiled) | ||
{ | ||
} | ||
} | ||
|
||
public class TypeWithToStringOverride | ||
{ | ||
private readonly string _value; | ||
|
||
public TypeWithToStringOverride(string value) | ||
{ | ||
_value = value; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return _value; | ||
} | ||
} | ||
|
||
public class UriMaskingOperator : RegexMaskingOperator | ||
{ | ||
private const string SomePattern = | ||
"someparam=.*?(.(?:&|$))"; | ||
|
||
public UriMaskingOperator() : base(SomePattern, RegexOptions.IgnoreCase | RegexOptions.Compiled) | ||
{ | ||
} | ||
|
||
protected override string PreprocessInput(string input) | ||
{ | ||
return input; | ||
} | ||
} | ||
} |