Skip to content
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

AWS SDK Compatibility: Check for http_proxy & https_proxy environment variables #2991

Merged
merged 5 commits into from Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -474,6 +474,14 @@ public virtual void ConfigureRequest(IRequestContext requestContext)
requestContext.Metrics.AddProperty(Metric.ProxyPort, requestContext.ClientConfig.ProxyPort);
_request.Proxy = proxy;
}
else if (_request.RequestUri.Scheme == Uri.UriSchemeHttp)
{
dscpinheiro marked this conversation as resolved.
Show resolved Hide resolved
_request.Proxy = requestContext.ClientConfig.GetHttpProxy();
}
else if (_request.RequestUri.Scheme == Uri.UriSchemeHttps)
dscpinheiro marked this conversation as resolved.
Show resolved Hide resolved
{
_request.Proxy = requestContext.ClientConfig.GetHttpsProxy();
}

// Set service point properties.
_request.ServicePoint.ConnectionLimit = clientConfig.ConnectionLimit;
Expand Down
Expand Up @@ -270,7 +270,11 @@ private static HttpClient CreateManagedHttpClient(IClientConfig clientConfig)
}

try
{
{
// HTTP Client will automatically read `HTTP_PROXY`,
// and `HTTPS_PROXY`. So we let it use those variables, since
// we don't know at client construction time whether they're
// using HTTP or HTTPS.
var proxy = clientConfig.GetWebProxy();
if (proxy != null)
{
Expand Down
47 changes: 47 additions & 0 deletions sdk/src/Core/Amazon.Runtime/_bcl/ClientConfig.cs
Expand Up @@ -38,6 +38,25 @@ private static RegionEndpoint GetDefaultRegionEndpoint()
return FallbackRegionFactory.GetRegionEndpoint();
}

private static WebProxy GetWebProxyWithCredentials(string value)
{
if (!string.IsNullOrEmpty(value))
{
var asUri = new Uri(value);
var parsedProxy = new WebProxy(asUri);
if (!string.IsNullOrEmpty(asUri.UserInfo)) {
var userAndPass = asUri.UserInfo.Split(':');
parsedProxy.Credentials = new NetworkCredential(
userAndPass[0],
userAndPass.Length > 1 ? userAndPass[1] : string.Empty
);
}
return parsedProxy;
}

return null;
}

/// <summary>
/// Gets and sets of the ProxyHost property.
/// </summary>
Expand Down Expand Up @@ -133,6 +152,34 @@ public WebProxy GetWebProxy()
return proxy;
}

/// <summary>
/// Returns a WebProxy instance to use for HTTPS connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
public WebProxy GetHttpsProxy()
{
var explicitProxy = GetWebProxy();
if (explicitProxy != null)
{
return explicitProxy;
}
return ClientConfig.GetWebProxyWithCredentials(Environment.GetEnvironmentVariable("https_proxy"));
}

/// <summary>
/// Returns a WebProxy instance to use for HTTP connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
public WebProxy GetHttpProxy()
{
var explicitProxy = GetWebProxy();
if (explicitProxy != null)
{
return explicitProxy;
}
return ClientConfig.GetWebProxyWithCredentials(Environment.GetEnvironmentVariable("http_proxy"));
}

/// <summary>
/// Unpacks the host, port and any credentials info into the instance's
/// proxy-related fields.
Expand Down
11 changes: 11 additions & 0 deletions sdk/src/Core/Amazon.Runtime/_bcl/IClientConfig.bcl.cs
Expand Up @@ -70,5 +70,16 @@ public partial interface IClientConfig
/// </summary>
WebProxy GetWebProxy();

/// <summary>
/// Returns a WebProxy instance to use for HTTPS connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
WebProxy GetHttpsProxy();

/// <summary>
/// Returns a WebProxy instance to use for HTTP connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
WebProxy GetHttpProxy();
Mythra marked this conversation as resolved.
Show resolved Hide resolved
}
}
45 changes: 45 additions & 0 deletions sdk/src/Core/Amazon.Runtime/_netstandard/ClientConfig.cs
Expand Up @@ -37,6 +37,25 @@ private static RegionEndpoint GetDefaultRegionEndpoint()
return FallbackRegionFactory.GetRegionEndpoint();
}

