Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed partial class DependencyManager : IDisposable, ICompilationInfoCon
private readonly ILogger logger;
private readonly IDiagnosticsWriter diagnosticsWriter;
private readonly NugetPackageRestorer nugetPackageRestorer;
private readonly IDependabotProxy? dependabotProxy;
private readonly IRegistryProxy? registryProxy;
private readonly IDotNet dotnet;
private readonly FileContent fileContent;
private readonly IFileProvider fileProvider;
Expand Down Expand Up @@ -106,11 +106,11 @@ void exitCallback(int ret, string msg, bool silent)
return BuildScript.Success;
}).Run(SystemBuildActions.Instance, startCallback, exitCallback);

dependabotProxy = DependabotProxy.Make(logger, diagnosticsWriter, tempWorkingDirectory);
registryProxy = RegistryProxy.Make(logger, diagnosticsWriter, tempWorkingDirectory);

try
{
this.dotnet = DotNet.Make(logger, dotnetPath, tempWorkingDirectory, dependabotProxy);
this.dotnet = DotNet.Make(logger, dotnetPath, tempWorkingDirectory, registryProxy);
runtimeLazy = new Lazy<Runtime>(() => new Runtime(dotnet));
}
catch
Expand All @@ -119,7 +119,7 @@ void exitCallback(int ret, string msg, bool silent)
throw;
}

nugetPackageRestorer = new NugetPackageRestorer(fileProvider, fileContent, dotnet, dependabotProxy, diagnosticsWriter, logger, this);
nugetPackageRestorer = new NugetPackageRestorer(fileProvider, fileContent, dotnet, registryProxy, diagnosticsWriter, logger, this);

var dllLocations = fileProvider.Dlls.Select(x => new AssemblyLookupLocation(x)).ToHashSet();
dllLocations.UnionWith(nugetPackageRestorer.Restore());
Expand Down Expand Up @@ -544,7 +544,7 @@ private void AnalyseProject(FileInfo project)
public void Dispose()
{
nugetPackageRestorer?.Dispose();
dependabotProxy?.Dispose();
registryProxy?.Dispose();
if (cleanupTempWorkingDirectory)
{
tempWorkingDirectory?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ private DotNet(IDotNetCliInvoker dotnetCliInvoker, ILogger logger, bool runDotne
}
}

private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, IDependabotProxy? dependabotProxy) : this(new DotNetCliInvoker(logger, Path.Join(dotNetPath ?? string.Empty, "dotnet"), dependabotProxy), logger, dotNetPath is null, tempWorkingDirectory) { }
private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, IRegistryProxy? registryProxy) : this(new DotNetCliInvoker(logger, Path.Join(dotNetPath ?? string.Empty, "dotnet"), registryProxy), logger, dotNetPath is null, tempWorkingDirectory) { }

internal static IDotNet Make(IDotNetCliInvoker dotnetCliInvoker, ILogger logger, bool runDotnetInfo) => new DotNet(dotnetCliInvoker, logger, runDotnetInfo);

public static IDotNet Make(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, IDependabotProxy? dependabotProxy) => new DotNet(logger, dotNetPath, tempWorkingDirectory, dependabotProxy);
public static IDotNet Make(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, IRegistryProxy? registryProxy) => new DotNet(logger, dotNetPath, tempWorkingDirectory, registryProxy);

