Skip to content

Commit

Permalink
Merge pull request #182 from rackerlabs/empty-key-value-protection-fo…
Browse files Browse the repository at this point in the history
…r-scan_scheduler.py

Empty key-value protection for master/scan_scheduler.py
  • Loading branch information
derpadoo authored Mar 3, 2020
2 parents 42c71b8 + 0089877 commit dde6eb3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def go(self):

if scan_jobs:
for scan_job in scan_jobs:
modules.logger.ROOT_LOGGER.info(f"Executing scan job ID: {scan_job['id']}")
modules.logger.ROOT_LOGGER.info(f"scan_job: {scan_job}")

# Create new dictionary that will contain scan_job and config_data information.
scan_job_dict = {}
Expand Down
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.22"
__version__ = "1.23"
4 changes: 0 additions & 4 deletions master/django_scantron/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,11 @@ class ScheduledScanAdmin(admin.ModelAdmin):
list_display = (
"id",
"site_name",
"site_name_id",
"scan_id",
"start_time",
"scan_agent",
"scan_agent_id",
"start_datetime",
"scan_binary",
"scan_command",
"scan_command_id",
"targets",
"excluded_targets",
"scan_status",
Expand Down
15 changes: 1 addition & 14 deletions master/django_scantron/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,6 @@ class ScheduledScan(models.Model):
],
verbose_name="Site Name",
)
site_name_id = models.IntegerField(
validators=[MinValueValidator(1, message="Site name ID must be greater than 0")], verbose_name="Site name ID"
)
scan_id = models.IntegerField(
validators=[MinValueValidator(1, message="Scan ID must be greater than 0")], verbose_name="Scan ID"
)
start_time = models.TimeField(verbose_name="Scan start time")
scan_agent = models.CharField(
unique=False,
Expand All @@ -221,16 +215,9 @@ class ScheduledScan(models.Model):
],
verbose_name="Agent Name",
)
scan_agent_id = models.IntegerField(
validators=[MinValueValidator(1, message="Scan agent ID must be greater than 0")], verbose_name="Scan agent ID"
)
start_datetime = models.DateTimeField(verbose_name="Scheduled scan start date and time")
scan_binary = models.CharField(max_length=7, default="nmap", verbose_name="Scan binary")
scan_command = models.TextField(unique=False, verbose_name="Scan command")
scan_command_id = models.IntegerField(
validators=[MinValueValidator(1, message="Scan command ID must be greater than 0")],
verbose_name="Scan command ID",
)
targets = models.CharField(
unique=False,
max_length=1_048_576, # 2^20 = 1048576
Expand Down Expand Up @@ -258,7 +245,7 @@ class ScheduledScan(models.Model):
max_length=9, choices=SCAN_STATUS_CHOICES, default="pending", verbose_name="Scan status"
)
completed_time = models.DateTimeField(null=True, blank=True, verbose_name="Scan completion time")
result_file_base_name = models.CharField(max_length=255, blank=True, verbose_name="Result file base name")
result_file_base_name = models.CharField(max_length=255, blank=False, verbose_name="Result file base name")

def __str__(self):
return str(self.id)
Expand Down
25 changes: 17 additions & 8 deletions master/scan_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,17 @@ def main():
# ScanCommand models is updated, it will update the Site model, and cascade to the Scan model.

# Scan model.
scan_id = scan.id # Can delete in future.
scan_start_time = scan.start_time

# Site model.
site_name_id = scan.site.id # Can delete in future.
site_name = scan.site.site_name
targets = scan.site.targets
excluded_targets = scan.site.excluded_targets

# Agent model.
scan_agent_id = scan.site.scan_agent_id # Can delete in future.
scan_agent = scan.site.scan_agent.scan_agent

# ScanCommand model.
scan_command_id = scan.site.scan_command.id # Can delete in future.
scan_command = scan.site.scan_command.scan_command
scan_binary = scan.site.scan_command.scan_binary

Expand All @@ -116,21 +112,34 @@ def main():

scan_dict = {
"site_name": site_name,
"site_name_id": site_name_id, # Can delete in future.
"scan_id": scan_id, # Can delete in future.
"start_time": scan_start_time,
"scan_agent": scan_agent,
"scan_agent_id": scan_agent_id, # Can delete in future.
"start_datetime": start_datetime,
"scan_binary": scan_binary,
"scan_command": scan_command,
"scan_command_id": scan_command_id, # Can delete in future.
"targets": targets,
"excluded_targets": excluded_targets,
"result_file_base_name": result_file_base_name,
"scan_status": "pending",
}

# Ensure none of the values are empty. blank=False is only enforced through forms, which this method of
# creating a scheduled scan does not honor.
empty_scan_dict_value_detected = False

for key, value in scan_dict.items():

# Ignore fields that are allowed to be empty.
if key in ["excluded_targets"]:
continue

if not value:
ROOT_LOGGER.error(f"scan_dict['{key}'] has an empty value.")
empty_scan_dict_value_detected = True

if empty_scan_dict_value_detected:
continue

try:
# Add entry to ScheduledScan model. Convert dictionary to kwargs using **.
# https://stackoverflow.com/questions/5710391/converting-python-dict-to-kwargs
Expand Down

0 comments on commit dde6eb3

Please sign in to comment.