Fixes #759 doesn't change proxy environment variables (#760)

* Fixes #759 doesn't change proxy environment variables

* Update RunnerWebProxy.cs

* Update RunnerWebProxyL0.cs

Co-authored-by: Tingluo Huang <tingluohuang@github.com>
This commit is contained in:
Jason Laqua
2020-11-03 09:47:30 -06:00
committed by GitHub
parent 9d678cb270
commit 3ebaeb9f19
2 changed files with 46 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -71,7 +71,7 @@ namespace GitHub.Runner.Sdk
if (!string.IsNullOrEmpty(httpProxyAddress) && Uri.TryCreate(httpProxyAddress, UriKind.Absolute, out var proxyHttpUri)) if (!string.IsNullOrEmpty(httpProxyAddress) && Uri.TryCreate(httpProxyAddress, UriKind.Absolute, out var proxyHttpUri))
{ {
_httpProxyAddress = proxyHttpUri.AbsoluteUri; _httpProxyAddress = proxyHttpUri.OriginalString;
// Set both environment variables since there are tools support both casing (curl, wget) and tools support only one casing (docker) // Set both environment variables since there are tools support both casing (curl, wget) and tools support only one casing (docker)
Environment.SetEnvironmentVariable("HTTP_PROXY", _httpProxyAddress); Environment.SetEnvironmentVariable("HTTP_PROXY", _httpProxyAddress);
@@ -101,7 +101,7 @@ namespace GitHub.Runner.Sdk
if (!string.IsNullOrEmpty(httpsProxyAddress) && Uri.TryCreate(httpsProxyAddress, UriKind.Absolute, out var proxyHttpsUri)) if (!string.IsNullOrEmpty(httpsProxyAddress) && Uri.TryCreate(httpsProxyAddress, UriKind.Absolute, out var proxyHttpsUri))
{ {
_httpsProxyAddress = proxyHttpsUri.AbsoluteUri; _httpsProxyAddress = proxyHttpsUri.OriginalString;
// Set both environment variables since there are tools support both casing (curl, wget) and tools support only one casing (docker) // Set both environment variables since there are tools support both casing (curl, wget) and tools support only one casing (docker)
Environment.SetEnvironmentVariable("HTTPS_PROXY", _httpsProxyAddress); Environment.SetEnvironmentVariable("HTTPS_PROXY", _httpsProxyAddress);

View File

@@ -1,4 +1,4 @@
using GitHub.Runner.Common.Util; using GitHub.Runner.Common.Util;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -127,11 +127,11 @@ namespace GitHub.Runner.Common.Tests
Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,"); Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,");
var proxy = new RunnerWebProxy(); var proxy = new RunnerWebProxy();
Assert.Equal("http://127.0.0.1:8888/", proxy.HttpProxyAddress); Assert.Equal("http://127.0.0.1:8888", proxy.HttpProxyAddress);
Assert.Null(proxy.HttpProxyUsername); Assert.Null(proxy.HttpProxyUsername);
Assert.Null(proxy.HttpProxyPassword); Assert.Null(proxy.HttpProxyPassword);
Assert.Equal("http://user:pass@127.0.0.1:9999/", proxy.HttpsProxyAddress); Assert.Equal("http://user:pass@127.0.0.1:9999", proxy.HttpsProxyAddress);
Assert.Equal("user", proxy.HttpsProxyUsername); Assert.Equal("user", proxy.HttpsProxyUsername);
Assert.Equal("pass", proxy.HttpsProxyPassword); Assert.Equal("pass", proxy.HttpsProxyPassword);
@@ -161,11 +161,11 @@ namespace GitHub.Runner.Common.Tests
Environment.SetEnvironmentVariable("NO_PROXY", "github.com, google.com,"); Environment.SetEnvironmentVariable("NO_PROXY", "github.com, google.com,");
var proxy = new RunnerWebProxy(); var proxy = new RunnerWebProxy();
Assert.Equal("http://127.0.0.1:7777/", proxy.HttpProxyAddress); Assert.Equal("http://127.0.0.1:7777", proxy.HttpProxyAddress);
Assert.Null(proxy.HttpProxyUsername); Assert.Null(proxy.HttpProxyUsername);
Assert.Null(proxy.HttpProxyPassword); Assert.Null(proxy.HttpProxyPassword);
Assert.Equal("http://user:pass@127.0.0.1:8888/", proxy.HttpsProxyAddress); Assert.Equal("http://user:pass@127.0.0.1:8888", proxy.HttpsProxyAddress);
Assert.Equal("user", proxy.HttpsProxyUsername); Assert.Equal("user", proxy.HttpsProxyUsername);
Assert.Equal("pass", proxy.HttpsProxyPassword); Assert.Equal("pass", proxy.HttpsProxyPassword);
@@ -218,19 +218,19 @@ namespace GitHub.Runner.Common.Tests
Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,"); Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,");
var proxy = new RunnerWebProxy(); var proxy = new RunnerWebProxy();
Assert.Equal("http://user1@127.0.0.1:8888/", proxy.HttpProxyAddress); Assert.Equal("http://user1@127.0.0.1:8888", proxy.HttpProxyAddress);
Assert.Equal("user1", proxy.HttpProxyUsername); Assert.Equal("user1", proxy.HttpProxyUsername);
Assert.Null(proxy.HttpProxyPassword); Assert.Null(proxy.HttpProxyPassword);
var cred = proxy.Credentials.GetCredential(new Uri("http://user1@127.0.0.1:8888/"), "Basic"); var cred = proxy.Credentials.GetCredential(new Uri("http://user1@127.0.0.1:8888"), "Basic");
Assert.Equal("user1", cred.UserName); Assert.Equal("user1", cred.UserName);
Assert.Equal(string.Empty, cred.Password); Assert.Equal(string.Empty, cred.Password);
Assert.Equal("http://user2:pass@127.0.0.1:9999/", proxy.HttpsProxyAddress); Assert.Equal("http://user2:pass@127.0.0.1:9999", proxy.HttpsProxyAddress);
Assert.Equal("user2", proxy.HttpsProxyUsername); Assert.Equal("user2", proxy.HttpsProxyUsername);
Assert.Equal("pass", proxy.HttpsProxyPassword); Assert.Equal("pass", proxy.HttpsProxyPassword);
cred = proxy.Credentials.GetCredential(new Uri("http://user2:pass@127.0.0.1:9999/"), "Basic"); cred = proxy.Credentials.GetCredential(new Uri("http://user2:pass@127.0.0.1:9999"), "Basic");
Assert.Equal("user2", cred.UserName); Assert.Equal("user2", cred.UserName);
Assert.Equal("pass", cred.Password); Assert.Equal("pass", cred.Password);
@@ -256,19 +256,19 @@ namespace GitHub.Runner.Common.Tests
Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,"); Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,");
var proxy = new RunnerWebProxy(); var proxy = new RunnerWebProxy();
Assert.Equal("http://user1:pass1%40@127.0.0.1:8888/", proxy.HttpProxyAddress); Assert.Equal("http://user1:pass1%40@127.0.0.1:8888", proxy.HttpProxyAddress);
Assert.Equal("user1", proxy.HttpProxyUsername); Assert.Equal("user1", proxy.HttpProxyUsername);
Assert.Equal("pass1@", proxy.HttpProxyPassword); Assert.Equal("pass1@", proxy.HttpProxyPassword);
var cred = proxy.Credentials.GetCredential(new Uri("http://user1:pass1%40@127.0.0.1:8888/"), "Basic"); var cred = proxy.Credentials.GetCredential(new Uri("http://user1:pass1%40@127.0.0.1:8888"), "Basic");
Assert.Equal("user1", cred.UserName); Assert.Equal("user1", cred.UserName);
Assert.Equal("pass1@", cred.Password); Assert.Equal("pass1@", cred.Password);
Assert.Equal("http://user2:pass2%40@127.0.0.1:9999/", proxy.HttpsProxyAddress); Assert.Equal("http://user2:pass2%40@127.0.0.1:9999", proxy.HttpsProxyAddress);
Assert.Equal("user2", proxy.HttpsProxyUsername); Assert.Equal("user2", proxy.HttpsProxyUsername);
Assert.Equal("pass2@", proxy.HttpsProxyPassword); Assert.Equal("pass2@", proxy.HttpsProxyPassword);
cred = proxy.Credentials.GetCredential(new Uri("http://user2:pass2%40@127.0.0.1:9999/"), "Basic"); cred = proxy.Credentials.GetCredential(new Uri("http://user2:pass2%40@127.0.0.1:9999"), "Basic");
Assert.Equal("user2", cred.UserName); Assert.Equal("user2", cred.UserName);
Assert.Equal("pass2@", cred.Password); Assert.Equal("pass2@", cred.Password);
@@ -405,6 +405,36 @@ namespace GitHub.Runner.Common.Tests
} }
} }
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public void WebProxyFromEnvironmentVariablesWithPort80()
{
try
{
Environment.SetEnvironmentVariable("http_proxy", "http://127.0.0.1:80");
Environment.SetEnvironmentVariable("https_proxy", "http://user:pass@127.0.0.1:80");
Environment.SetEnvironmentVariable("no_proxy", "github.com, google.com,");
var proxy = new RunnerWebProxy();
Assert.Equal("http://127.0.0.1:80", Environment.GetEnvironmentVariable("http_proxy"));
Assert.Null(proxy.HttpProxyUsername);
Assert.Null(proxy.HttpProxyPassword);
Assert.Equal("http://user:pass@127.0.0.1:80", Environment.GetEnvironmentVariable("https_proxy"));
Assert.Equal("user", proxy.HttpsProxyUsername);
Assert.Equal("pass", proxy.HttpsProxyPassword);
Assert.Equal(2, proxy.NoProxyList.Count);
Assert.Equal("github.com", proxy.NoProxyList[0].Host);
Assert.Equal("google.com", proxy.NoProxyList[1].Host);
}
finally
{
CleanProxyEnv();
}
}
private void CleanProxyEnv() private void CleanProxyEnv()
{ {
Environment.SetEnvironmentVariable("http_proxy", null); Environment.SetEnvironmentVariable("http_proxy", null);