|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Security.Cryptography.X509Certificates; |
| 5 | +using Semmle.Util; |
| 6 | +using Semmle.Util.Logging; |
| 7 | + |
| 8 | +namespace Semmle.Extraction.CSharp.DependencyFetching |
| 9 | +{ |
| 10 | + public class DependabotProxy : IDisposable |
| 11 | + { |
| 12 | + private readonly string host; |
| 13 | + private readonly string port; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// The full address of the Dependabot proxy, if available. |
| 17 | + /// </summary> |
| 18 | + internal string Address { get; } |
| 19 | + /// <summary> |
| 20 | + /// The path to the temporary file where the certificate is stored. |
| 21 | + /// </summary> |
| 22 | + internal string? CertificatePath { get; private set; } |
| 23 | + /// <summary> |
| 24 | + /// The certificate used for the Dependabot proxy. |
| 25 | + /// </summary> |
| 26 | + internal X509Certificate2? Certificate { get; private set; } |
| 27 | + |
| 28 | + internal static DependabotProxy? GetDependabotProxy(ILogger logger, TemporaryDirectory tempWorkingDirectory) |
| 29 | + { |
| 30 | + // Setting HTTP(S)_PROXY and SSL_CERT_FILE have no effect on Windows or macOS, |
| 31 | + // but we would still end up using the Dependabot proxy to check for feed reachability. |
| 32 | + // This would result in us discovering that the feeds are reachable, but `dotnet` would |
| 33 | + // fail to connect to them. To prevent this from happening, we do not initialise an |
| 34 | + // instance of `DependabotProxy` on those platforms. |
| 35 | + if (SystemBuildActions.Instance.IsWindows() || SystemBuildActions.Instance.IsMacOs()) return null; |
| 36 | + |
| 37 | + // Obtain and store the address of the Dependabot proxy, if available. |
| 38 | + var host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost); |
| 39 | + var port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort); |
| 40 | + |
| 41 | + if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port)) |
| 42 | + { |
| 43 | + logger.LogInfo("No Dependabot proxy credentials are configured."); |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + var result = new DependabotProxy(host, port); |
| 48 | + logger.LogInfo($"Dependabot proxy configured at {result.Address}"); |
| 49 | + |
| 50 | + // Obtain and store the proxy's certificate, if available. |
| 51 | + var cert = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate); |
| 52 | + |
| 53 | + if (!string.IsNullOrWhiteSpace(cert)) |
| 54 | + { |
| 55 | + logger.LogInfo("No certificate configured for Dependabot proxy."); |
| 56 | + |
| 57 | + var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".dependabot-proxy")); |
| 58 | + Directory.CreateDirectory(certDirPath.FullName); |
| 59 | + |
| 60 | + result.CertificatePath = Path.Join(certDirPath.FullName, "proxy.crt"); |
| 61 | + var certFile = new FileInfo(result.CertificatePath); |
| 62 | + |
| 63 | + using var writer = certFile.CreateText(); |
| 64 | + writer.Write(cert); |
| 65 | + writer.Close(); |
| 66 | + |
| 67 | + logger.LogInfo($"Stored Dependabot proxy certificate at {result.CertificatePath}"); |
| 68 | + |
| 69 | + result.Certificate = X509Certificate2.CreateFromPem(cert); |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + private DependabotProxy(string host, string port) |
| 76 | + { |
| 77 | + this.host = host; |
| 78 | + this.port = port; |
| 79 | + this.Address = $"http://{this.host}:{this.port}"; |
| 80 | + } |
| 81 | + |
| 82 | + public void Dispose() |
| 83 | + { |
| 84 | + this.Certificate?.Dispose(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments