mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
C#: Apply suggestions from code review for DependabotProxy
This commit is contained in:
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// The full address of the Dependabot proxy, if available.
|
||||
/// </summary>
|
||||
internal readonly string? Address;
|
||||
internal string Address { get; }
|
||||
/// <summary>
|
||||
/// The path to the temporary file where the certificate is stored.
|
||||
/// </summary>
|
||||
internal readonly string? CertificatePath;
|
||||
internal string? CertificatePath { get; private set; }
|
||||
/// <summary>
|
||||
/// The certificate used for the Dependabot proxy.
|
||||
/// </summary>
|
||||
internal readonly X509Certificate2? Certificate;
|
||||
internal X509Certificate2? Certificate { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether a Dependabot proxy is configured.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user