Skip to content

Commit

Permalink
Search by vulnerability (allegro#2993)
Browse files Browse the repository at this point in the history
  • Loading branch information
xliiv authored and ar4s committed Aug 28, 2017
1 parent df7e3f2 commit 389e485
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/ralph/admin/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def get_base_ids(self, model, value):
"""
Return IDs for related models.
"""
# TODO: what if search_fileds are empty
search_fields = ralph_site._registry[model].search_fields
if not search_fields:
return []
Expand Down
1 change: 1 addition & 0 deletions src/ralph/admin/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'ralph.virtual.models.CloudProvider': 'ralph.virtual.tests.factories.CloudProviderFactory', # noqa
'ralph.virtual.models.VirtualServer': 'ralph.virtual.tests.factories.VirtualServerFullFactory', # noqa
'ralph.virtual.models.VirtualServerType': 'ralph.virtual.tests.factories.VirtualServerTypeFactory', # noqa
'ralph.security.models.Vulnerability': 'ralph.security.tests.factories.VulnerabilityFactory', # noqa
}

EXCLUDE_MODELS = [
Expand Down
12 changes: 10 additions & 2 deletions src/ralph/data_center/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.db.models import Prefetch, Q
from django.utils.translation import ugettext_lazy as _

from ralph.admin import RalphAdmin, RalphTabularInline, register
from ralph.admin import filters, RalphAdmin, RalphTabularInline, register
from ralph.admin.filters import (
BaseObjectHostnameFilter,
ChoicesListFilter,
Expand Down Expand Up @@ -349,7 +349,11 @@ class DataCenterAssetAdmin(
'budget_info', 'rack', 'rack__server_room',
'rack__server_room__data_center', 'position', 'property_of',
LiquidatedStatusFilter, IPFilter, TagsListFilter,
'fibrechannelcard_set__wwn'
'fibrechannelcard_set__wwn',
(
'securityscan__vulnerabilities',
filters.RelatedAutocompleteFieldListFilter
),
]
date_hierarchy = 'created'
list_select_related = [
Expand Down Expand Up @@ -558,6 +562,10 @@ class DCHostAdmin(ScanStatusInChangeListMixin, RalphAdmin):
('content_type', DCHostTypeListFilter),
MacAddressFilter,
IPFilter,
(
'securityscan__vulnerabilities',
filters.RelatedAutocompleteFieldListFilter
),
]
list_select_related = [
'content_type',
Expand Down
14 changes: 13 additions & 1 deletion src/ralph/security/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from django.utils.translation import ugettext_lazy as _

from ralph.assets.models.base import BaseObject
from ralph.lib.mixins.models import TaggableMixin, TimeStampMixin
from ralph.lib.mixins.models import (
AdminAbsoluteUrlMixin,
TaggableMixin,
TimeStampMixin
)
from ralph.lib.permissions import PermByFieldMixin


Expand All @@ -27,6 +31,7 @@ class Risk(Choices):


class Vulnerability(
AdminAbsoluteUrlMixin,
PermByFieldMixin,
TimeStampMixin,
TaggableMixin,
Expand All @@ -51,6 +56,13 @@ class Vulnerability(
def is_deadline_exceeded(self):
return self.patch_deadline < datetime.now()

def __str__(self):
deadline = (
self.patch_deadline.strftime('%Y-%m-%d') if
self.patch_deadline else '-'
)
return "{} ({})".format(self.name, deadline)


class SecurityScan(
PermByFieldMixin,
Expand Down
7 changes: 7 additions & 0 deletions src/ralph/security/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ralph.admin import RalphAdmin, register
from ralph.admin.views.extra import RalphDetailView
from ralph.security.models import Vulnerability


class ScanStatusInChangeListMixin(object):
Expand Down Expand Up @@ -43,6 +45,11 @@ def scan_status(self, obj):
scan_status.short_description = _('Security scan')


@register(Vulnerability)
class Vulnerability(RalphAdmin):
search_fields = ['name', ]


class SecurityInfo(RalphDetailView):

icon = 'lock'
Expand Down
9 changes: 8 additions & 1 deletion src/ralph/virtual/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
BaseObjectHostnameFilter,
IPFilter,
MacAddressFilter,
RelatedAutocompleteFieldListFilter,
TagsListFilter,
TreeRelatedAutocompleteFilterWithDescendants
)
Expand Down Expand Up @@ -96,7 +97,10 @@ class VirtualServerAdmin(
list_filter = [
BaseObjectHostnameFilter, 'sn', 'service_env', IPFilter,
'parent', TagsListFilter, MacAddressFilter,
('configuration_path__module', TreeRelatedAutocompleteFilterWithDescendants) # noqa
('configuration_path__module', TreeRelatedAutocompleteFilterWithDescendants), # noqa
(
'securityscan__vulnerabilities', RelatedAutocompleteFieldListFilter
),
]
list_display = [
'hostname', 'type', 'sn', 'service_env', 'configuration_path',
Expand Down Expand Up @@ -211,6 +215,9 @@ class CloudHostAdmin(
list_filter = [
BaseObjectHostnameFilter, 'cloudprovider', 'service_env',
'cloudflavor', TagsListFilter,
(
'securityscan__vulnerabilities', RelatedAutocompleteFieldListFilter
),
]
list_select_related = [
'cloudflavor', 'cloudprovider', 'parent__cloudproject',
Expand Down

0 comments on commit 389e485

Please sign in to comment.