diff --git a/Tests/SonarScanner.MSBuild.PreProcessor.Tests/WebClientDownloaderTest.cs b/Tests/SonarScanner.MSBuild.PreProcessor.Tests/WebClientDownloaderTest.cs index 0401fd2d1..7039e7229 100644 --- a/Tests/SonarScanner.MSBuild.PreProcessor.Tests/WebClientDownloaderTest.cs +++ b/Tests/SonarScanner.MSBuild.PreProcessor.Tests/WebClientDownloaderTest.cs @@ -36,16 +36,16 @@ public void Credentials() ILogger logger = new TestLogger(); WebClientDownloader downloader; - downloader = new WebClientDownloader(null, null, null, logger); + downloader = new WebClientDownloader(null, null, logger); downloader.GetHeader(HttpRequestHeader.Authorization).Should().BeNull(); - downloader = new WebClientDownloader("da39a3ee5e6b4b0d3255bfef95601890afd80709", null, null, logger); + downloader = new WebClientDownloader("da39a3ee5e6b4b0d3255bfef95601890afd80709", null, logger); downloader.GetHeader(HttpRequestHeader.Authorization).Should().Be("Basic ZGEzOWEzZWU1ZTZiNGIwZDMyNTViZmVmOTU2MDE4OTBhZmQ4MDcwOTo="); - downloader = new WebClientDownloader(null, "password", null, logger); + downloader = new WebClientDownloader(null, "password", logger); downloader.GetHeader(HttpRequestHeader.Authorization).Should().BeNull(); - downloader = new WebClientDownloader("admin", "password", null, logger); + downloader = new WebClientDownloader("admin", "password", logger); downloader.GetHeader(HttpRequestHeader.Authorization).Should().Be("Basic YWRtaW46cGFzc3dvcmQ="); } @@ -53,7 +53,7 @@ public void Credentials() public void UserAgent() { // Arrange - var downloader = new WebClientDownloader(null, null, null, new TestLogger()); + var downloader = new WebClientDownloader(null, null, new TestLogger()); // Act var userAgent = downloader.GetHeader(HttpRequestHeader.UserAgent); @@ -69,7 +69,7 @@ public void UserAgent_OnSubsequentCalls() // Arrange var expectedUserAgent = string.Format("ScannerMSBuild/{0}", typeof(WebClientDownloaderTest).Assembly.GetName().Version.ToDisplayString()); - var downloader = new WebClientDownloader(null, null, null, new TestLogger()); + var downloader = new WebClientDownloader(null, null, new TestLogger()); // Act & Assert var userAgent = downloader.GetHeader(HttpRequestHeader.UserAgent); @@ -92,27 +92,28 @@ public void UserAgent_OnSubsequentCalls() [TestMethod] public void SemicolonInUsername() { - Action act = () => new WebClientDownloader("user:name", "", null, new TestLogger()); + Action act = () => new WebClientDownloader("user:name", "", new TestLogger()); act.Should().ThrowExactly().WithMessage("username cannot contain the ':' character due to basic authentication limitations"); } [TestMethod] public void AccentsInUsername() { - Action act = () => new WebClientDownloader("héhé", "password", null, new TestLogger()); + Action act = () => new WebClientDownloader("héhé", "password", new TestLogger()); act.Should().ThrowExactly().WithMessage("username and password should contain only ASCII characters due to basic authentication limitations"); } [TestMethod] public void AccentsInPassword() { - Action act = () => new WebClientDownloader("username", "héhé", null, new TestLogger()); + Action act = () => new WebClientDownloader("username", "héhé", new TestLogger()); act.Should().ThrowExactly().WithMessage("username and password should contain only ASCII characters due to basic authentication limitations"); } + [TestMethod] public void UsingClientCert() { - Action act = () => new WebClientDownloader(null, null, "certtestsonar.pem", new TestLogger()); + Action act = () => new WebClientDownloader(null, null, new TestLogger(), "certtestsonar.pem", "dummypw"); act.Should().NotThrow(); } } diff --git a/src/SonarScanner.MSBuild.Common/SonarProperties.cs b/src/SonarScanner.MSBuild.Common/SonarProperties.cs index 16fdd24a3..bc1a71ed1 100644 --- a/src/SonarScanner.MSBuild.Common/SonarProperties.cs +++ b/src/SonarScanner.MSBuild.Common/SonarProperties.cs @@ -59,5 +59,6 @@ public static class SonarProperties public const string VsTestReportsPaths = "sonar.cs.vstest.reportsPaths"; public const string ClientCertPath = "sonar.clientcert.path"; + public const string ClientCertPassword = "sonar.clientcert.password"; } } diff --git a/src/SonarScanner.MSBuild.PreProcessor/PreprocessorObjectFactory.cs b/src/SonarScanner.MSBuild.PreProcessor/PreprocessorObjectFactory.cs index 1119419e2..d25d62775 100644 --- a/src/SonarScanner.MSBuild.PreProcessor/PreprocessorObjectFactory.cs +++ b/src/SonarScanner.MSBuild.PreProcessor/PreprocessorObjectFactory.cs @@ -59,9 +59,10 @@ public ISonarQubeServer CreateSonarQubeServer(ProcessedArgs args) var username = args.GetSetting(SonarProperties.SonarUserName, null); var password = args.GetSetting(SonarProperties.SonarPassword, null); var clientCertPath = args.GetSetting(SonarProperties.ClientCertPath, null); + var clientCertPassword = args.GetSetting(SonarProperties.ClientCertPassword, null); var hostUrl = args.SonarQubeUrl; - this.server = new SonarWebService(new WebClientDownloader(username, password, clientCertPath, this.logger), hostUrl, this.logger); + this.server = new SonarWebService(new WebClientDownloader(username, password, this.logger, clientCertPath, clientCertPassword), hostUrl, this.logger); return this.server; } diff --git a/src/SonarScanner.MSBuild.PreProcessor/WebClientDownloader.cs b/src/SonarScanner.MSBuild.PreProcessor/WebClientDownloader.cs index 27294bf85..1be2dfa8b 100644 --- a/src/SonarScanner.MSBuild.PreProcessor/WebClientDownloader.cs +++ b/src/SonarScanner.MSBuild.PreProcessor/WebClientDownloader.cs @@ -35,7 +35,7 @@ public class WebClientDownloader : IDownloader private readonly ILogger logger; private readonly HttpClient client; - public WebClientDownloader(string userName, string password, string clientCertPath, ILogger logger) + public WebClientDownloader(string userName, string password, ILogger logger, string clientCertPath = null, string clientCertPassword = null) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; @@ -48,11 +48,11 @@ public WebClientDownloader(string userName, string password, string clientCertPa if (this.client == null) { - if (clientCertPath != null) + if (clientCertPath != null && clientCertPassword != null) // password mandatory, as to use client cert in .jar it cannot be with empty password { var clientHandler = new HttpClientHandler(); clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual; - clientHandler.ClientCertificates.Add(new X509Certificate2(clientCertPath)); + clientHandler.ClientCertificates.Add(new X509Certificate2(clientCertPath, clientCertPassword)); this.client = new HttpClient(clientHandler); } diff --git a/src/SonarScanner.MSBuild/SonarScanner.MSBuild.csproj b/src/SonarScanner.MSBuild/SonarScanner.MSBuild.csproj index 5d1582fe8..8ced4ff9e 100644 --- a/src/SonarScanner.MSBuild/SonarScanner.MSBuild.csproj +++ b/src/SonarScanner.MSBuild/SonarScanner.MSBuild.csproj @@ -4,6 +4,9 @@ SonarScanner.MSBuild Exe + + 5 + PreserveNewest