Skip to content

Commit 4d10c51

Browse files
committed
Improving logging notifications for PDF reports and jira exceptions.
1 parent ebb8237 commit 4d10c51

File tree

5 files changed

+64
-38
lines changed

5 files changed

+64
-38
lines changed

dojo/engagement/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def edit_engagement(request, eid):
117117
form = EngForm2(request.POST, instance=eng)
118118
if 'jiraform-push_to_jira' in request.POST:
119119
jform = JIRAFindingForm(request.POST, prefix='jiraform', enabled=True)
120-
if form.is_valid():
120+
if form.is_valid() and jform and jform.is_valid():
121121
if 'jiraform-push_to_jira' in request.POST:
122122
try:
123123
jissue = JIRA_Issue.objects.get(engagement=eng)

dojo/tasks.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131
logger = get_task_logger(__name__)
3232

33+
# Logs the error to the alerts table, which appears in the notification toolbar
34+
def log_generic_alert(source, title, description):
35+
create_notification(event='other', title=title, description=description,
36+
icon='bullseye', source=source)
37+
3338
@app.task(bind=True)
3439
def add_alerts(self, runinterval):
3540
now = timezone.now()
@@ -72,8 +77,8 @@ def async_pdf_report(self,
7277
cover = context['host'] + reverse(
7378
'report_cover_page') + "?" + x
7479

75-
config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)
7680
try:
81+
config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH)
7782
report.task_id = async_pdf_report.request.id
7883
report.save()
7984
bytes = render_to_string(template, context)
@@ -102,8 +107,7 @@ def async_pdf_report(self,
102107
except Exception as e:
103108
report.status = 'error'
104109
report.save()
105-
# email_requester(report, uri, error=e)
106-
raise e
110+
log_generic_alert("PDF Report", "Report Creation Failure", "Make sure WKHTMLTOPDF is installed. " + str(e))
107111
return True
108112

109113

@@ -187,7 +191,8 @@ def async_custom_pdf_report(self,
187191
report.status = 'error'
188192
report.save()
189193
# email_requester(report, uri, error=e)
190-
raise e
194+
#raise e
195+
log_generic_alert("PDF Report", "Report Creation Failure", "Make sure WKHTMLTOPDF is installed. " + str(e))
191196
finally:
192197
if temp is not None:
193198
# deleting temp xsl file

dojo/templates/dojo/view_product.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<div class="clearfix">
1313
<h3 class="pull-left">
1414
{{ prod }}
15-
{% if system_settings.enable_benchmark%}
15+
{% if system_settings.enable_benchmark %}
1616
{% for benchmark in benchmarks%}
1717
<sup><span class="fa fa-bookmark has-popover" data-trigger="hover" data-content="{{ benchmark|asvs_level }}"></label></span>
1818
{{ benchmark.desired_level }}</sup>

dojo/tools/checkmarx/parser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def __init__(self, filename, test):
1717
for query in root.findall('Query'):
1818
categories = ''
1919
language = ''
20-
mitigation = ''
21-
impact = ''
20+
mitigation = 'N/A'
21+
impact = 'N/A'
2222
references = ''
2323
findingdetail = ''
2424
title = ''
@@ -69,8 +69,7 @@ def __init__(self, filename, test):
6969
title = query.get('name').replace('_', ' ') + ' (' + path.get('PathId') + ')'
7070
for pathnode in path.findall('PathNode'):
7171
findingdetail += 'Source Object: %s\n' % (pathnode.find('Name').text)
72-
#findingdetail += 'Filename: %s\n' % (pathnode.find('FileName').text)
73-
#findingdetail += 'Line Number: %s\n' % (pathnode.find('Line').text)
72+
7473
for codefragment in pathnode.findall('Snippet/Line'):
7574
findingdetail += 'Code: %s\n' % (codefragment.find('Code').text.strip())
7675

dojo/utils.py

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,11 @@ def jira_get_resolution_id(jira, issue, status):
701701
def jira_change_resolution_id(jira, issue, id):
702702
jira.transition_issue(issue, id)
703703

704+
# Logs the error to the alerts table, which appears in the notification toolbar
705+
def log_jira_generic_alert(title, description):
706+
create_notification(event='jira_update', title=title, description=description,
707+
icon='bullseye', source='Jira')
708+
704709
# Logs the error to the alerts table, which appears in the notification toolbar
705710
def log_jira_alert(error, finding):
706711
create_notification(event='jira_update', title='Jira update issue', description='Finding: ' + str(finding.id) + ', ' + error,
@@ -856,22 +861,30 @@ def close_epic(eng, push_to_jira):
856861
jpkey = JIRA_PKey.objects.get(product=prod)
857862
jira_conf = jpkey.conf
858863
if jpkey.enable_engagement_epic_mapping and push_to_jira:
859-
j_issue = JIRA_Issue.objects.get(engagement=eng)
860-
req_url = jira_conf.url+'/rest/api/latest/issue/'+ j_issue.jira_id+'/transitions'
861-
j_issue = JIRA_Issue.objects.get(engagement=eng)
862-
json_data = {'transition':{'id':jira_conf.close_status_key}}
863-
r = requests.post(url=req_url, auth=HTTPBasicAuth(jira_conf.username, jira_conf.password), json=json_data)
864+
try:
865+
j_issue = JIRA_Issue.objects.get(engagement=eng)
866+
req_url = jira_conf.url+'/rest/api/latest/issue/'+ j_issue.jira_id+'/transitions'
867+
j_issue = JIRA_Issue.objects.get(engagement=eng)
868+
json_data = {'transition':{'id':jira_conf.close_status_key}}
869+
r = requests.post(url=req_url, auth=HTTPBasicAuth(jira_conf.username, jira_conf.password), json=json_data)
870+
except Exception as e:
871+
log_jira_generic_alert('Jira Engagement/Epic Close Error', e)
872+
pass
864873

865874
def update_epic(eng, push_to_jira):
866875
engagement = eng
867876
prod = Product.objects.get(engagement=engagement)
868877
jpkey = JIRA_PKey.objects.get(product=prod)
869878
jira_conf = jpkey.conf
870879
if jpkey.enable_engagement_epic_mapping and push_to_jira:
871-
jira = JIRA(server=jira_conf.url, basic_auth=(jira_conf.username, jira_conf.password))
872-
j_issue = JIRA_Issue.objects.get(engagement=eng)
873-
issue = jira.issue(j_issue.jira_id)
874-
issue.update(summary=eng.name, description=eng.name)
880+
try:
881+
jira = JIRA(server=jira_conf.url, basic_auth=(jira_conf.username, jira_conf.password))
882+
j_issue = JIRA_Issue.objects.get(engagement=eng)
883+
issue = jira.issue(j_issue.jira_id)
884+
issue.update(summary=eng.name, description=eng.name)
885+
except Exception as e:
886+
log_jira_generic_alert('Jira Engagement/Epic Update Error', e)
887+
pass
875888

876889
def add_epic(eng, push_to_jira):
877890
engagement = eng
@@ -886,19 +899,27 @@ def add_epic(eng, push_to_jira):
886899
'issuetype': {'name': 'Epic'},
887900
'customfield_' + str(jira_conf.epic_name_id) : engagement.name,
888901
}
889-
jira = JIRA(server=jira_conf.url, basic_auth=(jira_conf.username, jira_conf.password))
890-
new_issue = jira.create_issue(fields=issue_dict)
891-
j_issue = JIRA_Issue(jira_id=new_issue.id, jira_key=new_issue, engagement=engagement)
892-
j_issue.save()
902+
try:
903+
jira = JIRA(server=jira_conf.url, basic_auth=(jira_conf.username, jira_conf.password))
904+
new_issue = jira.create_issue(fields=issue_dict)
905+
j_issue = JIRA_Issue(jira_id=new_issue.id, jira_key=new_issue, engagement=engagement)
906+
j_issue.save()
907+
except Exception as e:
908+
log_jira_generic_alert('Jira Engagement/Epic Creation Error', e)
909+
pass
893910

894911
def add_comment(find, note, force_push=False):
895912
prod = Product.objects.get(engagement=Engagement.objects.get(test=find.test))
896913
jpkey = JIRA_PKey.objects.get(product=prod)
897914
jira_conf = jpkey.conf
898915
if jpkey.push_notes or force_push == True:
899-
jira = JIRA(server=jira_conf.url, basic_auth=(jira_conf.username, jira_conf.password))
900-
j_issue = JIRA_Issue.objects.get(finding=find)
901-
jira.add_comment(j_issue.jira_id, '(%s): %s' % (note.author.get_full_name(), note.entry))
916+
try:
917+
jira = JIRA(server=jira_conf.url, basic_auth=(jira_conf.username, jira_conf.password))
918+
j_issue = JIRA_Issue.objects.get(finding=find)
919+
jira.add_comment(j_issue.jira_id, '(%s): %s' % (note.author.get_full_name(), note.entry))
920+
except Exception as e:
921+
log_jira_generic_alert('Jira Add Comment Error', e)
922+
pass
902923

903924
def send_review_email(request, user, finding, users, new_note):
904925
recipients = [u.email for u in users]
@@ -1062,15 +1083,15 @@ def create_notification_message(event, notification_type):
10621083
return notification
10631084

10641085
def send_slack_notification(channel):
1065-
#try:
1066-
res = requests.request(method='POST', url='https://slack.com/api/chat.postMessage',
1067-
data={'token':get_system_setting('slack_token'),
1068-
'channel':channel,
1069-
'username':get_system_setting('slack_username'),
1070-
'text':create_notification_message(event, 'slack')})
1071-
#except Exception as e:
1072-
# log_alert(e)
1073-
# pass
1086+
try:
1087+
res = requests.request(method='POST', url='https://slack.com/api/chat.postMessage',
1088+
data={'token':get_system_setting('slack_token'),
1089+
'channel':channel,
1090+
'username':get_system_setting('slack_username'),
1091+
'text':create_notification_message(event, 'slack')})
1092+
except Exception as e:
1093+
log_alert(e)
1094+
pass
10741095

10751096
def send_hipchat_notification(channel):
10761097
try:
@@ -1079,7 +1100,6 @@ def send_hipchat_notification(channel):
10791100
url='https://%s/v2/room/%s/notification?auth_token=%s' % (get_system_setting('hipchat_site'), channel, get_system_setting('hipchat_token')),
10801101
data={'message':create_notification_message(event, 'slack'),
10811102
'message_format':'text'})
1082-
print res
10831103
except Exception as e:
10841104
log_alert(e)
10851105
pass
@@ -1110,8 +1130,10 @@ def send_alert_notification(user=None):
11101130

11111131

11121132
def log_alert(e):
1113-
alert = Alerts(user_id=Dojo_User.objects.get(is_superuser=True), title='Notification issue', description="%s" % e, icon="exclamation-triangle", source="Notifications")
1114-
alert.save()
1133+
users = Dojo_User.objects.filter(is_superuser=True)
1134+
for user in users:
1135+
alert = Alerts(user_id=user, url=kwargs.get('url', reverse('alerts')), title='Notification issue', description="%s" % e, icon="exclamation-triangle", source="Notifications")
1136+
alert.save()
11151137

11161138
# Global notifications
11171139
try:

0 commit comments

Comments
 (0)