Skip to content

Commit c9dc6b3

Browse files
committed
#36 Add a old version because did to many refactorings so that's hard to see the code duplication
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 4ff0892 commit c9dc6b3

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* secureCodeBox (SCB)
3+
* Copyright 2021 iteratec GmbH
4+
* https://www.iteratec.com
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package io.securecodebox.persistence.defectdojo.service;
19+
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import io.securecodebox.persistence.defectdojo.ScanType;
22+
import io.securecodebox.persistence.defectdojo.config.Config;
23+
import io.securecodebox.persistence.defectdojo.exception.PersistenceException;
24+
import io.securecodebox.persistence.defectdojo.http.Foo;
25+
import io.securecodebox.persistence.defectdojo.model.ScanFile;
26+
import lombok.Data;
27+
import org.apache.http.HttpHost;
28+
import org.apache.http.auth.AuthScope;
29+
import org.apache.http.auth.UsernamePasswordCredentials;
30+
import org.apache.http.client.CredentialsProvider;
31+
import org.apache.http.impl.client.BasicCredentialsProvider;
32+
import org.apache.http.impl.client.CloseableHttpClient;
33+
import org.apache.http.impl.client.HttpClientBuilder;
34+
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
35+
import org.springframework.core.io.ByteArrayResource;
36+
import org.springframework.http.HttpEntity;
37+
import org.springframework.http.HttpHeaders;
38+
import org.springframework.http.HttpMethod;
39+
import org.springframework.http.MediaType;
40+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
41+
import org.springframework.http.converter.FormHttpMessageConverter;
42+
import org.springframework.http.converter.ResourceHttpMessageConverter;
43+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
44+
import org.springframework.util.LinkedMultiValueMap;
45+
import org.springframework.util.MultiValueMap;
46+
import org.springframework.web.client.HttpClientErrorException;
47+
import org.springframework.web.client.RestTemplate;
48+
49+
import java.nio.charset.StandardCharsets;
50+
import java.util.List;
51+
52+
/**
53+
* Copied the version before I did refactoring for easier compare of duplicated code
54+
*/
55+
public class ImportScanService2 {
56+
57+
private final Config config;
58+
@Deprecated
59+
protected String defectDojoUrl;
60+
@Deprecated
61+
protected String defectDojoApiKey;
62+
63+
public ImportScanService2(Config config) {
64+
super();
65+
this.config = config;
66+
this.defectDojoUrl = config.getUrl();
67+
this.defectDojoApiKey = config.getApiKey();
68+
}
69+
70+
/**
71+
* @return The DefectDojo Authentication Header
72+
*/
73+
private HttpHeaders getDefectDojoAuthorizationHeaders() {
74+
return new Foo(config).getDefectDojoAuthorizationHeaders();
75+
}
76+
77+
protected RestTemplate getRestTemplate() {
78+
if (System.getProperty("http.proxyUser") != null && System.getProperty("http.proxyPassword") != null) {
79+
// Configuring Proxy Authentication explicitly as it isn't done by default for spring rest templates :(
80+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
81+
credsProvider.setCredentials(
82+
new AuthScope(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort"))),
83+
new UsernamePasswordCredentials(System.getProperty("http.proxyUser"), System.getProperty("http.proxyPassword"))
84+
);
85+
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
86+
87+
clientBuilder.useSystemProperties();
88+
clientBuilder.setProxy(new HttpHost(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort"))));
89+
clientBuilder.setDefaultCredentialsProvider(credsProvider);
90+
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
91+
92+
CloseableHttpClient client = clientBuilder.build();
93+
94+
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
95+
factory.setHttpClient(client);
96+
return new RestTemplate(factory);
97+
} else {
98+
return new RestTemplate();
99+
}
100+
}
101+
102+
/**
103+
* Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterwards, you can have somethings else
104+
*/
105+
protected ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, Object> options) {
106+
var restTemplate = this.getRestTemplate();
107+
HttpHeaders headers = getDefectDojoAuthorizationHeaders();
108+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
109+
restTemplate.setMessageConverters(List.of(
110+
new FormHttpMessageConverter(),
111+
new ResourceHttpMessageConverter(),
112+
new MappingJackson2HttpMessageConverter())
113+
);
114+
115+
MultiValueMap<String, Object> mvn = new LinkedMultiValueMap<>();
116+
117+
mvn.add("lead", Long.toString(lead));
118+
mvn.add("scan_date", currentDate);
119+
mvn.add("scan_type", scanType.getTestType());
120+
mvn.add("close_old_findings", "true");
121+
mvn.add("skip_duplicates", "false");
122+
mvn.add("test_type", String.valueOf(testType));
123+
124+
for (String theKey : options.keySet()) {
125+
mvn.remove(theKey);
126+
}
127+
mvn.addAll(options);
128+
129+
try {
130+
ByteArrayResource contentsAsResource = new ByteArrayResource(scanFile.getContent().getBytes(StandardCharsets.UTF_8)) {
131+
@Override
132+
public String getFilename() {
133+
return scanFile.getName();
134+
}
135+
};
136+
137+
mvn.add("file", contentsAsResource);
138+
139+
var payload = new HttpEntity<>(mvn, headers);
140+
141+
return restTemplate.exchange(defectDojoUrl + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody();
142+
} catch (HttpClientErrorException e) {
143+
throw new PersistenceException("Failed to attach findings to engagement.");
144+
}
145+
}
146+
147+
148+
public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) {
149+
var additionalValues = new LinkedMultiValueMap<String, Object>();
150+
additionalValues.add("engagement", Long.toString(engagementId));
151+
152+
return this.importScan(scanFile, engagementId, lead, currentDate, scanType, testType, additionalValues);
153+
}
154+
155+
public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType) {
156+
var additionalValues = new LinkedMultiValueMap<String, Object>();
157+
additionalValues.add("test", Long.toString(testId));
158+
159+
return this.reimportScan(scanFile, testId, lead, currentDate, scanType, testType, additionalValues);
160+
}
161+
162+
//overloading with optional parameter
163+
public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, LinkedMultiValueMap<String, Object> additionalValues) {
164+
additionalValues.add("engagement", Long.toString(engagementId));
165+
166+
return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, additionalValues);
167+
}
168+
169+
public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, LinkedMultiValueMap<String, Object> additionalValues) {
170+
additionalValues.add("test", Long.toString(testId));
171+
172+
return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, additionalValues);
173+
}
174+
175+
@Data
176+
public static class ImportScanResponse {
177+
@JsonProperty
178+
protected Boolean verified;
179+
180+
@JsonProperty
181+
protected Boolean active;
182+
183+
@JsonProperty("test")
184+
protected long testId;
185+
}
186+
}

0 commit comments

Comments
 (0)