Skip to content

Commit cfecb06

Browse files
authored
Merge pull request #437 from secureCodeBox/defectdojo_generic_parser_integration
2 parents a32c6e2 + 20bf7cf commit cfecb06

7 files changed

Lines changed: 36 additions & 20 deletions

File tree

‎hooks/persistence-defectdojo/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repositories {
1818
dependencies {
1919
implementation 'io.kubernetes:client-java:12.0.0'
2020

21-
implementation 'io.securecodebox:defectdojo-client:0.0.12-SNAPSHOT'
21+
implementation 'io.securecodebox:defectdojo-client:0.0.16-SNAPSHOT'
2222

2323
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
2424
implementation 'org.slf4j:slf4j-log4j12:1.7.30'

‎hooks/persistence-defectdojo/src/main/java/io/securecodebox/persistence/DefectDojoPersistenceProvider.java‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919

2020
import io.securecodebox.persistence.config.PersistenceProviderConfig;
2121
import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
22+
import io.securecodebox.persistence.defectdojo.models.ScanFile;
2223
import io.securecodebox.persistence.defectdojo.service.EndpointService;
2324
import io.securecodebox.persistence.mapping.DefectDojoFindingToSecureCodeBoxMapper;
2425
import io.securecodebox.persistence.models.Scan;
2526
import io.securecodebox.persistence.service.KubernetesService;
2627
import io.securecodebox.persistence.service.S3Service;
2728
import io.securecodebox.persistence.strategies.VersionedEngagementsStrategy;
29+
import io.securecodebox.persistence.util.ScanNameMapping;
30+
import org.apache.commons.io.FilenameUtils;
2831
import org.slf4j.Logger;
2932
import org.slf4j.LoggerFactory;
30-
33+
import java.net.URL;
3134
import java.util.stream.Collectors;
3235

3336
public class DefectDojoPersistenceProvider {
@@ -46,17 +49,27 @@ public static void main(String[] args) throws Exception {
4649
scan.validate();
4750

4851
var config = DefectDojoConfig.fromEnv();
49-
50-
LOG.info("Downloading Scan Report (RawResults)");
51-
var rawResults = s3Service.downloadRawResults(persistenceProviderConfig.getRawResultDownloadUrl());
52-
LOG.info("Finished Downloading Scan Report (RawResults)");
53-
LOG.debug("RawResults: {}", rawResults);
52+
LOG.info("Downloading Scan Result");
53+
String downloadUrl;
54+
String scanType = scan.getSpec().getScanType();
55+
ScanNameMapping scanNameMapping = ScanNameMapping.bySecureCodeBoxScanType(scanType);
56+
if (scanNameMapping == ScanNameMapping.GENERIC) {
57+
LOG.debug("No explicit Parser specified for ScanType {}, using Findings JSON Scan Result", scanType);
58+
downloadUrl = persistenceProviderConfig.getFindingDownloadUrl();
59+
} else {
60+
LOG.debug("Explicit Parser is specified for ScanType {}, using Raw Scan Result", scanNameMapping.scanType);
61+
downloadUrl = persistenceProviderConfig.getRawResultDownloadUrl();
62+
}
63+
var scanResults = s3Service.downloadFile(downloadUrl);
64+
LOG.info("Finished Downloading Scan Result");
65+
LOG.debug("Scan Result: {}", scanResults);
5466

5567
LOG.info("Uploading Findings to DefectDojo at: {}", config.getUrl());
5668

5769
var defectdojoImportStrategy = new VersionedEngagementsStrategy();
5870
defectdojoImportStrategy.init(config);
59-
var defectDojoFindings = defectdojoImportStrategy.run(scan, rawResults);
71+
var scanResultFile = new ScanFile(scanResults, FilenameUtils.getName(new URL(downloadUrl).getPath()));
72+
var defectDojoFindings = defectdojoImportStrategy.run(scan, scanResultFile);
6073

6174
LOG.info("Identified total Number of findings in DefectDojo: {}", defectDojoFindings.size());
6275

‎hooks/persistence-defectdojo/src/main/java/io/securecodebox/persistence/service/S3Service.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public void overwriteFindings(String url, List<Finding> findings) throws IOExcep
2929
HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.discarding());
3030
}
3131

32-
public String downloadRawResults(String rawResultDownloadUrl) throws IOException, InterruptedException {
32+
public String downloadFile(String downloadUrl) throws IOException, InterruptedException {
3333
var request = java.net.http.HttpRequest
3434
.newBuilder()
35-
.uri(URI.create(rawResultDownloadUrl))
35+
.uri(URI.create(downloadUrl))
3636
.build();
3737

3838
var response = HttpClient.newHttpClient()

‎hooks/persistence-defectdojo/src/main/java/io/securecodebox/persistence/strategies/Strategy.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
2121
import io.securecodebox.persistence.defectdojo.models.Finding;
22+
import io.securecodebox.persistence.defectdojo.models.ScanFile;
2223
import io.securecodebox.persistence.models.Scan;
2324

2425
import java.util.List;
@@ -29,5 +30,5 @@
2930
public interface Strategy {
3031
void init(DefectDojoConfig defectDojoConfig);
3132

32-
List<Finding> run(Scan scan, String rawResults) throws Exception;
33+
List<Finding> run(Scan scan, ScanFile scanResultFile) throws Exception;
3334
}

‎hooks/persistence-defectdojo/src/main/java/io/securecodebox/persistence/strategies/VersionedEngagementsStrategy.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void init(DefectDojoConfig defectDojoConfig) {
7979
}
8080

8181
@Override
82-
public List<Finding> run(Scan scan, String rawResults) throws Exception {
82+
public List<Finding> run(Scan scan, ScanFile scanResultFile) throws Exception {
8383
LOG.debug("Getting DefectDojo User Id");
8484
var userId = userService.searchUnique(User.builder().username(this.config.getUsername()).build())
8585
.orElseThrow(() -> new DefectDojoPersistenceException("Failed to find user with name: '" + this.config.getUsername() + "'"))
@@ -100,9 +100,9 @@ public List<Finding> run(Scan scan, String rawResults) throws Exception {
100100

101101
ScanType scanType = ScanNameMapping.bySecureCodeBoxScanType(scan.getSpec().getScanType()).scanType;
102102
TestType testType = testTypeService.searchUnique(TestType.builder().name(scanType.getTestType()).build()).orElseThrow(() -> new DefectDojoPersistenceException("Could not find test type '" + scanType.getTestType() + "' in DefectDojo API. DefectDojo might be running in an unsupported version."));
103-
103+
104104
importScanService.reimportScan(
105-
rawResults,
105+
scanResultFile,
106106
testId,
107107
userId,
108108
this.descriptionGenerator.currentDate(),

‎hooks/persistence-defectdojo/src/main/java/io/securecodebox/persistence/util/ScanNameMapping.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121
import lombok.NonNull;
2222

2323
public enum ScanNameMapping {
24-
NMAP("nmap", ScanType.NMAP_SCAN),
24+
NMAP("nmap", ScanType.NMAP_XML_SCAN),
2525
ZAP_BASELINE("zap-baseline", ScanType.ZAP_SCAN),
2626
ZAP_API_SCAN("zap-api-scan", ScanType.ZAP_SCAN),
2727
ZAP_FULL_SCAN("zap-full-scan", ScanType.ZAP_SCAN),
28-
SSLYZE("sslyze", ScanType.SS_LYZE_3_SCAN_JSON),
28+
SSLYZE("sslyze", ScanType.SSLYZE_3_JSON_SCAN),
2929
TRIVY("trivy", ScanType.TRIVY_SCAN),
3030
GITLEAKS("gitleaks", ScanType.GITLEAKS_SCAN),
31-
// WPSCAN("wpscan", ScanType.WPSCAN),
32-
// NIKTO("nikto", ScanType.NIKTO_SCAN),
33-
// SSH("ssh-scan, ScanType.?),
31+
NIKTO("nikto", ScanType.NIKTO_SCAN),
32+
GENERIC(null, ScanType.SECURECODEBOX_FINDINGS_IMPORT)
3433
;
3534

3635
/**
@@ -58,5 +57,7 @@ public static ScanNameMapping bySecureCodeBoxScanType(@NonNull String scanType)
5857
}
5958

6059
throw new IllegalArgumentException("No Mapping found for ScanType '" + scanType + "'");
60+
// use this as soon as generic parser is released (in DD or this Hook)
61+
// return ScanNameMapping.GENERIC;
6162
}
6263
}

‎hooks/persistence-defectdojo/src/test/java/io/securecodebox/persistence/strategies/VersionedEngagementsStrategyTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.securecodebox.models.V1ScanSpec;
2222
import io.securecodebox.models.V1ScanStatus;
2323
import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
24+
import io.securecodebox.persistence.defectdojo.models.ScanFile;
2425
import io.securecodebox.persistence.defectdojo.models.User;
2526
import io.securecodebox.persistence.defectdojo.service.*;
2627
import io.securecodebox.persistence.exceptions.DefectDojoPersistenceException;
@@ -87,7 +88,7 @@ void requiresUserToBeFound() throws Exception {
8788
when(userService.searchUnique(any(User.class))).thenReturn(Optional.empty());
8889

8990
Assertions.assertThrows(DefectDojoPersistenceException.class, () -> {
90-
versionedEngagementsStrategy.run(scan, "<!-- Nmap Report -->");
91+
versionedEngagementsStrategy.run(scan, new ScanFile("nmap.xml","<!-- Nmap Report -->"));
9192
});
9293
}
9394
}

0 commit comments

Comments
 (0)