forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_import_reimport.py
1877 lines (1472 loc) · 102 KB
/
test_import_reimport.py
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
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
from django.urls import reverse
from dojo.models import Test_Type, User, Test, Finding
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient
from django.test.client import Client
from django.utils import timezone
from .dojo_test_case import DojoAPITestCase, get_unit_tests_path
from .test_utils import assertTestImportModelsCreated
from django.test import override_settings
# from unittest import skip
import logging
logger = logging.getLogger(__name__)
ENGAGEMENT_NAME_DEFAULT = 'Engagement 1'
PRODUCT_NAME_DEFAULT = 'Product A'
PRODUCT_TYPE_NAME_DEFAULT = 'Type type'
# 0_zap_sample.xml: basic file with 4 out of 5 findings reported, zap4 absent
# 1 active
# 2 active
# 3 active
# 4 absent
# 5 active
# 1_zap_sample_0_and_new_absent: based on 0, but zap1 absent, zap4 reported
# 1 absent
# 2 active
# 3 active
# 4 active
# 5 active
# 2_zap_sample_0_and_new_endpoint: bases on 0: just adding an endpoint to zap1
# 1 active, extra endpoint
# 2 active
# 3 active
# 4 absent
# 5 active
# 3_zap_sampl_0_and_different_severities
# 1 active
# 2 active sev medium
# 3 active
# 4 absent
# 5 active sev medium
# test methods to be used both by API Test and UI Test
class ImportReimportMixin(object):
def __init__(self, *args, **kwargs):
self.scans_path = '/scans/'
self.zap_sample0_filename = self.scans_path + 'zap/0_zap_sample.xml'
self.zap_sample1_filename = self.scans_path + 'zap/1_zap_sample_0_and_new_absent.xml'
self.zap_sample2_filename = self.scans_path + 'zap/2_zap_sample_0_and_new_endpoint.xml'
self.zap_sample3_filename = self.scans_path + 'zap/3_zap_sampl_0_and_different_severities.xml'
self.anchore_file_name = self.scans_path + 'anchore_engine/one_vuln_many_files.json'
self.scan_type_anchore = 'Anchore Engine Scan'
self.acunetix_file_name = self.scans_path + 'acunetix/one_finding.xml'
self.scan_type_acunetix = 'Acunetix Scan'
self.gitlab_dep_scan_components_filename = f'{self.scans_path}gitlab_dep_scan/gl-dependency-scanning-report-many-vuln_v15.json'
self.scan_type_gtlab_dep_scan = 'GitLab Dependency Scanning Report'
self.sonarqube_file_name1 = self.scans_path + 'sonarqube/sonar-6-findings.html'
self.sonarqube_file_name2 = self.scans_path + 'sonarqube/sonar-6-findings-1-unique_id_changed.html'
self.scan_type_sonarqube_detailed = 'SonarQube Scan detailed'
self.veracode_many_findings = self.scans_path + 'veracode/many_findings.xml'
self.veracode_same_hash_code_different_unique_id = self.scans_path + 'veracode/many_findings_same_hash_code_different_unique_id.xml'
self.veracode_same_unique_id_different_hash_code = self.scans_path + 'veracode/many_findings_same_unique_id_different_hash_code.xml'
self.veracode_different_hash_code_different_unique_id = self.scans_path + 'veracode/many_findings_different_hash_code_different_unique_id.xml'
self.veracode_mitigated_findings = self.scans_path + 'veracode/mitigated_finding.xml'
self.scan_type_veracode = 'Veracode Scan'
self.clair_few_findings = self.scans_path + 'clair/few_vuln.json'
self.clair_empty = self.scans_path + 'clair/empty.json'
self.scan_type_clair = 'Clair Scan'
self.generic_filename_with_file = self.scans_path + "generic/test_with_image.json"
self.aws_prowler_file_name = self.scans_path + 'aws_prowler/many_vuln.json'
self.aws_prowler_file_name_plus_one = self.scans_path + 'aws_prowler/many_vuln_plus_one.json'
self.scan_type_aws_prowler = 'AWS Prowler Scan'
self.nuclei_empty = self.scans_path + 'nuclei/empty.jsonl'
self.gitlab_dast_file_name = f'{self.scans_path}gitlab_dast/gitlab_dast_one_vul_v15.json'
self.scan_type_gitlab_dast = 'GitLab DAST Report'
self.anchore_grype_file_name = self.scans_path + 'anchore_grype/check_all_fields.json'
self.anchore_grype_scan_type = 'Anchore Grype'
# import zap scan, testing:
# - import
# - active/verifed = True
def test_zap_scan_base_active_verified(self):
logger.debug('importing original zap xml report')
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
with assertTestImportModelsCreated(self, imports=1, affected_findings=4, created=4):
import0 = self.import_scan_with_params(self.zap_sample0_filename)
# 0_zap_sample.xml: basic file with 4 out of 5 findings reported, zap4 absent
# 1 active
# 2 active
# 3 active
# 4 absent
# 5 active
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# imported count must match count in xml report
self.assert_finding_count_json(4, findings)
# the zap scan contains 2 endpoints (uris from findings)
self.assertEqual(endpoint_count_before + 2, self.db_endpoint_count())
# 4 findings, total 7 endpoint statuses (1 + 2 + 2 + 2)
# finding 1 have 1 endpoints => 1 status
# finding 2 have 2 endpoints => 2 status
# finding 3 have 2 endpoints => 2 status
# finding 5 have 2 endpoints => 2 status
self.assertEqual(endpoint_status_count_before_active + 7, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated, self.db_endpoint_status_count(mitigated=True))
# no notes expected
self.assertEqual(notes_count_before, self.db_notes_count())
return test_id
# import zap scan, testing:
# - import
# - active/verifed = False
def test_zap_scan_base_not_active_not_verified(self):
logger.debug('importing original zap xml report')
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
with assertTestImportModelsCreated(self, imports=1, affected_findings=4, created=4):
import0 = self.import_scan_with_params(self.zap_sample0_filename, active=False, verified=False)
# 0_zap_sample.xml: basic file with 4 out of 5 findings reported, zap4 absent
# 1 inactive
# 2 inactive
# 3 inactive
# 4 absent
# 5 inactive
test_id = import0['test']
findings = self.get_test_findings_api(test_id, active=False, verified=False)
self.log_finding_summary_json_api(findings)
# imported count must match count in xml report
self.assert_finding_count_json(4, findings)
# the zap scan contains 2 endpoints (uris from findings)
self.assertEqual(endpoint_count_before + 2, self.db_endpoint_count())
# 4 findings, total 7 endpoint statuses (1 + 2 + 2 + 2)
# finding 1 have 1 endpoints => 1 status
# finding 2 have 2 endpoints => 2 status
# finding 3 have 2 endpoints => 2 status
# finding 5 have 2 endpoints => 2 status
self.assertEqual(endpoint_status_count_before_active + 7, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated, self.db_endpoint_status_count(mitigated=True))
# no notes expected
self.assertEqual(notes_count_before, self.db_notes_count())
return test_id
# Test Scan_Date logic for Import. Reimport without a test_id cannot work for UI, so those tests are only in the API class below.
# import zap scan without dates
# - import
# - no scan_date and date not set by parser leads to today as date
def test_import_default_scan_date_parser_not_sets_date(self):
logger.debug('importing zap xml report with date set by parser')
with assertTestImportModelsCreated(self, imports=1, affected_findings=4, created=4):
import0 = self.import_scan_with_params(self.zap_sample0_filename, active=False, verified=False)
test_id = import0['test']
findings = self.get_test_findings_api(test_id, active=False, verified=False)
self.log_finding_summary_json_api(findings)
# Get the date
date = findings['results'][0]['date']
self.assertEqual(date, str(timezone.localtime(timezone.now()).date()))
return test_id
# import acunetix scan with dates
# - import
# - no scan scan_date does not overrides date set by parser
def test_import_default_scan_date_parser_sets_date(self):
logger.debug('importing original acunetix xml report')
with assertTestImportModelsCreated(self, imports=1, affected_findings=1, created=1):
import0 = self.import_scan_with_params(self.acunetix_file_name, scan_type=self.scan_type_acunetix, active=False, verified=False)
test_id = import0['test']
findings = self.get_test_findings_api(test_id, active=False, verified=False)
self.log_finding_summary_json_api(findings)
# Get the date
date = findings['results'][0]['date']
self.assertEqual(date, '2018-09-24')
return test_id
# import zap scan without dates
# - import
# - set scan_date overrides date not set by parser
def test_import_set_scan_date_parser_not_sets_date(self):
logger.debug('importing original zap xml report')
with assertTestImportModelsCreated(self, imports=1, affected_findings=4, created=4):
import0 = self.import_scan_with_params(self.zap_sample0_filename, active=False, verified=False, scan_date='2006-12-26')
test_id = import0['test']
findings = self.get_test_findings_api(test_id, active=False, verified=False)
self.log_finding_summary_json_api(findings)
# Get the date
date = findings['results'][0]['date']
self.assertEqual(date, '2006-12-26')
return test_id
# import acunetix scan with dates
# - import
# - set scan_date overrides date set by parser
def test_import_set_scan_date_parser_sets_date(self):
logger.debug('importing acunetix xml report with date set by parser')
with assertTestImportModelsCreated(self, imports=1, affected_findings=1, created=1):
import0 = self.import_scan_with_params(self.acunetix_file_name, scan_type=self.scan_type_acunetix, active=False, verified=False, scan_date='2006-12-26')
test_id = import0['test']
findings = self.get_test_findings_api(test_id, active=False, verified=False)
self.log_finding_summary_json_api(findings)
# Get the date
date = findings['results'][0]['date']
self.assertEqual(date, '2006-12-26')
return test_id
# Test Scan_Date for reimport in UI. UI can only rupload for existing tests, non UI tests are in API class below
def test_import_reimport_no_scan_date_parser_no_date(self):
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
# reimport report with 1 extra finding
reimport0 = self.reimport_scan_with_params(test_id, self.zap_sample1_filename)
test_id = reimport0['test']
# 1 new finding imported
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(5, findings)
# no scan_date, so date should be today
self.assertEqual(findings['results'][4]['date'], str(timezone.localtime(timezone.now()).date()))
def test_import_reimport_scan_date_parser_no_date(self):
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
# reimport report with 1 extra finding
reimport0 = self.reimport_scan_with_params(test_id, self.zap_sample1_filename, scan_date='2020-02-02')
test_id = reimport0['test']
# 1 new finding imported
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(5, findings)
# scan_date provided, so date should be equal to that
self.assertEqual(findings['results'][4]['date'], "2020-02-02")
def test_import_reimport_no_scan_date_parser_date(self):
import0 = self.import_scan_with_params(self.aws_prowler_file_name, scan_type=self.scan_type_aws_prowler)
test_id = import0['test']
# reimport report with 1 extra finding
reimport0 = self.reimport_scan_with_params(test_id, self.aws_prowler_file_name_plus_one, scan_type=self.scan_type_aws_prowler)
test_id = reimport0['test']
# 1 new finding imported
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(5, findings)
self.log_finding_summary_json_api(findings)
# no scan_date, so date should be date from parser
# findings order by priority, third finding is the new one
self.assertEqual(findings['results'][2]['date'], "2021-08-23")
def test_import_reimport_scan_date_parser_date(self):
import0 = self.import_scan_with_params(self.aws_prowler_file_name, scan_type=self.scan_type_aws_prowler)
test_id = import0['test']
# reimport report with 1 extra finding
reimport0 = self.reimport_scan_with_params(test_id, self.aws_prowler_file_name_plus_one, scan_type=self.scan_type_aws_prowler, scan_date='2020-02-02')
test_id = reimport0['test']
# 1 new finding imported
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(5, findings)
# scan_date provided, so date should be equal to that overriding that from the parser
self.log_finding_summary_json_api(findings)
self.assertEqual(findings['results'][2]['date'], "2020-02-02")
# Test re-import with unique_id_from_tool algorithm
# import sonar scan with detailed parser, testing:
# - import
# - active/verifed = True
def test_sonar_detailed_scan_base_active_verified(self):
logger.debug('importing original sonar report')
notes_count_before = self.db_notes_count()
with assertTestImportModelsCreated(self, imports=1, affected_findings=6, created=6):
import0 = self.import_scan_with_params(self.sonarqube_file_name1, scan_type=self.scan_type_sonarqube_detailed)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# imported count must match count in the report
self.assert_finding_count_json(6, findings)
# no notes expected
self.assertEqual(notes_count_before, self.db_notes_count())
return test_id
# Test re-import with unique_id_from_tool_or_hash_code algorithm
# import veracode scan, testing:
# - import
# - active/verifed = True
def test_veracode_scan_base_active_verified(self):
logger.debug('importing original veracode report')
notes_count_before = self.db_notes_count()
with assertTestImportModelsCreated(self, imports=1, affected_findings=4, created=4):
import0 = self.import_scan_with_params(self.veracode_many_findings, scan_type=self.scan_type_veracode)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# imported count must match count in the report
self.assert_finding_count_json(4, findings)
# no notes expected
self.assertEqual(notes_count_before, self.db_notes_count())
return test_id
# import veracode and then reimport veracode again
# - reimport, findings stay the same, stay active
# - active = True, verified = True
# - existing findings with verified is true should stay verified
def test_import_veracode_reimport_veracode_active_verified_mitigated(self):
logger.debug('reimporting exact same original veracode mitigated xml report again')
import_veracode_many_findings = self.import_scan_with_params(self.veracode_mitigated_findings, scan_type=self.scan_type_veracode,
verified=True, forceActive=True, forceVerified=True)
test_id = import_veracode_many_findings['test']
notes_count_before = self.db_notes_count()
# reimport exact same report
with assertTestImportModelsCreated(self, reimports=1, affected_findings=1, created=0, closed=1, reactivated=0, untouched=0):
reimport_veracode_mitigated_findings = self.reimport_scan_with_params(test_id, self.veracode_mitigated_findings, scan_type=self.scan_type_veracode)
test_id = reimport_veracode_mitigated_findings['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in veracode report
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(1, findings)
# inversely, we should see no findings with verified=False
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(0, findings)
# reimporting the exact same scan shouldn't create any notes, but there will be a new mitigated note
self.assertEqual(notes_count_before, self.db_notes_count() - 1)
mitigated_findings = self.get_test_findings_api(test_id, is_mitigated=True)
self.assert_finding_count_json(1, mitigated_findings)
# import 0 and then reimport 0 again
# - reimport, findings stay the same, stay active
# - active = True, verified = Trie
# - existing findings with verified is true should stay verified
def test_import_0_reimport_0_active_verified(self):
logger.debug('reimporting exact same original zap xml report again')
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
# reimport exact same report
with assertTestImportModelsCreated(self, reimports=1, untouched=4):
reimport0 = self.reimport_scan_with_params(test_id, self.zap_sample0_filename)
test_id = reimport0['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in xml report
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(4, findings)
# reimporting the exact same scan shouldn't modify the number of endpoints and statuses
self.assertEqual(endpoint_count_before, self.db_endpoint_count())
self.assertEqual(endpoint_status_count_before_active, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated, self.db_endpoint_status_count(mitigated=True))
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
# import 0 and then reimport 0 again with verified is false
# - reimport, findings stay the same, stay active
# - active = True, verified = False
# - existing findings with verified is true should stay verified
def test_import_0_reimport_0_active_not_verified(self):
logger.debug('reimporting exact same original zap xml report again, verified=False')
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
# reimport exact same report
with assertTestImportModelsCreated(self, reimports=1, untouched=4):
reimport0 = self.reimport_scan_with_params(test_id, self.zap_sample0_filename, verified=False)
test_id = reimport0['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in xml report
# we set verified=False in this reimport, but currently DD does not update this flag, so it's still True from previous import
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# inversely, we should see no findings with verified=False
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(4, findings)
# reimporting the exact same scan shouldn't modify the number of endpoints
self.assertEqual(endpoint_count_before, self.db_endpoint_count())
self.assertEqual(endpoint_status_count_before_active, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated, self.db_endpoint_status_count(mitigated=True))
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
# Test re-import with unique_id_from_tool algorithm
# import sonar1 and then reimport sonar1 again with verified is false
# - reimport, findings stay the same, stay active
# - active = True, verified = False
# - existing findings with verified is true should stay verified
def test_import_sonar1_reimport_sonar1_active_not_verified(self):
logger.debug('reimporting exact same original sonar report again, verified=False')
importsonar1 = self.import_scan_with_params(self.sonarqube_file_name1, scan_type=self.scan_type_sonarqube_detailed)
test_id = importsonar1['test']
notes_count_before = self.db_notes_count()
# reimport exact same report
with assertTestImportModelsCreated(self, reimports=1, untouched=6):
reimportsonar1 = self.reimport_scan_with_params(test_id, self.sonarqube_file_name1, scan_type=self.scan_type_sonarqube_detailed, verified=False)
test_id = reimportsonar1['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in sonar report
# we set verified=False in this reimport but DD keeps true as per the previous import (reimport doesn't "unverify" findings)
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# inversely, we should see no findings with verified=False
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(6, findings)
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
# Test re-import with unique_id_from_tool_or_hash_code algorithm
# import veracode_many_findings and then reimport veracode_many_findings again with verified is false
# - reimport, findings stay the same, stay active
# - existing findings with verified is true should stay verified
def test_import_veracode_reimport_veracode_active_not_verified(self):
logger.debug('reimporting exact same original veracode report again, verified=False')
import_veracode_many_findings = self.import_scan_with_params(self.veracode_many_findings, scan_type=self.scan_type_veracode)
test_id = import_veracode_many_findings['test']
notes_count_before = self.db_notes_count()
# reimport exact same report
with assertTestImportModelsCreated(self, reimports=1, untouched=4):
reimport_veracode_many_findings = self.reimport_scan_with_params(test_id, self.veracode_many_findings, scan_type=self.scan_type_veracode, verified=False)
test_id = reimport_veracode_many_findings['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in sonar report
# we set verified=False in this reimport but DD keeps true as per the previous import (reimport doesn't "unverify" findings)
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# inversely, we should see no findings with verified=False
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(4, findings)
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
# import sonar1 and then reimport sonar2 which has 1 different unique_id_from_tool
# - 5 findings stay the same and active
# - 1 finding is mitigated
# - 1 finding is added
def test_import_sonar1_reimport_sonar2(self):
logger.debug('reimporting same findings except one with a different unique_id_from_tool')
importsonar1 = self.import_scan_with_params(self.sonarqube_file_name1, scan_type=self.scan_type_sonarqube_detailed)
test_id = importsonar1['test']
notes_count_before = self.db_notes_count()
# reimport other report
with assertTestImportModelsCreated(self, reimports=1, affected_findings=2, created=1, closed=1, untouched=5):
reimportsonar1 = self.reimport_scan_with_params(test_id, self.sonarqube_file_name2, scan_type=self.scan_type_sonarqube_detailed, verified=False)
test_id = reimportsonar1['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in sonar report
# (reimport doesn't unverify findings that ware previously verified)
# (the mitigated finding stays verified)
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# one mitigated (the one previously imported which has changed unique_id_from_tool)
findings = self.get_test_findings_api(test_id, is_mitigated=True)
self.assert_finding_count_json(1, findings)
# one verified False (the new one, as reimport was done with verified false)
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(7, findings)
# one added note for mitigated finding
self.assertEqual(notes_count_before + 1, self.db_notes_count())
# Test re-import with unique_id_from_tool_or_hash_code algorithm
# import veracode_many_findings and then reimport veracode_same_hash_code_different_unique_id with verified is false
# - reimport, all findings stay the same, stay active
# - existing findings with verified is true should stay verified
def test_import_veracode_reimport_veracode_same_hash_code_different_unique_id(self):
logger.debug('reimporting report with one finding having same hash_code but different unique_id_from_tool, verified=False')
import_veracode_many_findings = self.import_scan_with_params(self.veracode_many_findings, scan_type=self.scan_type_veracode)
test_id = import_veracode_many_findings['test']
notes_count_before = self.db_notes_count()
# reimport
with assertTestImportModelsCreated(self, reimports=1, untouched=4):
reimport_veracode_many_findings = self.reimport_scan_with_params(test_id, self.veracode_same_hash_code_different_unique_id, scan_type=self.scan_type_veracode, verified=False)
test_id = reimport_veracode_many_findings['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# we set verified=False in this reimport but DD keeps true as per the previous import (reimport doesn't "unverify" findings)
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# inversely, we should see no findings with verified=False
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(4, findings)
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
# Test re-import with unique_id_from_tool_or_hash_code algorithm
# import veracode_many_findings and then reimport veracode_same_unique_id_different_hash_code with verified is false
# - reimport, all findings stay the same, stay active
# - existing findings with verified is true should stay verified
def test_import_veracode_reimport_veracode_same_unique_id_different_hash_code(self):
logger.debug('reimporting report with one finding having same unique_id_from_tool but different hash_code, verified=False')
import_veracode_many_findings = self.import_scan_with_params(self.veracode_many_findings, scan_type=self.scan_type_veracode)
test_id = import_veracode_many_findings['test']
notes_count_before = self.db_notes_count()
# reimport
with assertTestImportModelsCreated(self, reimports=1, untouched=4):
reimport_veracode_many_findings = self.reimport_scan_with_params(test_id, self.veracode_same_unique_id_different_hash_code, scan_type=self.scan_type_veracode, verified=False)
test_id = reimport_veracode_many_findings['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# we set verified=False in this reimport but DD keeps true as per the previous import (reimport doesn't "unverify" findings)
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# inversely, we should see no findings with verified=False
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(4, findings)
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
# Test re-import with unique_id_from_tool_or_hash_code algorithm
# import veracode_many_findings and then reimport veracode_different_hash_code_different_unique_id with verified is false
# - reimport, existing findings stay active and the same
# - 1 added finding, 1 mitigated finding
# - existing findings with verified is true should stay verified
def test_import_veracode_reimport_veracode_different_hash_code_different_unique_id(self):
logger.debug('reimporting report with one finding having different hash_code and different unique_id_from_tool, verified=False')
import_veracode_many_findings = self.import_scan_with_params(self.veracode_many_findings, scan_type=self.scan_type_veracode)
test_id = import_veracode_many_findings['test']
notes_count_before = self.db_notes_count()
# reimport
with assertTestImportModelsCreated(self, reimports=1, affected_findings=2, created=1, closed=1, untouched=3):
reimport_veracode_many_findings = self.reimport_scan_with_params(test_id, self.veracode_different_hash_code_different_unique_id, scan_type=self.scan_type_veracode, verified=False)
test_id = reimport_veracode_many_findings['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# we set verified=False in this reimport but DD keeps true as per the previous import (reimport doesn't "unverify" findings)
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
# The new finding has verified=false
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(5, findings)
# 1 added note for the migitated finding
self.assertEqual(notes_count_before + 1, self.db_notes_count())
# import 0 and then reimport 1 with zap4 as extra finding, zap1 closed.
# - active findings count should be 4
# - total findings count should be 5
# - zap1 is closed, so endpoints should be mitigated
# - verified is false, so zap4 should not be verified.
# - existing findings with verified is true should stay verified
def test_import_0_reimport_1_active_not_verified(self):
logger.debug('reimporting updated zap xml report, 1 new finding and 1 no longer present, verified=False')
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
finding_count_before = self.db_finding_count()
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
# reimport updated report
with assertTestImportModelsCreated(self, reimports=1, affected_findings=2, created=1, closed=1, untouched=3):
reimport1 = self.reimport_scan_with_params(test_id, self.zap_sample1_filename, verified=False)
test_id = reimport1['test']
self.assertEqual(test_id, test_id)
test = self.get_test_api(test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# active findings must be equal to those in both reports
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(4 + 1, findings)
# verified findings must be equal to those in report 0
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(5, findings)
# the updated scan report has
# - 1 new finding
self.assertEqual(finding_count_before + 1, self.db_finding_count())
# zap4 only uses 2 endpoints that already exist
self.assertEqual(endpoint_count_before, self.db_endpoint_count())
# but 2 statuses should be created for those endpoints, 2 statuses for zap1 closed
self.assertEqual(endpoint_status_count_before_active + 2 - 3, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated + 2, self.db_endpoint_status_count(mitigated=True))
# - 1 new note for zap1 being closed now
self.assertEqual(notes_count_before + 1, self.db_notes_count())
# import 0 and then reimport 1 with zap4 as extra finding, zap1 closed and then reimport 0 again
# - active findings count should be 4
# - total findings count should be 5
# - zap1 active, zap4 inactive
def test_import_0_reimport_1_active_verified_reimport_0_active_verified(self):
logger.debug('reimporting updated zap xml report, 1 new finding and 1 no longer present, verified=True and then 0 again')
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
finding_count_before = self.db_finding_count()
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
reimport1 = self.reimport_scan_with_params(test_id, self.zap_sample1_filename)
# zap1 should be closed 2 endpoint statuses less, but 2 extra for zap4
self.assertEqual(endpoint_status_count_before_active - 3 + 2, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated + 2, self.db_endpoint_status_count(mitigated=True))
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
with assertTestImportModelsCreated(self, reimports=1, affected_findings=2, closed=1, reactivated=1, untouched=3):
reimport0 = self.reimport_scan_with_params(test_id, self.zap_sample0_filename)
test_id = reimport1['test']
self.assertEqual(test_id, test_id)
test = self.get_test_api(test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# active findings must be equal to those in both reports
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(4 + 1, findings)
zap1_ok = False
zap4_ok = False
for finding in findings['results']:
if 'Zap1' in finding['title']:
self.assertTrue(finding['active'])
zap1_ok = True
if 'Zap4' in finding['title']:
self.assertFalse(finding['active'])
zap4_ok = True
self.assertTrue(zap1_ok)
self.assertTrue(zap4_ok)
# verified findings must be equal to those in report 0
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0, findings)
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(5, findings)
self.assertEqual(endpoint_count_before, self.db_endpoint_count())
# zap4 should be closed again so 2 mitigated eps, zap1 should be open again so 3 active extra
self.assertEqual(endpoint_status_count_before_active + 3 - 2, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated - 3 + 2, self.db_endpoint_status_count(mitigated=True))
# zap1 was closed and then opened -> 2 notes
# zap4 was created and then closed -> only 1 note
self.assertEqual(notes_count_before + 2 + 1, self.db_notes_count())
# import 0 and then reimport 2 with an extra endpoint for zap1
# - extra endpoint should be present in db
# - reimport doesn't look at endpoints to match against existing findings
def test_import_0_reimport_2_extra_endpoint(self):
logger.debug('reimporting exact same original zap xml report again, with an extra endpoint for zap1')
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
finding_count_before = self.db_finding_count()
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
with assertTestImportModelsCreated(self, reimports=1, affected_findings=0, untouched=4):
reimport2 = self.reimport_scan_with_params(test_id, self.zap_sample2_filename)
test_id = reimport2['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in xml report
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(4, findings)
self.assertEqual(endpoint_count_before + 1, self.db_endpoint_count())
self.assertEqual(endpoint_status_count_before_active + 1, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated, self.db_endpoint_status_count(mitigated=True))
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
self.assertEqual(finding_count_before, self.db_finding_count())
# import 0 and then reimport 2 with an extra endpoint for zap1 and then 0 again to remove the extra endpoint again
# - extra endpoint should no long be present in db
# - reimport doesn't look at endpoints to match against existing findings
def test_import_0_reimport_2_extra_endpoint_reimport_0(self):
logger.debug('reimporting exact same original zap xml report again, with an extra endpoint for zap1')
# self.log_finding_summary_json_api()
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
with assertTestImportModelsCreated(self, reimports=1, affected_findings=0, untouched=4):
reimport2 = self.reimport_scan_with_params(test_id, self.zap_sample2_filename)
test_id = reimport2['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
finding_count_before = self.db_finding_count()
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
reimport0 = self.reimport_scan_with_params(test_id, self.zap_sample0_filename)
test_id = reimport0['test']
self.assertEqual(test_id, test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
# reimported count must match count in xml report
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(4, findings)
self.assertEqual(endpoint_count_before, self.db_endpoint_count())
# 1 endpoint was marked as mitigated
self.assertEqual(endpoint_status_count_before_active - 1, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated + 1, self.db_endpoint_status_count(mitigated=True))
# reimporting the exact same scan shouldn't create any notes
self.assertEqual(notes_count_before, self.db_notes_count())
self.assertEqual(finding_count_before, self.db_finding_count())
# import 0 and then reimport 3 with severities changed for zap1 and zap2
# - reimport will match on severity, so now should create 2 new findings
# - and close the 2 old findings because they have a different severity
# - so zap1 + zap2 closed
# - 2 new findings zap1' and zap2'
def test_import_0_reimport_3_active_verified(self):
logger.debug('reimporting updated zap xml report, with different severities for zap2 and zap5')
import0 = self.import_scan_with_params(self.zap_sample0_filename)
test_id = import0['test']
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
finding_count_before = self.db_finding_count()
endpoint_count_before = self.db_endpoint_count()
endpoint_status_count_before_active = self.db_endpoint_status_count(mitigated=False)
endpoint_status_count_before_mitigated = self.db_endpoint_status_count(mitigated=True)
notes_count_before = self.db_notes_count()
# reimport updated report
with assertTestImportModelsCreated(self, reimports=1, affected_findings=4, created=2, closed=2, untouched=2):
reimport1 = self.reimport_scan_with_params(test_id, self.zap_sample3_filename)
test_id = reimport1['test']
self.assertEqual(test_id, test_id)
test = self.get_test_api(test_id)
findings = self.get_test_findings_api(test_id)
self.log_finding_summary_json_api(findings)
self.assert_finding_count_json(4 + 2, findings)
zap2_ok = False
zap5_ok = False
for finding in findings['results']:
if 'Zap2' in finding['title']:
self.assertTrue(finding['active'] or finding['severity'] == 'Low')
self.assertTrue(not finding['active'] or finding['severity'] == 'Medium')
zap2_ok = True
if 'Zap5' in finding['title']:
self.assertTrue(finding['active'] or finding['severity'] == 'Low')
self.assertTrue(not finding['active'] or finding['severity'] == 'Medium')
zap5_ok = True
self.assertTrue(zap2_ok)
self.assertTrue(zap5_ok)
# verified findings must be equal to those in report 0
findings = self.get_test_findings_api(test_id, verified=True)
self.assert_finding_count_json(0 + 0, findings)
# unverified findings must be equal to those in report 0
findings = self.get_test_findings_api(test_id, verified=False)
self.assert_finding_count_json(4 + 2, findings)
# the updated scan report has
# - 2 new findings, 2 new endpoints, 2 + 2 new endpoint statuses active, 3 + 3 endpoint statues mitigated due to zap1+2 closed
self.assertEqual(finding_count_before + 2, self.db_finding_count())
self.assertEqual(endpoint_count_before, self.db_endpoint_count())
self.assertEqual(endpoint_status_count_before_active + 3 + 3 - 3 - 3, self.db_endpoint_status_count(mitigated=False))
self.assertEqual(endpoint_status_count_before_mitigated + 2 + 2, self.db_endpoint_status_count(mitigated=True))
# - zap2 and zap5 closed
self.assertEqual(notes_count_before + 2, self.db_notes_count())
# import 1 and then reimport 2 without closing old findings
# - reimport should not mitigate the zap1
def test_import_reimport_without_closing_old_findings(self):
logger.debug('reimporting updated zap xml report and keep old findings open')
import1 = self.import_scan_with_params(self.zap_sample1_filename)
test_id = import1['test']
findings = self.get_test_findings_api(test_id)
self.assert_finding_count_json(4, findings)
with assertTestImportModelsCreated(self, reimports=1, affected_findings=1, created=1, untouched=3):
reimport1 = self.reimport_scan_with_params(test_id, self.zap_sample2_filename, close_old_findings=False)
test_id = reimport1['test']
self.assertEqual(test_id, test_id)