Skip to content

Commit

Permalink
Added configs for Proxy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
joviegas committed Sep 22, 2023
1 parent 52c322b commit d502ccd
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 29 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public ProxyEnvironmentConfiguration(String scheme) {


private Optional<URL> silentlyGetURL(){
String stringURL = Objects.equals(this.scheme, HTTPS) ? ProxyEnvironmentSetting.HTTPS_PROXY.environmentVariable()
: ProxyEnvironmentSetting.HTTP_PROXY.environmentVariable();
String stringURL = Objects.equals(this.scheme, HTTPS) ? ProxyEnvironmentSetting.HTTPS_PROXY.getStringValue().orElse(null)
: ProxyEnvironmentSetting.HTTP_PROXY.getStringValue().orElse(null);
if(StringUtils.isNotBlank(stringURL)){
try {
return Optional.of(new URL(stringURL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.utils.LocalProxyConfiguration;
import software.amazon.awssdk.utils.ProxySystemSetting;
import software.amazon.awssdk.utils.StringUtils;

/**
* The system properties related to http and https proxies
Expand All @@ -39,26 +40,26 @@ public ProxySystemConfiguration(String scheme) {
@Override
public int port() {
return Objects.equals(this.scheme, HTTPS) ?
ProxySystemSetting.HTTPS_PROXY_PORT.getStringValue().map(Integer::parseInt).orElse(0) :
ProxySystemSetting.PROXY_PORT.getStringValue().map(Integer::parseInt).orElse(0);
ProxySystemSetting.HTTPS_PROXY_PORT.getStringValue().filter(StringUtils::isNotBlank).map(Integer::parseInt).orElse(0) :
ProxySystemSetting.PROXY_PORT.getStringValue().filter(StringUtils::isNotBlank).map(Integer::parseInt).orElse(0);
}

@Override
public Optional<String> userName() {
return Objects.equals(this.scheme, HTTPS) ? ProxySystemSetting.HTTPS_PROXY_USERNAME.getStringValue():
ProxySystemSetting.PROXY_USERNAME.getStringValue();
return Objects.equals(this.scheme, HTTPS) ? ProxySystemSetting.HTTPS_PROXY_USERNAME.getStringValue().filter(StringUtils::isNotBlank):
ProxySystemSetting.PROXY_USERNAME.getStringValue().filter(StringUtils::isNotBlank);
}

@Override
public Optional<String> password() {
return Objects.equals(scheme, HTTPS) ? ProxySystemSetting.HTTPS_PROXY_PASSWORD.getStringValue() :
ProxySystemSetting.PROXY_PASSWORD.getStringValue();
return Objects.equals(scheme, HTTPS) ? ProxySystemSetting.HTTPS_PROXY_PASSWORD.getStringValue().filter(StringUtils::isNotBlank) :
ProxySystemSetting.PROXY_PASSWORD.getStringValue().filter(StringUtils::isNotBlank);
}

@Override
public String host() {
return Objects.equals(scheme, HTTPS) ? ProxySystemSetting.HTTPS_PROXY_HOST.getStringValue().orElse(null) :
ProxySystemSetting.PROXY_HOST.getStringValue().orElse(null);
return Objects.equals(scheme, HTTPS) ? ProxySystemSetting.HTTPS_PROXY_HOST.getStringValue().filter(StringUtils::isNotBlank).orElse(null) :
ProxySystemSetting.PROXY_HOST.getStringValue().filter(StringUtils::isNotBlank).orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* 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.
*/

package software.amazon.awssdk.utils.proxy;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;
import software.amazon.awssdk.utils.LocalProxyConfiguration;
import software.amazon.awssdk.utils.Pair;


public class ProxyConfigurationTest {

private static final EnvironmentVariableHelper ENVIRONMENT_VARIABLE_HELPER = new EnvironmentVariableHelper();

public static Stream<Arguments> stubResponses() {
return Stream.of(
Arguments.of(Arrays.asList(
Pair.of("%s.proxyHost", "foo.com"),
Pair.of("%s.proxyPort", "555"),
Pair.of("http.nonProxyHosts", "bar.com"),
Pair.of("%s.proxyUser", "UserOne"),
Pair.of("%s.proxyPassword", "passwordSecret")),
Arrays.asList(
Pair.of("%s_proxy", "http://UserOne:passwordSecret@foo.com:555/"),
Pair.of("no_proxy", "bar.com")
),
new ExpectedProxySetting().host("foo.com").port(555).userName("UserOne").password("passwordSecret").nonProxyHost("bar.com"),
"All Proxy Parameters are Set"),

Arguments.of(Arrays.asList(
Pair.of("%s.proxyHost", "foo.com"),
Pair.of("%s.proxyPort", "555")),
Collections.singletonList(
Pair.of("%s_proxy", "http://foo.com:555/")
),
new ExpectedProxySetting().host("foo.com").port(555),
"Optional Parameters are not set"),


Arguments.of(Arrays.asList(
Pair.of("%s.proxyHost", ""),
Pair.of("%s.proxyPort", ""),
Pair.of("%s.nonProxyHosts", ""),
Pair.of("%s.proxyUser", ""),
Pair.of("%s.nonProxyHosts", ""),
Pair.of("%s.proxyPassword", "")),
Arrays.asList(
Pair.of("%s_proxy", ""),
Pair.of("no_proxy", "")

),
new ExpectedProxySetting().port(0),
"All parameters are Blank"),


Arguments.of(Collections.singletonList(
Pair.of("%s.nonProxyHosts", "")),
Collections.singletonList(
Pair.of("no_proxy", "")
),
new ExpectedProxySetting().port(0),
"Only Non Proxy Hosts are set with multiple value"),


Arguments.of(Arrays.asList(
Pair.of("%s.proxyVaildHost", "foo.com"),
Pair.of("%s.proxyPorts", "555")),
Collections.singletonList(
Pair.of("%s_proxy", "http://foo:com:Incorrects:555/")
),
new ExpectedProxySetting().port(0),
"Incorrect local Setting")
);
}


@BeforeEach
void setUp() {
Stream.of("http", "https").forEach(protocol ->
Stream.of("%s.proxyHost", "%s.proxyPort", "%s.nonProxyHosts", "%s.proxyUser",
"%s.proxyPassword")
.forEach(property -> System.clearProperty(String.format(property, protocol)))
);

ENVIRONMENT_VARIABLE_HELPER.reset();

}

@ParameterizedTest(name = "{index} - {3}.")
@MethodSource("stubResponses")
void given_LocalSetting_when_httpProtocol_then_correctProxyConfiguration(List<Pair<String, String>> systemSettingsPair,
List<Pair<String, String>> envSystemSetting,
ExpectedProxySetting expectedProxySetting,
String testCaseName) {

setSystemProperties(systemSettingsPair, "http");
setEnvironmentProperties(envSystemSetting, "http");

assertProxyEquals(LocalProxyConfiguration.fromSystemPropertySettings("http"), expectedProxySetting);
assertProxyEquals(LocalProxyConfiguration.fromEnvironmentSettings("http"), expectedProxySetting);

}

@ParameterizedTest(name = "{index} - {3}.")
@MethodSource("stubResponses")
void given_LocalSetting_when_httpsProtocol_then_correctProxyConfiguration(List<Pair<String, String>> systemSettingsPair,
List<Pair<String, String>> envSystemSetting,
ExpectedProxySetting expectedProxySetting,
String testCaseName) {

setSystemProperties(systemSettingsPair, "https");
setEnvironmentProperties(envSystemSetting, "https");

assertProxyEquals(LocalProxyConfiguration.fromSystemPropertySettings("https"), expectedProxySetting);
assertProxyEquals(LocalProxyConfiguration.fromEnvironmentSettings("https"), expectedProxySetting);

}

private static void assertProxyEquals(LocalProxyConfiguration actualConfiguration,
ExpectedProxySetting expectedProxySetting) {
assertThat(actualConfiguration.port()).isEqualTo(expectedProxySetting.port);
assertThat(actualConfiguration.host()).isEqualTo(expectedProxySetting.host);
assertThat(actualConfiguration.nonProxyHosts()).isEqualTo(expectedProxySetting.nonProxyHosts);

assertThat(actualConfiguration.userName().orElse(null)).isEqualTo(expectedProxySetting.userName);
assertThat(actualConfiguration.password().orElse(null)).isEqualTo(expectedProxySetting.password);
}

static void setSystemProperties(List<Pair<String, String>> settingsPairs, String protocol) {
settingsPairs.forEach(settingsPair -> System.setProperty(String.format(settingsPair.left(), protocol),
settingsPair.right()));
}

static void setEnvironmentProperties(List<Pair<String, String>> settingsPairs, String protocol) {


settingsPairs.forEach(settingsPair -> ENVIRONMENT_VARIABLE_HELPER.set(String.format(settingsPair.left(), protocol),
settingsPair.right()));
}

static void clearProperties(List<Pair<String, String>> settingsPairs, String protocol) {
settingsPairs.forEach(settingsPair -> System.setProperty(String.format(settingsPair.left(), protocol),
settingsPair.right()));
}


private static class ExpectedProxySetting {
private int port;
private String host;
private String userName;
private String password;
private Set<String> nonProxyHosts = new HashSet<>();


public ExpectedProxySetting port(int port) {
this.port = port;
return this;
}

public ExpectedProxySetting host(String host) {
this.host = host;
return this;
}

public ExpectedProxySetting userName(String userName) {
this.userName = userName;
return this;
}

public ExpectedProxySetting password(String password) {
this.password = password;
return this;
}

public ExpectedProxySetting nonProxyHost(String... nonProxyHosts) {
this.nonProxyHosts = nonProxyHosts != null ? Arrays.stream(nonProxyHosts)
.collect(Collectors.toSet()) : new HashSet<>();
return this;
}


}


}

0 comments on commit d502ccd

Please sign in to comment.