Skip to content

Commit f64ecc1

Browse files
committed
Non Proxy configs should be independently picked up irrespective of Proxy Host settings
1 parent 626e49d commit f64ecc1

File tree

11 files changed

+234
-19
lines changed

11 files changed

+234
-19
lines changed

core/crt-core/src/main/java/software/amazon/awssdk/crtcore/CrtProxyConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Objects;
2121
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.utils.NonProxyHostConfigProvider;
2223
import software.amazon.awssdk.utils.ProxyConfigProvider;
2324
import software.amazon.awssdk.utils.ProxySystemSetting;
2425
import software.amazon.awssdk.utils.StringUtils;
@@ -45,7 +46,9 @@ protected CrtProxyConfiguration(DefaultBuilder<?> builder) {
4546
ProxyConfigProvider proxyConfigProvider = fromSystemEnvironmentSettings(builder.useSystemPropertyValues,
4647
builder.useEnvironmentVariableValues ,
4748
builder.scheme);
49+
4850
this.host = resolveHost(builder, proxyConfigProvider);
51+
4952
this.port = resolvePort(builder, proxyConfigProvider);
5053
this.username = resolveUsername(builder, proxyConfigProvider);
5154
this.password = resolvePassword(builder, proxyConfigProvider);

http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/ProxyConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashSet;
2424
import java.util.Set;
2525
import software.amazon.awssdk.annotations.SdkPublicApi;
26+
import software.amazon.awssdk.utils.NonProxyHostConfigProvider;
2627
import software.amazon.awssdk.utils.ProxyConfigProvider;
2728
import software.amazon.awssdk.utils.ProxySystemSetting;
2829
import software.amazon.awssdk.utils.ToString;
@@ -58,11 +59,16 @@ private ProxyConfiguration(DefaultClientProxyConfigurationBuilder builder) {
5859
ProxyConfigProvider proxyConfiguration = fromSystemEnvironmentSettings(builder.useSystemPropertyValues,
5960
builder.useEnvironmentVariableValues,
6061
resolvedScheme);
62+
63+
NonProxyHostConfigProvider nonProxyHostConfiguration = NonProxyHostConfigProvider.
64+
fromSystemEnvironmentSettings(builder.useSystemPropertyValues,
65+
builder.useEnvironmentVariableValues);
66+
6167
this.username = resolveUsername(builder, proxyConfiguration);
6268
this.password = resolvePassword(builder, proxyConfiguration);
6369
this.ntlmDomain = builder.ntlmDomain;
6470
this.ntlmWorkstation = builder.ntlmWorkstation;
65-
this.nonProxyHosts = resolveNonProxyHosts(builder, proxyConfiguration);
71+
this.nonProxyHosts = resolveNonProxyHosts(builder, nonProxyHostConfiguration);
6672
this.preemptiveBasicAuthenticationEnabled = builder.preemptiveBasicAuthenticationEnabled == null ? Boolean.FALSE :
6773
builder.preemptiveBasicAuthenticationEnabled;
6874
this.useSystemPropertyValues = builder.useSystemPropertyValues;
@@ -91,7 +97,7 @@ private static String resolveUsername(DefaultClientProxyConfigurationBuilder bui
9197

9298

9399
private static Set<String> resolveNonProxyHosts(DefaultClientProxyConfigurationBuilder builder,
94-
ProxyConfigProvider proxyConfiguration) {
100+
NonProxyHostConfigProvider proxyConfiguration) {
95101
if (builder.nonProxyHosts != null || proxyConfiguration == null) {
96102
return builder.nonProxyHosts;
97103
}

http-clients/apache-client/src/test/java/software/amazon/awssdk/http/apache/ApacheHttpProxyTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ protected void assertProxyConfiguration(TestProxySetting userSetProxySettings,
6363
builder.useEnvironmentVariableValues(useEnvironmentVariable);
6464
}
6565
ProxyConfiguration proxyConfiguration = builder.build();
66-
assertThat(proxyConfiguration.host()).isEqualTo(expectedProxySettings.getHost());
67-
assertThat(proxyConfiguration.port()).isEqualTo(expectedProxySettings.getPort());
68-
assertThat(proxyConfiguration.username()).isEqualTo(expectedProxySettings.getUserName());
69-
assertThat(proxyConfiguration.password()).isEqualTo(expectedProxySettings.getPassword());
70-
assertThat(proxyConfiguration.nonProxyHosts()).isEqualTo(expectedProxySettings.getNonProxyHosts());
66+
assertThat(expectedProxySettings.getHost()).isEqualTo(proxyConfiguration.host());
67+
assertThat(expectedProxySettings.getPort()).isEqualTo(proxyConfiguration.port());
68+
assertThat(expectedProxySettings.getUserName()).isEqualTo(proxyConfiguration.username());
69+
assertThat(expectedProxySettings.getPassword()).isEqualTo(proxyConfiguration.password());
70+
assertThat(expectedProxySettings.getNonProxyHosts()).isEqualTo(proxyConfiguration.nonProxyHosts());
7171
}
7272
}

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/ProxyConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.HashSet;
2020
import java.util.Set;
2121
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.utils.NonProxyHostConfigProvider;
2223
import software.amazon.awssdk.utils.ProxyConfigProvider;
2324
import software.amazon.awssdk.utils.ProxyEnvironmentSetting;
2425
import software.amazon.awssdk.utils.ProxySystemSetting;
@@ -52,14 +53,18 @@ private ProxyConfiguration(BuilderImpl builder) {
5253
ProxyConfigProvider.fromSystemEnvironmentSettings(builder.useSystemPropertyValues,
5354
builder.useEnvironmentVariablesValues,
5455
builder.scheme);
56+
57+
NonProxyHostConfigProvider nonProxyHostConfigProvider =
58+
NonProxyHostConfigProvider.fromSystemEnvironmentSettings(builder.useSystemPropertyValues,
59+
builder.useEnvironmentVariablesValues);
5560
this.host = resolveHost(builder, proxyConfigProvider);
5661
this.port = resolvePort(builder, proxyConfigProvider);
5762
this.username = resolveUserName(builder, proxyConfigProvider);
5863
this.password = resolvePassword(builder, proxyConfigProvider);
59-
this.nonProxyHosts = resolveNonProxyHosts(builder, proxyConfigProvider);
64+
this.nonProxyHosts = resolveNonProxyHosts(builder, nonProxyHostConfigProvider);
6065
}
6166

62-
private static Set<String> resolveNonProxyHosts(BuilderImpl builder, ProxyConfigProvider proxyConfigProvider) {
67+
private static Set<String> resolveNonProxyHosts(BuilderImpl builder, NonProxyHostConfigProvider proxyConfigProvider) {
6368
if (builder.nonProxyHosts != null || proxyConfigProvider == null) {
6469
return builder.nonProxyHosts;
6570
} else {

http-clients/url-connection-client/src/main/java/software/amazon/awssdk/http/urlconnection/ProxyConfiguration.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashSet;
2323
import java.util.Set;
2424
import software.amazon.awssdk.annotations.SdkPublicApi;
25+
import software.amazon.awssdk.utils.NonProxyHostConfigProvider;
2526
import software.amazon.awssdk.utils.ProxyConfigProvider;
2627
import software.amazon.awssdk.utils.ProxyEnvironmentSetting;
2728
import software.amazon.awssdk.utils.ProxySystemSetting;
@@ -62,9 +63,15 @@ private ProxyConfiguration(DefaultClientProxyConfigurationBuilder builder) {
6263
builder.useEnvironmentVariablesValues,
6364
resolvedScheme);
6465

66+
NonProxyHostConfigProvider nonProxyHostConfigProvider =
67+
NonProxyHostConfigProvider.fromSystemEnvironmentSettings(
68+
builder.useSystemPropertyValues,
69+
builder.useEnvironmentVariablesValues);
70+
71+
6572
this.username = resolveUsername(builder, proxyConfigProvider);
6673
this.password = resolvePassword(builder, proxyConfigProvider);
67-
this.nonProxyHosts = resolveNonProxyHosts(builder, proxyConfigProvider);
74+
this.nonProxyHosts = resolveNonProxyHosts(builder, nonProxyHostConfigProvider);
6875
this.useSystemPropertyValues = builder.useSystemPropertyValues;
6976

7077
if (builder.endpoint != null) {
@@ -94,7 +101,7 @@ private static String resolvePassword(DefaultClientProxyConfigurationBuilder bui
94101
}
95102

96103
private static Set<String> resolveNonProxyHosts(DefaultClientProxyConfigurationBuilder builder,
97-
ProxyConfigProvider proxyConfigProvider) {
104+
NonProxyHostConfigProvider proxyConfigProvider) {
98105
return builder.nonProxyHosts != null || proxyConfigProvider == null ? builder.nonProxyHosts :
99106
proxyConfigProvider.nonProxyHosts();
100107
}

test/http-client-tests/src/main/java/software/amazon/awssdk/http/proxy/ProxyConfigCommonTestData.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ null, null, getSystemPropertyProxySettings().host(USER_HOST_ON_BUILDER).port(USE
199199
environmentSettingsWithNoPassword(),
200200
new TestProxySetting(), null, null,
201201
new TestProxySetting().host(SYSTEM_PROPERTY_HOST).port(Integer.parseInt(SYSTEM_PROPERTY_PORT_NUMBER))
202-
.password(SYSTEM_PROPERTY_PASSWORD)),
202+
.password(SYSTEM_PROPERTY_PASSWORD).nonProxyHost(ENVIRONMENT_VARIABLE_NON_PROXY)),
203203

204204
Arguments.of(
205205
"Given password in System property when Password present in system property "
@@ -209,7 +209,7 @@ null, null, getSystemPropertyProxySettings().host(USER_HOST_ON_BUILDER).port(USE
209209
new TestProxySetting().password("passwordFromBuilder"), null, null,
210210
getSystemPropertyProxySettings().password("passwordFromBuilder")
211211
.userName(null)
212-
.nonProxyHost(null)),
212+
.nonProxyHost(ENVIRONMENT_VARIABLE_NON_PROXY)),
213213

214214
Arguments.of(
215215
"Given partial System Property and partial Environment variables when Builder method with Host "
@@ -222,7 +222,7 @@ null, null, getSystemPropertyProxySettings().host(USER_HOST_ON_BUILDER).port(USE
222222
getSystemPropertyProxySettings().host(USER_HOST_ON_BUILDER)
223223
.port(USER_PORT_NUMBER_ON_BUILDER)
224224
.userName(null)
225-
.nonProxyHost(null)),
225+
.nonProxyHost(ENVIRONMENT_VARIABLE_NON_PROXY)),
226226

227227
Arguments.of(
228228
"Given System Property and Environment variables when valid empty Proxy config on Builder then "
@@ -236,7 +236,7 @@ null, null, getSystemPropertyProxySettings().host(USER_HOST_ON_BUILDER).port(USE
236236
+ "set useEnvironmentVariable to true and default System property then default system property gets used.",
237237
getSystemPropertiesWithNoUserName(),
238238
environmentSettingsWithNoPassword(),
239-
new TestProxySetting(), null, true, getSystemPropertyProxySettings().nonProxyHost(null)
239+
new TestProxySetting(), null, true, getSystemPropertyProxySettings().nonProxyHost(ENVIRONMENT_VARIABLE_NON_PROXY)
240240
.userName(null)),
241241

242242
Arguments.of(
@@ -245,7 +245,21 @@ null, null, getSystemPropertyProxySettings().host(USER_HOST_ON_BUILDER).port(USE
245245
+ "resolved",
246246
getSystemPropertiesWithNoUserName(),
247247
environmentSettingsWithNoPassword(),
248-
new TestProxySetting(), false, true, getEnvironmentVariableProxySettings().password(null))
248+
new TestProxySetting(), false, true, getEnvironmentVariableProxySettings().password(null)),
249+
250+
Arguments.of(
251+
"Given",
252+
systemPropertySettingsWithNoNonProxyHosts(),
253+
environmentSettings(),
254+
new TestProxySetting(), true, true,
255+
getSystemPropertyProxySettings().nonProxyHost(ENVIRONMENT_VARIABLE_NON_PROXY)),
256+
257+
Arguments.of(
258+
"Given",
259+
systemPropertySettingsWithNoNonProxyHosts(),
260+
environmentSettingsWithNoNonProxy(),
261+
new TestProxySetting(), true, true,
262+
getSystemPropertyProxySettings().nonProxyHost(null))
249263
);
250264
}
251265

@@ -264,6 +278,13 @@ private static TestProxySetting getTestProxySettings() {
264278
.nonProxyHost(USER_NONPROXY_ON_BUILDER);
265279
}
266280

281+
private static TestProxySetting getTestProxySettingsWithNoProxy() {
282+
return new TestProxySetting().host(USER_HOST_ON_BUILDER)
283+
.port(USER_PORT_NUMBER_ON_BUILDER)
284+
.userName(USER_USERNAME_ON_BUILDER)
285+
.password(USER_PASSWORD_ON_BUILDER);
286+
}
287+
267288

268289
private static TestProxySetting getSystemPropertyProxySettings() {
269290
return new TestProxySetting().host(SYSTEM_PROPERTY_HOST)
@@ -292,6 +313,15 @@ private static List<Pair<String, String>> environmentSettings() {
292313
);
293314
}
294315

316+
317+
private static List<Pair<String, String>> environmentSettingsWithNoNonProxy() {
318+
return Arrays.asList(
319+
Pair.of("%s_proxy",
320+
"http://" + ENV_VARIABLE_USER + ":" + ENV_VARIABLE_PASSWORD + "@" + ENVIRONMENT_HOST
321+
+ ":" + ENVIRONMENT_VARIABLE_PORT_NUMBER + "/")
322+
);
323+
}
324+
295325
private static List<Pair<String, String>> environmentSettingsWithNoPassword() {
296326
return Arrays.asList(
297327
Pair.of("%s_proxy",
@@ -308,4 +338,13 @@ private static List<Pair<String, String>> systemPropertySettings() {
308338
Pair.of("%s.proxyUser", SYSTEM_PROPERTY_USER),
309339
Pair.of("%s.proxyPassword", SYSTEM_PROPERTY_PASSWORD));
310340
}
341+
342+
343+
private static List<Pair<String, String>> systemPropertySettingsWithNoNonProxyHosts() {
344+
return Arrays.asList(
345+
Pair.of("%s.proxyHost", SYSTEM_PROPERTY_HOST),
346+
Pair.of("%s.proxyPort", SYSTEM_PROPERTY_PORT_NUMBER),
347+
Pair.of("%s.proxyUser", SYSTEM_PROPERTY_USER),
348+
Pair.of("%s.proxyPassword", SYSTEM_PROPERTY_PASSWORD));
349+
}
311350
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.utils;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
import software.amazon.awssdk.annotations.SdkProtectedApi;
21+
import software.amazon.awssdk.utils.internal.proxy.NonProxyHostEnvironmentVariableConfigProvider;
22+
import software.amazon.awssdk.utils.internal.proxy.NonProxyHostSystemPropertyConfigProvider;
23+
24+
25+
/**
26+
* Interface for providing proxy configuration settings. Implementations of this interface can retrieve proxy configuration from
27+
* various sources such as system properties and environment variables.
28+
**/
29+
30+
@SdkProtectedApi
31+
public interface NonProxyHostConfigProvider {
32+
33+
34+
/**
35+
* Returns a {@link NonProxyHostConfigProvider} based on the specified settings for using system properties, environment
36+
* variables, and the scheme.
37+
*
38+
* @param useSystemPropertyValues A {@code Boolean} indicating whether to use system property values.
39+
* @param useEnvironmentVariableValues A {@code Boolean} indicating whether to use environment variable values.
40+
* @return A {@link NonProxyHostConfigProvider} based on the specified settings.
41+
*/
42+
static NonProxyHostConfigProvider fromSystemEnvironmentSettings(Boolean useSystemPropertyValues,
43+
Boolean useEnvironmentVariableValues) {
44+
NonProxyHostConfigProvider resultProxyConfig = null;
45+
if (Boolean.TRUE.equals(useSystemPropertyValues)) {
46+
resultProxyConfig = fromSystemPropertySettings();
47+
} else if (Boolean.TRUE.equals(useEnvironmentVariableValues)) {
48+
return fromEnvironmentSettings();
49+
}
50+
if (resultProxyConfig != null && resultProxyConfig.nonProxyHosts() == null
51+
&& Boolean.TRUE.equals(useEnvironmentVariableValues)) {
52+
return fromEnvironmentSettings();
53+
}
54+
return resultProxyConfig;
55+
}
56+
57+
static NonProxyHostConfigProvider fromEnvironmentSettings() {
58+
return new NonProxyHostEnvironmentVariableConfigProvider();
59+
}
60+
61+
static NonProxyHostConfigProvider fromSystemPropertySettings() {
62+
return new NonProxyHostSystemPropertyConfigProvider();
63+
}
64+
65+
/**
66+
* Gets the set of non-proxy hosts.
67+
*
68+
* @return A set containing the non-proxy host names.
69+
*/
70+
Set<String> nonProxyHosts();
71+
}

utils/src/main/java/software/amazon/awssdk/utils/ProxyConfigProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ static ProxyConfigProvider fromSystemEnvironmentSettings(Boolean useSystemProper
7878
boolean isProxyConfigurationNotSet = resultProxyConfig != null && resultProxyConfig.host() == null
7979
&& resultProxyConfig.port() == 0
8080
&& !resultProxyConfig.password().isPresent()
81-
&& !resultProxyConfig.userName().isPresent()
82-
&& CollectionUtils.isNullOrEmpty(resultProxyConfig.nonProxyHosts());
81+
&& !resultProxyConfig.userName().isPresent();
8382

8483
if (isProxyConfigurationNotSet && Boolean.TRUE.equals(useEnvironmentVariableValues)) {
8584
return fromEnvironmentSettings(scheme);

utils/src/main/java/software/amazon/awssdk/utils/http/SdkHttpUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ private static Set<String> extractNonProxyHosts(String nonProxyHosts) {
441441
.map(s -> StringUtils.replace(s, "*", ".*?"))
442442
.collect(Collectors.toSet());
443443
}
444-
return Collections.emptySet();
444+
return null;
445445
}
446446

447447
public static Set<String> parseNonProxyHostsEnvironmentVariable() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.utils.internal.proxy;
17+
18+
import static software.amazon.awssdk.utils.http.SdkHttpUtils.parseNonProxyHostsEnvironmentVariable;
19+
20+
import java.util.Set;
21+
import software.amazon.awssdk.annotations.SdkInternalApi;
22+
import software.amazon.awssdk.utils.NonProxyHostConfigProvider;
23+
24+
/**
25+
* An implementation of the {@link NonProxyHostConfigProvider} interface that retrieves non-proxy host configuration settings from
26+
* environment variables.
27+
*
28+
* @see NonProxyHostConfigProvider
29+
*/
30+
@SdkInternalApi
31+
public class NonProxyHostEnvironmentVariableConfigProvider implements NonProxyHostConfigProvider {
32+
33+
/**
34+
* Retrieves the set of non-proxy hosts from environment variables.
35+
*
36+
* @return The set of non-proxy hosts.
37+
*/
38+
@Override
39+
public Set<String> nonProxyHosts() {
40+
return parseNonProxyHostsEnvironmentVariable();
41+
}
42+
}

0 commit comments

Comments
 (0)