Skip to content

Commit df2dae9

Browse files
committed
Revert "#36 Extract network side effect into interface so we can mock this out in the tests"
This reverts commit 99b039f. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent fd7af2f commit df2dae9

File tree

3 files changed

+234
-249
lines changed

3 files changed

+234
-249
lines changed

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

Lines changed: 94 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DefaultImportScanService implements ImportScanService {
4444
new FormHttpMessageConverter(),
4545
new ResourceHttpMessageConverter(),
4646
new MappingJackson2HttpMessageConverter());
47-
private final HttpRequester requester = new DefaultHttpRequester();
47+
private final SystemPropertyFinder properties = new SystemPropertyFinder();
4848
@Getter
4949
private final String defectDojoUrl;
5050
@Getter
@@ -118,12 +118,26 @@ public String getFilename() {
118118
body.add("file", contentsAsResource);
119119

120120
final var payload = new HttpEntity<MultiValueMap<String, Object>>(body, headers);
121-
return requester.exchange(generateApiUrl(endpoint), payload);
121+
return exchangeRequest(endpoint, payload);
122122
} catch (HttpClientErrorException e) {
123123
throw new PersistenceException("Failed to attach findings to engagement.");
124124
}
125125
}
126126

127+
ImportScanResponse exchangeRequest(String endpoint, HttpEntity<?> payload) {
128+
final var restTemplate = this.createRestTemplate();
129+
return restTemplate.exchange(
130+
generateApiUrl(endpoint),
131+
HttpMethod.POST,
132+
payload,
133+
ImportScanResponse.class)
134+
.getBody();
135+
}
136+
137+
String generateApiUrl(final String endpoint) {
138+
return String.format("%s/api/v2/%s/", getDefectDojoUrl(), endpoint);
139+
}
140+
127141
/**
128142
* The DefectDojo Authentication Header
129143
*
@@ -135,133 +149,104 @@ HttpHeaders createDefectDojoAuthorizationHeaders() {
135149
return authorizationHeader;
136150
}
137151

138-
String generateApiUrl(final String endpoint) {
139-
return String.format("%s/api/v2/%s/", getDefectDojoUrl(), endpoint);
140-
}
152+
private RestTemplate createRestTemplate() {
153+
final var template = new RestTemplate();
141154

142-
private static class SystemPropertyFinder {
143-
private boolean hasProperty(@NonNull final ProxyConfigNames name) {
144-
return System.getProperty(name.getLiterat()) != null;
155+
if (shouldConfigureProxySettings()) {
156+
template.setRequestFactory(createRequestFactoryWithProxyAuthConfig());
145157
}
146158

147-
private boolean notHasProperty(@NonNull final ProxyConfigNames name) {
148-
return !hasProperty(name);
149-
}
159+
template.setMessageConverters(HTTP_MESSAGE_CONVERTERS);
150160

151-
private String getProperty(@NonNull final ProxyConfigNames name) {
152-
return System.getProperty(name.getLiterat());
153-
}
161+
return template;
154162
}
155163

156-
final static class MissingProxyAuthenticationConfig extends RuntimeException {
157-
MissingProxyAuthenticationConfig(ProxyConfigNames name) {
158-
super(String.format("Expected System property '%s' not set!", name.getLiterat()));
159-
}
164+
boolean shouldConfigureProxySettings() {
165+
return properties.hasProperty(ProxyConfigNames.HTTP_PROXY_USER)
166+
&& properties.hasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD);
160167
}
161168

162169
/**
163-
* This interface abstracts the network side effect done by the underlying HTTP
170+
* Configuring proxy authentication explicitly
171+
*
172+
* <p>
173+
* This isn't done by default for spring rest templates.This method expects these four system properties (Java flag
174+
* {@literal -DpropertyName}) to be set:
175+
* </p>
176+
* <ul>
177+
* <li>http.proxyUser</li>
178+
* <li>http.proxyPassword</li>
179+
* <li>http.proxyHost</li>
180+
* <li>http.proxyPort</li>
181+
* </ul>
182+
*
183+
* @return never {@code null}
164184
*/
165-
interface HttpRequester {
166-
ImportScanResponse exchange(String endpoint, HttpEntity<?> payload);
167-
}
185+
ClientHttpRequestFactory createRequestFactoryWithProxyAuthConfig() {
186+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_USER)) {
187+
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_USER);
188+
}
168189

169-
/**
170-
* Default implementation which utilizes {@link RestTemplate}
171-
*/
172-
static final class DefaultHttpRequester implements HttpRequester {
173-
private final SystemPropertyFinder properties = new SystemPropertyFinder();
174-
175-
@Override
176-
public ImportScanResponse exchange(final String url, final HttpEntity<?> payload) {
177-
final var restTemplate = this.createRestTemplate();
178-
return restTemplate.exchange(
179-
url,
180-
HttpMethod.POST,
181-
payload,
182-
ImportScanResponse.class)
183-
.getBody();
190+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD)) {
191+
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_PASSWORD);
192+
}
193+
194+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_HOST)) {
195+
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_HOST);
196+
}
197+
198+
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PORT)) {
199+
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_PORT);
184200
}
185201

186-
private RestTemplate createRestTemplate() {
187-
final var template = new RestTemplate();
202+
final var proxyHost = properties.getProperty(ProxyConfigNames.HTTP_PROXY_HOST);
203+
final int proxyPort;
204+
try {
205+
proxyPort = Integer.parseInt(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PORT));
206+
} catch (final NumberFormatException e) {
207+
throw new IllegalArgumentException(
208+
String.format("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'.",
209+
ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(),
210+
System.getProperty("http.proxyPort")),
211+
e);
212+
}
188213

189-
if (shouldConfigureProxySettings()) {
190-
template.setRequestFactory(createRequestFactoryWithProxyAuthConfig());
191-
}
214+
final var credentials = new BasicCredentialsProvider();
215+
credentials.setCredentials(
216+
new AuthScope(proxyHost, proxyPort),
217+
new UsernamePasswordCredentials(
218+
properties.getProperty(ProxyConfigNames.HTTP_PROXY_USER),
219+
properties.getProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD))
220+
);
221+
222+
final var clientBuilder = HttpClientBuilder.create();
223+
clientBuilder.useSystemProperties();
224+
clientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
225+
clientBuilder.setDefaultCredentialsProvider(credentials);
226+
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
227+
228+
final var factory = new HttpComponentsClientHttpRequestFactory();
229+
factory.setHttpClient(clientBuilder.build());
230+
return factory;
231+
}
192232

193-
template.setMessageConverters(HTTP_MESSAGE_CONVERTERS);
233+
private static class SystemPropertyFinder {
234+
private boolean hasProperty(@NonNull final ProxyConfigNames name) {
235+
return System.getProperty(name.getLiterat()) != null;
236+
}
194237

195-
return template;
238+
private boolean notHasProperty(@NonNull final ProxyConfigNames name) {
239+
return !hasProperty(name);
196240
}
197241

198-
boolean shouldConfigureProxySettings() {
199-
return properties.hasProperty(ProxyConfigNames.HTTP_PROXY_USER)
200-
&& properties.hasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD);
242+
private String getProperty(@NonNull final ProxyConfigNames name) {
243+
return System.getProperty(name.getLiterat());
201244
}
245+
}
202246

203-
/**
204-
* Configuring proxy authentication explicitly
205-
*
206-
* <p>
207-
* This isn't done by default for spring rest templates.This method expects these four system properties (Java flag
208-
* {@literal -DpropertyName}) to be set:
209-
* </p>
210-
* <ul>
211-
* <li>http.proxyUser</li>
212-
* <li>http.proxyPassword</li>
213-
* <li>http.proxyHost</li>
214-
* <li>http.proxyPort</li>
215-
* </ul>
216-
*
217-
* @return never {@code null}
218-
*/
219-
ClientHttpRequestFactory createRequestFactoryWithProxyAuthConfig() {
220-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_USER)) {
221-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_USER);
222-
}
223-
224-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD)) {
225-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_PASSWORD);
226-
}
227-
228-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_HOST)) {
229-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_HOST);
230-
}
231-
232-
if (properties.notHasProperty(ProxyConfigNames.HTTP_PROXY_PORT)) {
233-
throw new MissingProxyAuthenticationConfig(ProxyConfigNames.HTTP_PROXY_PORT);
234-
}
235-
236-
final var proxyHost = properties.getProperty(ProxyConfigNames.HTTP_PROXY_HOST);
237-
final int proxyPort;
238-
try {
239-
proxyPort = Integer.parseInt(properties.getProperty(ProxyConfigNames.HTTP_PROXY_PORT));
240-
} catch (final NumberFormatException e) {
241-
throw new IllegalArgumentException(
242-
String.format("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'.",
243-
ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(),
244-
System.getProperty("http.proxyPort")),
245-
e);
246-
}
247-
248-
final var credentials = new BasicCredentialsProvider();
249-
credentials.setCredentials(
250-
new AuthScope(proxyHost, proxyPort),
251-
new UsernamePasswordCredentials(
252-
properties.getProperty(ProxyConfigNames.HTTP_PROXY_USER),
253-
properties.getProperty(ProxyConfigNames.HTTP_PROXY_PASSWORD))
254-
);
255-
256-
final var clientBuilder = HttpClientBuilder.create();
257-
clientBuilder.useSystemProperties();
258-
clientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
259-
clientBuilder.setDefaultCredentialsProvider(credentials);
260-
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
261-
262-
final var factory = new HttpComponentsClientHttpRequestFactory();
263-
factory.setHttpClient(clientBuilder.build());
264-
return factory;
247+
final static class MissingProxyAuthenticationConfig extends RuntimeException {
248+
MissingProxyAuthenticationConfig(ProxyConfigNames name) {
249+
super(String.format("Expected System property '%s' not set!", name.getLiterat()));
265250
}
266251
}
267252
}

src/test/java/io/securecodebox/persistence/defectdojo/service/DefaultHttpRequesterTest.java

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)