Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogMaskedAttribute to handle int,long and guid values #126

Merged
merged 7 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 32 additions & 2 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 int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123******"
/// </summary>
Expand All @@ -230,11 +254,17 @@ 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>
/// 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>
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
129 changes: 128 additions & 1 deletion src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -57,6 +69,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 int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123******"
/// </summary>
Expand All @@ -76,11 +100,17 @@ 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>
/// 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>
Expand Down Expand Up @@ -289,6 +319,25 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_
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();
Dismissed Show dismissed Hide dismissed
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 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_
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();
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
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();
Dismissed Show dismissed Hide dismissed
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();
Dismissed Show dismissed Hide dismissed
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();
Dismissed Show dismissed Hide dismissed
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