mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +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
|
public class DependabotProxy : IDisposable
|
||||||
{
|
{
|
||||||
private readonly string? host;
|
private readonly string host;
|
||||||
private readonly string? port;
|
private readonly string port;
|
||||||
private readonly FileInfo? certFile;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The full address of the Dependabot proxy, if available.
|
/// The full address of the Dependabot proxy, if available.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal readonly string? Address;
|
internal string Address { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The path to the temporary file where the certificate is stored.
|
/// The path to the temporary file where the certificate is stored.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal readonly string? CertificatePath;
|
internal string? CertificatePath { get; private set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The certificate used for the Dependabot proxy.
|
/// The certificate used for the Dependabot proxy.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal readonly X509Certificate2? Certificate;
|
internal X509Certificate2? Certificate { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
internal static DependabotProxy? GetDependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory)
|
||||||
/// Gets a value indicating whether a Dependabot proxy is configured.
|
|
||||||
/// </summary>
|
|
||||||
internal bool IsConfigured => !string.IsNullOrEmpty(this.Address);
|
|
||||||
|
|
||||||
internal DependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory)
|
|
||||||
{
|
{
|
||||||
// Obtain and store the address of the Dependabot proxy, if available.
|
// Obtain and store the address of the Dependabot proxy, if available.
|
||||||
this.host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost);
|
var host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost);
|
||||||
this.port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort);
|
var port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort);
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port))
|
if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port))
|
||||||
{
|
{
|
||||||
logger.LogInfo("No Dependabot proxy credentials are configured.");
|
logger.LogInfo("No Dependabot proxy credentials are configured.");
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.Address = $"http://{this.host}:{this.port}";
|
var result = new DependabotProxy(host, port);
|
||||||
logger.LogInfo($"Dependabot proxy configured at {this.Address}");
|
logger.LogInfo($"Dependabot proxy configured at {result.Address}");
|
||||||
|
|
||||||
// Obtain and store the proxy's certificate, if available.
|
// Obtain and store the proxy's certificate, if available.
|
||||||
var cert = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate);
|
var cert = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate);
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(cert))
|
if (!string.IsNullOrWhiteSpace(cert))
|
||||||
{
|
{
|
||||||
logger.LogInfo("No certificate configured for Dependabot proxy.");
|
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"));
|
return result;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ApplyProxy(ILogger logger, ProcessStartInfo startInfo)
|
private DependabotProxy(string host, string port)
|
||||||
{
|
{
|
||||||
// If the proxy isn't configured, we have nothing to do.
|
this.host = host;
|
||||||
if (!this.IsConfigured) return;
|
this.port = port;
|
||||||
|
this.Address = $"http://{this.host}:{this.port}";
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
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 ILogger logger;
|
||||||
private readonly IDiagnosticsWriter diagnosticsWriter;
|
private readonly IDiagnosticsWriter diagnosticsWriter;
|
||||||
private readonly NugetPackageRestorer nugetPackageRestorer;
|
private readonly NugetPackageRestorer nugetPackageRestorer;
|
||||||
private readonly DependabotProxy dependabotProxy;
|
private readonly DependabotProxy? dependabotProxy;
|
||||||
private readonly IDotNet dotnet;
|
private readonly IDotNet dotnet;
|
||||||
private readonly FileContent fileContent;
|
private readonly FileContent fileContent;
|
||||||
private readonly FileProvider fileProvider;
|
private readonly FileProvider fileProvider;
|
||||||
@@ -107,7 +107,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
return BuildScript.Success;
|
return BuildScript.Success;
|
||||||
}).Run(SystemBuildActions.Instance, startCallback, exitCallback);
|
}).Run(SystemBuildActions.Instance, startCallback, exitCallback);
|
||||||
|
|
||||||
dependabotProxy = new DependabotProxy(logger, tempWorkingDirectory);
|
dependabotProxy = DependabotProxy.GetDependabotProxy(logger, tempWorkingDirectory);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,11 +27,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
Info();
|
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);
|
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()
|
private void Info()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
internal sealed class DotNetCliInvoker : IDotNetCliInvoker
|
internal sealed class DotNetCliInvoker : IDotNetCliInvoker
|
||||||
{
|
{
|
||||||
private readonly ILogger logger;
|
private readonly ILogger logger;
|
||||||
private readonly DependabotProxy proxy;
|
private readonly DependabotProxy? proxy;
|
||||||
|
|
||||||
public string Exec { get; }
|
public string Exec { get; }
|
||||||
|
|
||||||
public DotNetCliInvoker(ILogger logger, string exec, DependabotProxy dependabotProxy)
|
public DotNetCliInvoker(ILogger logger, string exec, DependabotProxy? dependabotProxy)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.proxy = dependabotProxy;
|
this.proxy = dependabotProxy;
|
||||||
@@ -42,7 +42,14 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
startInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "true";
|
startInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "true";
|
||||||
|
|
||||||
// Configure the proxy settings, if applicable.
|
// 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;
|
return startInfo;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
private readonly FileProvider fileProvider;
|
private readonly FileProvider fileProvider;
|
||||||
private readonly FileContent fileContent;
|
private readonly FileContent fileContent;
|
||||||
private readonly IDotNet dotnet;
|
private readonly IDotNet dotnet;
|
||||||
private readonly DependabotProxy dependabotProxy;
|
private readonly DependabotProxy? dependabotProxy;
|
||||||
private readonly IDiagnosticsWriter diagnosticsWriter;
|
private readonly IDiagnosticsWriter diagnosticsWriter;
|
||||||
private readonly TemporaryDirectory legacyPackageDirectory;
|
private readonly TemporaryDirectory legacyPackageDirectory;
|
||||||
private readonly TemporaryDirectory missingPackageDirectory;
|
private readonly TemporaryDirectory missingPackageDirectory;
|
||||||
@@ -35,7 +35,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
FileProvider fileProvider,
|
FileProvider fileProvider,
|
||||||
FileContent fileContent,
|
FileContent fileContent,
|
||||||
IDotNet dotnet,
|
IDotNet dotnet,
|
||||||
DependabotProxy dependabotProxy,
|
DependabotProxy? dependabotProxy,
|
||||||
IDiagnosticsWriter diagnosticsWriter,
|
IDiagnosticsWriter diagnosticsWriter,
|
||||||
ILogger logger,
|
ILogger logger,
|
||||||
ICompilationInfoContainer compilationInfoContainer)
|
ICompilationInfoContainer compilationInfoContainer)
|
||||||
@@ -596,7 +596,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
|||||||
|
|
||||||
// Configure the HttpClient to be aware of the Dependabot Proxy, if used.
|
// Configure the HttpClient to be aware of the Dependabot Proxy, if used.
|
||||||
HttpClientHandler httpClientHandler = new();
|
HttpClientHandler httpClientHandler = new();
|
||||||
if (this.dependabotProxy.IsConfigured)
|
if (this.dependabotProxy != null)
|
||||||
{
|
{
|
||||||
httpClientHandler.Proxy = new WebProxy(this.dependabotProxy.Address);
|
httpClientHandler.Proxy = new WebProxy(this.dependabotProxy.Address);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user