private static void HandleRetryExitCode143(string dotnet, int attempt, ILogger logger)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
internal sealed class DotNetCliInvoker : IDotNetCliInvoker
{
private readonly ILogger logger;
private readonly IDependabotProxy? proxy;
private readonly IRegistryProxy? proxy;

public string Exec { get; }

public DotNetCliInvoker(ILogger logger, string exec, IDependabotProxy? dependabotProxy)
public DotNetCliInvoker(ILogger logger, string exec, IRegistryProxy? registryProxy)
{
this.logger = logger;
this.proxy = dependabotProxy;
this.proxy = registryProxy;
this.Exec = exec;
logger.LogInfo($"Using .NET CLI executable: '{Exec}'");
}
Expand All @@ -46,7 +46,7 @@ private ProcessStartInfo MakeDotnetStartInfo(List<string> args, string? workingD
// Configure the proxy settings, if applicable.
if (this.proxy != null)
{
logger.LogDebug($"Configuring environment variables for the Dependabot proxy at {this.proxy.Address}");
logger.LogDebug($"Configuring environment variables for the Registry proxy at {this.proxy.Address}");

startInfo.EnvironmentVariables["HTTP_PROXY"] = this.proxy.Address;
startInfo.EnvironmentVariables["HTTPS_PROXY"] = this.proxy.Address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ internal static class EnvironmentVariableNames
public const string DiagnosticDir = "CODEQL_EXTRACTOR_CSHARP_DIAGNOSTIC_DIR";

/// <summary>
/// Specifies the hostname of the Dependabot proxy.
/// Specifies the hostname of the Registry proxy.
/// </summary>
public const string ProxyHost = "CODEQL_PROXY_HOST";

/// <summary>
/// Specifies the hostname of the Dependabot proxy.
/// Specifies the port of the Registry proxy.
/// </summary>
public const string ProxyPort = "CODEQL_PROXY_PORT";

/// <summary>
/// Contains the certificate used by the Dependabot proxy.
/// Contains the certificate used by the Registry proxy.
/// </summary>
public const string ProxyCertificate = "CODEQL_PROXY_CA_CERTIFICATE";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ internal sealed partial class FeedManager : IDisposable
/// </summary>
public ImmutableHashSet<string> ReachableDefaultFeeds => lazyReachableDefaultFeeds.Value;

public FeedManager(ILogger logger, IDotNet dotnet, IDependabotProxy? dependabotProxy, IFileProvider fileProvider, IFeedManagerIO feedManagerIo)
public FeedManager(ILogger logger, IDotNet dotnet, IRegistryProxy? registryProxy, IFileProvider fileProvider, IFeedManagerIO feedManagerIo)
{
this.logger = logger;
this.dotnet = dotnet;
this.fileProvider = fileProvider;
this.feedManagerIo = feedManagerIo;
privateRegistryFeeds = dependabotProxy?.RegistryURLs ?? [];
privateRegistryFeeds = registryProxy?.RegistryURLs ?? [];
HasPrivateRegistryFeeds = privateRegistryFeeds.Count > 0;
privateRegistryBaseFeeds = dependabotProxy?.RegistryBaseURLs ?? [];
privateRegistryBaseFeeds = registryProxy?.RegistryBaseURLs ?? [];
hasPrivateRegistryBaseFeeds = privateRegistryBaseFeeds.Count > 0;

DefaultFeeds = hasPrivateRegistryBaseFeeds
Expand All @@ -122,8 +122,8 @@ public FeedManager(ILogger logger, IDotNet dotnet, IDependabotProxy? dependabotP
lazyReachableDefaultFeeds = new Lazy<ImmutableHashSet<string>>(() => CheckSpecifiedFeeds(DefaultFeeds));
}

public FeedManager(ILogger logger, IDotNet dotnet, IDependabotProxy? dependabotProxy, IFileProvider fileProvider)
: this(logger, dotnet, dependabotProxy, fileProvider, new FeedManagerIO(logger, dependabotProxy))
public FeedManager(ILogger logger, IDotNet dotnet, IRegistryProxy? registryProxy, IFileProvider fileProvider)
: this(logger, dotnet, registryProxy, fileProvider, new FeedManagerIO(logger, registryProxy))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
public class FeedManagerIO : IFeedManagerIO
{
private readonly ILogger logger;
private readonly IDependabotProxy? dependabotProxy;
private readonly IRegistryProxy? registryProxy;

public FeedManagerIO(ILogger logger, IDependabotProxy? dependabotProxy)
public FeedManagerIO(ILogger logger, IRegistryProxy? registryProxy)
{
this.logger = logger;
this.dependabotProxy = dependabotProxy;
this.registryProxy = registryProxy;
}

public string? GetDirectoryName(string path)
Expand All @@ -43,13 +43,13 @@ public bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount)
{
logger.LogInfo($"Checking if NuGet feed '{feed}' is reachable...");

// Configure the HttpClient to be aware of the Dependabot Proxy, if used.
// Configure the HttpClient to be aware of the Registry proxy, if used.
HttpClientHandler httpClientHandler = new();
if (dependabotProxy != null)
if (registryProxy != null)
{
httpClientHandler.Proxy = new WebProxy(dependabotProxy.Address);
httpClientHandler.Proxy = new WebProxy(registryProxy.Address);

if (dependabotProxy.Certificate != null)
if (registryProxy.Certificate != null)
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, _) =>
{
Expand All @@ -60,11 +60,11 @@ public bool IsFeedReachable(string feed, int timeoutMilliSeconds, int tryCount)
: chain is null
? "chain"
: "certificate";
logger.LogWarning($"Dependabot proxy certificate validation failed due to missing {msg}");
logger.LogWarning($"Registry proxy certificate validation failed due to missing {msg}");
return false;
}
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chain.ChainPolicy.CustomTrustStore.Add(dependabotProxy.Certificate);
chain.ChainPolicy.CustomTrustStore.Add(registryProxy.Certificate);
return chain.Build(cert);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Semmle.Extraction.CSharp.DependencyFetching
{
public interface IDependabotProxy : IDisposable
public interface IRegistryProxy : IDisposable
{
/// <summary>
/// The full address of the Dependabot proxy, if available.
/// The full address of the Registry proxy, if available.
/// </summary>
string Address { get; }

Expand All @@ -27,7 +27,7 @@ public interface IDependabotProxy : IDisposable
string? CertificatePath { get; }

/// <summary>
/// The certificate used for the Dependabot proxy.
/// The certificate used for the Registry proxy.
/// </summary>
X509Certificate2? Certificate { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Semmle.Extraction.CSharp.DependencyFetching
{
public interface IDependabotProxyConfiguration
public interface IRegistryProxyConfiguration
{
// The host of the Dependabot proxy, if available.
// The host of the Registry proxy, if available.
string? Host { get; }

// The port of the Dependabot proxy, if available.
// The port of the Registry proxy, if available.
string? Port { get; }

// The certificate of the Dependabot proxy, if available.
// The certificate of the Registry proxy, if available.
string? Certificate { get; }

// The list of package registries that are configured for the proxy, if any.
// The list of package registries that are configured for the Registry proxy, if any.
// The value of the environment variable should be a JSON array of objects, such as:
// [ { "type": "nuget_feed", "url": "https://nuget.pkg.github.com/org/index.json" } ]
string? RegistryURLs { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public NugetPackageRestorer(
IFileProvider fileProvider,
FileContent fileContent,
IDotNet dotnet,
IDependabotProxy? dependabotProxy,
IRegistryProxy? registryProxy,
IDiagnosticsWriter diagnosticsWriter,
ILogger logger,
ICompilationInfoContainer compilationInfoContainer)
Expand All @@ -47,7 +47,7 @@ public NugetPackageRestorer(
PackageDirectory = new DependencyDirectory("packages", "package", logger);
legacyPackageDirectory = new DependencyDirectory("legacypackages", "legacy package", logger);
missingPackageDirectory = new DependencyDirectory("missingpackages", "missing package", logger);
feedManager = new FeedManager(logger, dotnet, dependabotProxy, fileProvider);
feedManager = new FeedManager(logger, dotnet, registryProxy, fileProvider);
}

public string? TryRestore(string package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Semmle.Extraction.CSharp.DependencyFetching
{
public class DependabotProxy : IDependabotProxy
public class RegistryProxy : IRegistryProxy
{
/// <summary>
/// Represents configurations for package registries.
Expand Down Expand Up @@ -65,13 +65,13 @@ public class RegistryConfig

public X509Certificate2? Certificate { get; private set; }

private DependabotProxy(IDependabotProxyConfiguration config, ILogger logger, TemporaryDirectory tempWorkingDirectory)
private RegistryProxy(IRegistryProxyConfiguration config, ILogger logger, TemporaryDirectory tempWorkingDirectory)
{
Address = $"http://{config.Host}:{config.Port}";

if (!string.IsNullOrWhiteSpace(config.Certificate))
{
var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".dependabot-proxy"));
var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".registry-proxy"));
Directory.CreateDirectory(certDirPath.FullName);

CertificatePath = Path.Join(certDirPath.FullName, "proxy.crt");
Expand All @@ -81,7 +81,7 @@ private DependabotProxy(IDependabotProxyConfiguration config, ILogger logger, Te
writer.Write(config.Certificate);
writer.Close();

logger.LogInfo($"Stored Dependabot proxy certificate at {CertificatePath}");
logger.LogInfo($"Stored Registry proxy certificate at {CertificatePath}");

Certificate = X509Certificate2.CreateFromPem(config.Certificate);
}
Expand Down Expand Up @@ -127,37 +127,37 @@ private DependabotProxy(IDependabotProxyConfiguration config, ILogger logger, Te
}
}

internal static IDependabotProxy? Make(ILogger logger, IDiagnosticsWriter diagnosticsWriter, TemporaryDirectory tempWorkingDirectory)
internal static IRegistryProxy? Make(ILogger logger, IDiagnosticsWriter diagnosticsWriter, TemporaryDirectory tempWorkingDirectory)
{
// Setting HTTP(S)_PROXY and SSL_CERT_FILE have no effect on Windows or macOS,
// but we would still end up using the Dependabot proxy to check for feed reachability.
// but we would still end up using the Registry proxy to check for feed reachability.
// This would result in us discovering that the feeds are reachable, but `dotnet` would
// fail to connect to them. To prevent this from happening, we do not initialise an
// instance of `DependabotProxy` on those platforms.
// instance of `RegistryProxy` on those platforms.
if (SystemBuildActions.Instance.IsWindows() || SystemBuildActions.Instance.IsMacOs())
{
return null;
}

return Make(new DependabotProxyConfiguration(), logger, diagnosticsWriter, tempWorkingDirectory);
return Make(new RegistryProxyConfiguration(), logger, diagnosticsWriter, tempWorkingDirectory);
}

/// <summary>
/// Creates an instance of the Dependabot proxy using the specified configuration.
/// Creates an instance of the Registry proxy using the specified configuration.
/// Returns null if the proxy cannot be created.
/// This overload is exposed primarily to enable platform-independent unit testing.
/// </summary>
internal static IDependabotProxy? Make(
IDependabotProxyConfiguration proxyConfig, ILogger logger, IDiagnosticsWriter diagnosticsWriter, TemporaryDirectory tempWorkingDirectory)
internal static IRegistryProxy? Make(
IRegistryProxyConfiguration proxyConfig, ILogger logger, IDiagnosticsWriter diagnosticsWriter, TemporaryDirectory tempWorkingDirectory)
{
if (string.IsNullOrWhiteSpace(proxyConfig.Host) || string.IsNullOrWhiteSpace(proxyConfig.Port))
{
logger.LogDebug("No Dependabot proxy credentials are configured.");
logger.LogDebug("No Registry proxy credentials are configured.");
return null;
}

var result = new DependabotProxy(proxyConfig, logger, tempWorkingDirectory);
logger.LogInfo($"Dependabot proxy configured at {result.Address}");
var result = new RegistryProxy(proxyConfig, logger, tempWorkingDirectory);
logger.LogInfo($"Registry proxy configured at {result.Address}");

// Emit a diagnostic for the discovered private registries, so that it is easy
// for users to see that they were picked up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Semmle.Extraction.CSharp.DependencyFetching
{
public class DependabotProxyConfiguration : IDependabotProxyConfiguration
public class RegistryProxyConfiguration : IRegistryProxyConfiguration
{
public string? Host { get; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost);

Expand Down
Loading
Loading