Skip to content

Commit

Permalink
Added Proxy setup #3260
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed Jul 1, 2024
1 parent 99783a6 commit fd20b6e
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class PrepareWrapperPreparationService {
@Autowired
List<PrepareWrapperModule> modules = new ArrayList<>();

@Autowired
PrepareWrapperProxySupport proxySupport;

public AdapterExecutionResult startPreparation() throws IOException {

LOG.debug("Start preparation");
Expand Down Expand Up @@ -61,6 +64,7 @@ public AdapterExecutionResult startPreparation() throws IOException {
if (module.isResponsibleToPrepare(context)) {
LOG.debug("Module: {} is responsible and will be used to prepare", module);

setUpSystemProperties(context);
module.prepare(context);

PrepareResult result = new PrepareResult(PrepareStatus.OK);
Expand All @@ -81,4 +85,8 @@ private AdapterExecutionResult createAdapterExecutionResult(PrepareStatus status

return new AdapterExecutionResult(result.toString(), messages);
}

private void setUpSystemProperties(PrepareWrapperContext context) {
proxySupport.setUpProxy(context.getRemoteDataConfiguration().getLocation());
}
}
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>");
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,19 @@ public class PrepareWrapperKeyConstants {

public static final String KEY_PDS_PREPARE_MODULE_GIT_CLONE_WITHOUT_GIT_HISTORY = PREPARE_MODULE_GIT + ".clone.without.git.history";

/**
* HTTPS Proxy URL
*/
public static final String KEY_PDS_HTTPS_PROXY = "https.proxy";

/**
* Flag to enable the prepare proxy
*/
public static final String KEY_PDS_PREPARE_PROXY_ENABLED = "pds.prepare.proxy.enabled";

/**
* Comma seperated list to define which hosts must be accessed without proxy
*/
public static final String KEY_PDS_NO_PROXY = "no.proxy";

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
GitLocationConverter.class,
FileNameSupport.class,
PrepareWrapperSharedVolumePropertiesSetup.class,
PrepareWrapperS3PropertiesSetup.class })
PrepareWrapperS3PropertiesSetup.class,
PrepareWrapperProxySupport.class,
PrepareWrapperSystemPropertySupport.class})
/* @formatter:on */
@ExtendWith(SpringExtension.class)
@TestPropertySource(locations = "classpath:init-testdata-prepare-wrapper-spring-boot.properties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ void beforeEach() {
context = mock(PrepareWrapperContext.class);
PrepareWrapperEnvironment environment = mock(PrepareWrapperEnvironment.class);
PrepareWrapperContextFactory contextFactory = mock(PrepareWrapperContextFactory.class);
PrepareWrapperProxySupport proxySupport = mock(PrepareWrapperProxySupport.class);
when(contextFactory.create(environment)).thenReturn(context);

serviceToTest = new PrepareWrapperPreparationService();
serviceToTest.environment = environment;
serviceToTest.contextFactory = contextFactory;
serviceToTest.modules = new ArrayList<>();
serviceToTest.proxySupport = proxySupport;
}

@Test
Expand Down
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: "));
}
}

0 comments on commit fd20b6e

Please sign in to comment.