Skip to content

Commit 86834d3

Browse files
committed
#36 Use new proxy config in import scan service
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent bf4ddab commit 86834d3

File tree

7 files changed

+70
-231
lines changed

7 files changed

+70
-231
lines changed

src/main/java/io/securecodebox/persistence/defectdojo/config/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public final class Config {
2020
* Default for {@link #maxPageCountForGets}
2121
*/
2222
static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100;
23+
/**
24+
* Null pattern object.
25+
*/
26+
public static final Config NULL = new Config("", "", DEFAULT_MAX_PAGE_COUNT_FOR_GETS);
2327

2428
/**
2529
* URL of the host which serves the DefectDojo API.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*/
1414
@Value
1515
@Builder
16-
class ProxyConfig {
16+
public class ProxyConfig {
1717
/**
1818
* Null pattern object.
1919
*/
20-
static final ProxyConfig NULL = ProxyConfig.builder().build();
20+
public static final ProxyConfig NULL = ProxyConfig.builder().build();
2121
private static final String DEFAULT_STRING = "";
2222
private static final int DEFAULT_INT = 0;
2323

@@ -62,7 +62,7 @@ class ProxyConfig {
6262
*
6363
* @return {@code true} if all values are set else {@code false}
6464
*/
65-
boolean isComplete() {
65+
public boolean isComplete() {
6666
if (getUser().equals(DEFAULT_STRING)) {
6767
return false;
6868
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* port numbers.
1515
* </p>
1616
*/
17-
final class ProxyConfigFactory {
17+
public final class ProxyConfigFactory {
1818
private final SystemPropertyFinder properties = new SystemPropertyFinder();
1919

20-
ProxyConfig create() {
20+
public ProxyConfig create() {
2121
final var builder = ProxyConfig.builder();
2222

2323
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_USER)) {
@@ -66,7 +66,7 @@ ProxyConfig create() {
6666
* </p>
6767
*/
6868
@Getter
69-
enum ProxyConfigNames {
69+
public enum ProxyConfigNames {
7070
/**
7171
* System property name for the proxy username
7272
*/

src/main/java/io/securecodebox/persistence/defectdojo/service/DefaultImportScanService.java

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.securecodebox.persistence.defectdojo.ScanType;
88
import io.securecodebox.persistence.defectdojo.config.Config;
99
import io.securecodebox.persistence.defectdojo.exception.PersistenceException;
10+
import io.securecodebox.persistence.defectdojo.http.ProxyConfig;
1011
import io.securecodebox.persistence.defectdojo.model.ScanFile;
1112
import lombok.Getter;
1213
import lombok.NonNull;
@@ -44,22 +45,23 @@ class DefaultImportScanService implements ImportScanService {
4445
new FormHttpMessageConverter(),
4546
new ResourceHttpMessageConverter(),
4647
new MappingJackson2HttpMessageConverter());
47-
@Deprecated
48-
private final SystemPropertyFinder properties = new SystemPropertyFinder();
4948
@Getter
5049
private final String defectDojoUrl;
5150
@Getter
5251
private final String defectDojoApiKey;
52+
private final ProxyConfig proxyConfig;
5353

5454
/**
5555
* Dedicated constructor.
5656
*
57-
* @param config not {@code null}
57+
* @param config not {@code null}
58+
* @param proxyConfig not {@code null}
5859
*/
59-
DefaultImportScanService(final @NonNull Config config) {
60+
DefaultImportScanService(final @NonNull Config config, @NonNull ProxyConfig proxyConfig) {
6061
super();
6162
this.defectDojoUrl = config.getUrl();
6263
this.defectDojoApiKey = config.getApiKey();
64+
this.proxyConfig = proxyConfig;
6365
}
6466

6567
@Override
@@ -163,8 +165,7 @@ private RestTemplate createRestTemplate() {
163165
}
164166

165167
boolean shouldConfigureProxySettings() {
166-
return properties.hasProperty(ProxyConfigNames.HTTP_PROXY_USER)
167-
&& properties.hasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD);
168+
return proxyConfig.isComplete();
168169
}
169170

170171
/**
@@ -184,72 +185,22 @@ boolean shouldConfigureProxySettings() {
184185
* @return never {@code null}
185186
*/
186187
ClientHttpRequestFactory createRequestFactoryWithProxyAuthConfig() {
187-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_USER)) {
188-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_USER);
189-
}
190-
191-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD)) {
192-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_PASSWORD);
193-
}
194-
195-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_HOST)) {
196-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_HOST);
197-
}
198-
199-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PORT)) {
200-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_PORT);
201-
}
202-
203-
final var proxyHost = properties.getProperty(ProxyConfigNames.HTTP_PROXY_HOST);
204-
final int proxyPort;
205-
try {
206-
proxyPort = Integer.parseInt(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PORT));
207-
} catch (final NumberFormatException e) {
208-
throw new IllegalArgumentException(
209-
String.format("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'.",
210-
ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(),
211-
System.getProperty("http.proxyPort")),
212-
e);
213-
}
214-
215188
final var credentials = new BasicCredentialsProvider();
216189
credentials.setCredentials(
217-
new AuthScope(proxyHost, proxyPort),
190+
new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()),
218191
new UsernamePasswordCredentials(
219-
properties.getProperty(ProxyConfigNames.HTTP_PROXY_USER),
220-
properties.getProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD))
192+
proxyConfig.getUser(),
193+
proxyConfig.getPassword())
221194
);
222195

