-
Notifications
You must be signed in to change notification settings - Fork 23
/
CheckovResultConverter.java
74 lines (60 loc) · 3 KB
/
CheckovResultConverter.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
package io.codety.scanner.analyzer.checkov;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.codety.scanner.analyzer.checkov.dto.*;
import io.codety.scanner.reporter.dto.CodeAnalysisIssueDto;
import io.codety.scanner.util.CodetyConsoleLogger;
import io.codety.scanner.util.JsonFactoryUtil;
import java.util.ArrayList;
import java.util.List;
public class CheckovResultConverter {
public static List<CodeAnalysisIssueDto> convertResult(String errorOutput) {
ArrayList<CodeAnalysisIssueDto> codeAnalysisIssueDtos = new ArrayList<>();
CheckovRoot[] checkovResultArray = new CheckovRoot[1];
try {
if(errorOutput.trim().startsWith("{")){
CheckovRoot checkovRoot = JsonFactoryUtil.objectMapper.readValue(errorOutput, CheckovRoot.class);
checkovResultArray[0] = checkovRoot;
}else {
checkovResultArray = JsonFactoryUtil.objectMapper.readValue(errorOutput, checkovResultArray.getClass());
}
for(CheckovRoot checkovRoot : checkovResultArray){
CheckovResults results = checkovRoot.getResults();
if(results == null){
continue;
}
ArrayList<CheckovFailedCheck> failedChecks = results.getFailed_checks();
for(CheckovFailedCheck failedCheck : failedChecks){
String externalRuleId = failedCheck.getCheck_id();
String shortDescription = failedCheck.getCheck_name();
ArrayList<Integer> fileLineRange = failedCheck.getFile_line_range();
CodeAnalysisIssueDto issueDto = new CodeAnalysisIssueDto();
if(fileLineRange!=null && fileLineRange.size() > 0) {
if(fileLineRange.size()>=1){
issueDto.setStartLineNumber(fileLineRange.get(0));
}
if(fileLineRange.size()>=2){
issueDto.setStartLineNumber(fileLineRange.get(1));
}
}
issueDto.setIssueCode(externalRuleId);
issueDto.setIssueCategory("security");
issueDto.setDescription(shortDescription);
issueDto.setPriority(4);
String filePath = failedCheck.getFile_path();
if(filePath.startsWith("/")){
filePath = filePath.substring(1);
}
issueDto.setFilePath(filePath);
String resource = failedCheck.getResource();
if(resource!=null && !resource.isEmpty()) {
issueDto.setPackagePath(resource);
}
codeAnalysisIssueDtos.add(issueDto);
}
}
} catch (Exception e) {
CodetyConsoleLogger.debug("Failed to deserialize checkov json result");
}
return codeAnalysisIssueDtos;
}
}