Skip to content
2 changes: 1 addition & 1 deletion hooks/persistence-defectdojo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {
dependencies {
implementation 'io.kubernetes:client-java:12.0.0'

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

implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
implementation 'org.slf4j:slf4j-log4j12:1.7.30'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

import io.securecodebox.persistence.config.PersistenceProviderConfig;
import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
import io.securecodebox.persistence.defectdojo.models.ScanFile;
import io.securecodebox.persistence.defectdojo.service.EndpointService;
import io.securecodebox.persistence.mapping.DefectDojoFindingToSecureCodeBoxMapper;
import io.securecodebox.persistence.models.Scan;
import io.securecodebox.persistence.service.KubernetesService;
import io.securecodebox.persistence.service.S3Service;
import io.securecodebox.persistence.strategies.VersionedEngagementsStrategy;
import io.securecodebox.persistence.util.ScanNameMapping;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.stream.Collectors;

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

var config = DefectDojoConfig.fromEnv();

LOG.info("Downloading Scan Report (RawResults)");
var rawResults = s3Service.downloadRawResults(persistenceProviderConfig.getRawResultDownloadUrl());
LOG.info("Finished Downloading Scan Report (RawResults)");
LOG.debug("RawResults: {}", rawResults);
LOG.info("Downloading Scan Result");
String downloadUrl;
String scanType = scan.getSpec().getScanType();
ScanNameMapping scanNameMapping = ScanNameMapping.bySecureCodeBoxScanType(scanType);
if (scanNameMapping == ScanNameMapping.GENERIC) {
LOG.debug("No explicit Parser specified for ScanType {}, using Findings JSON Scan Result", scanType);
downloadUrl = persistenceProviderConfig.getFindingDownloadUrl();
} else {
LOG.debug("Explicit Parser is specified for ScanType {}, using Raw Scan Result", scanNameMapping.scanType);
downloadUrl = persistenceProviderConfig.getRawResultDownloadUrl();
}
var scanResults = s3Service.downloadFile(downloadUrl);
LOG.info("Finished Downloading Scan Result");
LOG.debug("Scan Result: {}", scanResults);

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

var defectdojoImportStrategy = new VersionedEngagementsStrategy();
defectdojoImportStrategy.init(config);
var defectDojoFindings = defectdojoImportStrategy.run(scan, rawResults);
var scanResultFile = new ScanFile(scanResults, FilenameUtils.getName(new URL(downloadUrl).getPath()));
var defectDojoFindings = defectdojoImportStrategy.run(scan, scanResultFile);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public void overwriteFindings(String url, List<Finding> findings) throws IOExcep
HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.discarding());
}

public String downloadRawResults(String rawResultDownloadUrl) throws IOException, InterruptedException {
public String downloadFile(String downloadUrl) throws IOException, InterruptedException {
var request = java.net.http.HttpRequest
.newBuilder()
.uri(URI.create(rawResultDownloadUrl))
.uri(URI.create(downloadUrl))
.build();

var response = HttpClient.newHttpClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
import io.securecodebox.persistence.defectdojo.models.Finding;
import io.securecodebox.persistence.defectdojo.models.ScanFile;
import io.securecodebox.persistence.models.Scan;

import java.util.List;
Expand All @@ -29,5 +30,5 @@
public interface Strategy {
void init(DefectDojoConfig defectDojoConfig);

List<Finding> run(Scan scan, String rawResults) throws Exception;
List<Finding> run(Scan scan, ScanFile scanResultFile) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void init(DefectDojoConfig defectDojoConfig) {
}

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

ScanType scanType = ScanNameMapping.bySecureCodeBoxScanType(scan.getSpec().getScanType()).scanType;
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."));

importScanService.reimportScan(
rawResults,
scanResultFile,
testId,
userId,
this.descriptionGenerator.currentDate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
import lombok.NonNull;

public enum ScanNameMapping {
NMAP("nmap", ScanType.NMAP_SCAN),
NMAP("nmap", ScanType.NMAP_XML_SCAN),
ZAP_BASELINE("zap-baseline", ScanType.ZAP_SCAN),
ZAP_API_SCAN("zap-api-scan", ScanType.ZAP_SCAN),
ZAP_FULL_SCAN("zap-full-scan", ScanType.ZAP_SCAN),
SSLYZE("sslyze", ScanType.SS_LYZE_3_SCAN_JSON),
SSLYZE("sslyze", ScanType.SSLYZE_3_JSON_SCAN),
TRIVY("trivy", ScanType.TRIVY_SCAN),
GITLEAKS("gitleaks", ScanType.GITLEAKS_SCAN),
// WPSCAN("wpscan", ScanType.WPSCAN),
// NIKTO("nikto", ScanType.NIKTO_SCAN),
// SSH("ssh-scan, ScanType.?),
NIKTO("nikto", ScanType.NIKTO_SCAN),
GENERIC(null, ScanType.SECURECODEBOX_FINDINGS_IMPORT)
;

/**
Expand Down Expand Up @@ -58,5 +57,7 @@ public static ScanNameMapping bySecureCodeBoxScanType(@NonNull String scanType)
}

throw new IllegalArgumentException("No Mapping found for ScanType '" + scanType + "'");
// use this as soon as generic parser is released (in DD or this Hook)
// return ScanNameMapping.GENERIC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.securecodebox.models.V1ScanSpec;
import io.securecodebox.models.V1ScanStatus;
import io.securecodebox.persistence.defectdojo.config.DefectDojoConfig;
import io.securecodebox.persistence.defectdojo.models.ScanFile;
import io.securecodebox.persistence.defectdojo.models.User;
import io.securecodebox.persistence.defectdojo.service.*;
import io.securecodebox.persistence.exceptions.DefectDojoPersistenceException;
Expand Down Expand Up @@ -87,7 +88,7 @@ void requiresUserToBeFound() throws Exception {
when(userService.searchUnique(any(User.class))).thenReturn(Optional.empty());

Assertions.assertThrows(DefectDojoPersistenceException.class, () -> {
versionedEngagementsStrategy.run(scan, "<!-- Nmap Report -->");
versionedEngagementsStrategy.run(scan, new ScanFile("nmap.xml","<!-- Nmap Report -->"));
});
}
}