Skip to content

Commit 9c7ee1d

Browse files
andreas-moneygateandreaschristos-pavlidessungam3r
authored
LogMaskedAttribute to handle int,long and guid values (#126)
* 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 <andreasph91@gmail.com> Co-authored-by: Christos Pavlides <christos.pavlides@outlook.com> Co-authored-by: Ivan Maximov <sungam3r@yandex.ru>
1 parent 88a8732 commit 9c7ee1d

File tree

3 files changed

+164
-4
lines changed

3 files changed

+164
-4
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ public class CustomizedMaskedLogs
169169
[LogMasked]
170170
public string? DefaultMasked { get; set; }
171171

172+
/// <summary>
173+
/// 9223372036854775807 results in "***"
174+
/// </summary>
175+
[LogMasked]
176+
public long? DefaultMaskedLong { get; set; }
177+
178+
/// <summary>
179+
/// 2147483647 results in "***"
180+
/// </summary>
181+
[LogMasked]
182+
public int? DefaultMaskedInt { get; set; }
183+
172184
/// <summary>
173185
/// [123456789,123456789,123456789] results in [***,***,***]
174186
/// </summary>
@@ -211,6 +223,18 @@ public class CustomizedMaskedLogs
211223
[LogMasked(ShowFirst = 3)]
212224
public string? ShowFirstThreeThenDefaultMasked { get; set; }
213225

226+
/// <summary>
227+
/// 9223372036854775807 results in "922***807"
228+
/// </summary>
229+
[LogMasked(ShowFirst = 3, ShowLast = 3)]
230+
public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; }
231+
232+
/// <summary>
233+
/// 2147483647 results in "214****647"
234+
/// </summary>
235+
[LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
236+
public int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }
237+
214238
/// <summary>
215239
/// 123456789 results in "123******"
216240
/// </summary>
@@ -230,11 +254,17 @@ public class CustomizedMaskedLogs
230254
public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }
231255

232256
/// <summary>
233-
/// 123456789 results in "123REMOVED"
257+
/// 123456789 results in "123_REMOVED_"
234258
/// </summary>
235259
[LogMasked(Text = "_REMOVED_", ShowFirst = 3)]
236260
public string? ShowFirstThreeThenCustomMask { get; set; }
237261

