Skip to content

Commit

Permalink
expanding sanitizer; adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsam committed Oct 30, 2017
1 parent 7e6f825 commit ac8465f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/WebJobs.Script.WebHost/Diagnostics/Sanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class Sanitizer
{
private const string SecretReplacement = "[Hidden Credential]";
private static readonly char[] ValueTerminators = new char[] { '<', '"' };
private static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&amp;sig=" };
private static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&amp;sig=", "SharedAccessKey=" };

/// <summary>
/// Removes well-known credential strings from strings.
Expand Down
74 changes: 74 additions & 0 deletions test/WebJobs.Script.Tests/ScriptHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,80 @@ public void Initialize_AppliesLoggerConfig()
}
}

[Fact]
public void Initialize_Sanitizes_HostJsonLog()
{
TestTraceWriter traceWriter = new TestTraceWriter(TraceLevel.Info);
TestLoggerProvider loggerProvider = null;
var loggerFactoryHookMock = new Mock<ILoggerFactoryBuilder>(MockBehavior.Strict);
loggerFactoryHookMock
.Setup(m => m.AddLoggerProviders(It.IsAny<ILoggerFactory>(), It.IsAny<ScriptHostConfiguration>(), It.IsAny<ScriptSettingsManager>()))
.Callback<ILoggerFactory, ScriptHostConfiguration, ScriptSettingsManager>((factory, scriptConfig, settings) =>
{
loggerProvider = new TestLoggerProvider(scriptConfig.LogFilter.Filter);
factory.AddProvider(loggerProvider);
});

string rootPath = Path.Combine(Environment.CurrentDirectory, "ScriptHostTests");
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}

// Turn off all logging. We shouldn't see any output.
string hostJsonContent = @"
{
'functionTimeout': '00:05:00',
'functions': [ 'FunctionA', 'FunctionB' ],
'logger': {
'categoryFilter': {
'defaultLevel': 'Information'
}
},
'Values': {
'MyCustomValue': 'abc'
}
}";

File.WriteAllText(Path.Combine(rootPath, "host.json"), hostJsonContent);

ScriptHostConfiguration config = new ScriptHostConfiguration()
{
RootScriptPath = rootPath,
TraceWriter = traceWriter
};

config.LoggerFactoryBuilder = loggerFactoryHookMock.Object;

config.HostConfig.HostId = ID;
var environment = new Mock<IScriptHostEnvironment>();
var eventManager = new Mock<IScriptEventManager>();

ScriptHost.Create(environment.Object, eventManager.Object, config, _settingsManager);

string hostJsonSanitized = @"
{
'functionTimeout': '00:05:00',
'functions': [ 'FunctionA', 'FunctionB' ],
'logger': {
'categoryFilter': {
'defaultLevel': 'Information'
}
}
}";

// for formatting
var hostJson = JObject.Parse(hostJsonSanitized);
string expectedMessgae = $"Host configuration file read:{Environment.NewLine}{hostJson}";

var logger = loggerProvider.CreatedLoggers.Single(l => l.Category == LogCategories.Startup);
var logMessage = logger.LogMessages.Single(l => l.FormattedMessage.StartsWith("Host configuration ")).FormattedMessage;
Assert.Equal(expectedMessgae, logMessage);

var traceMessage = traceWriter.Traces.Single(t => t.Message.StartsWith("Host configuration ")).Message;
Assert.Equal(expectedMessgae, traceMessage);
}

[Fact]
public void Initialize_LogsException_IfParseError()
{
Expand Down

0 comments on commit ac8465f

Please sign in to comment.