Skip to content

Commit fc75659

Browse files
SCAN4NET-287 sonar.scanner.truststorePassword should be required for the end step (#2356)
1 parent 29eabcf commit fc75659

File tree

25 files changed

+1052
-644
lines changed

25 files changed

+1052
-644
lines changed

Tests/SonarScanner.MSBuild.Common.Test/AnalysisConfig/AnalysisConfigTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020

21-
using System;
22-
using System.Collections.Generic;
23-
using System.IO;
24-
using System.Linq;
25-
using FluentAssertions;
26-
using Microsoft.VisualStudio.TestTools.UnitTesting;
27-
using TestUtilities;
28-
2921
namespace SonarScanner.MSBuild.Common.Test;
3022

3123
[TestClass]

Tests/SonarScanner.MSBuild.Common.Test/ProcessRunnerTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,23 @@ public void ProcRunner_DoNotLogSensitiveData()
457457
"/dsonar.token =secret data token typo",
458458
};
459459
var allArgs = sensitiveArgs.Union(publicArgs).ToArray();
460-
var runnerArgs = new ProcessRunnerArguments(LogArgsPath(), false) { CmdLineArgs = allArgs, WorkingDirectory = testDir };
460+
var runnerArgs = new ProcessRunnerArguments(LogArgsPath(), false)
461+
{
462+
CmdLineArgs = allArgs,
463+
WorkingDirectory = testDir,
464+
EnvironmentVariables = new Dictionary<string, string>
465+
{
466+
{ "SENSITIVE_DATA", "-Djavax.net.ssl.trustStorePassword=changeit" },
467+
{ "OVERWRITING_DATA", "-Djavax.net.ssl.trustStorePassword=changeit" },
468+
{ "EXISTING_SENSITIVE_DATA", "-Djavax.net.ssl.trustStorePassword=changeit" },
469+
{ "NOT_SENSITIVE", "Something" },
470+
{ "MIXED_DATA", "-DBefore=true -Djavax.net.ssl.trustStorePassword=changeit -DAfter=false" }
471+
}
472+
};
473+
using var scope = new EnvironmentVariableScope();
474+
scope.SetVariable("OVERWRITING_DATA", "Not sensitive");
475+
scope.SetVariable("EXISTING_SENSITIVE_DATA", "-Djavax.net.ssl.trustStorePassword=password");
476+
461477
var result = runner.Execute(runnerArgs);
462478

463479
result.Succeeded.Should().BeTrue("Expecting the process to have succeeded");
@@ -467,7 +483,12 @@ public void ProcRunner_DoNotLogSensitiveData()
467483
{
468484
logger.AssertSingleDebugMessageExists(arg);
469485
}
470-
logger.AssertSingleDebugMessageExists("<sensitive data removed>");
486+
logger.AssertSingleDebugMessageExists("Setting environment variable 'SENSITIVE_DATA'. Value: -D<sensitive data removed>");
487+
logger.AssertSingleDebugMessageExists("Setting environment variable 'NOT_SENSITIVE'. Value: Something");
488+
logger.AssertSingleDebugMessageExists("Setting environment variable 'MIXED_DATA'. Value: -DBefore=true -D<sensitive data removed>");
489+
logger.AssertSingleDebugMessageExists("Overwriting the value of environment variable 'OVERWRITING_DATA'. Old value: Not sensitive, new value: -D<sensitive data removed>");
490+
logger.AssertSingleDebugMessageExists("Overwriting the value of environment variable 'EXISTING_SENSITIVE_DATA'. Old value: -D<sensitive data removed>, new value: -D<sensitive data removed>");
491+
logger.AssertSingleDebugMessageExists("Args: public1 public2 /dmy.key=value /d:sonar.projectKey=my.key <sensitive data removed>");
471492
AssertTextDoesNotAppearInLog("secret", logger);
472493
// Check that the public and private arguments are passed to the child process
473494
AssertExpectedLogContents(testDir, allArgs);

Tests/SonarScanner.MSBuild.Common.Test/SonarPropertiesTests.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020

21-
using System.Collections.Generic;
22-
using System.Linq;
23-
using FluentAssertions;
24-
using Microsoft.VisualStudio.TestTools.UnitTesting;
25-
2621
namespace SonarScanner.MSBuild.Common.Test;
2722

