-
-
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.
Merge branch 'master' into feature/pathmask
- Loading branch information
Showing
11 changed files
with
377 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
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,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; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
test/Serilog.Enrichers.Sensitive.Tests.Unit/Serilog.Enrichers.Sensitive.Tests.Unit.csproj
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.