Skip to content

Commit 9178fe6

Browse files
[v3.x] Filter the secret for preventing logging - cherry pick PR (#8953)
* Filter the secret for preventing logging * Remove Sanitizer testing as the same as before
1 parent 4bca94c commit 9178fe6

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

src/WebJobs.Script.WebHost/Management/LinuxSpecialization/BashCommandHandler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6-
using System.IO;
7-
using System.IO.Compression;
6+
using Microsoft.Azure.WebJobs.Logging;
87
using Microsoft.Azure.WebJobs.Script.Diagnostics;
98
using Microsoft.Extensions.Logging;
109

@@ -41,7 +40,7 @@ public BashCommandHandler(IMetricsLogger metricsLogger, ILogger<BashCommandHandl
4140
CreateNoWindow = true
4241
}
4342
};
44-
_logger.LogInformation($"Running: {process.StartInfo.FileName} {process.StartInfo.Arguments}");
43+
_logger.LogInformation($"Running: bash.exe (arguments omitted)");
4544
process.Start();
4645
var output = process.StandardOutput.ReadToEnd().Trim();
4746
var error = process.StandardError.ReadToEnd().Trim();

src/WebJobs.Script.WebHost/Management/LinuxSpecialization/PackageDownloadHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.IO.Abstractions;
78
using System.Net;
@@ -10,6 +11,7 @@
1011
using System.Threading;
1112
using System.Threading.Tasks;
1213
using DryIoc;
14+
using Microsoft.Azure.WebJobs.Logging;
1315
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1416
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
1517
using Microsoft.Extensions.Logging;
@@ -124,15 +126,18 @@ private async Task<string> Download(RunFromPackageContext pkgContext, Uri zipUri
124126

125127
private void AriaDownload(string directory, string fileName, Uri zipUri, bool isWarmupRequest, string downloadMetricName)
126128
{
129+
var command = $"{Aria2CExecutable} --allow-overwrite -x12 -d {directory} -o {fileName} '{zipUri}'";
127130
(string stdout, string stderr, int exitCode) = _bashCommandHandler.RunBashCommand(
128-
$"{Aria2CExecutable} --allow-overwrite -x12 -d {directory} -o {fileName} '{zipUri}'",
131+
command,
129132
downloadMetricName);
130133
if (exitCode != 0)
131134
{
132135
var msg = $"Error downloading package. stdout: {stdout}, stderr: {stderr}, exitCode: {exitCode}";
133136
_logger.LogError(msg);
134137
throw new InvalidOperationException(msg);
135138
}
139+
_logger.LogInformation($"Executed: {Sanitizer.Sanitize(command)}");
140+
136141
var fileInfo = FileUtility.FileInfoFromFileName(Path.Combine(directory, fileName));
137142
_logger.LogInformation("'{fileInfo.Length}' bytes downloaded. IsWarmupRequest = '{isWarmupRequest}'",
138143
fileInfo.Length, isWarmupRequest);

src/WebJobs.Script.WebHost/Management/LinuxSpecialization/RunFromPackageHandler.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
78
using System.Threading.Tasks;
9+
using Microsoft.Azure.WebJobs.Logging;
810
using Microsoft.Azure.WebJobs.Script.Diagnostics;
911
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
1012
using Microsoft.Extensions.Logging;
13+
using NuGet.Protocol.Plugins;
1114

1215
namespace Microsoft.Azure.WebJobs.Script.WebHost.Management.LinuxSpecialization
1316
{
@@ -149,6 +152,7 @@ private CodePackageType GetPackageType(string filePath, RunFromPackageContext pk
149152

150153
// Check file magic-number using `file` command.
151154
(var output, _, _) = _bashCommandHandler.RunBashCommand($"{BashCommandHandler.FileCommand} -b {filePath}", MetricEventNames.LinuxContainerSpecializationFileCommand);
155+
_logger.LogInformation(Sanitizer.Sanitize($"Executed: {BashCommandHandler.FileCommand} -b {filePath} {MetricEventNames.LinuxContainerSpecializationFileCommand}"));
152156
if (output.StartsWith(SquashfsPrefix, StringComparison.OrdinalIgnoreCase))
153157
{
154158
return CodePackageType.Squashfs;
@@ -177,9 +181,9 @@ private async Task CreateBindMount(string sourcePath, string targetPath)
177181
private void UnsquashImage(string filePath, string scriptPath)
178182
{
179183
_logger.LogDebug($"Unsquashing remote zip to {scriptPath}");
180-
181-
_bashCommandHandler.RunBashCommand($"{UnsquashFSExecutable} -f -d '{scriptPath}' '{filePath}'",
182-
MetricEventNames.LinuxContainerSpecializationUnsquash);
184+
var command = $"{UnsquashFSExecutable} -f -d '{scriptPath}' '{filePath}'";
185+
_bashCommandHandler.RunBashCommand(command, MetricEventNames.LinuxContainerSpecializationUnsquash);
186+
_logger.LogInformation(Sanitizer.Sanitize($"Executed: {command}"));
183187
}
184188

185189
public async Task<bool> MountAzureFileShare(HostAssignmentContext assignmentContext)

src/WebJobs.Script/Sanitizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal static class Sanitizer
1616

1717
// List of keywords that should not be replaced with [Hidden Credential]
1818
private static readonly string[] AllowedTokens = new string[] { "PublicKeyToken=" };
19-
private static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&amp;sig=", "SharedAccessKey=" };
19+
internal static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&amp;sig=", "&sig=", "SharedAccessKey=" };
2020

2121
/// <summary>
2222
/// Removes well-known credential strings from strings.

test/WebJobs.Script.Tests.Integration/Management/InstanceManagerTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public async void StartAssignment_Succeeds_With_NonEmpty_ScmRunFromPackage_Blob(
194194

195195
IConfiguration configuration = TestHelpers.GetTestConfiguration();
196196
string connectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage);
197+
197198
Uri sasUri = await TestHelpers.CreateBlobSas(connectionString, zipFilePath, "scm-run-from-pkg-test", "NonEmpty.zip");
198199

199200
_environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1");
@@ -226,7 +227,7 @@ public async void StartAssignment_Succeeds_With_NonEmpty_ScmRunFromPackage_Blob(
226227

227228
var logs = _loggerProvider.GetAllLogMessages().Select(p => p.FormattedMessage).ToArray();
228229

229-
if (logs.Length == 9)
230+
if (logs.Length == 10)
230231
{
231232
Assert.Collection(logs,
232233
p => Assert.StartsWith("Starting Assignment", p),
@@ -237,6 +238,7 @@ public async void StartAssignment_Succeeds_With_NonEmpty_ScmRunFromPackage_Blob(
237238
p => Assert.StartsWith("Output:", p),
238239
p => Assert.True(true), // this line varies depending on whether WSL is on the machine; just ignore it
239240
p => Assert.StartsWith("exitCode:", p),
241+
p => Assert.StartsWith("Executed: ", p),
240242
p => Assert.StartsWith("Triggering specialization", p));
241243
}
242244
else
@@ -248,6 +250,7 @@ public async void StartAssignment_Succeeds_With_NonEmpty_ScmRunFromPackage_Blob(
248250
p => Assert.StartsWith("Unsquashing remote zip", p),
249251
p => Assert.StartsWith("Running: ", p),
250252
p => Assert.StartsWith("Error running bash", p),
253+
p => Assert.StartsWith("Executed: ", p),
251254
p => Assert.StartsWith("Triggering specialization", p));
252255
}
253256
}

0 commit comments

Comments
 (0)