diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxy.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxy.cs index 7d0f21d65b1..d1a5df4dbc5 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxy.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxy.cs @@ -9,84 +9,71 @@ namespace Semmle.Extraction.CSharp.DependencyFetching { public class DependabotProxy : IDisposable { - private readonly string? host; - private readonly string? port; - private readonly FileInfo? certFile; + private readonly string host; + private readonly string port; /// /// The full address of the Dependabot proxy, if available. /// - internal readonly string? Address; + internal string Address { get; } /// /// The path to the temporary file where the certificate is stored. /// - internal readonly string? CertificatePath; + internal string? CertificatePath { get; private set; } /// /// The certificate used for the Dependabot proxy. /// - internal readonly X509Certificate2? Certificate; + internal X509Certificate2? Certificate { get; private set; } - /// - /// Gets a value indicating whether a Dependabot proxy is configured. - /// - internal bool IsConfigured => !string.IsNullOrEmpty(this.Address); - - internal DependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory) + internal static DependabotProxy? GetDependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory) { // Obtain and store the address of the Dependabot proxy, if available. - this.host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost); - this.port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort); + var host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost); + var port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort); if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port)) { logger.LogInfo("No Dependabot proxy credentials are configured."); - return; + return null; } - this.Address = $"http://{this.host}:{this.port}"; - logger.LogInfo($"Dependabot proxy configured at {this.Address}"); + var result = new DependabotProxy(host, port); + logger.LogInfo($"Dependabot proxy configured at {result.Address}"); // Obtain and store the proxy's certificate, if available. var cert = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate); - if (string.IsNullOrWhiteSpace(cert)) + if (!string.IsNullOrWhiteSpace(cert)) { logger.LogInfo("No certificate configured for Dependabot proxy."); - return; + + var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".dependabot-proxy")); + Directory.CreateDirectory(certDirPath.FullName); + + result.CertificatePath = Path.Join(certDirPath.FullName, "proxy.crt"); + var certFile = new FileInfo(result.CertificatePath); + + using var writer = certFile.CreateText(); + writer.Write(cert); + + logger.LogInfo($"Stored Dependabot proxy certificate at {result.CertificatePath}"); + + result.Certificate = new X509Certificate2(result.CertificatePath); } - var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".dependabot-proxy")); - Directory.CreateDirectory(certDirPath.FullName); - - this.CertificatePath = Path.Join(certDirPath.FullName, "proxy.crt"); - this.certFile = new FileInfo(this.CertificatePath); - - using var writer = this.certFile.CreateText(); - writer.Write(cert); - - logger.LogInfo($"Stored Dependabot proxy certificate at {this.CertificatePath}"); - - this.Certificate = new X509Certificate2(this.CertificatePath); + return result; } - internal void ApplyProxy(ILogger logger, ProcessStartInfo startInfo) + private DependabotProxy(string host, string port) { - // If the proxy isn't configured, we have nothing to do. - if (!this.IsConfigured) return; - - logger.LogInfo($"Setting up Dependabot proxy at {this.Address}"); - - startInfo.EnvironmentVariables.Add("HTTP_PROXY", this.Address); - startInfo.EnvironmentVariables.Add("HTTPS_PROXY", this.Address); - startInfo.EnvironmentVariables.Add("SSL_CERT_FILE", this.certFile?.FullName); + this.host = host; + this.port = port; + this.Address = $"http://{this.host}:{this.port}"; } public void Dispose() { - if (this.Certificate != null) - { - this.Certificate.Dispose(); - } + this.Certificate?.Dispose(); } } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs index bbd5ecbd127..cf4c6d73bd6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs @@ -27,7 +27,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching private readonly ILogger logger; private readonly IDiagnosticsWriter diagnosticsWriter; private readonly NugetPackageRestorer nugetPackageRestorer; - private readonly DependabotProxy dependabotProxy; + private readonly DependabotProxy? dependabotProxy; private readonly IDotNet dotnet; private readonly FileContent fileContent; private readonly FileProvider fileProvider; @@ -107,7 +107,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching return BuildScript.Success; }).Run(SystemBuildActions.Instance, startCallback, exitCallback); - dependabotProxy = new DependabotProxy(logger, tempWorkingDirectory); + dependabotProxy = DependabotProxy.GetDependabotProxy(logger, tempWorkingDirectory); try { diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index a82a0a47f41..c1fdcc06e91 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -27,11 +27,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching Info(); } - private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy dependabotProxy) : this(new DotNetCliInvoker(logger, Path.Combine(dotNetPath ?? string.Empty, "dotnet"), dependabotProxy), logger, tempWorkingDirectory) { } + private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy? dependabotProxy) : this(new DotNetCliInvoker(logger, Path.Combine(dotNetPath ?? string.Empty, "dotnet"), dependabotProxy), logger, tempWorkingDirectory) { } internal static IDotNet Make(IDotNetCliInvoker dotnetCliInvoker, ILogger logger) => new DotNet(dotnetCliInvoker, logger); - public static IDotNet Make(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy dependabotProxy) => new DotNet(logger, dotNetPath, tempWorkingDirectory, dependabotProxy); + public static IDotNet Make(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy? dependabotProxy) => new DotNet(logger, dotNetPath, tempWorkingDirectory, dependabotProxy); private void Info() { diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs index cdadfe1f5b8..19f0f3dbe0d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs @@ -12,11 +12,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching internal sealed class DotNetCliInvoker : IDotNetCliInvoker { private readonly ILogger logger; - private readonly DependabotProxy proxy; + private readonly DependabotProxy? proxy; public string Exec { get; } - public DotNetCliInvoker(ILogger logger, string exec, DependabotProxy dependabotProxy) + public DotNetCliInvoker(ILogger logger, string exec, DependabotProxy? dependabotProxy) { this.logger = logger; this.proxy = dependabotProxy; @@ -42,7 +42,14 @@ namespace Semmle.Extraction.CSharp.DependencyFetching startInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "true"; // Configure the proxy settings, if applicable. - this.proxy.ApplyProxy(this.logger, startInfo); + if (this.proxy != null) + { + logger.LogInfo($"Setting up Dependabot proxy at {this.proxy.Address}"); + + startInfo.EnvironmentVariables.Add("HTTP_PROXY", this.proxy.Address); + startInfo.EnvironmentVariables.Add("HTTPS_PROXY", this.proxy.Address); + startInfo.EnvironmentVariables.Add("SSL_CERT_FILE", this.proxy.CertificatePath); + } return startInfo; } diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs index a01b3ae9649..d0c0af6b768 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs @@ -22,7 +22,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching private readonly FileProvider fileProvider; private readonly FileContent fileContent; private readonly IDotNet dotnet; - private readonly DependabotProxy dependabotProxy; + private readonly DependabotProxy? dependabotProxy; private readonly IDiagnosticsWriter diagnosticsWriter; private readonly TemporaryDirectory legacyPackageDirectory; private readonly TemporaryDirectory missingPackageDirectory; @@ -35,7 +35,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching FileProvider fileProvider, FileContent fileContent, IDotNet dotnet, - DependabotProxy dependabotProxy, + DependabotProxy? dependabotProxy, IDiagnosticsWriter diagnosticsWriter, ILogger logger, ICompilationInfoContainer compilationInfoContainer) @@ -596,7 +596,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching // Configure the HttpClient to be aware of the Dependabot Proxy, if used. HttpClientHandler httpClientHandler = new(); - if (this.dependabotProxy.IsConfigured) + if (this.dependabotProxy != null) { httpClientHandler.Proxy = new WebProxy(this.dependabotProxy.Address);