-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C#: Set proxy environment variables, if Dependabot proxy is detected #18029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
5376012
232caa7
8ca7560
6cd5711
de415d6
87bd21e
e999ec1
984091d
ca251fb
ee7f0b0
2e80e09
7369d04
952488c
653d68e
c8ccfe4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using Semmle.Util; | ||
| using Semmle.Util.Logging; | ||
|
|
||
| namespace Semmle.Extraction.CSharp.DependencyFetching | ||
| { | ||
| public class DependabotProxy : IDisposable | ||
| { | ||
| private readonly string host; | ||
| private readonly string port; | ||
|
|
||
| /// <summary> | ||
| /// The full address of the Dependabot proxy, if available. | ||
| /// </summary> | ||
| internal string Address { get; } | ||
| /// <summary> | ||
| /// The path to the temporary file where the certificate is stored. | ||
| /// </summary> | ||
| internal string? CertificatePath { get; private set; } | ||
| /// <summary> | ||
| /// The certificate used for the Dependabot proxy. | ||
| /// </summary> | ||
| internal X509Certificate2? Certificate { get; private set; } | ||
|
|
||
| internal static DependabotProxy? GetDependabotProxy(ILogger logger, 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. | ||
| // 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. | ||
| if (SystemBuildActions.Instance.IsWindows() || SystemBuildActions.Instance.IsMacOs()) return null; | ||
|
|
||
| // Obtain and store the address of the Dependabot proxy, if available. | ||
| 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 null; | ||
| } | ||
|
|
||
| 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)) | ||
| { | ||
| logger.LogInfo("No certificate configured for Dependabot proxy."); | ||
|
|
||
| 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); | ||
|
Check warning on line 68 in csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependabotProxy.cs
|
||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private DependabotProxy(string host, string port) | ||
| { | ||
| this.host = host; | ||
| this.port = port; | ||
| this.Address = $"http://{this.host}:{this.port}"; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.Certificate?.Dispose(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this, because we need to support a certificate that's not signed by a well-known trusted root cert authority?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. The Dependabot Proxy uses a self-signed certificate that is generated when we initialise it in the Default Setup workflow. That's then passed to us in an environment variable, and we should trust it.