Skip to content

Commit

Permalink
Merge pull request #3197 from corbob/proxies
Browse files Browse the repository at this point in the history
(#3181) Let NuGet.Client get the system proxy if none is explicitly set.
  • Loading branch information
gep13 authored Jun 14, 2023
2 parents 917471c + 661464c commit bdba4f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 85 deletions.
48 changes: 0 additions & 48 deletions src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public static void SetupConfiguration(IList<string> args, ChocolateyConfiguratio
SetGlobalOptions(args, config, container);
SetEnvironmentOptions(config);
EnvironmentSettings.SetEnvironmentVariables(config);
SetProxyOptions(config, container);
// must be done last for overrides
SetLicensedOptions(config, license, configFileSettings);
// save all changes if there are any
Expand Down Expand Up @@ -464,53 +463,6 @@ private static void SetGlobalOptions(IList<string> args, ChocolateyConfiguration
});
}

private static void SetProxyOptions(ChocolateyConfiguration config, Container container)
{
// Evaluation order of Proxy settings: System Set -> Environment Variable Set -> Chocolatey Configuration File Set -> CLI Passed in Argument
var proxyAlreadySet = !string.IsNullOrWhiteSpace(config.Proxy.Location);
var onWindows = Platform.GetPlatform() == PlatformType.Windows;

// Only Windows has a registry provider, if it's already set, or we're not on Windows we don't need to continue.
if (proxyAlreadySet || !onWindows)
{
return;
}

// We don't yet have a Proxy Location, check if the system has one configured in the registry
var registryService = container.GetInstance<IRegistryService>();
var internetSettingsRegKey = registryService.GetKey(RegistryHive.CurrentUser, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");

if (!internetSettingsRegKey.GetValue("ProxyEnable").ToStringSafe().IsEqualTo("1"))
{
return;
}

var proxySetting = internetSettingsRegKey.GetValue("ProxyServer").ToStringSafe();

if (string.IsNullOrWhiteSpace(proxySetting))
{
return;
}

if (proxySetting.IndexOf(';') != -1)
{
var allProxies = proxySetting.Split(';');
proxySetting = allProxies.FirstOrDefault(s => s.TrimSafe().StartsWith("https="));

if (string.IsNullOrWhiteSpace(proxySetting))
{
proxySetting = allProxies.FirstOrDefault(s => s.TrimSafe().StartsWith("http="));
}
}

if (proxySetting?.IndexOf('=') != -1 && !proxySetting.StartsWith("http"))
{
return;
}

config.Proxy.Location = proxySetting.Split('=').LastOrDefault();
}

private static void SetEnvironmentOptions(ChocolateyConfiguration config)
{
config.Information.PlatformType = Platform.GetPlatform();
Expand Down
5 changes: 4 additions & 1 deletion src/chocolatey/infrastructure.app/nuget/NugetCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public static IEnumerable<SourceRepository> GetRemoteRepositories(ChocolateyConf
}
else
{
ProxyCache.Instance.Override(new System.Net.WebProxy());
// We need to override the proxy so that we don't use the NuGet configuration.
// We must however also be able to use the system proxy.
// Our changes to ProxyCache test for a null overridden proxy and get the system proxy if it's null.
ProxyCache.Instance.Override(proxy: null);
}

IEnumerable<string> sources = configuration.Sources.ToStringSafe().Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);
Expand Down
4 changes: 3 additions & 1 deletion tests/chocolatey-tests/chocolatey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ Describe "Ensuring Chocolatey is correctly installed" -Tag Environment, Chocolat
# These tests are not a true test of PowerShell v2 compatibility as -Version 2 does not guarantee that things run exactly as in a PowerShell 2 instance, but it is as close as we can get in a testing environment.
# Full proper testing on v2 would require a VM with only v2 installed.
# This is skipped when not run in CI because it modifies the local system.
Context "PowerShell v2 compatibility" -Skip:(-not $env:TEST_KITCHEN) {
# These are skipped on Proxy tests because the proxy server we use doesn't allow
# the Windows updates access this needs to install PowerShell 2 support
Context "PowerShell v2 compatibility" -Skip:(-not $env:TEST_KITCHEN) -Tag ProxySkip {
BeforeAll {
# TODO: This doesn't work on client OSes (might be Install-WindowsOptionalFeature). Make sure this works on both server and client.
Install-WindowsFeature powershell-v2
Expand Down
57 changes: 57 additions & 0 deletions tests/chocolatey-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,63 @@ To install a local, or remote file, you may use:
}
}

# Tagged as Internal since this package is only available internally and downloads from internal infrastructure.
Context 'Installing package with Open Source Get-ChocolateyWebFile, Get-WebFileName and Get-WebHeaders' -Tag Internal, FossOnly {
BeforeAll {
$paths = New-ChocolateyInstallSnapshot

# Cache directory is set here to prevent assertion failures
$Output = Invoke-Choco install get-chocolateywebfile "--cache-location=$($paths.PackagesPath)" --confirm
}

AfterAll {
$null = Invoke-Choco uninstall get-chocolateywebfile --confirm
}

It 'Exits with Success (0)' {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It 'Runs under background Service' -Tag Background {
$Output.Lines | Should -Contain 'Running in background mode' -Because $Output.String
}

It 'Outputs name of remote file' {
$Output.Lines | Should -Contain 'FileName: ChocolateyGUI.msi' -Because $Output.String
}

# We only get an output of System.Collections.Hashtable here,
# but that is enough for us to assert against the call to
# Get-WebHeaders
It 'Outputs information from web headers' {
$Output.Lines | Should -Contain 'System.Collections.Hashtable' -Because $Output.String
}

It 'Outputs downloading software' {
$Output.Lines | Should -Contain 'Downloading get-chocolateywebfile' -Because $Output.String
}

It 'Outputs download completed' {
$Output.Lines | Should -Contain "Download of ChocolateyGUI.msi (16.23 MB) completed." -Because $Output.String
}

It 'Outputs path to msi executable' {
$Output.Lines | Should -Contain "Path: $env:ChocolateyInstall\lib\get-chocolateywebfile\tools\ChocolateyGUI.msi" -Because $Output.String
}

It 'Outputs installing msi executable' {
$Output.Lines | Should -Contain 'Installing get-chocolateywebfile...' -Because $Output.String
}

It 'Outputs installation was successful' {
$Output.Lines | Should -Contain 'get-chocolateywebfile has been installed.' -Because $Output.String
}

It 'Installs software to expected directory' {
"${env:ProgramFiles(x86)}\Chocolatey GUI\ChocolateyGui.exe" | Should -Exist
}
}

# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
# Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI
Test-NuGetPaths
Expand Down
35 changes: 0 additions & 35 deletions tests/chocolatey-tests/features/Proxy.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,6 @@ Describe "Proxy configuration (<Name>)" -Tag Proxy, ProxySkip -ForEach $TestCase
continue
}

$ConfigurationsToTest.System {
$Output.String | Should -MatchExactly "Proxy\.Location='$SystemSet'"
continue
}

default {
$Output.String | Should -Not -Match "Proxy\.Location"
$Output.String | Should -Not -Match "Proxy\.BypassList"
Expand All @@ -234,33 +229,3 @@ Describe "Proxy configuration (<Name>)" -Tag Proxy, ProxySkip -ForEach $TestCase
}
}
}

Describe "Multi-Protocol Proxy configuration" -Tag Proxy, ProxySkip -Skip:(-not $env:TEST_KITCHEN) {
BeforeAll {
Initialize-ChocolateyTestInstall
New-ChocolateyInstallSnapshot
$arguments = $null

$SystemSet = "SystemSetProxy"
Set-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyServer -Value "ftp=something;socks=another"
Set-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyEnable -Value 1
}

AfterAll {
Remove-ChocolateyTestInstall
Remove-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyServer -ErrorAction Ignore
Remove-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyEnable -ErrorAction Ignore
$env:https_proxy = $null
}

Context "Configured for command (<Command>)" -ForEach $CommandsToTest {
BeforeAll {
$Output = Invoke-Choco $Command @arguments @ExtraArguments --debug --verbose --noop
}

It "Should output the correct Proxy setting" {
$Output.String | Should -Not -MatchExactly "Proxy\.Location='$SystemSet'"
$Output.String | Should -Not -MatchExactly "Proxy\.Location"
}
}
}

0 comments on commit bdba4f7

Please sign in to comment.