Skip to content

Commit bdba4f7

Browse files
authored
Merge pull request chocolatey#3197 from corbob/proxies
(chocolatey#3181) Let NuGet.Client get the system proxy if none is explicitly set.
2 parents 917471c + 661464c commit bdba4f7

File tree

5 files changed

+64
-85
lines changed

5 files changed

+64
-85
lines changed

src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs

-48
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public static void SetupConfiguration(IList<string> args, ChocolateyConfiguratio
8989
SetGlobalOptions(args, config, container);
9090
SetEnvironmentOptions(config);
9191
EnvironmentSettings.SetEnvironmentVariables(config);
92-
SetProxyOptions(config, container);
9392
// must be done last for overrides
9493
SetLicensedOptions(config, license, configFileSettings);
9594
// save all changes if there are any
@@ -464,53 +463,6 @@ private static void SetGlobalOptions(IList<string> args, ChocolateyConfiguration
464463
});
465464
}
466465

467-
private static void SetProxyOptions(ChocolateyConfiguration config, Container container)
468-
{
469-
// Evaluation order of Proxy settings: System Set -> Environment Variable Set -> Chocolatey Configuration File Set -> CLI Passed in Argument
470-
var proxyAlreadySet = !string.IsNullOrWhiteSpace(config.Proxy.Location);
471-
var onWindows = Platform.GetPlatform() == PlatformType.Windows;
472-
473-
// Only Windows has a registry provider, if it's already set, or we're not on Windows we don't need to continue.
474-
if (proxyAlreadySet || !onWindows)
475-
{
476-
return;
477-
}
478-
479-
// We don't yet have a Proxy Location, check if the system has one configured in the registry
480-
var registryService = container.GetInstance<IRegistryService>();
481-
var internetSettingsRegKey = registryService.GetKey(RegistryHive.CurrentUser, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
482-
483-
if (!internetSettingsRegKey.GetValue("ProxyEnable").ToStringSafe().IsEqualTo("1"))
484-
{
485-
return;
486-
}
487-
488-
var proxySetting = internetSettingsRegKey.GetValue("ProxyServer").ToStringSafe();
489-
490-
if (string.IsNullOrWhiteSpace(proxySetting))
491-
{
492-
return;
493-
}
494-
495-
if (proxySetting.IndexOf(';') != -1)
496-
{
497-
var allProxies = proxySetting.Split(';');
498-
proxySetting = allProxies.FirstOrDefault(s => s.TrimSafe().StartsWith("https="));
499-
500-
if (string.IsNullOrWhiteSpace(proxySetting))
501-
{
502-
proxySetting = allProxies.FirstOrDefault(s => s.TrimSafe().StartsWith("http="));
503-
}
504-
}
505-
506-
if (proxySetting?.IndexOf('=') != -1 && !proxySetting.StartsWith("http"))
507-
{
508-
return;
509-
}
510-
511-
config.Proxy.Location = proxySetting.Split('=').LastOrDefault();
512-
}
513-
514466
private static void SetEnvironmentOptions(ChocolateyConfiguration config)
515467
{
516468
config.Information.PlatformType = Platform.GetPlatform();

src/chocolatey/infrastructure.app/nuget/NugetCommon.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ public static IEnumerable<SourceRepository> GetRemoteRepositories(ChocolateyConf
124124
}
125125
else
126126
{
127-
ProxyCache.Instance.Override(new System.Net.WebProxy());
127+
// We need to override the proxy so that we don't use the NuGet configuration.
128+
// We must however also be able to use the system proxy.
129+
// Our changes to ProxyCache test for a null overridden proxy and get the system proxy if it's null.
130+
ProxyCache.Instance.Override(proxy: null);
128131
}
129132

130133
IEnumerable<string> sources = configuration.Sources.ToStringSafe().Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);

tests/chocolatey-tests/chocolatey.Tests.ps1

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ Describe "Ensuring Chocolatey is correctly installed" -Tag Environment, Chocolat
170170
# 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.
171171
# Full proper testing on v2 would require a VM with only v2 installed.
172172
# This is skipped when not run in CI because it modifies the local system.
173-
Context "PowerShell v2 compatibility" -Skip:(-not $env:TEST_KITCHEN) {
173+
# These are skipped on Proxy tests because the proxy server we use doesn't allow
174+
# the Windows updates access this needs to install PowerShell 2 support
175+
Context "PowerShell v2 compatibility" -Skip:(-not $env:TEST_KITCHEN) -Tag ProxySkip {
174176
BeforeAll {
175177
# TODO: This doesn't work on client OSes (might be Install-WindowsOptionalFeature). Make sure this works on both server and client.
176178
Install-WindowsFeature powershell-v2

tests/chocolatey-tests/commands/choco-install.Tests.ps1

+57
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,63 @@ To install a local, or remote file, you may use:
18061806
}
18071807
}
18081808

1809+
# Tagged as Internal since this package is only available internally and downloads from internal infrastructure.
1810+
Context 'Installing package with Open Source Get-ChocolateyWebFile, Get-WebFileName and Get-WebHeaders' -Tag Internal, FossOnly {
1811+
BeforeAll {
1812+
$paths = New-ChocolateyInstallSnapshot
1813+
1814+
# Cache directory is set here to prevent assertion failures
1815+
$Output = Invoke-Choco install get-chocolateywebfile "--cache-location=$($paths.PackagesPath)" --confirm
1816+
}
1817+
1818+
AfterAll {
1819+
$null = Invoke-Choco uninstall get-chocolateywebfile --confirm
1820+
}
1821+
1822+
It 'Exits with Success (0)' {
1823+
$Output.ExitCode | Should -Be 0 -Because $Output.String
1824+
}
1825+
1826+
It 'Runs under background Service' -Tag Background {
1827+
$Output.Lines | Should -Contain 'Running in background mode' -Because $Output.String
1828+
}
1829+
1830+
It 'Outputs name of remote file' {
1831+
$Output.Lines | Should -Contain 'FileName: ChocolateyGUI.msi' -Because $Output.String
1832+
}
1833+
1834+
# We only get an output of System.Collections.Hashtable here,
1835+
# but that is enough for us to assert against the call to
1836+
# Get-WebHeaders
1837+
It 'Outputs information from web headers' {
1838+
$Output.Lines | Should -Contain 'System.Collections.Hashtable' -Because $Output.String
1839+
}
1840+
1841+
It 'Outputs downloading software' {
1842+
$Output.Lines | Should -Contain 'Downloading get-chocolateywebfile' -Because $Output.String
1843+
}
1844+
1845+
It 'Outputs download completed' {
1846+
$Output.Lines | Should -Contain "Download of ChocolateyGUI.msi (16.23 MB) completed." -Because $Output.String
1847+
}
1848+
1849+
It 'Outputs path to msi executable' {
1850+
$Output.Lines | Should -Contain "Path: $env:ChocolateyInstall\lib\get-chocolateywebfile\tools\ChocolateyGUI.msi" -Because $Output.String
1851+
}
1852+
1853+
It 'Outputs installing msi executable' {
1854+
$Output.Lines | Should -Contain 'Installing get-chocolateywebfile...' -Because $Output.String
1855+
}
1856+
1857+
It 'Outputs installation was successful' {
1858+
$Output.Lines | Should -Contain 'get-chocolateywebfile has been installed.' -Because $Output.String
1859+
}
1860+
1861+
It 'Installs software to expected directory' {
1862+
"${env:ProgramFiles(x86)}\Chocolatey GUI\ChocolateyGui.exe" | Should -Exist
1863+
}
1864+
}
1865+
18091866
# This needs to be the last test in this block, to ensure NuGet configurations aren't being created.
18101867
# Any tests after this block are expected to generate the configuration as they're explicitly using the NuGet CLI
18111868
Test-NuGetPaths

tests/chocolatey-tests/features/Proxy.Tests.ps1

-35
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,6 @@ Describe "Proxy configuration (<Name>)" -Tag Proxy, ProxySkip -ForEach $TestCase
221221
continue
222222
}
223223

224-
$ConfigurationsToTest.System {
225-
$Output.String | Should -MatchExactly "Proxy\.Location='$SystemSet'"
226-
continue
227-
}
228-
229224
default {
230225
$Output.String | Should -Not -Match "Proxy\.Location"
231226
$Output.String | Should -Not -Match "Proxy\.BypassList"
@@ -234,33 +229,3 @@ Describe "Proxy configuration (<Name>)" -Tag Proxy, ProxySkip -ForEach $TestCase
234229
}
235230
}
236231
}
237-
238-
Describe "Multi-Protocol Proxy configuration" -Tag Proxy, ProxySkip -Skip:(-not $env:TEST_KITCHEN) {
239-
BeforeAll {
240-
Initialize-ChocolateyTestInstall
241-
New-ChocolateyInstallSnapshot
242-
$arguments = $null
243-
244-
$SystemSet = "SystemSetProxy"
245-
Set-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyServer -Value "ftp=something;socks=another"
246-
Set-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyEnable -Value 1
247-
}
248-
249-
AfterAll {
250-
Remove-ChocolateyTestInstall
251-
Remove-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyServer -ErrorAction Ignore
252-
Remove-ItemProperty -Path 'HKCU:/Software/Microsoft/Windows/CurrentVersion/Internet Settings' -Name ProxyEnable -ErrorAction Ignore
253-
$env:https_proxy = $null
254-
}
255-
256-
Context "Configured for command (<Command>)" -ForEach $CommandsToTest {
257-
BeforeAll {
258-
$Output = Invoke-Choco $Command @arguments @ExtraArguments --debug --verbose --noop
259-
}
260-
261-
It "Should output the correct Proxy setting" {
262-
$Output.String | Should -Not -MatchExactly "Proxy\.Location='$SystemSet'"
263-
$Output.String | Should -Not -MatchExactly "Proxy\.Location"
264-
}
265-
}
266-
}

0 commit comments

Comments
 (0)