262+
/// <summary>
263+
/// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_"
264+
/// </summary>
265+
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
266+
public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }
267+
238268
/// <summary>
239269
/// 123456789 results in "123_REMOVED_"
240270
/// </summary>
@@ -284,7 +314,7 @@ public class CustomizedMaskedLogs
284314
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
285315
}
286316
```
287-
<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>
317+
<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>
288318
<!-- endSnippet -->
289319

290320
## 7. Masking a string property with regular expressions

src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ public class CustomizedMaskedLogs
1515
[LogMasked]
1616
public string? DefaultMasked { get; set; }
1717

18+
/// <summary>
19+
/// 9223372036854775807 results in "***"
20+
/// </summary>
21+
[LogMasked]
22+
public long? DefaultMaskedLong { get; set; }
23+
24+
/// <summary>
25+
/// 2147483647 results in "***"
26+
/// </summary>
27+
[LogMasked]
28+
public int? DefaultMaskedInt { get; set; }
29+
1830
/// <summary>
1931
/// [123456789,123456789,123456789] results in [***,***,***]
2032
/// </summary>
@@ -57,6 +69,18 @@ public class CustomizedMaskedLogs
5769
[LogMasked(ShowFirst = 3)]
5870
public string? ShowFirstThreeThenDefaultMasked { get; set; }
5971

72+
/// <summary>
73+
/// 9223372036854775807 results in "922***807"
74+
/// </summary>
75+
[LogMasked(ShowFirst = 3, ShowLast = 3)]
76+
public long? ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle { get; set; }
77+
78+
/// <summary>
79+
/// 2147483647 results in "214****647"
80+
/// </summary>
81+
[LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
82+
public int? ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength { get; set; }
83+
6084
/// <summary>
6185
/// 123456789 results in "123******"
6286
/// </summary>
@@ -76,11 +100,17 @@ public class CustomizedMaskedLogs
76100
public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }
77101

78102
/// <summary>
79-
/// 123456789 results in "123REMOVED"
103+
/// 123456789 results in "123_REMOVED_"
80104
/// </summary>
81105
[LogMasked(Text = "_REMOVED_", ShowFirst = 3)]
82106
public string? ShowFirstThreeThenCustomMask { get; set; }
83107

108+
/// <summary>
109+
/// d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c results in "d3c4a_REMOVED_"
110+
/// </summary>
111+
[LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
112+
public Guid? ShowFirstFiveThenCustomMaskGuid { get; set; }
113+
84114
/// <summary>
85115
/// 123456789 results in "123_REMOVED_"
86116
/// </summary>
@@ -289,6 +319,25 @@ public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_
289319
props["ShowFirstThreeThenCustomMask"].LiteralValue().ShouldBe("123_REMOVED_");
290320
}
291321

322+
[Test]
323+
public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_Guid()
324+
{
325+
// [LogMasked(Text = "_REMOVED_", ShowFirst = 5)]
326+
// -> "d3c4a_REMOVED_"
327+
var customized = new CustomizedMaskedLogs
328+
{
329+
ShowFirstFiveThenCustomMaskGuid = Guid.Parse("d3c4a1f2-3b4e-4f5a-9b6c-7d8e9f0a1b2c")
330+
};
331+
332+
var evt = DelegatingSink.Execute(customized);
333+
334+
var sv = (StructureValue)evt.Properties["Customized"];
335+
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
336+
337+
props.ContainsKey("ShowFirstFiveThenCustomMaskGuid").ShouldBeTrue();
338+
props["ShowFirstFiveThenCustomMaskGuid"].LiteralValue().ShouldBe("d3c4a_REMOVED_");
339+
}
340+
292341
[Test]
293342
public void LogMaskedAttribute_Shows_First_NChars_Then_Replaces_All_With_Custom_Mask_PreservedLength_Ignored()
294343
{
@@ -691,6 +740,84 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_
691740
props["ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored"].LiteralValue().ShouldBe("123_REMOVED_321");
692741
}
693742

743+
[Test]
744+
public void LogMaskedAttribute_Replaces_Long_Value_With_DefaultStars_Mask()
745+
{
746+
// [LogMasked]
747+
// 9223372036854775807 -> "***"
748+
var customized = new CustomizedMaskedLogs
749+
{
750+
DefaultMaskedLong = long.MaxValue
751+
};
752+
753+
var evt = DelegatingSink.Execute(customized);
754+
755+
var sv = (StructureValue)evt.Properties["Customized"];
756+
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
757+
758+
props.ContainsKey("DefaultMaskedLong").ShouldBeTrue();
759+
props["DefaultMaskedLong"].LiteralValue().ShouldBe("***");
760+
}
761+
762+
[Test]
763+
public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Long_Value_With_Default_StarMask()
764+
{
765+
// [LogMasked(ShowFirst = 3, ShowLast = 3)]
766+
// 9223372036854775807 -> "922***807"
767+
var customized = new CustomizedMaskedLogs
768+
{
769+
ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle = long.MaxValue
770+
};
771+
772+
var evt = DelegatingSink.Execute(customized);
773+
774+
var sv = (StructureValue)evt.Properties["Customized"];
775+
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
776+
777+
props.ContainsKey("ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle").ShouldBeTrue();
778+
props["ShowFirstAndLastThreeAndDefaultMaskLongInTheMiddle"].LiteralValue().ShouldBe("922***807");
779+
}
780+
781+
[Test]
782+
public void LogMaskedAttribute_Replaces_Int_Value_With_DefaultStars_Mask()
783+
{
784+
// [LogMasked]
785+
// 2147483647 -> "***"
786+
var customized = new CustomizedMaskedLogs
787+
{
788+
DefaultMaskedInt = int.MaxValue
789+
};
790+
791+
var evt = DelegatingSink.Execute(customized);
792+
793+
var sv = (StructureValue)evt.Properties["Customized"];
794+
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
795+
796+
props.ContainsKey("DefaultMaskedInt").ShouldBeTrue();
797+
props["DefaultMaskedInt"].LiteralValue().ShouldBe("***");
798+
}
799+
800+
[Test]
801+
public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Int_Value_With_Default_StarMask_And_PreservedLength()
802+
{
803+
// [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
804+
// 2147483647 -> "214****647"
805+
806+
var customized = new CustomizedMaskedLogs
807+
{
808+
ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength = int.MaxValue
809+
};
810+
811+
var evt = DelegatingSink.Execute(customized);
812+
813+
var sv = (StructureValue)evt.Properties["Customized"];
814+
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);
815+
816+
props.ContainsKey("ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength").ShouldBeTrue();
817+
props["ShowFirstAndLastThreeAndDefaultMaskIntInTheMiddlePreservedLength"].LiteralValue().ShouldBe("214****647");
818+
}
819+
820+
694821
[Test]
695822
public void LogMaskedAttribute_Nullify_Bool_Property()
696823
{

src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ private LogEventPropertyValue CreateValue(object? value)
119119
{
120120
IEnumerable<string> strings => new SequenceValue(strings.Select(s => new ScalarValue(FormatMaskedValue(s)))),
121121
string s => new ScalarValue(FormatMaskedValue(s)),
122-
_ => ScalarValue.Null
122+
long l => new ScalarValue(FormatMaskedValue(l.ToString())),
123+
int i => new ScalarValue(FormatMaskedValue(i.ToString())),
124+
Guid g => new ScalarValue(FormatMaskedValue(g.ToString())),
125+
_ => ScalarValue.Null,
123126
};
124127
}
125128
}

0 commit comments

Comments
 (0)