Skip to content

Commit

Permalink
Feature/115 mask sensitive values (#175)
Browse files Browse the repository at this point in the history
* Added LoggingConfig.SensitiveValues
* Updated DefaultFormatter to mask sensitive values
* Added LoggingConfig.ScopeFormatter
* Added DefaultScopeFormatter
* Updated ScopeWriter to use Config.ScopeFormatter
* Renamed name parameters to categoryName
* Updated documentation
  • Loading branch information
roryprimrose authored Jun 23, 2022
1 parent 59372ab commit 4c4bbe4
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 139 deletions.
6 changes: 3 additions & 3 deletions Divergic.Logging.Xunit.UnitTests/CustomFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CustomFormatter : ILogFormatter
{
public string Format(
int scopeLevel,
string name,
string categoryName,
LogLevel logLevel,
EventId eventId,
string message,
Expand All @@ -23,9 +23,9 @@ public string Format(

sb.Append($"{GetShortLogLevelString(logLevel)} ");

if (!string.IsNullOrEmpty(name))
if (!string.IsNullOrEmpty(categoryName))
{
sb.Append($"{name} ");
sb.Append($"{categoryName} ");
}

if (eventId.Id != 0)
Expand Down
96 changes: 76 additions & 20 deletions Divergic.Logging.Xunit.UnitTests/DefaultFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,60 @@ public DefaultFormatterTests(ITestOutputHelper output)
_output = output;
}

[Theory]
[InlineData(null, "this message", "this message")]
[InlineData("secret", "this message", "this message")]
[InlineData("secret", "this secret message", "this **** message")]
public void FormatHidesSensitiveDataInException(string? sensitiveValue, string message, string expected)
{
var config = new LoggingConfig();
var scopeLevel = 1;
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var logMessage = Guid.NewGuid().ToString();
var exception = new InvalidOperationException(message);

if (sensitiveValue != null)
{
config.SensitiveValues.Add(sensitiveValue);
}

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, logMessage, exception);

actual.Should().Contain(expected);

if (sensitiveValue != null)
{
actual.Should().NotContain(sensitiveValue);
}
}

[Theory]
[InlineData(null, "this message", "this message")]
[InlineData("secret", "this secret message", "this **** message")]
public void FormatHidesSensitiveDataInMessage(string? sensitiveValue, string message, string expected)
{
var config = new LoggingConfig();
var scopeLevel = 1;
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();

if (sensitiveValue != null)
{
config.SensitiveValues.Add(sensitiveValue);
}

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

actual.Should().Be($" Information [{eventId.Id}]: {expected}");
}

[Theory]
[InlineData(null, false)]
[InlineData("", false)]
Expand All @@ -30,7 +84,7 @@ public void FormatIncludesNewLineBetweenMessageAndException(string message, bool
{
var config = new LoggingConfig();
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
Exception? exception = exceptionExists
Expand All @@ -39,7 +93,7 @@ public void FormatIncludesNewLineBetweenMessageAndException(string message, bool

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

actual.Should().NotStartWith(Environment.NewLine);
actual.Should().NotEndWith(Environment.NewLine);
Expand All @@ -57,7 +111,9 @@ public void FormatIncludesNewLineBetweenMessageAndException(string message, bool
}
else if (exception != null)
{
actual.Should().Be($" Information [{eventId.Id}]: stuff{Environment.NewLine} Information [{eventId.Id}]: {exception}");
actual.Should()
.Be(
$" Information [{eventId.Id}]: stuff{Environment.NewLine} Information [{eventId.Id}]: {exception}");
}
else
{
Expand All @@ -73,13 +129,13 @@ public void FormatReturnsEmptyWhenMessageIsNullEmptyOrWhiteSpace(string message)
{
var config = new LoggingConfig().Set(x => x.ScopePaddingSpaces = 2);
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, null);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

actual.Should().BeEmpty();
}
Expand All @@ -89,15 +145,15 @@ public void FormatReturnsValueWithEventId()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

_output.WriteLine(actual);

Expand All @@ -109,15 +165,15 @@ public void FormatReturnsValueWithException()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

_output.WriteLine(actual);

Expand All @@ -138,11 +194,11 @@ public void FormatReturnsValueWithLogLevel(LogLevel logLevel)
var scopeLevel = 1;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, null);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

_output.WriteLine(actual);

Expand All @@ -154,15 +210,15 @@ public void FormatReturnsValueWithMessage()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

_output.WriteLine(actual);

Expand All @@ -174,14 +230,14 @@ public void FormatReturnsValueWithoutException()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, null);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

_output.WriteLine(actual);

Expand All @@ -193,19 +249,19 @@ public void FormatReturnsValueWithoutName()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

_output.WriteLine(actual);

actual.Should().NotContain(name);
actual.Should().NotContain(categoryName);
}

[Theory]
Expand All @@ -216,15 +272,15 @@ public void FormatReturnsValueWithPadding(int scopeLevel)
{
var config = new LoggingConfig();
var padding = new string(' ', config.ScopePaddingSpaces * scopeLevel);
var name = Guid.NewGuid().ToString();
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

var sut = new DefaultFormatter(config);

var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, exception);
var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

_output.WriteLine(actual);

Expand Down
61 changes: 61 additions & 0 deletions Divergic.Logging.Xunit.UnitTests/DefaultScopeFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Divergic.Logging.Xunit.UnitTests
{
using System;
using FluentAssertions;
using global::Xunit;
using global::Xunit.Abstractions;
using Microsoft.Extensions.Logging;
using ModelBuilder;

public class DefaultScopeFormatterTests
{
private readonly ITestOutputHelper _output;

public DefaultScopeFormatterTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void FormatReturnsRawValue()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();

var sut = new DefaultScopeFormatter(config);

var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

_output.WriteLine(actual);

actual.Should().Be($" {message}");
}

[Fact]
public void FormatReturnsValueWithException()
{
var config = new LoggingConfig();
var scopeLevel = 1;
var categoryName = Guid.NewGuid().ToString();
var logLevel = LogLevel.Information;
var eventId = Model.Create<EventId>();
var message = Guid.NewGuid().ToString();
var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

var sut = new DefaultScopeFormatter(config);

var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

_output.WriteLine(actual);

actual.Should().Contain(message);
actual.Should().Contain(exception.ToString());
actual.Should().NotContain(logLevel.ToString());
actual.Should().NotContain(eventId.Id.ToString());
}
}
}
4 changes: 2 additions & 2 deletions Divergic.Logging.Xunit.UnitTests/Formatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ internal static class Formatters
// This an example message formatter.
public static string MyCustomFormatter(
int scopeLevel,
string name,
string categoryName,
LogLevel logLevel,
EventId eventId,
string message,
Exception exception)
{
var formatter = new CustomFormatter();

return formatter.Format(scopeLevel, name, logLevel, eventId, message, exception);
return formatter.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);
}
}
}
8 changes: 8 additions & 0 deletions Divergic.Logging.Xunit.UnitTests/LoggingConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public void CreatesWithDefaultFormatter()
sut.Formatter.Should().BeOfType<DefaultFormatter>();
}

[Fact]
public void CreatesWithDefaultScopeFormatter()
{
var sut = new LoggingConfig();

sut.ScopeFormatter.Should().BeOfType<DefaultScopeFormatter>();
}

[Fact]
public void CreatesWithIgnoreTestBoundaryExceptionAsFalse()
{
Expand Down
Loading

0 comments on commit 4c4bbe4

Please sign in to comment.