Skip to content

Commit

Permalink
Add VulnerabilitesByPatchDeadline field to search (allegro#2994)
Browse files Browse the repository at this point in the history
  • Loading branch information
xliiv authored and ar4s committed Aug 28, 2017
1 parent 389e485 commit 1f382b2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/ralph/admin/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ def choices(self, cl):
}


class VulnerabilitesByPatchDeadline(DateListFilter):

def queryset(self, request, queryset):
from ralph.security.models import SecurityScan
if any(self.value()):
filters = {}
date_start, date_end = self.value()
if date_start:
filters['vulnerabilities__patch_deadline__gte'] = date_start
if date_end:
filters['vulnerabilities__patch_deadline__lte'] = date_end
base_objects_ids = SecurityScan.objects.filter(
**filters
).values_list('base_object_id', flat=True)
queryset = queryset.filter(id__in=base_objects_ids)
return queryset


class NumberListFilter(DateListFilter):

"""Renders filter form with decimal field."""
Expand Down
5 changes: 4 additions & 1 deletion src/ralph/data_center/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
MacAddressFilter,
RelatedAutocompleteFieldListFilter,
TagsListFilter,
TreeRelatedAutocompleteFilterWithDescendants
TreeRelatedAutocompleteFilterWithDescendants,
VulnerabilitesByPatchDeadline
)
from ralph.admin.helpers import generate_html_link
from ralph.admin.m2m import RalphTabularM2MInline
Expand Down Expand Up @@ -350,6 +351,7 @@ class DataCenterAssetAdmin(
'rack__server_room__data_center', 'position', 'property_of',
LiquidatedStatusFilter, IPFilter, TagsListFilter,
'fibrechannelcard_set__wwn',
('securityscan__vulnerabilities__patch_deadline', VulnerabilitesByPatchDeadline), # noqa
(
'securityscan__vulnerabilities',
filters.RelatedAutocompleteFieldListFilter
Expand Down Expand Up @@ -562,6 +564,7 @@ class DCHostAdmin(ScanStatusInChangeListMixin, RalphAdmin):
('content_type', DCHostTypeListFilter),
MacAddressFilter,
IPFilter,
('securityscan__vulnerabilities__patch_deadline', VulnerabilitesByPatchDeadline), # noqa
(
'securityscan__vulnerabilities',
filters.RelatedAutocompleteFieldListFilter
Expand Down
72 changes: 71 additions & 1 deletion src/ralph/data_center/tests/test_view.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datetime import datetime, timedelta
from urllib.parse import urlencode

from django.core.urlresolvers import reverse
from django.test import TestCase

Expand All @@ -6,7 +9,10 @@
DataCenterAssetFullFactory
)
from ralph.security.models import ScanStatus
from ralph.security.tests.factories import SecurityScanFactory
from ralph.security.tests.factories import (
SecurityScanFactory,
VulnerabilityFactory
)
from ralph.tests.mixins import ClientMixin
from ralph.virtual.tests.factories import (
CloudHostFullFactory,
Expand Down Expand Up @@ -152,3 +158,67 @@ def test_listing_show_failed_icon_when_scan_error(self):
reverse('admin:data_center_dchost_changelist'),
)
self.assertContains(result, "Scan failed")


class DCHostFilterByPatchDeadline(ClientMixin, TestCase):
def setUp(self):
self.login_as_user()
self.asset_no_vuls = DataCenterAssetFullFactory(
rack__name='Rack #1',
rack__server_room__name='SR1',
rack__server_room__data_center__name='DC1',
)
self.scan_no_vuls = SecurityScanFactory(
base_object=self.asset_no_vuls.baseobject_ptr, vulnerabilities=[],
)

self.today = datetime.now()
self.yesterday = self.today + timedelta(days=-1)
self.tomorrow = self.today + timedelta(days=1)

self.asset_with_today_vul = DataCenterAssetFullFactory(
rack__name='Rack #1',
rack__server_room__name='SR1',
rack__server_room__data_center__name='DC1',
)
self.scan_with_vuls2 = SecurityScanFactory(
base_object=self.asset_with_today_vul.baseobject_ptr,
vulnerabilities=[
VulnerabilityFactory(
patch_deadline=self.today,
)
]
)

self.asset_vuls2 = DataCenterAssetFullFactory(
rack__name='Rack #1',
rack__server_room__name='SR1',
rack__server_room__data_center__name='DC1',
)
self.scan_with_vuls2 = SecurityScanFactory(
base_object=self.asset_vuls2.baseobject_ptr,
vulnerabilities=[
VulnerabilityFactory(
patch_deadline=self.today + timedelta(days=30)
)
]
)

def test_patch_deadline_filters_hosts(self):
FORMAT = '%Y-%m-%d'
url = (
'?'.join([
reverse('admin:data_center_dchost_changelist',),
urlencode({
'securityscan__vulnerabilities__patch_deadline__start': self.yesterday.strftime(FORMAT), # noqa
'securityscan__vulnerabilities__patch_deadline__end': self.tomorrow.strftime(FORMAT), # noqa
})
])
)

response = self.client.get(url, follow=True)

self.assertEqual(
int(response.context_data['object_id']),
self.asset_with_today_vul.id,
)
5 changes: 4 additions & 1 deletion src/ralph/virtual/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
MacAddressFilter,
RelatedAutocompleteFieldListFilter,
TagsListFilter,
TreeRelatedAutocompleteFilterWithDescendants
TreeRelatedAutocompleteFilterWithDescendants,
VulnerabilitesByPatchDeadline
)
from ralph.assets.models.components import Ethernet
from ralph.assets.views import ComponentsAdminView, RalphDetailViewAdmin
Expand Down Expand Up @@ -98,6 +99,7 @@ class VirtualServerAdmin(
BaseObjectHostnameFilter, 'sn', 'service_env', IPFilter,
'parent', TagsListFilter, MacAddressFilter,
('configuration_path__module', TreeRelatedAutocompleteFilterWithDescendants), # noqa
('securityscan__vulnerabilities__patch_deadline', VulnerabilitesByPatchDeadline), # noqa
(
'securityscan__vulnerabilities', RelatedAutocompleteFieldListFilter
),
Expand Down Expand Up @@ -215,6 +217,7 @@ class CloudHostAdmin(
list_filter = [
BaseObjectHostnameFilter, 'cloudprovider', 'service_env',
'cloudflavor', TagsListFilter,
('securityscan__vulnerabilities__patch_deadline', VulnerabilitesByPatchDeadline), # noqa
(
'securityscan__vulnerabilities', RelatedAutocompleteFieldListFilter
),
Expand Down

0 comments on commit 1f382b2

Please sign in to comment.