aboutsummaryrefslogtreecommitdiffstats
path: root/lib/acme/tables.py
blob: ee14136be7300c782f7279cb036d69cd13ef7629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Security Response Tool Implementation
#
# Copyright (C) 2017       Wind River Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from srtgui.widgets import ToasterTable
from orm.models import SrtSetting
from orm.models import Cve, Vulnerability, Investigation, CweTable, Product
from orm.models import Package, PackageToCve
from orm.models import CpeTable, CpeFilter, Defect, DataSource
from orm.models import PublishPending
from orm.models import Notify, NotifyAccess, NotifyCategories
from users.models import SrtUser, UserSafe

from django.db.models import Q, Max, Sum, Count, When, Case, Value, IntegerField
from django.conf.urls import url
from django.urls import reverse, resolve
from django.http import HttpResponse
from django.views.generic import TemplateView

from srtgui.tablefilter import TableFilter
from srtgui.tablefilter import TableFilterActionToggle
from srtgui.tablefilter import TableFilterActionDateRange
from srtgui.tablefilter import TableFilterActionDay

import os
import re

# quick development/debugging support
from srtgui.api import _log

class AcmeProductsTable(ToasterTable):
    """Table of All Products in SRTool"""

    def __init__(self, *args, **kwargs):
        super(AcmeProductsTable, self).__init__(*args, **kwargs)
        self.default_orderby = "order"

    def get_context_data(self, **kwargs):
        context = super(AcmeProductsTable, self).get_context_data(**kwargs)
        return context


    def setup_queryset(self, *args, **kwargs):
        self.queryset = \
                Product.objects.all()

        self.queryset = self.queryset.order_by(self.default_orderby)

    def setup_filters(self, *args, **kwargs):
        # Execution outcome types filter
        investigations = TableFilter(name="investigations",
                                       title="Filter Investigations by status")

        exec_investigation_action_open = TableFilterActionToggle(
            "open",
            "Open",
            Q(status=1))

        exec_investigation_action_open_high = TableFilterActionToggle(
            "not_executed",
            "Open High Priority",
            Q(status=1))

        investigations.add_action(exec_investigation_action_open)
        investigations.add_action(exec_investigation_action_open_high)
        self.add_filter(investigations)

    def setup_columns(self, *args, **kwargs):

        self.add_column(title="Order",
                        field_name="order",
                        hideable=False,
                        )

        key_link_template = '''
            <a href="{% url 'acmeproduct' data.id %}" id="dataid_{{data.id}}">{{data.key}}<a>
        '''
        self.add_column(title="ACME Key",
                        hideable=False,
                        orderable=True,
                        field_name="key",
                        static_data_name="key",
                        static_data_template=key_link_template,
                        )

        self.add_column(title="ACME Name",
                        field_name="name",
                        hideable=False,
                        )

        self.add_column(title="ACME Version",
                        field_name="version",
                        hideable=False,
                        )

        self.add_column(title="ACME Extension",
                        field_name="profile",
                        hideable=False,
                        )

        self.add_column(title="CPE",
                        field_name="cpe",
                        hideable=True,
                        hidden=True,
                        )

        self.add_column(title="Defect Tags",
                        field_name="defect_tags",
                        hideable=False,
                        )

        self.add_column(title="Product Tags",
                        field_name="product_tags",
                        hideable=False,
                        )


        investigations_link_template = '''
            {% if data.product_investigation.all.count %}
                <a href="{% url 'investigations' %}?filter=is_product:{{data.key}}&">
                    {{data.product_investigation.all.count}}
                </a>
            {% else %}0{% endif %}
        '''
        self.add_column(title="Investigations",
                    field_name="investigations",
                    hidden=False,
                    static_data_name="investigations",
                    static_data_template=investigations_link_template,
                    )

        defects_link_template = '''
            {% if data.product_defect.all.count %}
                <a href="{% url 'defects' %}?filter=is_product:{{data.key}}&">
                    {{data.product_defect.all.count}}
                </a>
            {% else %}0{% endif %}
        '''
        self.add_column(title="Defects",
                    field_name="defects",
                    hidden=False,
                    static_data_name="defects",
                    static_data_template=defects_link_template,
                    )


