Verified Commit 4174b6df authored by Elger Jonker's avatar Elger Jonker

determine which map layers can be reported, so a report can be made before it is displayed

parent 27df2385
Pipeline #36561699 (#1170) passed with stages
in 20 minutes and 31 seconds
......@@ -177,7 +177,8 @@ def add_configuration(country, organization_type):
@admin.register(Configuration)
class ConfigurationAdmin(SortableAdminMixin, ImportExportModelAdmin, admin.ModelAdmin):
list_display = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned', )
list_display = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned',
'is_reported')
search_fields = (['country', 'organization_type', ])
list_filter = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned',)
fields = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned', )
......@@ -220,6 +221,24 @@ class ConfigurationAdmin(SortableAdminMixin, ImportExportModelAdmin, admin.Model
stop_scanning.short_description = '💔 Stop scanning'
actions.append(stop_scanning)
def allow_reporting(self, request, queryset):
for configuration in queryset:
configuration.is_reported = True
configuration.save()
allow_reporting.short_description = '📄️ Allow Reporting'
actions.append(allow_reporting)
def stop_reporting(self, request, queryset):
for configuration in queryset:
configuration.is_reported = False
configuration.save()
stop_reporting.short_description = '📄 Stop Reporting'
actions.append(stop_reporting)
def set_default(self, request, queryset):
for configuration in queryset:
......
# Generated by Django 2.1.3 on 2018-11-14 11:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('map', '0029_auto_20181029_0946'),
]
operations = [
migrations.AddField(
model_name='configuration',
name='is_reported',
field=models.BooleanField(default=False, help_text='Whether this combination is shown on the map.'),
),
]
......@@ -267,6 +267,11 @@ class Configuration(models.Model):
default=False
)
is_reported = models.BooleanField(
help_text="Whether this combination is shown on the map.",
default=False
)
is_the_default_option = models.BooleanField(
help_text="Determines if this is the default view. Only one can be selected to be displayed first. If there "
"are multiple, the first one is used. This can lead to unexpected results.",
......
......@@ -12,7 +12,7 @@ from django.db.models import Q
from failmap.map.views import get_map_data
from failmap.organizations.models import Organization, OrganizationType, Url
from failmap.scanners.models import Endpoint, EndpointGenericScan, TlsQualysScan, UrlGenericScan
from failmap.scanners.scanner.scanner import q_configurations_to_display
from failmap.scanners.scanner.scanner import q_configurations_to_report
from ..celery import Task, app
from .calculate import get_calculation
......@@ -48,11 +48,14 @@ def compose_task(
if endpoints_filter:
raise NotImplementedError('This scanner does not work on a endpoint level.')
log.info(organizations_filter)
log.info("Organization filter: %s" % organizations_filter)
log.info("Url filter: %s" % urls_filter)
# Only displayed configurations are reported. Because why have reports on things you don't display?
# apply filter to organizations (or if no filter, all organizations)
organizations = Organization.objects.filter(q_configurations_to_display('organization'), **organizations_filter)
organizations = Organization.objects.filter(q_configurations_to_report('organization'), **organizations_filter)
log.debug("Organizations: %s" % len(organizations))
# Create tasks for rebuilding ratings for selected organizations and urls.
# Wheneven a url has been (re)rated the organization for that url need to
......@@ -64,7 +67,7 @@ def compose_task(
tasks = []
for organization in organizations:
urls = Url.objects.filter(q_configurations_to_display(), organization=organization, **urls_filter)
urls = Url.objects.filter(q_configurations_to_report(), organization=organization, **urls_filter)
if not urls:
continue
......@@ -76,8 +79,8 @@ def compose_task(
log.error("Could not rebuild reports, filters resulted in no tasks created.")
log.debug("Organization filter: %s" % organizations_filter)
log.debug("Url filter: %s" % urls_filter)
log.debug("urls to display: %s" % q_configurations_to_display())
log.debug("organizatins to display: %s" % q_configurations_to_display('organization'))
log.debug("urls to display: %s" % q_configurations_to_report())
log.debug("organizatins to display: %s" % q_configurations_to_report('organization'))
raise Exception('Applied filters resulted in no tasks!')
# when trying to report on a specific url or organization (so not everything) also don't rebuild all caches
......@@ -91,6 +94,8 @@ def compose_task(
# the entire database. So this can just be two days as well.
days = 2
log.debug("Number of tasks: %s" % len(tasks))
# finally, rebuild the graphs (which can mis-matchi a bit if the last reports aren't in yet. Will have to do for now
# mainly as we're trying to get away from canvas and it's buggyness.
tasks.append(calculate_vulnerability_statistics.si(days))
......
......@@ -103,6 +103,33 @@ def q_configurations_to_scan(level: str = 'url'):
return qs
def q_configurations_to_report(level: str = 'url'):
"""
Retrieves configurations and makes q-queries for them. You can select if you want to have the q-queries directly
for the organization tables, or with a join from url to organization.
To evaluate: This is a quick fix, the organization might refer to configuration in the future?
An emtpy set means EVERYTHING will be reported.
:param level:
:return:
"""
configurations = list(Configuration.objects.all().filter(is_reported=True).values('country', 'organization_type'))
qs = Q()
if level == 'organization':
for configuration in configurations:
qs.add(Q(type=configuration['organization_type'], country=configuration['country']), Q.OR)
if level == 'url':
for configuration in configurations:
qs.add(Q(organization__type=configuration['organization_type'],
organization__country=configuration['country']), Q.OR)
return qs
def q_configurations_to_display(level: str = 'url'):
"""
Retrieves configurations and makes q-queries for them. You can select if you want to have the q-queries directly
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment