-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDefaultImportScanService.java
209 lines (181 loc) · 7.84 KB
/
DefaultImportScanService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
package io.securecodebox.persistence.defectdojo.service;
import io.securecodebox.persistence.defectdojo.ScanType;
import io.securecodebox.persistence.defectdojo.config.Config;
import io.securecodebox.persistence.defectdojo.exception.PersistenceException;
import io.securecodebox.persistence.defectdojo.http.ProxyConfig;
import io.securecodebox.persistence.defectdojo.model.ScanFile;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
/*
* https://defectdojo.security.iteratec.dev/api/v2/oa3/swagger-ui/#operations-tag-import-scan
*/
@Slf4j
class DefaultImportScanService implements ImportScanService {
private static final List<HttpMessageConverter<?>> HTTP_MESSAGE_CONVERTERS = List.of(
new FormHttpMessageConverter(),
new ResourceHttpMessageConverter(),
new MappingJackson2HttpMessageConverter());
@Getter
private final String defectDojoUrl;
@Getter
private final String defectDojoApiKey;
private final ProxyConfig proxyConfig;
/**
* Dedicated constructor.
*
* @param config not {@code null}
* @param proxyConfig not {@code null}
*/
DefaultImportScanService(final @NonNull Config config, @NonNull ProxyConfig proxyConfig) {
super();
this.defectDojoUrl = config.getUrl();
this.defectDojoApiKey = config.getApiKey();
this.proxyConfig = proxyConfig;
}
@Override
public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options) {
options.put("engagement", Long.toString(engagementId));
return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, options);
}
@Override
public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options) {
options.put("test", Long.toString(testId));
return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, options);
}
/*
* Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterward, you can have something else.
*/
private ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, Map<String, String> options) {
final var headers = createDefectDojoAuthorizationHeaders();
// We use multipart because we send two "parts" in the request body:
// 1. generic info as key=value&key=value...
// 2. the raw scan result as file
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// FIXME: #36 Why do we use a multi value map here? Do we need multiple values for any given key?
final var body = new LinkedMultiValueMap<String, Object>();
body.add("lead", Long.toString(lead));
body.add("scan_date", currentDate);
body.add("scan_type", scanType.getTestType());
body.add("close_old_findings", "true");
body.add("skip_duplicates", "false");
body.add("test_type", String.valueOf(testType));
// Avoid duplicate entries:
for (final var optionName : options.keySet()) {
body.remove(optionName);
}
// FIXME: #36 Workaround due to type incompatibility of MultiValueMap<String, String> and MultiValueMap<String, Object>.
for (final var option : options.entrySet()) {
body.add(option.getKey(), option.getValue());
}
try {
// scanFile is the raw result from lurker.
final var contentsAsResource = new ByteArrayResource(scanFile.getContent().getBytes(StandardCharsets.UTF_8)) {
@Override
public String getFilename() {
return scanFile.getName();
}
};
// We send the whole file content, so DefectDojo can parse the finding by itself.
body.add("file", contentsAsResource);
final var payload = new HttpEntity<MultiValueMap<String, Object>>(body, headers);
return exchangeRequest(endpoint, payload);
} catch (HttpClientErrorException e) {
log.error("Exception while attaching findings to engagement: {}", e.getMessage());
throw new PersistenceException("Failed to attach findings to engagement.", e);
}
}
ImportScanResponse exchangeRequest(String endpoint, HttpEntity<?> payload) {
final var restTemplate = this.createRestTemplate();
return restTemplate.exchange(
generateApiUrl(endpoint),
HttpMethod.POST,
payload,
ImportScanResponse.class)
.getBody();
}
String generateApiUrl(final String endpoint) {
return String.format("%s/api/v2/%s/", getDefectDojoUrl(), endpoint);
}
/**
* The DefectDojo Authentication Header
*
* @return never {@code null}
*/
HttpHeaders createDefectDojoAuthorizationHeaders() {
final var authorizationHeader = new HttpHeaders();
authorizationHeader.set(HttpHeaders.AUTHORIZATION, String.format("Token %s", defectDojoApiKey));
return authorizationHeader;
}
private RestTemplate createRestTemplate() {
final var template = new RestTemplate();
if (shouldConfigureProxySettings()) {
template.setRequestFactory(createRequestFactoryWithProxyAuthConfig());
}
template.setMessageConverters(HTTP_MESSAGE_CONVERTERS);
return template;
}
boolean shouldConfigureProxySettings() {
return proxyConfig.isComplete();
}
/**
* Configuring proxy authentication explicitly
*
* <p>
* This isn't done by default for spring rest templates.This method expects these four system properties (Java flag
* {@literal -DpropertyName}) to be set:
* </p>
* <ul>
* <li>http.proxyUser</li>
* <li>http.proxyPassword</li>
* <li>http.proxyHost</li>
* <li>http.proxyPort</li>
* </ul>
*
* @return never {@code null}
*/
ClientHttpRequestFactory createRequestFactoryWithProxyAuthConfig() {
final var credentials = new BasicCredentialsProvider();
credentials.setCredentials(
new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()),
new UsernamePasswordCredentials(
proxyConfig.getUser(),
proxyConfig.getPassword())
);
final var clientBuilder = HttpClientBuilder.create();
clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost(proxyConfig.getHost(), proxyConfig.getPort()));
clientBuilder.setDefaultCredentialsProvider(credentials);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
final var factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(clientBuilder.build());
return factory;
}
}