Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.

Commit 6aeec1f

Browse files
committed
Add vuln handling
1 parent 63a4255 commit 6aeec1f

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

scb-persistenceproviders/defectdojo-persistenceprovider/src/main/java/io/securecodebox/persistence/DefectDojoService.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package io.securecodebox.persistence;
2020

2121
import io.securecodebox.persistence.models.*;
22+
23+
import org.camunda.bpm.model.bpmn.instance.ReceiveTask;
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
2426
import org.springframework.beans.factory.annotation.Value;
@@ -45,6 +47,7 @@
4547
import java.util.LinkedList;
4648
import java.util.List;
4749
import java.util.Optional;
50+
import java.util.Iterator;
4851

4952
@Component
5053
@ConditionalOnProperty(name = "securecodebox.persistence.defectdojo.enabled", havingValue = "true")
@@ -68,6 +71,8 @@ private String currentDate() {
6871

6972
private static final Logger LOG = LoggerFactory.getLogger(DefectDojoService.class);
7073

74+
private LinkedMultiValueMap options;
75+
7176
private HttpHeaders getHeaders(){
7277
HttpHeaders headers = new HttpHeaders();
7378
headers.set("Authorization", "Token " + defectDojoApiKey);
@@ -256,6 +261,10 @@ public ImportScanResponse createFindingsForEngagementName(String engagementName,
256261
return createFindingsForEngagementName(engagementName, rawResults, defectDojoScanName, productId, lead, engagementPayload, testName);
257262
}
258263

264+
private Optional<Long> getEngagementIdByEngagementName(String engagementName, String productName){
265+
long productId = retrieveProductId(productName);
266+
return getEngagementIdByEngagementName(engagementName, productId, 0L);
267+
}
259268
private Optional<Long> getEngagementIdByEngagementName(String engagementName, long productId){
260269
return getEngagementIdByEngagementName(engagementName, productId, 0L);
261270
}
@@ -355,11 +364,64 @@ public void deleteEnageament(long engagementId){
355364
String uri = defectDojoUrl + "/api/v2/engagements/" + engagementId + "/?id=" + engagementId;
356365
HttpEntity request = new HttpEntity(getHeaders());
357366
try {
358-
ResponseEntity<DefectDojoResponse> response = restTemplate.exchange(uri, HttpMethod.DELETE, request, DefectDojoResponse.class);
367+
ResponseEntity<DefectDojoResponse> response = restTemplate.exchange(uri, HttpMethod.GET, request, DefectDojoResponse.class);
359368
} catch (HttpClientErrorException e) {
360369
LOG.warn("Failed to delete engagment {}, engagementId: " + engagementId, e);
361370
LOG.warn("Failure response body. {}", e.getResponseBodyAsString());
362371
throw new DefectDojoPersistenceException("Failed to delete product", e);
363372
}
373+
}
374+
375+
/* options is created as follows:
376+
MultiValueMap<String, String> mvn = new LinkedMultiValueMap<>();
377+
mvn.add("engagement", Long.toString(engagementId));
378+
*/
379+
private List<Finding> getCurrentFindings(long engagementId, LinkedMultiValueMap<String, String> options){
380+
RestTemplate restTemplate = new RestTemplate();
381+
382+
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(defectDojoUrl + "/api/v2/findings/")
383+
.queryParam("active", "true")
384+
.queryParam("false_p", "false")
385+
.queryParam("duplicate", "false")
386+
.queryParam("test__engagement", Long.toString(engagementId));
387+
388+
if(options != null) {
389+
builder = prepareParameters(options, builder);
390+
}
391+
392+
HttpEntity request = new HttpEntity(getHeaders());
393+
try {
394+
ResponseEntity<DefectDojoResponse<Finding>> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, new ParameterizedTypeReference<DefectDojoResponse<Finding>>(){});
395+
List<Finding> findings = new LinkedList<Finding>();
396+
for(Finding finding : response.getBody().getResults()){
397+
findings.add(finding);
398+
}
399+
return findings;
400+
} catch (HttpClientErrorException e) {
401+
LOG.warn("Failed to get findings {}, engagementId: " + engagementId, e);
402+
LOG.warn("Failure response body. {}", e.getResponseBodyAsString());
403+
throw new DefectDojoPersistenceException("Failed to get findings", e);
404+
}
405+
}
406+
private UriComponentsBuilder prepareParameters(LinkedMultiValueMap<String, String> queryParameters, UriComponentsBuilder builder) {
407+
Iterator<String> it = queryParameters.keySet().iterator();
408+
409+
while(it.hasNext()){
410+
String theKey = (String)it.next();
411+
builder.replaceQueryParam(theKey, queryParameters.getFirst(theKey));
412+
}
413+
return builder;
364414
}
415+
416+
public List<Finding> receiveNonHandeldFindings(String productName, String engagementName, String minimumServerity, LinkedMultiValueMap<String, String> options){
417+
Long engagementId = getEngagementIdByEngagementName(engagementName, productName).orElse(0L);
418+
//getCurrentFindings
419+
List<Finding> findings = new LinkedList<Finding>();
420+
for (String serverity : Finding.getServeritiesAndHigherServerities(minimumServerity)) {
421+
LinkedMultiValueMap<String, String> optionTemp = options.clone();
422+
optionTemp.add("serverity", serverity);
423+
findings.addAll(getCurrentFindings(engagementId, optionTemp));
424+
}
425+
return findings;
426+
}
365427
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
*
3+
* SecureCodeBox (SCB)
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
* /
17+
*/
18+
package io.securecodebox.persistence.models;
19+
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import lombok.Data;
22+
23+
import java.util.Arrays;
24+
import java.util.Collections;
25+
import java.util.LinkedList;
26+
import java.util.List;
27+
28+
29+
@Data
30+
public class Finding {
31+
@JsonProperty
32+
protected long id;
33+
34+
@JsonProperty
35+
protected String title;
36+
37+
@JsonProperty
38+
protected long cwe;
39+
40+
@JsonProperty
41+
protected String cve;
42+
@JsonProperty
43+
44+
protected String severity;
45+
46+
@JsonProperty
47+
protected String description;
48+
49+
@JsonProperty
50+
protected boolean active = true;
51+
52+
@JsonProperty
53+
protected boolean verified = true;
54+
55+
@JsonProperty("false_p")
56+
protected boolean falsePostive = false;
57+
58+
@JsonProperty
59+
protected boolean duplicate = false;
60+
61+
@JsonProperty("is_Mitigated")
62+
protected boolean isMitigated = false;
63+
64+
enum FindingSeverities {
65+
66+
}
67+
public static final LinkedList<String> findingServerities = new LinkedList<String>(){{
68+
add("Low");
69+
add("Medium");
70+
add("High");
71+
add("Critical");
72+
}};
73+
public static LinkedList<String> getServeritiesAndHigherServerities(String minimumServerity){
74+
LinkedList<String> severities = new LinkedList<String>();
75+
boolean minimumFound = false;
76+
for(String serverity : findingServerities) {
77+
if(minimumFound || minimumServerity.equals(serverity)) {
78+
minimumFound = true;
79+
severities.add(serverity);
80+
}
81+
}
82+
83+
return severities;
84+
}
85+
}

0 commit comments

Comments
 (0)