-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDataProvider.java
More file actions
861 lines (694 loc) · 26.6 KB
/
Copy pathDataProvider.java
File metadata and controls
861 lines (694 loc) · 26.6 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
package com.checkmarx.eclipse.views;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.eclipse.jgit.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.checkmarx.ast.codebashing.CodeBashing;
import com.checkmarx.ast.learnMore.LearnMore;
import com.checkmarx.ast.predicate.CustomState;
import com.checkmarx.ast.predicate.Predicate;
import com.checkmarx.ast.project.Project;
import com.checkmarx.ast.results.Results;
import com.checkmarx.ast.results.result.Node;
import com.checkmarx.ast.results.result.Result;
import com.checkmarx.ast.scan.Scan;
import com.checkmarx.ast.wrapper.CxConfig;
import com.checkmarx.ast.wrapper.CxException;
import com.checkmarx.ast.wrapper.CxWrapper;
import com.checkmarx.eclipse.properties.Preferences;
import com.checkmarx.eclipse.runner.Authenticator;
import com.checkmarx.eclipse.utils.CxLogger;
import com.checkmarx.eclipse.utils.PluginConstants;
import com.checkmarx.eclipse.utils.PluginUtils;
import com.checkmarx.eclipse.views.filters.FilterState;
public class DataProvider {
private static final List<String> SEVERITY_ORDER = Arrays.asList("CRITICAL", "HIGH", "MEDIUM", "LOW", "INFO");
private static final String LIMIT_FILTER="limit=10000";
private static final String NAME_FILTER="name=";
private static final String FILTER_SCANS_FOR_PROJECT = "project-id=%s,branch=%s,limit=10000,statuses=Completed";
private static final String SAST_TREE_NAME = "SAST (%d)";
private static final String SCA_TREE_NAME = "SCA (%d)";
private static final String KICS_TREE_NAME = "KICS (%d)";
private static final String RESULTS_TREE_NAME = "%s (%d Issues)";
private static final String ECLIPSE_AGENT = "Eclipse";
private static DataProvider _dataProvider = null;
public static final AtomicBoolean abort = new AtomicBoolean(false);
private Results currentResults;
private String currentScanId;
private String projectId;
private List<DisplayModel> currentResultsTransformed;
private List<String> platformStates = new ArrayList<>();
/**
* Singleton data provider instance
*
* @return
*/
public static final DataProvider getInstance() {
if(_dataProvider == null) {
_dataProvider = new DataProvider();
}
return _dataProvider;
}
public String getCurrentScanId() {
return currentScanId;
}
public void setCurrentScanId(String currentScanId) {
this.currentScanId = currentScanId;
}
public Results getCurrentResults() {
return currentResults;
}
public void setCurrentResults(Results currentResults) {
this.currentResults = currentResults;
}
/**
* Get One projects
*
* @return
* @throws Exception
*/
public List<Project> getProjects() throws Exception {
List<Project> projectList = new ArrayList<Project>();
CxWrapper cxWrapper = authenticateWithAST();
if (cxWrapper != null) {
try {
projectList = cxWrapper.projectList(LIMIT_FILTER);
} catch (IOException | InterruptedException | CxException e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_PROJECTS, e.getMessage()), e);
}
}
return projectList;
}
/**
* Fetch a single project directly by its ID using the project show command.
* Returns null if the project cannot be retrieved.
*/
public Project getProjectById(String projectId) {
try {
CxWrapper cxWrapper = getWrapper();
if (cxWrapper != null && projectId != null && !projectId.isEmpty()) {
return cxWrapper.projectShow(UUID.fromString(projectId));
}
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_PROJECTS, e.getMessage()), e);
}
return null;
}
/**
* Get One projects filtered by name
*
* @return
* @throws Exception
*/
public List<Project> getProjects(String projectName) throws Exception {
List<Project> projectList = new ArrayList<Project>();
CxWrapper cxWrapper = authenticateWithAST();
String filterProject = NAME_FILTER+projectName;
if (cxWrapper != null) {
try {
projectList = cxWrapper.projectList(filterProject);
} catch (IOException | InterruptedException | CxException e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_PROJECTS, e.getMessage()), e);
}
}
return projectList;
}
/**
* Get the codeBashing link
* @throws Exception
*/
public CodeBashing getCodeBashingLink(String cwe, String language, String queryName) throws CxException, Exception {
CxWrapper cxWrapper = getWrapper();
return cxWrapper.codeBashingList(cwe, language, queryName).get(0);
}
/**
* Get branches for a specific project
*
* @param projectId
* @return
*/
public List<String> getBranchesForProject(String projectId) {
this.projectId = projectId;
List<String> branchList = new ArrayList<String>();
try {
CxWrapper cxWrapper = getWrapper();
if(!StringUtils.isEmptyOrNull(projectId)) {
branchList = cxWrapper.projectBranches(UUID.fromString(projectId), PluginConstants.EMPTY_STRING);
}
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_BRANCHES, projectId, e.getMessage()), e);
}
return branchList;
}
/**
* Get scans for a specific project based on a provided branch
*
* @param branch
* @return
*/
public List<Scan> getScansForProject(String branch) {
List<Scan> scanList = new ArrayList<>();
try {
String filter = String.format(FILTER_SCANS_FOR_PROJECT, projectId, branch);
CxWrapper cxWrapper = getWrapper();
scanList = cxWrapper.scanList(filter);
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_SCANS, projectId, branch, e.getMessage()), e);
}
return scanList;
}
/**
* Authenticate to One with current credentials
* @throws Exception
*/
private static CxWrapper authenticateWithAST() throws Exception {
CxWrapper cxWrapper = null;
try {
cxWrapper = getWrapper();
String validationResult = cxWrapper.authValidate();
CxLogger.info(String.format(PluginConstants.INFO_AUTHENTICATION_STATUS, validationResult));
} catch (CxException e) {
CxLogger.error(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, e.getMessage()), e);
throw new Exception(e);
}
return cxWrapper;
}
/**
* Get results for a specific scan id
*
* @param scanId
* @return
*/
public List<DisplayModel> getResultsForScanId(String scanId) {
abort.set(false);
Results scanResults = null;
setCurrentScanId(scanId);
// [AST-92100] Fetch all platform states before fetching results
try {
platformStates = getAllStatesFromPlatform();
} catch (Exception e) {
CxLogger.warning("Failed to fetch all platform states on scan load: " + e.getMessage());
}
try {
CxLogger.info(String.format(PluginConstants.INFO_FETCHING_RESULTS, scanId));
CxWrapper cxWrapper = getWrapper();
scanResults = cxWrapper.results(UUID.fromString(scanId), ECLIPSE_AGENT);
setCurrentResults(scanResults);
CxLogger.info(String.format(PluginConstants.INFO_SCAN_RESULTS_COUNT, scanResults.getTotalCount()));
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_RESULTS, scanId, e.getMessage()), e);
return Arrays.asList(PluginUtils.message("Error: " + e.getMessage()));
}
return processResults(scanResults, scanId);
}
/**
* Get scan information for a specific scan id
*
* @param scanId
* @return
* @throws Exception
*/
public Scan getScanInformation(String scanId) throws Exception {
Scan scan = null;
CxWrapper cxWrapper = getWrapper();
try {
CxLogger.info(String.format(PluginConstants.INFO_GETTING_SCAN_INFO, scanId));
scan = cxWrapper.scanShow(UUID.fromString(scanId));
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_SCAN_INFO, e.getMessage()), e);
throw new Exception(e);
}
return scan;
}
// [AST-92100] Get all states for a specific engine type
public List<String> getStatesForEngine(String engineType) {
if ("SAST".equalsIgnoreCase(engineType)) {
return platformStates != null ? platformStates : Collections.emptyList();
} else {
return new ArrayList<>(com.checkmarx.eclipse.enums.State.values().keySet());
}
}
/**
* Process results to be displayed in the tree
*
* @param scanResults
* @param scanId
* @return
*/
private List<DisplayModel> processResults(Results scanResults, String scanId) {
if(scanResults == null || scanResults.getResults() == null || scanResults.getResults().isEmpty()) {
return Collections.emptyList();
}
List<Result> resultsList = scanResults.getResults();
// Add Checkmarx vulnerabilities to Problems View
PluginUtils.addVulnerabilitiesToProblemsView(resultsList);
// transform all the results at once to avoid multiple transformation steps
currentResultsTransformed = resultsList.stream().map(resultItem -> transform(resultItem)).collect(Collectors.toList());
// Divide all the results by scanner type
Map<String, List<DisplayModel>> filteredResultsByScannerType = filterResultsByScannerType(currentResultsTransformed);
//filter the results by enabled state
filterStates(filteredResultsByScannerType);
// build results based on selected filters
return buildResults(scanId, filteredResultsByScannerType);
}
/**
* Sort results as they may have changed due to a triage update
*
* @return
*/
public List<DisplayModel> sortResults(){
// Divide all the results by scanner type
Map<String, List<DisplayModel>> filteredResultsByScannerType = filterResultsByScannerType(currentResultsTransformed);
// filter based on filter states
filterStates(filteredResultsByScannerType);
// build results based on selected filters
return buildResults(getCurrentScanId(), filteredResultsByScannerType);
}
/**
* Build results to be displayed in the tree
*
* @param scanId
* @param filteredResultsByScannerType
* @return
*/
private List<DisplayModel> buildResults(String scanId, Map<String, List<DisplayModel>> filteredResultsByScannerType){
// Filter results by selected severities
filteredResultsByScannerType.entrySet().stream().forEach(entry -> entry.getValue().removeIf(result -> !FilterState.isSeverityEnabled(result.getSeverity())));
if(FilterState.groupBySeverity) {
groupResultsBySeverity(filteredResultsByScannerType);
}
if(FilterState.groupByStateName) {
groupResultsByStateName(filteredResultsByScannerType);
}
if(FilterState.groupByQueryName) {
groupResultsByQueryName(filteredResultsByScannerType);
}
return addResults(scanId, filteredResultsByScannerType);
}
private void filterStates(Map<String, List<DisplayModel>> filteredResultsByScannerType) {
filteredResultsByScannerType.entrySet().stream().forEach(entry ->
{
entry.getValue().removeIf(x -> !FilterState.isFilterStateEnabled(x.getState().trim()));
});
}
/**
* Evaluates if each engine has results and adds it to the final map
*
* @param scanId
* @param filteredResultsByScannerType
* @return
*/
private List<DisplayModel> addResults(String scanId, Map<String, List<DisplayModel>> filteredResultsByScannerType) {
List<DisplayModel> returnList = new ArrayList<>();
List<DisplayModel> results = new ArrayList<>();
boolean constainsSASTResults = filteredResultsByScannerType.containsKey(PluginConstants.SAST) && filteredResultsByScannerType.get(PluginConstants.SAST).size() > 0;
List<DisplayModel> sastResults = constainsSASTResults ? filteredResultsByScannerType.get(PluginConstants.SAST) : Collections.emptyList();
int sastCount = constainsSASTResults ? getParentCounter(sastResults) : 0;
boolean constainsSCAResults = filteredResultsByScannerType.containsKey(PluginConstants.SCA_DEPENDENCY) && filteredResultsByScannerType.get(PluginConstants.SCA_DEPENDENCY).size() > 0;
List<DisplayModel> scaResults = constainsSCAResults ? filteredResultsByScannerType.get(PluginConstants.SCA_DEPENDENCY) : Collections.emptyList();
int scaCount = constainsSCAResults ? getParentCounter(scaResults) : 0;
boolean constainsKICKSResults = filteredResultsByScannerType.containsKey(PluginConstants.KICS_INFRASTRUCTURE) && filteredResultsByScannerType.get(PluginConstants.KICS_INFRASTRUCTURE).size() > 0;
List<DisplayModel> kicsResults = constainsKICKSResults ? filteredResultsByScannerType.get(PluginConstants.KICS_INFRASTRUCTURE) : Collections.emptyList();
int kicsCount = constainsKICKSResults ? getParentCounter(kicsResults) : 0;
if (sastCount > 0) {
DisplayModel sastModel = new DisplayModel.DisplayModelBuilder(String.format(SAST_TREE_NAME, sastCount)).setChildren(sastResults).build();
results.add(sastModel);
}
if (scaCount > 0) {
DisplayModel scaModel = new DisplayModel.DisplayModelBuilder(String.format(SCA_TREE_NAME, scaCount)).setChildren(scaResults).build();
results.add(scaModel);
}
if (kicsCount > 0) {
DisplayModel kicsModel = new DisplayModel.DisplayModelBuilder(String.format(KICS_TREE_NAME, kicsCount)).setChildren(kicsResults).build();
results.add(kicsModel);
}
int totalCount = sastCount + scaCount + kicsCount;
DisplayModel projectModel = new DisplayModel.DisplayModelBuilder(String.format(RESULTS_TREE_NAME, scanId, totalCount)).setChildren(results).build();
returnList.add(projectModel);
return returnList;
}
/**
* Creates a clean Result object with decoded HTML entities
*
* @param resultItem Original result object
* @return New Result object with cleaned values
*/
private Result createCleanResult(Result resultItem) {
String cleanDescription = resultItem.getDescription() != null ?
cleanHtmlEntities(resultItem.getDescription()) : null;
String cleanDescriptionHTML = resultItem.getDescriptionHTML() != null ?
cleanHtmlEntities(resultItem.getDescriptionHTML()) : null;
return new Result(
resultItem.getType(),
resultItem.getLabel(),
resultItem.getId(),
resultItem.getSimilarityId(),
resultItem.getStatus(),
resultItem.getState(),
resultItem.getSeverity(),
resultItem.getCreated(),
resultItem.getFirstFoundAt(),
resultItem.getFoundAt(),
resultItem.getFirstScan(),
resultItem.getFirstScanId(),
resultItem.getPublishedAt(),
resultItem.getRecommendations(),
cleanDescription,
cleanDescriptionHTML,
resultItem.getData(),
resultItem.getComments(),
resultItem.getVulnerabilityDetails(),
resultItem.getScaType()
);
}
/**
* Helper method to clean HTML entities from text
*
* @param input String containing HTML entities
* @return Cleaned string with decoded HTML entities
*/
private String cleanHtmlEntities(String input) {
if (input == null) {
return null;
}
return input
.replace(""", "\"")
.replace(""", "\"")
.replace("'", "'")
.replace("#", "#")
.replace("&", "&")
.replace("<", "<")
.replace(">", ">");
}
/**
* Creates a Display Model which represents each result
*
* @param resultItem Result object to transform
* @return DisplayModel representing the result
*/
private DisplayModel transform(Result resultItem) {
List<Node> nodes = Optional.ofNullable(resultItem.getData().getNodes()).orElse(Collections.emptyList());
Result cleanResult = createCleanResult(resultItem);
String queryName = cleanResult.getData().getQueryName() != null ?
cleanResult.getData().getQueryName() :
cleanResult.getSimilarityId();
String displayName = queryName;
if (nodes.size() > 0) {
Node node = nodes.get(0);
displayName += String.format(" (%s:%d)", new File(node.getFileName()).getName(), node.getLine());
}
return new DisplayModel.DisplayModelBuilder(displayName)
.setSeverity(cleanResult.getSeverity())
.setType(cleanResult.getType())
.setResult(cleanResult)
.setSate(cleanResult.getState())
.setQueryName(queryName)
.build();
}
/**
* Group results by scanner type
*
* @param allResultsTransformed
* @return
*/
private Map<String, List<DisplayModel>> filterResultsByScannerType(List<DisplayModel> allResultsTransformed) {
Map<String, List<DisplayModel>> filteredMap = new HashMap<>();
for (DisplayModel transformedResult : allResultsTransformed) {
String scanType = transformedResult.getType();
if (filteredMap.containsKey(scanType)) {
filteredMap.get(scanType).add(transformedResult);
} else {
filteredMap.put(scanType, new ArrayList<>(Arrays.asList(transformedResult)));
}
}
return filteredMap;
}
/**
* Group vulnerabilities by severity
*
* @param filteredResultsByScannerType
*/
private void groupResultsBySeverity(Map<String, List<DisplayModel>> filteredResultsByScannerType) {
filteredResultsByScannerType.entrySet().stream().forEach(entry -> {
Map<String, List<DisplayModel>> mapBySeverity = new LinkedHashMap<>();
String scanner = entry.getKey();
List<DisplayModel> vulnerabilities = entry.getValue();
for (DisplayModel result : vulnerabilities) {
String severityType = result.getSeverity();
if (mapBySeverity.containsKey(severityType)) {
mapBySeverity.get(severityType).add(result);
} else {
mapBySeverity.put(severityType, new ArrayList<>(Arrays.asList(result)));
}
}
Map<String, List<DisplayModel>> sortedMapBySeverity = new LinkedHashMap<>();
SEVERITY_ORDER.forEach(severity -> {
if (mapBySeverity.containsKey(severity)) {
sortedMapBySeverity.put(severity, mapBySeverity.get(severity));
}
});
List<DisplayModel> children = createParentNodeByScanner(mapBySeverity);
filteredResultsByScannerType.put(scanner, children);
});
}
/**
* Group vulnerabilities by query name based on groupBySeverity state
*
* @param results
*/
private void groupResultsByQueryName(Map<String, List<DisplayModel>> results) {
// when group by state is enabled, the query names but be grouped by and created as children to state label
if(FilterState.groupByStateName || FilterState.groupBySeverity) {
results.entrySet().stream().forEach(entry -> entry.getValue().stream().forEach(severity ->
{
// here check if severity is enabled or state is enabled and we need to populate the children based on the state and severity filters selected
// when both the filters are selected
if(FilterState.groupBySeverity && FilterState.groupByStateName) {
if(severity.getChildren() != null && severity.getChildren().size()>0) {
severity.getChildren().stream().forEach( state -> {
state.setChildren(groupByQueryName(state.getChildren()));
});
}
}
// when only one group by filter is selected
else {
severity.setChildren(groupByQueryName(severity.getChildren()));
}
}));
}
else {
results.entrySet().stream().forEach(entry -> results.put(entry.getKey(), groupByQueryName(entry.getValue())));
}
}
/**
* Group vulnerabilities by query name
*
* @param children
* @return
*/
private List<DisplayModel> groupByQueryName(List<DisplayModel> vulnerabilities){
Map<String, List<DisplayModel>> filteredByQueryName = new HashMap<>();
for (DisplayModel vulnerability : vulnerabilities) {
String queryName = vulnerability.getQueryName();
if (filteredByQueryName.containsKey(queryName)) {
filteredByQueryName.get(queryName).add(vulnerability);
} else {
filteredByQueryName.put(queryName, new ArrayList<>(Arrays.asList(vulnerability)));
}
}
return createParentNodeByScanner(filteredByQueryName);
}
private void groupResultsByStateName(Map<String, List<DisplayModel>> results) {
if(FilterState.groupBySeverity) {
results.entrySet().stream().forEach(entry -> entry.getValue().stream().forEach(severity -> severity.setChildren(groupByStateName(severity.getChildren()))));
}else {
results.entrySet().stream().forEach(entry -> results.put(entry.getKey(), groupByStateName(entry.getValue())));
}
}
private List<DisplayModel> groupByStateName(List<DisplayModel> vulnerabilities) {
Map<String, List<DisplayModel>> filteredByStateName = new HashMap<>();
for (DisplayModel vulnerability : vulnerabilities) {
String queryName = vulnerability.getState();
if (filteredByStateName.containsKey(queryName)) {
filteredByStateName.get(queryName).add(vulnerability);
} else {
filteredByStateName.put(queryName, new ArrayList<>(Arrays.asList(vulnerability)));
}
}
return createParentNodeByScanner(filteredByStateName);
}
/**
* Creates parent node for each scanner
*
* @param map
* @return
*/
private List<DisplayModel> createParentNodeByScanner(Map<String, List<DisplayModel>> map){
List<DisplayModel> resultList = new ArrayList<>();
for (Map.Entry<String, List<DisplayModel>> mapEntry : map.entrySet()) {
int childCounter = 0;
List<DisplayModel> listForEachSeverity = mapEntry.getValue();
// When grouped by query name we need to count children
for(DisplayModel dm : listForEachSeverity) {
if(dm.getChildren() != null && dm.getChildren().size() > 0) {
childCounter = childCounter + dm.getChildren().size();
}
}
int parentCounter = childCounter > 0 ? childCounter : listForEachSeverity.size();
DisplayModel parentModel = new DisplayModel.DisplayModelBuilder(mapEntry.getKey() + " (" + parentCounter + ")").setChildren(listForEachSeverity).build();
resultList.add(parentModel);
}
return resultList;
}
/**
* Counts the number of results evaluating if the model has children or not
*
* @param results
* @return
*/
private int getParentCounter(List<DisplayModel> results) {
int counter= 0;
for(DisplayModel dm : results) {
if(dm.getChildren() != null && dm.getChildren().size() > 0) {
counter = counter + Integer.parseInt(dm.getName().substring(dm.getName().indexOf("(") + 1, dm.getName().indexOf(")")));
}
else {
counter = counter + 1;
}
}
return counter;
}
/**
* Create a CxWrapper with current credentials
*
* @return
* @throws Exception
*/
private static CxWrapper getWrapper() throws Exception {
CxWrapper cxWrapper = null;
Logger log = LoggerFactory.getLogger(Authenticator.class.getName());
CxConfig config = CxConfig.builder().apiKey(Preferences.getApiKey()).additionalParameters(Preferences.getAdditionalOptions()).build();
try {
cxWrapper = new CxWrapper(config, log);
} catch (IOException e) {
CxLogger.error(String.format(PluginConstants.ERROR_BUILDING_CX_WRAPPER, e.getMessage()), e);
throw new Exception(e);
}
return cxWrapper;
}
/**
* Check if plugin has results loaded
*
* @return
*/
public boolean containsResults() {
return getCurrentResults() != null && getCurrentResults().getResults() != null && !getCurrentResults().getResults().isEmpty();
}
/*
*
*/
public int getBestFixLocation(UUID scanId, String queryId, List<Node> bflNodes) throws Exception {
CxWrapper cxWrapper = authenticateWithAST();
int bflNode = -1;
if(cxWrapper != null) {
bflNode = cxWrapper.getResultsBfl(scanId, queryId, bflNodes);
}
return bflNode;
}
/**
* Get One Triage details
*
* @return
* @throws Exception
*/
public List<Predicate> getTriageShow(UUID projectID, String similarityID, String scanType) throws Exception {
List<Predicate> triageList = new ArrayList<Predicate>();
CxWrapper cxWrapper = authenticateWithAST();
// TODO: remove this condition when CLI is updated to manage these checks
if(scanType.equals(PluginConstants.KICS_INFRASTRUCTURE)) {
scanType = "kics";
}
if (cxWrapper != null) {
try {
triageList = cxWrapper.triageShow(projectID, similarityID, scanType);
} catch (IOException | InterruptedException | CxException e) {
CxLogger.error(String.format(PluginConstants.ERROR_GETTING_TRIAGE_DETAILS, e.getMessage()), e);
}
}
return triageList;
}
/**
* Update a vulnerability severity or state
*
* @param projectId
* @param similarityId
* @param engineType
* @param state
* @param comment
* @param severity
* @throws Exception
*/
public void triageUpdate(UUID projectId, String similarityId, String engineType, String state, String comment, String severity) throws Exception {
try {
CxWrapper cxWrapper = authenticateWithAST();
if (cxWrapper != null) {
cxWrapper.triageUpdate(projectId, similarityId, engineType, state, comment, severity);
}
} catch (Exception e) {
CxLogger.error(String.format(PluginConstants.ERROR_UPDATING_TRIAGE, e.getMessage()), e);
throw new Exception(e.getMessage());
}
}
public List<LearnMore> learnMore(String queryId) throws Exception {
return authenticateWithAST().learnMore(queryId);
}
public Scan createScan(String sourcePath, String projectName, String branchName) throws IOException, InterruptedException, CxException, Exception {
Map<String, String> scanArguments = new HashMap<>();
scanArguments.put("-s", sourcePath);
scanArguments.put("--project-name", projectName);
scanArguments.put("--branch", branchName);
scanArguments.put("--agent", ECLIPSE_AGENT);
String additionalParameters = "--async --sast-incremental --resubmit";
return authenticateWithAST().scanCreate(scanArguments, additionalParameters);
}
public void cancelScan(String scanId) throws IOException, InterruptedException, CxException, Exception {
authenticateWithAST().scanCancel(scanId);
}
public boolean isScanAllowed() throws CxException, IOException, InterruptedException, Exception {
return authenticateWithAST().ideScansEnabled();
}
/**
* [AST-92100] Fetch ALL platform states (predefined + custom) irrespective of
* vulnerabilities.
*/
private List<String> getAllStatesFromPlatform() throws Exception {
if (currentScanId == null || projectId == null) {
return Collections.emptyList();
}
CxWrapper cxWrapper = authenticateWithAST();
List<String> allStates = new ArrayList<>();
try {
List<CustomState> customStates = cxWrapper.triageGetStates(false);
allStates = customStates.stream().map(CustomState::getName).collect(Collectors.toList());
} catch (Exception e) {
CxLogger.warning("Could not fetch platform states: " + e.getMessage());
}
return allStates;
}
public List<String> getCustomStates() {
return platformStates.stream().filter(state -> !FilterState.PREDEFINED_STATE_SET.contains(state.toUpperCase()))
.collect(Collectors.toList());
}
}