private static Amazon.Runtime.Internal.Util.WebProxy? GetWebProxyWithCredentials(string value)
{
if (!string.IsNullOrEmpty(value))
{
var asUri = new Uri(value);
var parsedProxy = new Amazon.Runtime.Internal.Util.WebProxy(asUri);
if (!string.IsNullOrEmpty(asUri.UserInfo)) {
var userAndPass = asUri.UserInfo.Split(':');
parsedProxy.Credentials = new NetworkCredential(
userAndPass[0],
userAndPass.Length > 1 ? userAndPass[1] : string.Empty
);
}
return parsedProxy;
}

return null;
}

/// <summary>
/// Returns a WebProxy instance configured to match the proxy settings
/// in the client configuration.
Expand All @@ -46,6 +65,32 @@ public IWebProxy GetWebProxy()
return proxy;
}

/// <summary>
/// Returns a WebProxy instance to use for HTTPS connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
Mythra marked this conversation as resolved.
Show resolved Hide resolved
public IWebProxy GetHttpsProxy()
{
if (proxy != null)
{
return proxy;
}
return GetWebProxyWithCredentials(Environment.GetEnvironmentVariable("https_proxy"));
}

/// <summary>
/// Returns a WebProxy instance to use for HTTP connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
public IWebProxy GetHttpProxy()
{
if (proxy != null)
{
return proxy;
}
return GetWebProxyWithCredentials(Environment.GetEnvironmentVariable("http_proxy"));
}

/// <summary>
/// Unpacks the host, port and any credentials info into the instance's
/// proxy-related fields.
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/Core/Amazon.Runtime/_netstandard/IClientConfig.cs
Expand Up @@ -43,6 +43,18 @@ public partial interface IClientConfig
/// </summary>
IWebProxy GetWebProxy();

/// <summary>
/// Returns a WebProxy instance to use for HTTPS connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
IWebProxy GetHttpsProxy();

/// <summary>
/// Returns a WebProxy instance to use for HTTP connections if an
/// explicit web proxy hasn't been configured.
/// </summary>
IWebProxy GetHttpProxy();

/// <summary>
/// HttpClientFactory used to create new HttpClients.
/// If null, an HttpClient will be created by the SDK.
Expand Down
24 changes: 22 additions & 2 deletions sdk/src/Services/EC2/Custom/Util/_async/ImageUtilities.async.cs
Expand Up @@ -82,10 +82,17 @@ private static async Task LoadDefinitionsFromWebAsync(AmazonEC2Config ec2Config)
if (ImageDefinitionsLoaded)
return;
}
const string httpPrefix = "http://";
const string httpsPrefix = "https://";

IWebProxy webProxy = null;
if (ec2Config != null)
IWebProxy httpProxy = null;
IWebProxy httpsProxy = null;
if (ec2Config != null) {
webProxy = ec2Config.GetWebProxy();
httpProxy = ec2Config.GetHttpProxy();
httpsProxy = ec2Config.GetHttpsProxy();
}

int retries = 0;
while (retries < MAX_DOWNLOAD_RETRIES)
Expand All @@ -95,9 +102,22 @@ private static async Task LoadDefinitionsFromWebAsync(AmazonEC2Config ec2Config)
HttpWebResponse response = null;
foreach (var location in DownloadLocations)
{
var useProxy = webProxy;
if (useProxy == null)
{
if (location.StartsWith(httpPrefix))
{
useProxy = httpProxy;
}
else if (location.StartsWith(httpsPrefix))
{
useProxy = httpsProxy;
}
}

try
{
response = await DownloadControlFileAsync(location, webProxy).ConfigureAwait(false);
response = await DownloadControlFileAsync(location, useProxy).ConfigureAwait(false);
if (response != null)
break;
}
Expand Down
24 changes: 22 additions & 2 deletions sdk/src/Services/EC2/Custom/Util/_bcl/ImageUtilities.bcl.cs
Expand Up @@ -82,10 +82,17 @@ private static void LoadDefinitionsFromWeb(AmazonEC2Config ec2Config)
if (ImageDefinitionsLoaded)
return;
}
const string httpPrefix = "http://";
const string httpsPrefix = "https://";

