Skip to content

Commit

Permalink
Merge pull request #188 from rackerlabs/email-alerting-bug
Browse files Browse the repository at this point in the history
Fixed email alerting issue
  • Loading branch information
derpadoo authored Mar 16, 2020
2 parents 506a189 + af5ac69 commit 2b95f7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion master/django_scantron/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.25"
__version__ = "1.26"
23 changes: 13 additions & 10 deletions master/django_scantron/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Third party Python libraries.
from django.conf import settings
from django.http import Http404, HttpResponse
from django.http import Http404
import redis
from rest_framework import viewsets
import rq
Expand Down Expand Up @@ -134,27 +134,30 @@ class ScheduledScanViewSet(DefaultsMixin, viewsets.ModelViewSet):
def partial_update(self, request, pk=None, **kwargs):

try:
# Filter only the applicable ScheduledScans for the agent. Prevents an agent modifying another agent's
# ScheduledScan information.
obj = ScheduledScan.objects.filter(scan_agent=request.user).get(pk=pk) # noqa

# Extract the json payload.
body = self.request.data

if body["scan_status"] in ["started", "completed", "error"]:

# Filter only the applicable ScheduledScans for the agent. Prevents an agent modifying another agent's
# ScheduledScan information.
scheduled_scan_dict = (
ScheduledScan.objects.filter(scan_agent=request.user).filter(pk=pk).values()[0]
) # noqa

# Update the scheduled_scan_dict with the most recent scan_status state from the PUT request. When
# originally querying above, the old state is passed to utility.py unless it is updated.
scheduled_scan_dict["scan_status"] = body["scan_status"]

# Create a redis connection object.
redis_conn = redis.Redis(host="127.0.0.1", port=6379, db=0)

# Create a redis queue object.
q = rq.Queue(connection=redis_conn)

queue_object = {
"site_name": obj.site_name,
"scan_status": body["scan_status"],
}

job = q.enqueue(utility.process_scan_status_change, queue_object) # noqa
# Queue up the scheduled_scan_dict to be processed by utility.py.
job = q.enqueue(utility.process_scan_status_change, scheduled_scan_dict) # noqa

else:
raise Http404
Expand Down
25 changes: 10 additions & 15 deletions master/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,18 @@
# https://github.com/pennersr/django-allauth/blob/7b81531bc89ae98dc6f687611743db5b36cda9a2/allauth/account/adapter.py#L448


def process_scan_status_change(queue_object):
def process_scan_status_change(scheduled_scan_dict):
"""When a scan finishes, execute other tasks based off settings."""

logger.info(f"queue_object: {process_scan_status_change}")
logger.info(f"scheduled_scan_dict: {scheduled_scan_dict}")

# Extract values from passed dictionary.
site_name = queue_object["site_name"]
scan_status = queue_object["scan_status"]

# Look up scheduled scan information.
scheduled_scan = django_connector.ScheduledScan.objects.filter(site_name=site_name)[0]
scheduled_scan_id = scheduled_scan.id

# Determine the scan binary used.
scan_binary = scheduled_scan.scan_binary
# Extract values from passed ScheduleScan object.
scheduled_scan_id = scheduled_scan_dict["id"]
scan_status = scheduled_scan_dict["scan_status"]
scan_binary = scheduled_scan_dict["scan_binary"]

# Retrieve site information.
site_name = scheduled_scan_dict["site_name"]
site = django_connector.Site.objects.filter(site_name=site_name)[0]

# Determine if site has email_scan_alerts enabled.
Expand All @@ -74,11 +69,11 @@ def process_scan_status_change(queue_object):

# Provide different links based off the scan binary used.
if scan_binary == "nmap":
body = f"""XML: https://{master_fqdn}/results/{scheduled_scan.id}?file_type=xml
NMAP: https://{master_fqdn}/results/{scheduled_scan.id}?file_type=nmap
body = f"""XML: https://{master_fqdn}/results/{scheduled_scan_id}?file_type=xml
NMAP: https://{master_fqdn}/results/{scheduled_scan_id}?file_type=nmap
"""
else:
body = f"""Results: https://{master_fqdn}/results/{scheduled_scan.id}?file_type=json"""
body = f"""Results: https://{master_fqdn}/results/{scheduled_scan_id}?file_type=json"""

elif scan_status in ["started", "error"]:
body = f""""""
Expand Down

0 comments on commit 2b95f7c

Please sign in to comment.