Skip to content

Commit b8654ca

Browse files
committed
#36 We assume a configured proxy only if username/password are set
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 9626012 commit b8654ca

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

src/main/java/io/securecodebox/persistence/defectdojo/http/ProxyConfigFactory.java

+30-13
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,39 @@
1818
* </p>
1919
*/
2020
public final class ProxyConfigFactory {
21+
static final ProxyConfig DEFAULT_CONFIG = ProxyConfig.NULL;
2122
private final SystemPropertyFinder properties = new SystemPropertyFinder();
2223

24+
/**
25+
* Creates a configuration based on {@link ProxyConfigNames environment variables}
26+
* <p>
27+
* We assume a complete proxy configuration only if {@link ProxyConfigNames#HTTP_PROXY_USER user} and
28+
* {@link ProxyConfigNames#HTTP_PROXY_PASSWORD password} is configured. Unless an
29+
* {@link #DEFAULT_CONFIG} configuration will be returned.
30+
* </p>
31+
* <p>
32+
* Throws {@link MissingProxyConfigValue}, if not all configuration values are present.
33+
* </p>
34+
*
35+
* @return never {@code null}
36+
*/
2337
public ProxyConfig create() {
24-
final var builder = ProxyConfig.builder();
25-
26-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_USER)) {
27-
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_USER);
38+
if (shouldCreateFromProperties()) {
39+
return createFromProperties();
2840
}
2941

30-
builder.user(properties.getProperty(ProxyConfigNames.HTTP_PROXY_USER));
42+
return DEFAULT_CONFIG;
43+
}
3144

32-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD)) {
33-
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_PASSWORD);
34-
}
45+
private boolean shouldCreateFromProperties() {
46+
return properties.hasProperty(ProxyConfigNames.HTTP_PROXY_USER) &&
47+
properties.hasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD);
48+
}
3549

36-
builder.password(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD));
50+
private ProxyConfig createFromProperties() {
51+
final var builder = ProxyConfig.builder()
52+
.user(properties.getProperty(ProxyConfigNames.HTTP_PROXY_USER))
53+
.password(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD));
3754

3855
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_HOST)) {
3956
throw new MissingProxyConfigValue(ProxyConfigNames.HTTP_PROXY_HOST);
@@ -49,10 +66,10 @@ public ProxyConfig create() {
4966
builder.port(Integer.parseInt(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PORT)));
5067
} catch (final NumberFormatException e) {
5168
throw new IllegalArgumentException(
52-
String.format("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'.",
53-
ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(),
54-
System.getProperty("http.proxyPort")),
55-
e);
69+
String.format("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'.",
70+
ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(),
71+
System.getProperty("http.proxyPort")),
72+
e);
5673
}
5774

5875
return builder.build();

src/test/java/io/securecodebox/persistence/defectdojo/http/ProxyConfigFactoryTest.java

+34-26
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,82 @@ class ProxyConfigFactoryTest {
2424
private final ProxyConfigFactory sut = new ProxyConfigFactory();
2525

2626
@Test
27-
void create_throesExceptionIfUserNotSet() {
28-
System.clearProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat());
29-
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
30-
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
31-
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
27+
void create_returnsDefaultIfUserAndPasswordNotPresent() {
28+
assertThat(sut.create(), is(ProxyConfigFactory.DEFAULT_CONFIG));
29+
}
3230

33-
final var thrown = assertThrows(
34-
MissingProxyConfigValue.class,
35-
sut::create);
31+
@Test
32+
void create_returnsDefaultIfUserNotPresent() {
33+
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
3634

37-
assertThat(thrown.getMessage(), containsString("'http.proxyUser'"));
35+
assertThat(sut.create(), is(ProxyConfigFactory.DEFAULT_CONFIG));
3836
}
3937

4038
@Test
41-
void create_throesExceptionIfPasswordNotSet() {
39+
void create_returnsDefaultIfPasswordNotPresent() {
40+
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
41+
42+
assertThat(sut.create(), is(ProxyConfigFactory.DEFAULT_CONFIG));
43+
}
44+
45+
@Test
46+
void create_returnsCompleteConfigIfAllPropertiesArePresent() {
4247
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
43-
System.clearProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat());
48+
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
4449
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
4550
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
4651

47-
final var thrown = assertThrows(
48-
MissingProxyConfigValue.class,
49-
sut::create);
52+
final var expected = ProxyConfig.builder()
53+
.user("user")
54+
.password("password")
55+
.host("host")
56+
.port(4242)
57+
.build();
5058

51-
assertThat(thrown.getMessage(), containsString("'http.proxyPassword'"));
59+
assertThat(sut.create(), is(expected));
5260
}
5361

5462
@Test
55-
void create_throesExceptionIfHostNotSet() {
63+
void create_throwsExceptionIfHostNotSet() {
5664
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
5765
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
5866
System.clearProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat());
5967
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242");
6068

6169
final var thrown = assertThrows(
62-
MissingProxyConfigValue.class,
63-
sut::create);
70+
MissingProxyConfigValue.class,
71+
sut::create);
6472

6573
assertThat(thrown.getMessage(), containsString("'http.proxyHost'"));
6674
}
6775

6876
@Test
69-
void create_throesExceptionIfPortNotSet() {
77+
void create_throwsExceptionIfPortNotSet() {
7078
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
7179
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
7280
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
7381
System.clearProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat());
7482

7583
final var thrown = assertThrows(
76-
MissingProxyConfigValue.class,
77-
sut::create);
84+
MissingProxyConfigValue.class,
85+
sut::create);
7886

7987
assertThat(thrown.getMessage(), containsString("'http.proxyPort'"));
8088
}
8189

8290
@Test
83-
void create_throesExceptionIfPortIsNotInteger() {
91+
void create_throwsExceptionIfPortIsNotInteger() {
8492
System.setProperty(ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user");
8593
System.setProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password");
8694
System.setProperty(ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host");
8795
System.setProperty(ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "FUBAR");
8896

8997
final var thrown = assertThrows(
90-
IllegalArgumentException.class,
91-
sut::create);
98+
IllegalArgumentException.class,
99+
sut::create);
92100

93101
assertThat(
94-
thrown.getMessage(),
95-
is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'."));
102+
thrown.getMessage(),
103+
is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'."));
96104
}
97105
}

0 commit comments

Comments
 (0)