Skip to content

Commit cb4dccc

Browse files
committed
#36 Add facotry to create proxy config from system properties
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 23db251 commit cb4dccc

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package io.securecodebox.persistence.defectdojo.http;
2+
3+
import lombok.Getter;
4+
import lombok.NonNull;
5+
6+
/**
7+
* This class is responsible to create a proxy configuration
8+
* <p>
9+
* This implementation collects the configuration values from Java system properties. It also treats the
10+
* cases of non-present values ot values of incorrect type.
11+
* </p>
12+
* <p>
13+
* This class does not validate for semantic errors of the values, e.g. malformed hostnames or invalid
14+
* port numbers.
15+
* </p>
16+
*/
17+
final class ProxyConfigFactory {
18+
private final SystemPropertyFinder properties = new SystemPropertyFinder();
19+
20+
ProxyConfig create() {
21+
final var builder = ProxyConfig.builder();
22+
23+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_USER)) {
24+
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_USER);
25+
}
26+
27+
builder.user(properties.getProperty(ProxyConfigNames.HTTP_PROXY_USER));
28+
29+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD)) {
30+
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_PASSWORD);
31+
}
32+
33+
builder.password(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD));
34+
35+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_HOST)) {
36+
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_HOST);
37+
}
38+
39+
builder.host(properties.getProperty(ProxyConfigNames.HTTP_PROXY_HOST));
40+
41+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PORT)) {
42+
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_PORT);
43+
}
44+
45+
try {
46+
builder.port(Integer.parseInt(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PORT)));
47+
} catch (final NumberFormatException e) {
48+
throw new IllegalArgumentException(
49+
String.format("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'.",
50+
ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(),
51+
System.getProperty("http.proxyPort")),
52+
e);
53+
}
54+
55+
return builder.build();
56+
}
57+
58+
/**
59+
* These properties can be configured by passing them to the running Java process w/ flag {@literal -D}
60+
* <p>
61+
* Example: {@literal java -Dhttp.proxyHost=... -D... -jar ...}
62+
* </p>
63+
* <p>
64+
* <strong>Important</strong>: All four parameters are mandatory. You must set them all
65+
* or none of them!
66+
* </p>
67+
*/
68+
@Getter
69+
enum ProxyConfigNames {
70+
/**
71+
* System property name for the proxy username
72+
*/
73+
HTTP_PROXY_USER("http.proxyUser"),
74+
/**
75+
* System property name for the proxy user's password
76+
*/
77+
HTTP_PROXY_PASSWORD("http.proxyPassword"),
78+
/**
79+
* System property name for the proxy's hostname
80+
*/
81+
HTTP_PROXY_HOST("http.proxyHost"),
82+
/**
83+
* System property for the proxy's port number
84+
*/
85+
HTTP_PROXY_PORT("http.proxyPort");
86+
87+
private final String literat;
88+
89+
ProxyConfigNames(String literat) {
90+
this.literat = literat;
91+
}
92+
}
93+
94+
private static class SystemPropertyFinder {
95+
private boolean hasProperty(@NonNull final ProxyConfigNames name) {
96+
return System.getProperty(name.getLiterat()) != null;
97+
}
98+
99+
private boolean notHasProperty(@NonNull final ProxyConfigNames name) {
100+
return !hasProperty(name);
101+
}
102+
103+
private String getProperty(@NonNull final ProxyConfigNames name) {
104+
return System.getProperty(name.getLiterat());
105+
}
106+
}
107+
108+
/**
109+
* This exception indicates a missing proxy config value
110+
*/
111+
final static class MissingProxyConfigValue extends RuntimeException {
112+
MissingProxyConfigValue(ProxyConfigNames name) {
113+
super(String.format("Expected System property '%s' not set!", name.getLiterat()));
114+
}
115+
}
116+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package io.securecodebox.persistence.defectdojo.http;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
6+
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
7+
import uk.org.webcompere.systemstubs.properties.SystemProperties;
8+
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.Matchers.is;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
/**
14+
* Tests for {@link ProxyConfigFactory}
15+
*/
16+
@ExtendWith(SystemStubsExtension.class)
17+
class ProxyConfigFactoryTest {
18+
@SystemStub
19+
private SystemProperties restoreSystemProperties;
20+
private final ProxyConfigFactory sut = new ProxyConfigFactory();
21+
22+
@Test
23+
void create_throesExceptionIfUserNotSet() {
24+
System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat());
25+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
26+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
27+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
28+
29+
final var thrown = assertThrows(
30+
ProxyConfigFactory.MissingProxyConfigValue.class,
31+
sut::create);
32+
33+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyUser' not set!"));
34+
}
35+
36+
@Test
37+
void create_throesExceptionIfPasswordNotSet() {
38+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
39+
System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat());
40+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
41+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
42+
43+
final var thrown = assertThrows(
44+
ProxyConfigFactory.MissingProxyConfigValue.class,
45+
sut::create);
46+
47+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPassword' not set!"));
48+
}
49+
50+
@Test
51+
void create_throesExceptionIfHostNotSet() {
52+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
53+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
54+
System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat());
55+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
56+
57+
final var thrown = assertThrows(
58+
ProxyConfigFactory.MissingProxyConfigValue.class,
59+
sut::create);
60+
61+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyHost' not set!"));
62+
}
63+
64+
@Test
65+
void create_throesExceptionIfPortNotSet() {
66+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
67+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
68+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
69+
System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat());
70+
71+
final var thrown = assertThrows(
72+
ProxyConfigFactory.MissingProxyConfigValue.class,
73+
sut::create);
74+
75+
assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPort' not set!"));
76+
}
77+
78+
@Test
79+
void create_throesExceptionIfPortIsNotInteger() {
80+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
81+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
82+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
83+
System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "FUBAR");
84+
85+
final var thrown = assertThrows(
86+
IllegalArgumentException.class,
87+
sut::create);
88+
89+
assertThat(
90+
thrown.getMessage(),
91+
is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'."));
92+
}
93+
}

0 commit comments

Comments
 (0)