Skip to content

Commit

Permalink
LogMaskedAttribute to handle int,long and guid values (#126)
Browse files Browse the repository at this point in the history
* LogMaskedAttribute to handle int and long values

* Add support for Guid

https://github.com/destructurama/attributed/pull/122/files

* Apply suggestions from code review

* Update src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs

---------

Co-authored-by: andreas <[email protected]>
Co-authored-by: Christos Pavlides <[email protected]>
Co-authored-by: Ivan Maximov <[email protected]>
  • Loading branch information
4 people authored Dec 15, 2024
1 parent 88a8732 commit 9c7ee1d
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 4 deletions.
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();
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();
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,
};
}
}

0 comments on commit 9c7ee1d

Please sign in to comment.