class AcmeDefectsTable(ToasterTable):
    """Table of All Defects in SRTool"""

    def __init__(self, *args, **kwargs):
        super(AcmeDefectsTable, self).__init__(*args, **kwargs)
        self.default_orderby = "name"

    def get_context_data(self, **kwargs):
        context = super(AcmeDefectsTable, self).get_context_data(**kwargs)
        return context


    def setup_filters(self, *args, **kwargs):
        # Priority filter
        is_priority = TableFilter(name="is_priority",
                                       title="Filter defects by 'Priority'")
        for priority in range(len(Defect.Priority)):
            is_priority.add_action(TableFilterActionToggle(
                Defect.Priority[priority][1].lower().replace(' ','_'),
                Defect.Priority[priority][1],
                Q(priority=Defect.Priority[priority][0]))
                )
        self.add_filter(is_priority)

        # Status filter
        is_status = TableFilter(name="is_status",
                                       title="Filter defects by 'Status'")
        for status in range(len(Defect.Status)):
            is_status.add_action(TableFilterActionToggle(
                Defect.Status[status][1].lower().replace(' ','_'),
                Defect.Status[status][1],
                Q(status=Defect.Status[status][0]))
                )
        self.add_filter(is_status)

        # Resolution filter
        is_resolution = TableFilter(name="is_resolution",
                                       title="Filter defects by 'Resolution'")
        for resolution in range(len(Defect.Resolution)):
            is_resolution.add_action(TableFilterActionToggle(
                Defect.Resolution[resolution][1].lower().replace(' ','_'),
                Defect.Resolution[resolution][1],
                Q(resolution=Defect.Resolution[resolution][0]))
                )
        self.add_filter(is_resolution)

        # Product filter
        #(name="Acme",version="Looney")
        is_product = TableFilter(name="is_product",
                                       title="Filter defects by 'Product'")
        for product in Product.objects.all():
            is_product.add_action( TableFilterActionToggle(
                product.key,
                product.long_name,
                Q(product=product))
                )
        self.add_filter(is_product)


    def setup_queryset(self, *args, **kwargs):
        self.queryset = \
                Defect.objects.all()

        self.queryset = self.queryset.order_by(self.default_orderby)

    def setup_columns(self, *args, **kwargs):

        name_link_template = '''
            <a href="{% url 'defect_name' data.name %}" id="dataid_{{data.id}}">{{data.name}}</a>
        '''
        self.add_column(title="Name",
                        hideable=False,
                        orderable=True,
                        field_name="name",
                        static_data_name="name",
                        static_data_template=name_link_template,
                        )

        self.add_column(title="ACME Summary",
                        field_name="summary",
                        )

        self.add_column(title="ACME Priority",
                        hideable=False,
                        field_name="priority",
                        orderable=True,
                        filter_name="is_priority",
                        static_data_name="priority",
                        static_data_template='{{data.get_priority_text}}',
                        )

        self.add_column(title="ACME Status",
                        hideable=False,
                        field_name="status",
                        orderable=True,
                        filter_name="is_status",
                        static_data_name="status",
                        static_data_template='{{data.get_status_text}}',
                        )

        self.add_column(title="ACME Resolution",
                        hideable=False,
                        field_name="resolution",
                        orderable=True,
                        filter_name="is_resolution",
                        static_data_name="resolution",
                        static_data_template='{{data.get_resolution_text}}',
                        )

        self.add_column(title="ACME Release Version",
                        hideable=True,
                        orderable=True,
                        field_name="release_version",
                        )

        self.add_column(title="ACME Publish",
                        hideable=True,
                        orderable=True,
                        field_name="publish",
                        )

        url_link_template = '''
            <a href="{{data.url}}" target="_blank">{{data.url}}</a>
        '''
        self.add_column(title="URL",
                        field_name="url",
                        hideable=True,
                        hidden=True,
                        static_data_name="url",
                        static_data_template=url_link_template,
                        )

        investigations_link_template = '''
            {% for di in data.defect_to_investigation.all %}
              {% if not forloop.first %} {% endif %}<a href="{% url 'investigation' di.investigation.name %}" target="_blank">{{di.investigation.name}} </a>
            {% endfor %}
        '''
        self.add_column(title="Investigation",
                        hideable=True,
                        static_data_name="investigation",
                        static_data_template=investigations_link_template,
                        )

        # !!! HACK: 'data.product' is returning '%s' when it is supposed to be null !!!
        product_link_template = '''
            {% if data.product != '%s' %}
              <a href="{% url 'product' data.product.id %}">
                {{data.product.long_name}}
              </a>
            {% endif %}
        '''
        self.add_column(title="Product",
                        hideable=True,
                        orderable=True,
                        filter_name="is_product",
                        static_data_name="product",
                        static_data_template=product_link_template,
                        )

        self.add_column(title="SRT Update",
                        hideable=True,
                        hidden=True,
                        orderable=True,
                        field_name="srt_updated",
                        static_data_name="srt_updated",
                        static_data_template='{{data.srt_updated | date:"m/d/y H:i"}}'
                        )