223196
final var clientBuilder = HttpClientBuilder.create();
224197
clientBuilder.useSystemProperties();
225-
clientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
198+
clientBuilder.setProxy(new HttpHost(proxyConfig.getHost(), proxyConfig.getPort()));
226199
clientBuilder.setDefaultCredentialsProvider(credentials);
227200
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
228201

229202
final var factory = new HttpComponentsClientHttpRequestFactory();
230203
factory.setHttpClient(clientBuilder.build());
231204
return factory;
232205
}
233-
234-
@Deprecated
235-
private static class SystemPropertyFinder {
236-
private boolean hasProperty(@NonNull final ProxyConfigNames name) {
237-
return System.getProperty(name.getLiterat()) != null;
238-
}
239-
240-
private boolean notHasProperty(@NonNull final ProxyConfigNames name) {
241-
return !hasProperty(name);
242-
}
243-
244-
private String getProperty(@NonNull final ProxyConfigNames name) {
245-
return System.getProperty(name.getLiterat());
246-
}
247-
}
248-
249-
@Deprecated
250-
final static class MissingProxyAuthenticationConfig extends RuntimeException {
251-
MissingProxyAuthenticationConfig(ProxyConfigNames name) {
252-
super(String.format("Expected System property '%s' not set!", name.getLiterat()));
253-
}
254-
}
255206
}

src/main/java/io/securecodebox/persistence/defectdojo/service/ImportScanService.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import com.fasterxml.jackson.annotation.JsonProperty;
88
import io.securecodebox.persistence.defectdojo.ScanType;
99
import io.securecodebox.persistence.defectdojo.config.Config;
10+
import io.securecodebox.persistence.defectdojo.http.ProxyConfig;
11+
import io.securecodebox.persistence.defectdojo.http.ProxyConfigFactory;
1012
import io.securecodebox.persistence.defectdojo.model.ScanFile;
1113
import lombok.Data;
12-
import lombok.Getter;
14+
import lombok.NonNull;
1315

1416
import java.util.HashMap;
1517
import java.util.Map;
@@ -25,7 +27,18 @@ public interface ImportScanService {
2527
* @return never {@code null}
2628
*/
2729
static ImportScanService createDefault(final Config config) {
28-
return new DefaultImportScanService(config);
30+
return createDefault(config, new ProxyConfigFactory().create());
31+
}
32+
33+
/**
34+
* Factory method to create new instance of service default implementation
35+
*
36+
* @param config must not be {@code null}
37+
* @param proxyConfig must not be {@code null}
38+
* @return never {@code null}
39+
*/
40+
static ImportScanService createDefault(@NonNull final Config config, @NonNull final ProxyConfig proxyConfig) {
41+
return new DefaultImportScanService(config, proxyConfig);
2942
}
3043

3144
default ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) {
@@ -51,29 +64,4 @@ class ImportScanResponse {
5164
@JsonProperty("test")
5265
protected long testId;
5366
}
54-
55-
/**
56-
* These properties can be configured by passing them to the running Java process w/ flag {@literal -D}
57-
* <p>
58-
* Example: {@literal java -Dhttp.proxyHost=... -D... -jar ...}
59-
* </p>
60-
* <p>
61-
* <strong>Important</strong>: All four parameters are mandatory. You must set them all
62-
* or none of them!
63-
* </p>
64-
*/
65-
@Deprecated
66-
enum ProxyConfigNames {
67-
HTTP_PROXY_HOST("http.proxyHost"),
68-
HTTP_PROXY_PORT("http.proxyPort"),
69-
HTTP_PROXY_USER("http.proxyUser"),
70-
HTTP_PROXY_PASSWORD("http.proxyPassword");
71-
72-
@Getter
73-
private final String literat;
74-
75-
ProxyConfigNames(String literat) {
76-
this.literat = literat;
77-
}
78-
}
7967
}

0 commit comments

Comments
 (0)