IWebProxy webProxy = null;
if (ec2Config != null)
IWebProxy httpProxy = null;
IWebProxy httpsProxy = null;
if (ec2Config != null) {
webProxy = ec2Config.GetWebProxy();
httpProxy = ec2Config.GetHttpProxy();
httpsProxy = ec2Config.GetHttpsProxy();
}

int retries = 0;
while (retries < MAX_DOWNLOAD_RETRIES)
Expand All @@ -95,9 +102,22 @@ private static void LoadDefinitionsFromWeb(AmazonEC2Config ec2Config)
HttpWebResponse response = null;
foreach (var location in DownloadLocations)
{
var useProxy = webProxy;
if (useProxy == null)
{
if (location.StartsWith(httpPrefix))
{
useProxy = httpProxy;
}
else if (location.StartsWith(httpsPrefix))
{
useProxy = httpsProxy;
}
}

try
{
response = DownloadControlFile(location, webProxy);
response = DownloadControlFile(location, useProxy);
if (response != null)
break;
}
Expand Down
11 changes: 11 additions & 0 deletions sdk/src/Services/S3/Custom/AmazonS3Client.Extensions.cs
Expand Up @@ -361,6 +361,17 @@ internal void ConfigureProxy(HttpWebRequest httpRequest)
{
httpRequest.Proxy.Credentials = Config.ProxyCredentials;
}
if (httpRequest.Proxy == null)
{
if (httpRequest.RequestUri.Scheme == Uri.UriSchemeHttp)
{
httpRequest.Proxy = Config.GetHttpProxy();
}
else if (httpRequest.RequestUri.Scheme == Uri.UriSchemeHttps)
{
httpRequest.Proxy = Config.GetHttpsProxy();
}
}
}


Expand Down
14 changes: 9 additions & 5 deletions sdk/src/Services/S3/Custom/Util/AmazonS3HttpUtil.cs
Expand Up @@ -112,15 +112,19 @@ private static GetHeadResponse HandleWebException(string header, WebException we

private static void SetProxyIfAvailableAndConfigured(IClientConfig config, HttpWebRequest httpWebRequest)
{
var proxy = GetProxyIfAvailableAndConfigured(config);
var proxy = config.GetWebProxy();
if (proxy != null)
{
httpWebRequest.Proxy = proxy;
}
}
private static IWebProxy GetProxyIfAvailableAndConfigured(IClientConfig config)
{
return config.GetWebProxy();
else if (httpWebRequest.RequestUri.Scheme == Uri.UriSchemeHttp)
{
httpWebRequest.Proxy = config.GetHttpProxy();
}
else if (httpWebRequest.RequestUri.Scheme == Uri.UriSchemeHttps)
{
httpWebRequest.Proxy = config.GetHttpsProxy();
}
}
}
}
Expand Up @@ -40,11 +40,25 @@ public partial class AmazonSecurityTokenServiceClient : AmazonServiceClient, IAm
TimeSpan credentialDuration,
ICredentials userCredential)
{
const string httpPrefix = "http://";
const string httpsPrefix = "https://";
SAMLAssertion assertion;

try
{
var authController = new SAMLAuthenticationController(Config.GetWebProxy());
var proxy = Config.GetWebProxy();
if (proxy == null)
{
if (endpoint.StartsWith(httpPrefix))
{
proxy = Config.GetHttpProxy();
}
else if (endpoint.StartsWith(httpsPrefix))
{
proxy = Config.GetHttpsProxy();
}
}
var authController = new SAMLAuthenticationController(proxy);
assertion = authController.GetSAMLAssertion(endpoint, userCredential, authenticationType);
}
catch (Exception e)
Expand Down