-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99783a6
commit fd20b6e
Showing
7 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
...are/src/main/java/com/mercedesbenz/sechub/wrapper/prepare/PrepareWrapperProxySupport.java
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,82 @@ | ||
package com.mercedesbenz.sechub.wrapper.prepare; | ||
|
||
import static com.mercedesbenz.sechub.wrapper.prepare.cli.PrepareWrapperKeyConstants.*; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PrepareWrapperProxySupport { | ||
|
||
@Value("${" + KEY_PDS_HTTPS_PROXY + "}") | ||
String httpsProxy; | ||
|
||
@Value("${" + KEY_PDS_NO_PROXY + "}") | ||
String noProxy; | ||
|
||
@Value("${" + KEY_PDS_PREPARE_PROXY_ENABLED + ":false}") | ||
boolean proxyEnabled; | ||
|
||
@Autowired | ||
PrepareWrapperSystemPropertySupport propertySupport; | ||
|
||
public void setUpProxy(String url) { | ||
if (!proxyEnabled) { | ||
return; | ||
} | ||
|
||
assertHttpsProxy(); | ||
|
||
if (isProxyRequiredForURL(url)) { | ||
setProxySystemProperty(); | ||
} | ||
} | ||
|
||
private boolean isProxyRequiredForURL(String url) { | ||
if (noProxy == null || noProxy.isBlank()) { | ||
return true; | ||
} | ||
String[] noProxyList = noProxy.split(","); | ||
|
||
for (String noProxy : noProxyList) { | ||
if (url.contains(noProxy)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private void assertHttpsProxy() { | ||
if (httpsProxy == null || httpsProxy.isBlank()) { | ||
throw new IllegalStateException( | ||
"No HTTPS proxy is set. Please set the environment variable: " + KEY_PDS_HTTPS_PROXY + " with the format: <hostname>:<port>"); | ||
} | ||
} | ||
|
||
private String resolveHostname() { | ||
return httpsProxy.split(":")[0]; | ||
} | ||
|
||
private String resolvePort() { | ||
String port = httpsProxy.split(":")[1]; | ||
assertPort(port); | ||
return port; | ||
} | ||
|
||
private void setProxySystemProperty() { | ||
propertySupport.setSystemProperty("https.proxyHost", resolveHostname()); | ||
propertySupport.setSystemProperty("https.proxyPort", resolvePort()); | ||
} | ||
|
||
private void assertPort(String port) { | ||
if (port == null || port.isBlank()) { | ||
throw new IllegalStateException( | ||
"No port number is set. Please set the environment variable: " + KEY_PDS_HTTPS_PROXY + " with the format: <hostname>:<port>"); | ||
} | ||
if (port.chars().noneMatch(Character::isDigit)) { | ||
throw new IllegalStateException( | ||
"Port number is not a number. Please set the environment variable: " + KEY_PDS_HTTPS_PROXY + " with the format: <hostname>:<port>"); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ain/java/com/mercedesbenz/sechub/wrapper/prepare/PrepareWrapperSystemPropertySupport.java
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,10 @@ | ||
package com.mercedesbenz.sechub.wrapper.prepare; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PrepareWrapperSystemPropertySupport { | ||
public void setSystemProperty(String key, String value) { | ||
System.setProperty(key, value); | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
...src/test/java/com/mercedesbenz/sechub/wrapper/prepare/PrepareWrapperProxySupportTest.java
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,107 @@ | ||
package com.mercedesbenz.sechub.wrapper.prepare; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class PrepareWrapperProxySupportTest { | ||
|
||
PrepareWrapperProxySupport supportToTest; | ||
|
||
PrepareWrapperSystemPropertySupport propertySupport; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
supportToTest = new PrepareWrapperProxySupport(); | ||
propertySupport = mock(PrepareWrapperSystemPropertySupport.class); | ||
|
||
supportToTest.propertySupport = propertySupport; | ||
supportToTest.proxyEnabled = true; | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { "some.example.url/my-example-page", "https://some.example/repository.git", "https://example.org/path" }) | ||
void systemProperties_are_not_set_when_url_is_in_noProxyList(String url) { | ||
/* prepare */ | ||
supportToTest.noProxy = "some.example,example.org"; | ||
supportToTest.httpsProxy = "some.example.proxy:8080"; | ||
|
||
/* execute */ | ||
supportToTest.setUpProxy(url); | ||
|
||
/* test */ | ||
verify(propertySupport, never()).setSystemProperty("https.proxyHost", "some.example.proxy"); | ||
verify(propertySupport, never()).setSystemProperty("https.proxyPort", "8080"); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { "some.example.url/my-example-page", "https://some.example/repository.git", "https://example.org/path" }) | ||
void systemProperties_are_set_when_when_url_is_not_in_noProxyList(String url) { | ||
/* prepare */ | ||
supportToTest.noProxy = "notMyUrl,some.other.example.com"; | ||
supportToTest.httpsProxy = "some.example.proxy:8080"; | ||
|
||
/* execute */ | ||
supportToTest.setUpProxy(url); | ||
|
||
/* test */ | ||
verify(propertySupport).setSystemProperty("https.proxyHost", "some.example.proxy"); | ||
verify(propertySupport).setSystemProperty("https.proxyPort", "8080"); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { "some.example.url/my-example-page", "https://some.example/repository.git", "https://example.org/path" }) | ||
void systemProperties_are_set_when_noProxyList_is_empty(String url) { | ||
/* prepare */ | ||
supportToTest.noProxy = ""; | ||
supportToTest.httpsProxy = "some.example.proxy:8080"; | ||
|
||
/* execute */ | ||
supportToTest.setUpProxy(url); | ||
|
||
/* test */ | ||
verify(propertySupport).setSystemProperty("https.proxyHost", "some.example.proxy"); | ||
verify(propertySupport).setSystemProperty("https.proxyPort", "8080"); | ||
} | ||
|
||
@Test | ||
void setProxySystemProperty_sets_https_proxyHost_and_proxyPort() { | ||
/* prepare */ | ||
supportToTest.httpsProxy = "some.example.proxy:8080"; | ||
|
||
/* execute */ | ||
supportToTest.setUpProxy("https://some.example/repository.git"); | ||
|
||
/* test */ | ||
verify(propertySupport).setSystemProperty("https.proxyHost", "some.example.proxy"); | ||
verify(propertySupport).setSystemProperty("https.proxyPort", "8080"); | ||
} | ||
|
||
@Test | ||
void setProxySystemProperty_throws_exception_when_port_not_valid() { | ||
/* prepare */ | ||
supportToTest.httpsProxy = "some.example.proxy:invalidPort"; | ||
|
||
/* execute */ | ||
IllegalStateException exception = assertThrows(IllegalStateException.class, () -> supportToTest.setUpProxy("https://some.example/repository.git")); | ||
|
||
/* test */ | ||
assertTrue(exception.getMessage().contains("Port number is not a number. Please set the environment variable: ")); | ||
} | ||
|
||
@Test | ||
void setProxySystemProperty_throws_exception_when_proxy_not_set() { | ||
/* prepare */ | ||
supportToTest.httpsProxy = ""; | ||
|
||
/* execute */ | ||
IllegalStateException exception = assertThrows(IllegalStateException.class, () -> supportToTest.setUpProxy("https://some.example/repository.git")); | ||
|
||
/* test */ | ||
assertTrue(exception.getMessage().contains("No HTTPS proxy is set. Please set the environment variable: ")); | ||
} | ||
} |