Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ public class CustomizedMaskedLogs
[LogMasked]
public string? DefaultMasked { get; set; }

/// <summary>
/// 9223372036854775807 results in "***"
/// </summary>
[LogMasked]
public long? DefaultMaskedLong { get; set; }

/// <summary>
/// 2147483647 results in "***"
/// </summary>
[LogMasked]
public int? DefaultMaskedInt { get; set; }

/// <summary>
/// [123456789,123456789,123456789] results in [***,***,***]
/// </summary>
Expand Down Expand Up @@ -211,6 +223,18 @@ public class CustomizedMaskedLogs
[LogMasked(ShowFirst = 3)]
public string? ShowFirstThreeThenDefaultMasked { get; set; }

/// <summary>
/// 9223372036854775807 results in "922***807"
/// </summary>
[LogMasked(ShowFirst = 3, ShowLast = 3)]
public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; }

/// <summary>
/// 2147483647 results in "214****647"
/// </summary>
[LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
public long? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123******"
/// </summary>
Expand All @@ -230,13 +254,19 @@ public class CustomizedMaskedLogs
public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123REMOVED"
/// 123456789 results in "123_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3)]
public string? ShowFirstThreeThenCustomMask { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)]
public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; }
Expand Down Expand Up @@ -284,7 +314,7 @@ public class CustomizedMaskedLogs
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}
```
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L8-L133' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomizedMaskedLogs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L8-L163' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomizedMaskedLogs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 7. Masking a string property with regular expressions
Expand Down
131 changes: 129 additions & 2 deletions src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
[LogMasked]
public string? DefaultMasked { get; set; }

/// <summary>
/// 9223372036854775807 results in "***"
/// </summary>
[LogMasked]
public long? DefaultMaskedLong { get; set; }

/// <summary>
/// 2147483647 results in "***"
/// </summary>
[LogMasked]
public int? DefaultMaskedInt { get; set; }

/// <summary>
/// [123456789,123456789,123456789] results in [***,***,***]
/// </summary>
Expand Down Expand Up @@ -57,6 +69,18 @@
[LogMasked(ShowFirst = 3)]
public string? ShowFirstThreeThenDefaultMasked { get; set; }

/// <summary>
/// 9223372036854775807 results in "922***807"
/// </summary>
[LogMasked(ShowFirst = 3, ShowLast = 3)]
public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; }

/// <summary>
/// 2147483647 results in "214****647"
/// </summary>
[LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
public long? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123******"
/// </summary>
Expand All @@ -76,13 +100,19 @@
public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123REMOVED"
/// 123456789 results in "123_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3)]
public string? ShowFirstThreeThenCustomMask { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)]
public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; }
Expand Down Expand Up @@ -289,6 +319,25 @@
props["ShowFirstThreeThenCustomMask"].LiteralValue().ShouldBe("123_REMOVED_");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_Guid()
{
// [LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
// -> "d3c4a_REMOVED_"
var customized = new CustomizedMaskedLogs
{
ShowFirstFiveThenCustomMaskGuid = Guid.Parse("d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c")
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("ShowFirstFiveThenCustomMaskGuid").ShouldBeTrue();
props["ShowFirstFiveThenCustomMaskGuid"].LiteralValue().ShouldBe("d3c4a_REMOVED_");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_PreservedLength_Ignored()
{
Expand Down Expand Up @@ -691,6 +740,84 @@
props["ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored"].LiteralValue().ShouldBe("123_REMOVED_321");
}

[Test]
public void LogMaskedAttribute_Replaces_Long_Value_With_DefaultStars_Mask()
{
// [LogMasked]
// 9223372036854775807 -> "***"
var customized = new CustomizedMaskedLogs
{
DefaultMaskedLong = long.MaxValue
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("DefaultMaskedLong").ShouldBeTrue();
props["DefaultMaskedLong"].LiteralValue().ShouldBe("***");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Long_Value_With_Default_StarMask()
{
// [LogMasked(ShowFirst = 3, ShowLast = 3)]
// 9223372036854775807 -> "922***807"
var customized = new CustomizedMaskedLogs
{
ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle = long.MaxValue
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle").ShouldBeTrue();
props["ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle"].LiteralValue().ShouldBe("922***807");
}

[Test]
public void LogMaskedAttribute_Replaces_Int_Value_With_DefaultStars_Mask()
{
// [LogMasked]
// 2147483647 -> "***"
var customized = new CustomizedMaskedLogs
{
DefaultMaskedInt = int.MaxValue
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("DefaultMaskedInt").ShouldBeTrue();
props["DefaultMaskedInt"].LiteralValue().ShouldBe("***");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Int_Value_With_Default_StarMask_And_PreservedLength()
{
// [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
// 2147483647 -> "214****647"

var customized = new CustomizedMaskedLogs
{
ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength = int.MaxValue
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength").ShouldBeTrue();
props["ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength"].LiteralValue().ShouldBe("214****647");
}


[Test]
public void LogMaskedAttribute_Nullify_Bool_Property()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ private LogEventPropertyValue CreateValue(object? value)
{
IEnumerable<string> strings => new SequenceValue(strings.Select(s => new ScalarValue(FormatMaskedValue(s)))),
string s => new ScalarValue(FormatMaskedValue(s)),
_ => ScalarValue.Null
long l => new ScalarValue(FormatMaskedValue(l.ToString())),
int i => new ScalarValue(FormatMaskedValue(i.ToString())),
Guid g => new ScalarValue(FormatMaskedValue(g.ToString())),
_ => ScalarValue.Null,
};
}
}
Loading