2823
[TestClass]
2924
public class SonarPropertiesTests
3025
{
3126
/// <summary>
32-
/// Strings that are used to indicate arguments that contain non sensitive data.
27+
/// Strings that are used to indicate arguments that contain non-sensitive data.
28+
///
29+
/// No properties holding a password, a secret, a token, a key or any sensitive
30+
/// data should be part of this list.
31+
/// Those properties should be part of the <see cref="SonarProperties.SensitivePropertyKeys"/> list and MUST
32+
/// be passed to both the begin step and the end step.
33+
///
34+
/// THINK TWICE BEFORE ADDING A NEW PROPERTY HERE.
35+
///
36+
/// ALWAYS REMEMBER SCAN4NET-287.
37+
///
3338
/// </summary>
3439
private static readonly IEnumerable<string> NonSensitivePropertyKeys =
3540
[
@@ -43,7 +48,6 @@ public class SonarPropertiesTests
4348
SonarProperties.SocketTimeout,
4449
SonarProperties.ResponseTimeout,
4550
SonarProperties.TruststorePath,
46-
SonarProperties.TruststorePassword,
4751
SonarProperties.UserHome,
4852
SonarProperties.LogLevel,
4953
SonarProperties.Organization,
@@ -57,6 +61,8 @@ public class SonarPropertiesTests
5761
SonarProperties.ProjectVersion,
5862
SonarProperties.PullRequestBase,
5963
SonarProperties.PullRequestCacheBasePath,
64+
SonarProperties.JavaxNetSslTrustStore,
65+
SonarProperties.JavaxNetSslTrustStoreType,
6066
SonarProperties.SourceEncoding,
6167
SonarProperties.Verbose,
6268
SonarProperties.VsCoverageXmlReportsPaths,

Tests/SonarScanner.MSBuild.Common.Test/StringExtensions_ReplaceCaseInsensitive.cs renamed to Tests/SonarScanner.MSBuild.Common.Test/StringExtensionsTests.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020

21-
using FluentAssertions;
22-
using Microsoft.VisualStudio.TestTools.UnitTesting;
23-
2421
namespace SonarScanner.MSBuild.Common.Test;
2522

2623
[TestClass]
27-
public class StringExtensions_ReplaceCaseInsensitive
24+
public class StringExtensionsTests
2825
{
26+
private static IEnumerable<object[]> SensitivePropertyKeys =>
27+
SonarProperties.SensitivePropertyKeys.Select(x => new object[] { x });
28+
2929
[TestMethod]
30-
public void ReplaceCaseInsensitiveTests()
30+
public void ReplaceCaseInsensitive()
3131
{
3232
"abcdef".ReplaceCaseInsensitive("abc", "xyz").Should().Be("xyzdef");
3333
"ABCdef".ReplaceCaseInsensitive("abc", "xyz").Should().Be("xyzdef");
@@ -37,4 +37,21 @@ public void ReplaceCaseInsensitiveTests()
3737
"ab$$$def".ReplaceCaseInsensitive("$", "x").Should().Be("abxxxdef");
3838
"aabcbcdef".ReplaceCaseInsensitive("abc", "x").Should().Be("axbcdef");
3939
}
40+
41+
[TestMethod]
42+
public void RedactSensitiveData_NoSensitiveData() =>
43+
"Some string with no sensitive data".RedactSensitiveData().Should().Be("Some string with no sensitive data");
44+
45+
[DataTestMethod]
46+
[DynamicData(nameof(SensitivePropertyKeys))]
47+
public void RedactSensitiveData_SensitiveData(string sensitiveKey) =>
48+
@$"Setting environment variable 'SONAR_SCANNER_OPTS'. Value: -D{sensitiveKey}=""changeit""".RedactSensitiveData()
49+
.Should().Be("Setting environment variable 'SONAR_SCANNER_OPTS'. Value: -D<sensitive data removed>");
50+
51+
[DataTestMethod]
52+
[DataRow(SonarProperties.SonarToken, SonarProperties.SonarPassword)]
53+
[DataRow(SonarProperties.SonarPassword, SonarProperties.SonarToken)]
54+
public void RedactSensitiveData_MixedSensitiveData(string sensitiveKey1, string sensitiveKey2) =>
55+
@$"Setting environment variable 'SONAR_SCANNER_OPTS'. Value: -D{sensitiveKey1}=""changeit"" -D{sensitiveKey2}=""changeit""".RedactSensitiveData()
56+
.Should().Be("Setting environment variable 'SONAR_SCANNER_OPTS'. Value: -D<sensitive data removed>");
4057
}

Tests/SonarScanner.MSBuild.PostProcessor.Test/Infrastructure/MockSonarScanner.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020

21-
using System;
22-
using System.Collections.Generic;
23-
using FluentAssertions;
24-
using SonarScanner.MSBuild.Common;
2521
using SonarScanner.MSBuild.Shim.Interfaces;
2622

2723
namespace SonarScanner.MSBuild.PostProcessor.Test;
@@ -48,11 +44,11 @@ public MockSonarScanner(ILogger logger)
4844

4945
#region ISonarScanner interface
5046

51-
public bool Execute(AnalysisConfig config, IEnumerable<string> userCmdLineArguments, string fullPropertiesFilePath)
47+
public bool Execute(AnalysisConfig config, IAnalysisPropertyProvider userCmdLineArguments, string fullPropertiesFilePath)
5248
{
5349
methodCalled.Should().BeFalse("Scanner should only be called once");
5450
methodCalled = true;
55-
SuppliedCommandLineArgs = userCmdLineArguments;
51+
SuppliedCommandLineArgs = userCmdLineArguments.GetAllProperties().Select(x => x.AsSonarScannerArg());
5652
if (ErrorToLog != null)
5753
{
5854
logger.LogError(ErrorToLog);

0 commit comments

Comments
 (0)