-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
views.py
1183 lines (1035 loc) · 52 KB
/
views.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
# # metrics
import collections
import logging
import operator
from calendar import monthrange
from collections import OrderedDict
from datetime import date, datetime, timedelta
from math import ceil
from operator import itemgetter
from dateutil.relativedelta import relativedelta
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from django.db.models import Q, Sum, Case, When, IntegerField, Value, Count
from django.db.models.query import QuerySet
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.utils.html import escape
from django.views.decorators.cache import cache_page
from django.utils import timezone
from dojo.filters import MetricsFindingFilter, UserFilter, MetricsEndpointFilter, MetricsFindingFilterWithoutObjectLookups
from dojo.forms import SimpleMetricsForm, ProductTypeCountsForm, ProductTagCountsForm
from dojo.models import Product_Type, Finding, Product, Engagement, Test, \
Risk_Acceptance, Dojo_User, Endpoint_Status
from dojo.utils import get_page_items, add_breadcrumb, findings_this_period, opened_in_period, count_findings, \
get_period_counts, get_system_setting, get_punchcard_data, queryset_check
from functools import reduce
from django.views.decorators.vary import vary_on_cookie
from dojo.authorization.roles_permissions import Permissions
from dojo.product.queries import get_authorized_products
from dojo.product_type.queries import get_authorized_product_types
from dojo.finding.queries import get_authorized_findings
from dojo.finding.helper import ACCEPTED_FINDINGS_QUERY, CLOSED_FINDINGS_QUERY
from dojo.endpoint.queries import get_authorized_endpoint_status
from dojo.authorization.authorization import user_has_permission_or_403
from django.utils.translation import gettext as _
logger = logging.getLogger(__name__)
"""
Greg, Jay
status: in production
generic metrics method
"""
def critical_product_metrics(request, mtype):
template = 'dojo/metrics.html'
page_name = _('Critical Product Metrics')
critical_products = get_authorized_product_types(Permissions.Product_Type_View)
critical_products = critical_products.filter(critical_product=True)
add_breadcrumb(title=page_name, top_level=not len(request.GET), request=request)
return render(request, template, {
'name': page_name,
'critical_prods': critical_products,
'url_prefix': get_system_setting('url_prefix')
})
def get_date_range(objects):
tz = timezone.get_current_timezone()
start_date = objects.earliest('date').date
start_date = datetime(start_date.year, start_date.month, start_date.day,
tzinfo=tz)
end_date = objects.latest('date').date
end_date = datetime(end_date.year, end_date.month, end_date.day,
tzinfo=tz)
return start_date, end_date
def severity_count(queryset, method, expression):
total_expression = expression + '__in'
return getattr(queryset, method)(
total=Sum(
Case(When(**{total_expression: ('Critical', 'High', 'Medium', 'Low', 'Info')},
then=Value(1)),
output_field=IntegerField(),
default=0)),
critical=Sum(
Case(When(**{expression: 'Critical'},
then=Value(1)),
output_field=IntegerField(),
default=0)),
high=Sum(
Case(When(**{expression: 'High'},
then=Value(1)),
output_field=IntegerField(),
default=0)),
medium=Sum(
Case(When(**{expression: 'Medium'},
then=Value(1)),
output_field=IntegerField(),
default=0)),
low=Sum(
Case(When(**{expression: 'Low'},
then=Value(1)),
output_field=IntegerField(),
default=0)),
info=Sum(
Case(When(**{expression: 'Info'},
then=Value(1)),
output_field=IntegerField(),
default=0)),
)
def identify_view(request):
get_data = request.GET
view = get_data.get('type', None)
if view:
return view
finding_severity = get_data.get('finding__severity', None)
false_positive = get_data.get('false_positive', None)
referer = request.META.get('HTTP_REFERER', None)
endpoint_in_referer = referer and referer.find('type=Endpoint') > -1
if finding_severity or false_positive or endpoint_in_referer:
return 'Endpoint'
return 'Finding'
def finding_querys(prod_type, request):
# Get the initial list of findings th use is authorized to see
findings_query = get_authorized_findings(
Permissions.Finding_View,
user=request.user,
).select_related(
'reporter',
'test',
'test__engagement__product',
'test__engagement__product__prod_type',
).prefetch_related(
'risk_acceptance_set',
'test__engagement__risk_acceptance',
'test__test_type',
)
filter_string_matching = get_system_setting("filter_string_matching", False)
finding_filter_class = MetricsFindingFilterWithoutObjectLookups if filter_string_matching else MetricsFindingFilter
findings = finding_filter_class(request.GET, queryset=findings_query)
findings_qs = queryset_check(findings)
# Quick check to determine if the filters were too tight and filtered everything away
if not findings_qs and not findings_query:
findings = findings_query
findings_qs = findings if isinstance(findings, QuerySet) else findings.qs
messages.add_message(
request,
messages.ERROR,
_('All objects have been filtered away. Displaying all objects'),
extra_tags='alert-danger')
# Attempt to parser the date ranges
try:
start_date, end_date = get_date_range(findings_qs)
except:
start_date = timezone.now()
end_date = timezone.now()
# Filter by the date ranges supplied
findings_query = findings_query.filter(date__range=[start_date, end_date])
# Get the list of closed and risk accepted findings
findings_closed = findings_query.filter(CLOSED_FINDINGS_QUERY)
accepted_findings = findings_query.filter(ACCEPTED_FINDINGS_QUERY)
# filter by product type if applicable
if len(prod_type) > 0:
findings_closed = findings_closed.filter(test__engagement__product__prod_type__in=prod_type)
accepted_findings = accepted_findings.filter(test__engagement__product__prod_type__in=prod_type)
# Get the severity counts of risk accepted findings
accepted_findings_counts = severity_count(accepted_findings, 'aggregate', 'severity')
r = relativedelta(end_date, start_date)
months_between = (r.years * 12) + r.months
# include current month
months_between += 1
weeks_between = int(ceil((((r.years * 12) + r.months) * 4.33) + (r.days / 7)))
if weeks_between <= 0:
weeks_between += 2
monthly_counts = get_period_counts(findings_qs, findings_closed, accepted_findings, months_between, start_date,
relative_delta='months')
weekly_counts = get_period_counts(findings_qs, findings_closed, accepted_findings, weeks_between, start_date,
relative_delta='weeks')
top_ten = get_authorized_products(Permissions.Product_View)
top_ten = top_ten.filter(engagement__test__finding__verified=True,
engagement__test__finding__false_p=False,
engagement__test__finding__duplicate=False,
engagement__test__finding__out_of_scope=False,
engagement__test__finding__mitigated__isnull=True,
engagement__test__finding__severity__in=(
'Critical', 'High', 'Medium', 'Low'),
prod_type__in=prod_type)
top_ten = severity_count(top_ten, 'annotate', 'engagement__test__finding__severity').order_by('-critical', '-high', '-medium', '-low')[:10]
return {
'all': findings,
'closed': findings_closed,
'accepted': accepted_findings,
'accepted_count': accepted_findings_counts,
'top_ten': top_ten,
'monthly_counts': monthly_counts,
'weekly_counts': weekly_counts,
'weeks_between': weeks_between,
'start_date': start_date,
'end_date': end_date,
}
def endpoint_querys(prod_type, request):
endpoints_query = Endpoint_Status.objects.filter(mitigated=False,
finding__severity__in=('Critical', 'High', 'Medium', 'Low', 'Info')).prefetch_related(
'finding__test__engagement__product',
'finding__test__engagement__product__prod_type',
'finding__test__engagement__risk_acceptance',
'finding__risk_acceptance_set',
'finding__reporter')
endpoints_query = get_authorized_endpoint_status(Permissions.Endpoint_View, endpoints_query, request.user)
endpoints = MetricsEndpointFilter(request.GET, queryset=endpoints_query)
endpoints_qs = queryset_check(endpoints)
if not endpoints_qs:
endpoints = endpoints_query
endpoints_qs = endpoints if isinstance(endpoints, QuerySet) else endpoints.qs
messages.add_message(request,
messages.ERROR,
_('All objects have been filtered away. Displaying all objects'),
extra_tags='alert-danger')
try:
start_date, end_date = get_date_range(endpoints_qs)
except:
start_date = timezone.now()
end_date = timezone.now()
if len(prod_type) > 0:
endpoints_closed = Endpoint_Status.objects.filter(mitigated_time__range=[start_date, end_date],
finding__test__engagement__product__prod_type__in=prod_type).prefetch_related(
'finding__test__engagement__product')
# capture the accepted findings in period
accepted_endpoints = Endpoint_Status.objects.filter(date__range=[start_date, end_date], risk_accepted=True,
finding__test__engagement__product__prod_type__in=prod_type). \
prefetch_related('finding__test__engagement__product')
accepted_endpoints_counts = Endpoint_Status.objects.filter(date__range=[start_date, end_date], risk_accepted=True,
finding__test__engagement__product__prod_type__in=prod_type). \
prefetch_related('finding__test__engagement__product')
else:
endpoints_closed = Endpoint_Status.objects.filter(mitigated_time__range=[start_date, end_date]).prefetch_related(
'finding__test__engagement__product')
accepted_endpoints = Endpoint_Status.objects.filter(date__range=[start_date, end_date], risk_accepted=True). \
prefetch_related('finding__test__engagement__product')
accepted_endpoints_counts = Endpoint_Status.objects.filter(date__range=[start_date, end_date], risk_accepted=True). \
prefetch_related('finding__test__engagement__product')
endpoints_closed = get_authorized_endpoint_status(Permissions.Endpoint_View, endpoints_closed, request.user)
accepted_endpoints = get_authorized_endpoint_status(Permissions.Endpoint_View, accepted_endpoints, request.user)
accepted_endpoints_counts = get_authorized_endpoint_status(Permissions.Endpoint_View, accepted_endpoints_counts, request.user)
accepted_endpoints_counts = severity_count(accepted_endpoints_counts, 'aggregate', 'finding__severity')
r = relativedelta(end_date, start_date)
months_between = (r.years * 12) + r.months
# include current month
months_between += 1
weeks_between = int(ceil((((r.years * 12) + r.months) * 4.33) + (r.days / 7)))
if weeks_between <= 0:
weeks_between += 2
monthly_counts = get_period_counts(endpoints_qs, endpoints_closed, accepted_endpoints, months_between, start_date,
relative_delta='months')
weekly_counts = get_period_counts(endpoints_qs, endpoints_closed, accepted_endpoints, weeks_between, start_date,
relative_delta='weeks')
top_ten = get_authorized_products(Permissions.Product_View)
top_ten = top_ten.filter(engagement__test__finding__status_finding__mitigated=False,
engagement__test__finding__status_finding__false_positive=False,
engagement__test__finding__status_finding__out_of_scope=False,
engagement__test__finding__status_finding__risk_accepted=False,
engagement__test__finding__severity__in=(
'Critical', 'High', 'Medium', 'Low'),
prod_type__in=prod_type)
top_ten = severity_count(top_ten, 'annotate', 'engagement__test__finding__severity').order_by('-critical', '-high', '-medium', '-low')[:10]
return {
'all': endpoints,
'closed': endpoints_closed,
'accepted': accepted_endpoints,
'accepted_count': accepted_endpoints_counts,
'top_ten': top_ten,
'monthly_counts': monthly_counts,
'weekly_counts': weekly_counts,
'weeks_between': weeks_between,
'start_date': start_date,
'end_date': end_date,
}
def get_in_period_details(findings):
in_period_counts = {"Critical": 0, "High": 0, "Medium": 0,
"Low": 0, "Info": 0, "Total": 0}
in_period_details = {}
age_detail = [0, 0, 0, 0]
for obj in findings:
if 0 <= obj.age <= 30:
age_detail[0] += 1
elif 30 < obj.age <= 60:
age_detail[1] += 1
elif 60 < obj.age <= 90:
age_detail[2] += 1
elif obj.age > 90:
age_detail[3] += 1
# This condition should be true in nearly all cases,
# but there are some far edge cases
if obj.severity in in_period_counts:
in_period_counts[obj.severity] += 1
in_period_counts['Total'] += 1
# This condition should be true in nearly all cases,
# but there are some far edge cases
if obj.severity in in_period_details:
if obj.test.engagement.product.name not in in_period_details:
in_period_details[obj.test.engagement.product.name] = {
'path': reverse('product_open_findings', args=(obj.test.engagement.product.id,)),
'Critical': 0, 'High': 0, 'Medium': 0, 'Low': 0, 'Info': 0, 'Total': 0}
in_period_details[obj.test.engagement.product.name][obj.severity] += 1
in_period_details[obj.test.engagement.product.name]['Total'] += 1
return in_period_counts, in_period_details, age_detail
def get_accepted_in_period_details(findings):
accepted_in_period_details = {}
for obj in findings:
if obj.test.engagement.product.name not in accepted_in_period_details:
accepted_in_period_details[obj.test.engagement.product.name] = {
'path': reverse('accepted_findings') + '?test__engagement__product=' + str(obj.test.engagement.product.id),
'Critical': 0, 'High': 0, 'Medium': 0, 'Low': 0, 'Info': 0, 'Total': 0}
accepted_in_period_details[
obj.test.engagement.product.name
][obj.severity] += 1
accepted_in_period_details[obj.test.engagement.product.name]['Total'] += 1
return accepted_in_period_details
def get_closed_in_period_details(findings):
closed_in_period_counts = {"Critical": 0, "High": 0, "Medium": 0,
"Low": 0, "Info": 0, "Total": 0}
closed_in_period_details = {}
for obj in findings:
closed_in_period_counts[obj.severity] += 1
closed_in_period_counts['Total'] += 1
if obj.test.engagement.product.name not in closed_in_period_details:
closed_in_period_details[obj.test.engagement.product.name] = {
'path': reverse('closed_findings') + '?test__engagement__product=' + str(
obj.test.engagement.product.id),
'Critical': 0, 'High': 0, 'Medium': 0, 'Low': 0, 'Info': 0, 'Total': 0}
closed_in_period_details[
obj.test.engagement.product.name
][obj.severity] += 1
closed_in_period_details[obj.test.engagement.product.name]['Total'] += 1
return closed_in_period_counts, closed_in_period_details
@cache_page(60 * 5) # cache for 5 minutes
@vary_on_cookie
def metrics(request, mtype):
template = 'dojo/metrics.html'
show_pt_filter = True
view = identify_view(request)
page_name = _('Metrics')
if mtype != 'All':
pt = Product_Type.objects.filter(id=mtype)
request.GET._mutable = True
request.GET.appendlist('test__engagement__product__prod_type', mtype)
request.GET._mutable = False
show_pt_filter = False
page_name = _('%(product_type)s Metrics') % {'product_type': mtype}
prod_type = pt
elif 'test__engagement__product__prod_type' in request.GET:
prod_type = Product_Type.objects.filter(id__in=request.GET.getlist('test__engagement__product__prod_type', []))
else:
prod_type = get_authorized_product_types(Permissions.Product_Type_View)
# legacy code calls has 'prod_type' as 'related_name' for product.... so weird looking prefetch
prod_type = prod_type.prefetch_related('prod_type')
filters = dict()
if view == 'Finding':
page_name = _('Product Type Metrics by Findings')
filters = finding_querys(prod_type, request)
elif view == 'Endpoint':
page_name = _('Product Type Metrics by Affected Endpoints')
filters = endpoint_querys(prod_type, request)
in_period_counts, in_period_details, age_detail = get_in_period_details([
obj.finding if view == 'Endpoint' else obj
for obj in queryset_check(filters['all'])
])
accepted_in_period_details = get_accepted_in_period_details([
obj.finding if view == 'Endpoint' else obj
for obj in filters['accepted']
])
closed_in_period_counts, closed_in_period_details = get_closed_in_period_details([
obj.finding if view == 'Endpoint' else obj
for obj in filters['closed']
])
punchcard = list()
ticks = list()
if 'view' in request.GET and 'dashboard' == request.GET['view']:
punchcard, ticks = get_punchcard_data(queryset_check(filters['all']), filters['start_date'], filters['weeks_between'], view)
page_name = _('%(team_name)s Metrics') % {'team_name': get_system_setting('team_name')}
template = 'dojo/dashboard-metrics.html'
add_breadcrumb(title=page_name, top_level=not len(request.GET), request=request)
return render(request, template, {
'name': page_name,
'start_date': filters['start_date'],
'end_date': filters['end_date'],
'findings': filters['all'],
'opened_per_month': filters['monthly_counts']['opened_per_period'],
'active_per_month': filters['monthly_counts']['active_per_period'],
'opened_per_week': filters['weekly_counts']['opened_per_period'],
'accepted_per_month': filters['monthly_counts']['accepted_per_period'],
'accepted_per_week': filters['weekly_counts']['accepted_per_period'],
'top_ten_products': filters['top_ten'],
'age_detail': age_detail,
'in_period_counts': in_period_counts,
'in_period_details': in_period_details,
'accepted_in_period_counts': filters['accepted_count'],
'accepted_in_period_details': accepted_in_period_details,
'closed_in_period_counts': closed_in_period_counts,
'closed_in_period_details': closed_in_period_details,
'punchcard': punchcard,
'ticks': ticks,
'show_pt_filter': show_pt_filter,
})
"""
Jay
status: in production
simple metrics for easy reporting
"""
@cache_page(60 * 5) # cache for 5 minutes
@vary_on_cookie
def simple_metrics(request):
page_name = _('Simple Metrics')
now = timezone.now()
if request.method == 'POST':
form = SimpleMetricsForm(request.POST)
if form.is_valid():
now = form.cleaned_data['date']
form = SimpleMetricsForm({'date': now})
else:
form = SimpleMetricsForm({'date': now})
findings_by_product_type = collections.OrderedDict()
# for each product type find each product with open findings and
# count the S0, S1, S2 and S3
# legacy code calls has 'prod_type' as 'related_name' for product.... so weird looking prefetch
product_types = get_authorized_product_types(Permissions.Product_Type_View)
product_types = product_types.prefetch_related('prod_type')
for pt in product_types:
total_critical = []
total_high = []
total_medium = []
total_low = []
total_info = []
total_closed = []
total_opened = []
findings_broken_out = {}
total = Finding.objects.filter(test__engagement__product__prod_type=pt,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
date__month=now.month,
date__year=now.year,
).distinct()
for f in total:
if f.severity == "Critical":
total_critical.append(f)
elif f.severity == 'High':
total_high.append(f)
elif f.severity == 'Medium':
total_medium.append(f)
elif f.severity == 'Low':
total_low.append(f)
else:
total_info.append(f)
if f.mitigated and f.mitigated.year == now.year and f.mitigated.month == now.month:
total_closed.append(f)
if f.date.year == now.year and f.date.month == now.month:
total_opened.append(f)
findings_broken_out['Total'] = len(total)
findings_broken_out['S0'] = len(total_critical)
findings_broken_out['S1'] = len(total_high)
findings_broken_out['S2'] = len(total_medium)
findings_broken_out['S3'] = len(total_low)
findings_broken_out['S4'] = len(total_info)
findings_broken_out['Opened'] = len(total_opened)
findings_broken_out['Closed'] = len(total_closed)
findings_by_product_type[pt] = findings_broken_out
add_breadcrumb(title=page_name, top_level=True, request=request)
return render(request, 'dojo/simple_metrics.html', {
'findings': findings_by_product_type,
'name': page_name,
'metric': True,
'user': request.user,
'form': form,
})
# @cache_page(60 * 5) # cache for 5 minutes
# @vary_on_cookie
def product_type_counts(request):
form = ProductTypeCountsForm()
opened_in_period_list = []
oip = None
cip = None
aip = None
all_current_in_pt = None
top_ten = None
pt = None
today = timezone.now()
first_of_month = today.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
mid_month = first_of_month.replace(day=15, hour=23, minute=59, second=59, microsecond=999999)
end_of_month = mid_month.replace(day=monthrange(today.year, today.month)[1], hour=23, minute=59, second=59,
microsecond=999999)
start_date = first_of_month
end_date = end_of_month
if request.method == 'GET' and 'month' in request.GET and 'year' in request.GET and 'product_type' in request.GET:
form = ProductTypeCountsForm(request.GET)
if form.is_valid():
pt = form.cleaned_data['product_type']
user_has_permission_or_403(request.user, pt, Permissions.Product_Type_View)
month = int(form.cleaned_data['month'])
year = int(form.cleaned_data['year'])
first_of_month = first_of_month.replace(month=month, year=year)
month_requested = datetime(year, month, 1)
end_of_month = month_requested.replace(day=monthrange(month_requested.year, month_requested.month)[1],
hour=23, minute=59, second=59, microsecond=999999)
start_date = first_of_month
start_date = datetime(start_date.year,
start_date.month, start_date.day,
tzinfo=timezone.get_current_timezone())
end_date = end_of_month
end_date = datetime(end_date.year,
end_date.month, end_date.day,
tzinfo=timezone.get_current_timezone())
oip = opened_in_period(start_date, end_date, test__engagement__product__prod_type=pt)
# trending data - 12 months
for x in range(12, 0, -1):
opened_in_period_list.append(
opened_in_period(start_date + relativedelta(months=-x), end_of_month + relativedelta(months=-x),
test__engagement__product__prod_type=pt))
opened_in_period_list.append(oip)
closed_in_period = Finding.objects.filter(mitigated__date__range=[start_date, end_date],
test__engagement__product__prod_type=pt,
severity__in=('Critical', 'High', 'Medium', 'Low')).values(
'numerical_severity').annotate(Count('numerical_severity')).order_by('numerical_severity')
total_closed_in_period = Finding.objects.filter(mitigated__date__range=[start_date, end_date],
test__engagement__product__prod_type=pt,
severity__in=(
'Critical', 'High', 'Medium', 'Low')).aggregate(
total=Sum(
Case(When(severity__in=('Critical', 'High', 'Medium', 'Low'),
then=Value(1)),
output_field=IntegerField())))['total']
overall_in_pt = Finding.objects.filter(date__lt=end_date,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
mitigated__isnull=True,
test__engagement__product__prod_type=pt,
severity__in=('Critical', 'High', 'Medium', 'Low')).values(
'numerical_severity').annotate(Count('numerical_severity')).order_by('numerical_severity')
total_overall_in_pt = Finding.objects.filter(date__lte=end_date,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
mitigated__isnull=True,
test__engagement__product__prod_type=pt,
severity__in=('Critical', 'High', 'Medium', 'Low')).aggregate(
total=Sum(
Case(When(severity__in=('Critical', 'High', 'Medium', 'Low'),
then=Value(1)),
output_field=IntegerField())))['total']
all_current_in_pt = Finding.objects.filter(date__lte=end_date,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
mitigated__isnull=True,
test__engagement__product__prod_type=pt,
severity__in=(
'Critical', 'High', 'Medium', 'Low')).prefetch_related(
'test__engagement__product',
'test__engagement__product__prod_type',
'test__engagement__risk_acceptance',
'reporter').order_by(
'numerical_severity')
top_ten = Product.objects.filter(engagement__test__finding__date__lte=end_date,
engagement__test__finding__verified=True,
engagement__test__finding__false_p=False,
engagement__test__finding__duplicate=False,
engagement__test__finding__out_of_scope=False,
engagement__test__finding__mitigated__isnull=True,
engagement__test__finding__severity__in=(
'Critical', 'High', 'Medium', 'Low'),
prod_type=pt)
top_ten = severity_count(top_ten, 'annotate', 'engagement__test__finding__severity').order_by('-critical', '-high', '-medium', '-low')[:10]
cip = {'S0': 0,
'S1': 0,
'S2': 0,
'S3': 0,
'Total': total_closed_in_period}
aip = {'S0': 0,
'S1': 0,
'S2': 0,
'S3': 0,
'Total': total_overall_in_pt}
for o in closed_in_period:
cip[o['numerical_severity']] = o['numerical_severity__count']
for o in overall_in_pt:
aip[o['numerical_severity']] = o['numerical_severity__count']
else:
messages.add_message(request, messages.ERROR, _("Please choose month and year and the Product Type."),
extra_tags='alert-danger')
add_breadcrumb(title=_("Bi-Weekly Metrics"), top_level=True, request=request)
return render(request,
'dojo/pt_counts.html',
{'form': form,
'start_date': start_date,
'end_date': end_date,
'opened_in_period': oip,
'trending_opened': opened_in_period_list,
'closed_in_period': cip,
'overall_in_pt': aip,
'all_current_in_pt': all_current_in_pt,
'top_ten': top_ten,
'pt': pt}
)
def product_tag_counts(request):
form = ProductTagCountsForm()
opened_in_period_list = []
oip = None
cip = None
aip = None
all_current_in_pt = None
top_ten = None
pt = None
today = timezone.now()
first_of_month = today.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
mid_month = first_of_month.replace(day=15, hour=23, minute=59, second=59, microsecond=999999)
end_of_month = mid_month.replace(day=monthrange(today.year, today.month)[1], hour=23, minute=59, second=59,
microsecond=999999)
start_date = first_of_month
end_date = end_of_month
if request.method == 'GET' and 'month' in request.GET and 'year' in request.GET and 'product_tag' in request.GET:
form = ProductTagCountsForm(request.GET)
if form.is_valid():
prods = get_authorized_products(Permissions.Product_View)
pt = form.cleaned_data['product_tag']
month = int(form.cleaned_data['month'])
year = int(form.cleaned_data['year'])
first_of_month = first_of_month.replace(month=month, year=year)
month_requested = datetime(year, month, 1)
end_of_month = month_requested.replace(day=monthrange(month_requested.year, month_requested.month)[1],
hour=23, minute=59, second=59, microsecond=999999)
start_date = first_of_month
start_date = datetime(start_date.year,
start_date.month, start_date.day,
tzinfo=timezone.get_current_timezone())
end_date = end_of_month
end_date = datetime(end_date.year,
end_date.month, end_date.day,
tzinfo=timezone.get_current_timezone())
oip = opened_in_period(start_date, end_date,
test__engagement__product__tags__name=pt,
test__engagement__product__in=prods)
# trending data - 12 months
for x in range(12, 0, -1):
opened_in_period_list.append(
opened_in_period(start_date + relativedelta(months=-x), end_of_month + relativedelta(months=-x),
test__engagement__product__tags__name=pt, test__engagement__product__in=prods))
opened_in_period_list.append(oip)
closed_in_period = Finding.objects.filter(mitigated__date__range=[start_date, end_date],
test__engagement__product__tags__name=pt,
test__engagement__product__in=prods,
severity__in=('Critical', 'High', 'Medium', 'Low')).values(
'numerical_severity').annotate(Count('numerical_severity')).order_by('numerical_severity')
total_closed_in_period = Finding.objects.filter(mitigated__date__range=[start_date, end_date],
test__engagement__product__tags__name=pt,
test__engagement__product__in=prods,
severity__in=(
'Critical', 'High', 'Medium', 'Low')).aggregate(
total=Sum(
Case(When(severity__in=('Critical', 'High', 'Medium', 'Low'),
then=Value(1)),
output_field=IntegerField())))['total']
overall_in_pt = Finding.objects.filter(date__lt=end_date,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
mitigated__isnull=True,
test__engagement__product__tags__name=pt,
test__engagement__product__in=prods,
severity__in=('Critical', 'High', 'Medium', 'Low')).values(
'numerical_severity').annotate(Count('numerical_severity')).order_by('numerical_severity')
total_overall_in_pt = Finding.objects.filter(date__lte=end_date,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
mitigated__isnull=True,
test__engagement__product__tags__name=pt,
test__engagement__product__in=prods,
severity__in=('Critical', 'High', 'Medium', 'Low')).aggregate(
total=Sum(
Case(When(severity__in=('Critical', 'High', 'Medium', 'Low'),
then=Value(1)),
output_field=IntegerField())))['total']
all_current_in_pt = Finding.objects.filter(date__lte=end_date,
verified=True,
false_p=False,
duplicate=False,
out_of_scope=False,
mitigated__isnull=True,
test__engagement__product__tags__name=pt,
test__engagement__product__in=prods,
severity__in=(
'Critical', 'High', 'Medium', 'Low')).prefetch_related(
'test__engagement__product',
'test__engagement__product__prod_type',
'test__engagement__risk_acceptance',
'reporter').order_by(
'numerical_severity')
top_ten = Product.objects.filter(engagement__test__finding__date__lte=end_date,
engagement__test__finding__verified=True,
engagement__test__finding__false_p=False,
engagement__test__finding__duplicate=False,
engagement__test__finding__out_of_scope=False,
engagement__test__finding__mitigated__isnull=True,
engagement__test__finding__severity__in=(
'Critical', 'High', 'Medium', 'Low'),
tags__name=pt, engagement__product__in=prods)
top_ten = severity_count(top_ten, 'annotate', 'engagement__test__finding__severity').order_by('-critical', '-high', '-medium', '-low')[:10]
cip = {'S0': 0,
'S1': 0,
'S2': 0,
'S3': 0,
'Total': total_closed_in_period}
aip = {'S0': 0,
'S1': 0,
'S2': 0,
'S3': 0,
'Total': total_overall_in_pt}
for o in closed_in_period:
cip[o['numerical_severity']] = o['numerical_severity__count']
for o in overall_in_pt:
aip[o['numerical_severity']] = o['numerical_severity__count']
else:
messages.add_message(request, messages.ERROR, _("Please choose month and year and the Product Tag."),
extra_tags='alert-danger')
add_breadcrumb(title=_("Bi-Weekly Metrics"), top_level=True, request=request)
return render(request,
'dojo/pt_counts.html',
{'form': form,
'start_date': start_date,
'end_date': end_date,
'opened_in_period': oip,
'trending_opened': opened_in_period_list,
'closed_in_period': cip,
'overall_in_pt': aip,
'all_current_in_pt': all_current_in_pt,
'top_ten': top_ten,
'pt': pt}
)
def engineer_metrics(request):
# only superusers can select other users to view
if request.user.is_superuser:
users = Dojo_User.objects.all().order_by('username')
else:
return HttpResponseRedirect(reverse('view_engineer', args=(request.user.id,)))
users = UserFilter(request.GET, queryset=users)
paged_users = get_page_items(request, users.qs, 25)
add_breadcrumb(title=_("Engineer Metrics"), top_level=True, request=request)
return render(request,
'dojo/engineer_metrics.html',
{'users': paged_users,
"filtered": users,
})
"""
Greg
Status: in prod
indvidual view of engineer metrics for a given month. Only superusers,
and root can view others metrics
"""
# noinspection DjangoOrm
@cache_page(60 * 5) # cache for 5 minutes
@vary_on_cookie
def view_engineer(request, eid):
user = get_object_or_404(Dojo_User, pk=eid)
if not (request.user.is_superuser
or request.user.username == user.username):
raise PermissionDenied()
now = timezone.now()
findings = Finding.objects.filter(reporter=user, verified=True)
closed_findings = Finding.objects.filter(mitigated_by=user)
open_findings = findings.exclude(mitigated__isnull=False)
open_month = findings.filter(date__year=now.year, date__month=now.month)
accepted_month = [finding for ra in Risk_Acceptance.objects.filter(
created__range=[datetime(now.year,
now.month, 1,
tzinfo=timezone.get_current_timezone()),
datetime(now.year,
now.month,
monthrange(now.year,
now.month)[1],
tzinfo=timezone.get_current_timezone())],
owner=user)
for finding in ra.accepted_findings.all()]
closed_month = []
for f in closed_findings:
if f.mitigated and f.mitigated.year == now.year and f.mitigated.month == now.month:
closed_month.append(f)
o_dict, open_count = count_findings(open_month)
c_dict, closed_count = count_findings(closed_month)
a_dict, accepted_count = count_findings(accepted_month)
day_list = [now - relativedelta(weeks=1,
weekday=x,
hour=0,
minute=0,
second=0)
for x in range(now.weekday())]
day_list.append(now)
q_objects = (Q(date=d) for d in day_list)
closed_week = []
open_week = findings.filter(reduce(operator.or_, q_objects))
accepted_week = [finding for ra in Risk_Acceptance.objects.filter(
owner=user, created__range=[day_list[0], day_list[-1]])
for finding in ra.accepted_findings.all()]
q_objects = (Q(mitigated=d) for d in day_list)
# closed_week= findings.filter(reduce(operator.or_, q_objects))
for f in closed_findings:
if f.mitigated and f.mitigated >= day_list[0]:
closed_week.append(f)
o_week_dict, open_week_count = count_findings(open_week)
c_week_dict, closed_week_count = count_findings(closed_week)
a_week_dict, accepted_week_count = count_findings(accepted_week)
stuff = []
o_stuff = []
a_stuff = []
findings_this_period(findings, 1, stuff, o_stuff, a_stuff)
# findings_this_period no longer fits the need for accepted findings
# however will use its week finding output to use here
for month in a_stuff:
month_start = datetime.strptime(
month[0].strip(), "%b %Y")
month_end = datetime(month_start.year,
month_start.month,
monthrange(
month_start.year,
month_start.month)[1],
tzinfo=timezone.get_current_timezone())
for finding in [finding for ra in Risk_Acceptance.objects.filter(
created__range=[month_start, month_end], owner=user)
for finding in ra.accepted_findings.all()]:
if finding.severity == 'Critical':
month[1] += 1
if finding.severity == 'High':
month[2] += 1
if finding.severity == 'Medium':
month[3] += 1
if finding.severity == 'Low':
month[4] += 1
month[5] = sum(month[1:])
week_stuff = []
week_o_stuff = []
week_a_stuff = []
findings_this_period(findings, 0, week_stuff, week_o_stuff, week_a_stuff)
# findings_this_period no longer fits the need for accepted findings
# however will use its week finding output to use here
for week in week_a_stuff:
wk_range = week[0].split('-')
week_start = datetime.strptime(
wk_range[0].strip() + " " + str(now.year), "%b %d %Y")
week_end = datetime.strptime(
wk_range[1].strip() + " " + str(now.year), "%b %d %Y")
for finding in [finding for ra in Risk_Acceptance.objects.filter(
created__range=[week_start, week_end], owner=user)
for finding in ra.accepted_findings.all()]:
if finding.severity == 'Critical':
week[1] += 1
if finding.severity == 'High':
week[2] += 1
if finding.severity == 'Medium':
week[3] += 1
if finding.severity == 'Low':
week[4] += 1
week[5] = sum(week[1:])
products = get_authorized_products(Permissions.Product_Type_View)
vulns = {}
for product in products:
f_count = 0
engs = Engagement.objects.filter(product=product)
for eng in engs:
tests = Test.objects.filter(engagement=eng)