-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for no_proxy environment variable for .NET Framework target.
The .NET Core targets rely on the .NET runtime to process the no_proxy environment variable.
- Loading branch information
Showing
10 changed files
with
229 additions
and
18 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
generator/.DevConfigs/C8926FB7-FC77-4478-ACC9-249E75851953.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"core": { | ||
"changeLogMessages": [ | ||
"Add support for no_proxy environment variable for .NET Framework targets." | ||
], | ||
"type": "patch", | ||
"updateMinimum": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
using Amazon.Runtime.Internal.Util; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Amazon.Util.Internal | ||
{ | ||
/// <summary> | ||
/// This type is used to check if an out going request should not use the https_proxy or http_proxy environment variable if the | ||
/// host matches a pattern in the no_proxy environment variable. | ||
/// | ||
/// The SDK does not do a full implementation of the no_proxy evaluation. It handles host name match with wildcard support. A full | ||
/// implementation would also support DNS IP resolution and CIDR pattern matching. | ||
/// </summary> | ||
public class NoProxyFilter | ||
{ | ||
internal const string NO_PROXY_ENV_NAME = "no_proxy"; | ||
|
||
public static readonly NoProxyFilter Instance = new NoProxyFilter(new EnvironmentVariableRetriever()); | ||
|
||
private IList<Regex> _proxyFilterRegex = new List<Regex>(); | ||
|
||
public NoProxyFilter(IEnvironmentVariableRetriever environmentVariableRetriever) | ||
{ | ||
var filters = environmentVariableRetriever.GetEnvironmentVariable(NO_PROXY_ENV_NAME)?.Split(','); | ||
if (filters == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var filter in filters) | ||
{ | ||
if (string.IsNullOrEmpty(filter)) | ||
{ | ||
continue; | ||
} | ||
|
||
try | ||
{ | ||
var regExPattern = string.Concat("^", Regex.Escape(filter.Trim()).Replace("\\*", ".*"), "$"); | ||
var regEx = new Regex(regExPattern, RegexOptions.Compiled); | ||
_proxyFilterRegex.Add(regEx); | ||
} | ||
catch(ArgumentException e) | ||
{ | ||
// We don't have any control if users have put invalid information in the no_proxy and the SDK's implementation is not a full | ||
// implementation of no_proxy, we will skip any segements in the no_proxy env var that we fail to parse. Since no_proxy has existed | ||
// before the SDK added this support we could introduce a breaking change if we start throwing fatal exceptions at this point. | ||
Logger.GetLogger(typeof(NoProxyFilter)).Error(e, "Failed parse segment \"{0}\" in no_proxy environment variable.", filter); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Evaluates if the host in the Uri should be exluded from proxy if it matches a pattern in the no_proxy environment variable. | ||
/// </summary> | ||
/// <param name="uri"></param> | ||
/// <returns></returns> | ||
public bool Match(Uri uri) | ||
{ | ||
var host = uri.Host; | ||
foreach(var filter in _proxyFilterRegex) | ||
{ | ||
if(filter.IsMatch(host)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
sdk/src/Core/Amazon.Util/Internal/_netstandard/NoProxyFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
using System; | ||
|
||
|
||
namespace Amazon.Util.Internal | ||
{ | ||
/// <summary> | ||
/// This is a stub implementation because in .NET Core we rely on the .NET runtime to evaluate the no_proxy environment variable. | ||
/// </summary> | ||
public class NoProxyFilter | ||
{ | ||
public static readonly NoProxyFilter Instance = new NoProxyFilter(); | ||
|
||
public NoProxyFilter() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// This is a stub method because in .NET Core we rely on the .NET runtime to evaluate the no_proxy environment variable. | ||
/// </summary> | ||
/// <param name="uri"></param> | ||
/// <returns></returns> | ||
public bool Match(Uri uri) | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
using Amazon.Runtime.Internal; | ||
using Amazon.Util.Internal; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
|
||
namespace AWSSDK.UnitTests | ||
{ | ||
[TestClass] | ||
public class NoProxyFilterTest | ||
{ | ||
[TestMethod] | ||
[DataRow("dynamodb.*.amazonaws.com", "dynamodb.us-west-2.amazonaws.com", true)] | ||
[DataRow("dynamodb.*.amazonaws.com", "ec2.us-west-2.amazonaws.com", false)] | ||
[DataRow("169.254.169.254", "169.254.169.254", true)] | ||
[DataRow("169.254.169.*", "169.254.169.254", true)] | ||
[DataRow("www.foo.com", "www.foo.com", true)] | ||
[DataRow(" www.foo.com ", "www.foo.com", true)] | ||
[DataRow("*.foo.com", "www.foo.com", true)] | ||
[DataRow("*foo.com", "foo.com", true)] | ||
[DataRow("www.foo.com", "www.bar.com", false)] | ||
[DataRow("*foo.com", "www.bar.com", false)] | ||
[DataRow("*foo.com", "bar.com", false)] | ||
[DataRow("", "bar.com", false)] | ||
[DataRow(null, "bar.com", false)] | ||
[DataRow("127.0.0.1,localhost,ssm.us-west-2.amazonaws.com", "ssm.us-west-2.amazonaws.com", true)] | ||
[DataRow("127.0.0.1,localhost,ssm.us-west-2.amazonaws.com", "autoscaling.us-west-2.amazonaws.com", false)] | ||
public void Match(string noProxyValue, string testUri, bool skip) | ||
{ | ||
var filter = new NoProxyFilter(new EnvironmentVariableStub(noProxyValue)); | ||
Assert.AreEqual(skip, filter.Match(new Uri("https://" + testUri + "/"))); | ||
} | ||
|
||
class EnvironmentVariableStub : IEnvironmentVariableRetriever | ||
{ | ||
private readonly string _noProxyValue; | ||
public EnvironmentVariableStub(string noProxyValue) | ||
{ | ||
_noProxyValue = noProxyValue; | ||
} | ||
|
||
public string GetEnvironmentVariable(string key) | ||
{ | ||
if (!string.Equals(key, NoProxyFilter.NO_PROXY_ENV_NAME, StringComparison.OrdinalIgnoreCase)) | ||
return null; | ||
|
||
return _noProxyValue; | ||
} | ||
} | ||
} | ||
} |