Skip to content

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

Merged
merged 15 commits into from
Dec 6, 2024

Conversation

mbg
Copy link
Member

@mbg mbg commented Nov 19, 2024

This PR is part of work to enable private package registries to be used in Default Setup.

The existing Default Setup workflow will initialise the Dependabot package proxy, if a private package registry configuration is set. The host, port, and certificate used by the proxy are then passed to CodeQL in the analyze step.

The changes in this PR modify the C# extractor to recognise when the corresponding environment variables are set. If so, we use the data from those environment variables to:

  1. Construct the proxy address from the host and port and pass it to dotnet via the HTTP_PROXY and HTTPS_PROXY environment variables.
  2. Store the certificate on disk, and pass the path to dotnet via SSL_CERT_FILE.
  3. We also configure a suitable proxy for the feed reachability checks.

In testing so far, this works fine on Linux with fairly arbitrary versions of dotnet. It does not seem to work on macOS and likely also does not work on Windows.

@mbg mbg self-assigned this Nov 19, 2024
@github-actions github-actions bot added the C# label Nov 19, 2024
@mbg mbg force-pushed the mbg/csharp/set-proxy-cert-file branch from c5c1260 to 9731006 Compare November 19, 2024 13:45
@mbg mbg force-pushed the mbg/csharp/set-proxy-cert-file branch from 9731006 to a107872 Compare November 29, 2024 13:19
@mbg mbg marked this pull request as ready for review December 2, 2024 14:25
@mbg mbg requested a review from a team as a code owner December 2, 2024 14:25
Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, added some comments/questions.

What's not working on macOS? Is it the dotnet calls, or the httpclient that's not working?

httpClientHandler.Proxy = new WebProxy(this.dependabotProxy.Address);

if (!String.IsNullOrEmpty(this.dependabotProxy.CertificatePath))
{
Copy link
Contributor

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?

Copy link
Member Author

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.


namespace Semmle.Extraction.CSharp.DependencyFetching
{
public class DependabotProxy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably implement System.Net.IWebProxy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but I am not sure I see any immediate benefit to it. Although I guess it would simplify the implementation for the feed reachability checking? Happy to do that in a follow-up PR if it makes sense, but it would be nice to get this functionality shipped for now.

@mbg
Copy link
Member Author

mbg commented Dec 3, 2024

What's not working on macOS? Is it the dotnet calls

dotnet on macOS (and Windows) does not seem to respect HTTP(S)_PROXY and SSL_CERT_FILE (likely because it only uses OpenSSL under the hood on Linux?)

@mbg mbg force-pushed the mbg/csharp/set-proxy-cert-file branch from fca0b6a to dcfcb88 Compare December 3, 2024 18:48
@tamasvajk
Copy link
Contributor

dotnet on macOS (and Windows) does not seem to respect HTTP(S)_PROXY and SSL_CERT_FILE (likely because it only uses OpenSSL under the hood on Linux?)

If we don't expect this change to work on macos and windows, we should be explicit about it. Can we only create the dependabot proxy if we're on linux?

@mbg
Copy link
Member Author

mbg commented Dec 4, 2024

If we don't expect this change to work on macos and windows, we should be explicit about it. Can we only create the dependabot proxy if we're on linux?

@tamasvajk this is done as part of the Default Setup workflow with a dedicated action. Until recently, this only supported Linux. It does now support Windows and macOS as well, but the docs for the private registries feature will note the current limitations. In other words, it's not something we need to worry about in this C#-specific part.

@tamasvajk
Copy link
Contributor

If we don't expect this change to work on macos and windows, we should be explicit about it. Can we only create the dependabot proxy if we're on linux?

@tamasvajk this is done as part of the Default Setup workflow with a dedicated action. Until recently, this only supported Linux. It does now support Windows and macOS as well, but the docs for the private registries feature will note the current limitations. In other words, it's not something we need to worry about in this C#-specific part.

Does this mean that on Windows and MacOS, the CODEQL_PROXY_* environment variables are not going to be set?

@mbg mbg force-pushed the mbg/csharp/set-proxy-cert-file branch from dcfcb88 to 2e80e09 Compare December 5, 2024 12:17
@mbg
Copy link
Member Author

mbg commented Dec 5, 2024

Does this mean that on Windows and MacOS, the CODEQL_PROXY_* environment variables are not going to be set?

@tamasvajk They do get set on Windows and macOS. The environment variables we set here just wouldn't affect dotnet on those platforms (though it would be desirable if they did).

However, the proxy we install for the feed reachability check would be affected. That would result in a weird situation on those platforms where we check whether the feeds that require the proxy (i.e. require authentication) are reachable, determine that they are, but then dotnet cannot connect them.

So I have now added a check to skip initialising the DependabotProxy instance on those platforms.

I have also addressed all of your other review comments.

tamasvajk
tamasvajk previously approved these changes Dec 5, 2024
Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mbg
Copy link
Member Author

mbg commented Dec 6, 2024

@tamasvajk I tested this a bit more after the changes in response to your review, and ran into some flakiness with the new X509Certificate2 call failing since the certificate hadn't been flushed to disk correctly yet in some cases. I have added an explicit Close call on the writer, but also am just loading the certificate from the existing string value obtained from the environment variable, so we can skip reading the certificate from disk entirely.

@mbg mbg requested a review from tamasvajk December 6, 2024 13:15

using var writer = certFile.CreateText();
writer.Write(cert);
writer.Close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm somewhat surprised that this is needed. The writer is disposed at the end of this method, and we wouldn't access the file elsewhere before that. If you want to control the disposal a bit more fine grained, you could still use a using block, and you could get rid of the Close call.

using(var writer = certFile.CreateText()) 
{
  writer.Write(cert);
}

@mbg mbg merged commit e16adda into main Dec 6, 2024
16 checks passed
@mbg mbg deleted the mbg/csharp/set-proxy-cert-file branch December 6, 2024 14:03
@mbg mbg restored the mbg/csharp/set-proxy-cert-file branch December 6, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants