Codebase list python-faraday / 62d1b14 tests / test_api_commands.py
62d1b14

Tree @62d1b14 (Download .tar.gz)

test_api_commands.py @62d1b14raw · history · blame

  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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
'''
Faraday Penetration Test IDE
Copyright (C) 2013  Infobyte LLC (http://www.infobytesec.com/)
See the file 'doc/LICENSE' for the license information

'''

"""Tests for many API endpoints that do not depend on workspace_name"""
from posixpath import join as urljoin
import datetime
import pytest
import time

from tests import factories
from tests.test_api_workspaced_base import ReadWriteAPITests
from faraday.server.models import (
    Command,
    Vulnerability)
from faraday.server.api.modules.commandsrun import CommandView
from tests.factories import VulnerabilityFactory, EmptyCommandFactory, CommandObjectFactory, HostFactory, \
    WorkspaceFactory, ServiceFactory


# Note: because of a bug with pytest, I can't simply mark TestListCommandView
# with @pytest.mark.skip. I had to made it inherit from object instad of
# ReadOnlyAPITests, and to manually skip the extra tests inside the class.
# See https://docs.pytest.org/en/latest/skipping.html#skip-all-test-functions-of-a-class-or-module
# and https://github.com/pytest-dev/pytest/issues/568 for more information

@pytest.mark.usefixtures('logged_user')
class TestListCommandView(ReadWriteAPITests):
    model = Command
    factory = factories.CommandFactory
    api_endpoint = 'commands'
    view_class = CommandView
    patchable_fields = ["ip"]

    @pytest.mark.usefixtures('ignore_nplusone')
    @pytest.mark.usefixtures('mock_envelope_list')
    def test_list_retrieves_all_items_from_workspace(self, test_client,
                                                     second_workspace,
                                                     session):
        super().test_list_retrieves_all_items_from_workspace(test_client, second_workspace, session)

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_backwards_compatibility_list(self, test_client, second_workspace, session):
        self.factory.create(workspace=second_workspace)
        session.commit()
        res = test_client.get(self.url())
        assert res.status_code == 200
        assert 'commands' in res.json
        for command in res.json['commands']:
            assert {'id', 'key', 'value'} == set(command.keys())
            object_properties = [
                '_id',
                'command',
                'duration',
                'hostname',
                'ip',
                'itime',
                'params',
                'user',
                'workspace',
                'tool',
                'import_source',
                'creator',
                'metadata'
            ]
            assert command['value']['workspace'] == self.workspace.name
            assert set(object_properties) == set(command['value'].keys())

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_can_list_readonly(self, test_client, session):
        super().test_can_list_readonly(test_client, session)

    def test_activity_feed(self, session, test_client):
        command = self.factory.create()
        another_command = EmptyCommandFactory.create(workspace=command.workspace)
        vuln = session.query(Vulnerability).get(command.command_objects[0].object_id)
        session.flush()
        CommandObjectFactory.create(
            command=another_command,
            object_type='vulnerability',
            object_id=vuln.id,
            workspace=command.workspace
        )
        CommandObjectFactory.create(
            command=another_command,
            object_type='host',
            object_id=vuln.host.id,
            workspace=command.workspace
        )
        session.commit()

        res = test_client.get(urljoin(self.url(workspace=command.workspace), 'activity_feed'))
        assert res.status_code == 200

        assert list(filter(lambda stats: stats['_id'] == command.id, res.json)) == [
            {'_id': command.id,
             'command': command.command,
             'import_source': 'shell',
             'user': command.user,
             'date': time.mktime(command.start_date.timetuple()) * 1000,
             'params': command.params,
             'tool': command.tool,
             'hosts_count': 1,
             'services_count': 0,
             'vulnerabilities_count': 1,
             'criticalIssue': 0}]

        assert list(filter(lambda stats: stats['_id'] == another_command.id,
                           res.json)) == [{
            '_id': another_command.id,
            'command': another_command.command,
            'import_source': 'shell',
            'tool': another_command.tool,
            'user': another_command.user,
            'date': time.mktime(
                another_command.start_date.timetuple()) * 1000,
            'params': another_command.params,
            'hosts_count': 0,
            'services_count': 0,
            'vulnerabilities_count': 0,
            'criticalIssue': 0}]

    def test_verify_created_critical_vulns_is_correctly_showing_sum_values(self, session, test_client):
        workspace = WorkspaceFactory.create()
        command = EmptyCommandFactory.create(workspace=workspace)
        host = HostFactory.create(workspace=workspace)
        vuln = VulnerabilityFactory.create(severity='critical', workspace=workspace, host=host, service=None)
        vuln_med = VulnerabilityFactory.create(severity='medium', workspace=workspace, host=host, service=None)
        session.flush()
        CommandObjectFactory.create(
            command=command,
            object_type='host',
            object_id=host.id,
            workspace=workspace
        )
        CommandObjectFactory.create(
            command=command,
            object_type='vulnerability',
            object_id=vuln.id,
            workspace=workspace
        )
        CommandObjectFactory.create(
            command=command,
            object_type='vulnerability',
            object_id=vuln_med.id,
            workspace=workspace
        )
        session.commit()
        res = test_client.get(urljoin(self.url(workspace=command.workspace), 'activity_feed'))
        assert res.status_code == 200
        assert res.json == [
            {'_id': command.id,
             'command': command.command,
             'import_source': 'shell',
             'tool': command.tool,
             'user': command.user,
             'date': time.mktime(command.start_date.timetuple()) * 1000,
             'params': command.params,
             'hosts_count': 1,
             'services_count': 0,
             'vulnerabilities_count': 2,
             'criticalIssue': 1}
        ]

    def test_verify_created_vulns_with_host_and_service_verification(self, session, test_client):
        workspace = WorkspaceFactory.create()
        command = EmptyCommandFactory.create(workspace=workspace)
        host = HostFactory.create(workspace=workspace)
        service = ServiceFactory.create(workspace=workspace)
        vuln = VulnerabilityFactory.create(severity='critical', workspace=workspace, host=host, service=None)
        vuln_med = VulnerabilityFactory.create(severity='medium', workspace=workspace, service=service, host=None)
        session.flush()
        CommandObjectFactory.create(
            command=command,
            object_type='host',
            object_id=host.id,
            workspace=workspace
        )
        CommandObjectFactory.create(
            command=command,
            object_type='vulnerability',
            object_id=vuln.id,
            workspace=workspace
        )
        CommandObjectFactory.create(
            command=command,
            object_type='service',
            object_id=service.id,
            workspace=workspace
        )
        CommandObjectFactory.create(
            command=command,
            object_type='vulnerability',
            object_id=vuln_med.id,
            workspace=workspace
        )
        session.commit()
        res = test_client.get(urljoin(self.url(workspace=command.workspace), 'activity_feed'))
        assert res.status_code == 200
        assert res.json == [{
            '_id': command.id,
            'command': command.command,
            'import_source': 'shell',
            'tool': command.tool,
            'user': command.user,
            'date': time.mktime(command.start_date.timetuple()) * 1000,
            'params': command.params,
            'hosts_count': 1,
            'services_count': 1,
            'vulnerabilities_count': 2,
            'criticalIssue': 1}
        ]

    def test_multiple_commands_executed_with_same_objects_found(self, session, test_client):
        """
            This text verifies that multiple command does not affect activity feed counters.
        """
        workspace = WorkspaceFactory.create()
        host = HostFactory.create(workspace=workspace)
        vuln = VulnerabilityFactory.create(severity='low', workspace=workspace, host=host, service=None)
        service = ServiceFactory.create(workspace=workspace)
        commands = []
        in_the_middle_commands = []
        first_command = None
        for index in range(0, 10):

            command = EmptyCommandFactory.create(workspace=workspace)
            commands.append(command)
            if index > 0:
                # in the middle commands should not affect counters (should be at 0)
                in_the_middle_commands.append(command)
            else:
                first_command = command
            session.flush()
            CommandObjectFactory.create(
                command=command,
                object_type='host',
                object_id=host.id,
                workspace=workspace
            )
            CommandObjectFactory.create(
                command=command,
                object_type='vulnerability',
                object_id=vuln.id,
                workspace=workspace
            )
        # This command will change activity feed counters
        vuln_med = VulnerabilityFactory.create(severity='medium', workspace=workspace, service=service, host=None)
        session.flush()
        last_command = EmptyCommandFactory.create(workspace=workspace)
        CommandObjectFactory.create(
            command=last_command,
            object_type='service',
            object_id=service.id,
            workspace=workspace
        )
        CommandObjectFactory.create(
            command=last_command,
            object_type='vulnerability',
            object_id=vuln_med.id,
            workspace=workspace
        )
        session.commit()
        res = test_client.get(urljoin(self.url(workspace=command.workspace), 'activity_feed'))
        assert res.status_code == 200
        raw_first_command = list(filter(lambda comm: comm['_id'] == commands[0].id, res.json))

        assert raw_first_command.pop() == {
            '_id': first_command.id,
            'command': first_command.command,
            'import_source': 'shell',
            'user': first_command.user,
            'date': time.mktime(first_command.start_date.timetuple()) * 1000,
            'params': first_command.params,
            'hosts_count': 1,
            'services_count': 0,
            'vulnerabilities_count': 1,
            'tool': first_command.tool,
            'criticalIssue': 0
        }

        for in_the_middle_command in in_the_middle_commands:
            raw_in_the_middle_command = list(filter(lambda comm: comm['_id'] == in_the_middle_command.id, res.json))
            assert raw_in_the_middle_command.pop() == {'_id': in_the_middle_command.id,
                                                       'command': in_the_middle_command.command,
                                                       'import_source': 'shell',
                                                       'user': in_the_middle_command.user,
                                                       'date': time.mktime(
                                                           in_the_middle_command.start_date.timetuple()) * 1000,
                                                       'params': in_the_middle_command.params,
                                                       'hosts_count': 0,
                                                       'tool': in_the_middle_command.tool,
                                                       'services_count': 0,
                                                       'vulnerabilities_count': 0,
                                                       'criticalIssue': 0}

        # new command must create new service and vuln
        raw_last_command = list(filter(lambda comm: comm['_id'] == last_command.id, res.json))
        assert raw_last_command.pop() == {'_id': last_command.id,
                                          'command': last_command.command,
                                          'import_source': 'shell',
                                          'user': last_command.user,
                                          'date': time.mktime(last_command.start_date.timetuple()) * 1000,
                                          'params': last_command.params,
                                          'hosts_count': 0,
                                          'tool': last_command.tool,
                                          'services_count': 1,
                                          'vulnerabilities_count': 1,
                                          'criticalIssue': 0}

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_sub_second_command_returns_correct_duration_value(self, test_client):
        command = self.factory(
            start_date=datetime.datetime(2017, 11, 14, 12, 29, 21, 248433),
            end_date=datetime.datetime(2017, 11, 14, 12, 29, 21, 690839)
        )
        res = test_client.get(self.url(workspace=command.workspace))
        assert res.status_code == 200
        assert res.json['commands'][0]['value']['duration'] == 0.442406

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_more_than_one_second_command_returns_correct_duration_value(self, test_client):
        command = self.factory(
            start_date=datetime.datetime(2017, 11, 14, 12, 29, 20, 248433),
            end_date=datetime.datetime(2017, 11, 14, 12, 29, 21, 690839)
        )
        res = test_client.get(self.url(workspace=command.workspace))
        assert res.status_code == 200
        assert res.json['commands'][0]['value']['duration'] == 1.442406

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_more_than_one_minute_command_returns_correct_duration_value(self, test_client):
        command = self.factory(
            start_date=datetime.datetime(2017, 11, 14, 12, 28, 20, 248433),
            end_date=datetime.datetime(2017, 11, 14, 12, 29, 21, 690839)
        )
        res = test_client.get(self.url(workspace=command.workspace))
        assert res.status_code == 200
        assert res.json['commands'][0]['value']['duration'] == 61.442406

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_more_than_one_day_none_end_date_command_returns_msg(self, test_client):
        command = self.factory(
            start_date=datetime.datetime(2017, 11, 14, 12, 28, 20, 0),
            end_date=None
        )
        res = test_client.get(self.url(workspace=command.workspace))
        assert res.status_code == 200
        assert res.json['commands'][0]['value']['duration'].lower() == "timeout"

    @pytest.mark.usefixtures('ignore_nplusone')
    def test_less_than_one_day_none_end_date_command_returns_msg(self, test_client):
        command = self.factory(
            start_date=datetime.datetime.now(),
            end_date=None
        )
        res = test_client.get(self.url(workspace=command.workspace))
        assert res.status_code == 200
        assert res.json['commands'][0]['value']['duration'].lower() == "in progress"

    def test_create_command(self, test_client):
        raw_data = {
            'command': 'Import Nessus:',
            'tool': 'nessus',
            'duration': None,
            'hostname': 'mandarina',
            'ip': '192.168.20.53',
            'itime': 1511387720.048548,
            'params': '/home/lcubo/.faraday/report/airbnb/nessus_report_Remote.nessus',
            'user': 'lcubo'
        }

        res = test_client.post(self.url(), data=raw_data)
        assert res.status_code == 201

    def test_update_command(self, test_client, session):
        command = self.factory()
        session.commit()
        start_date = datetime.datetime.utcnow()
        raw_data = {
            'command': 'Import Nessus:',
            'tool': 'nessus',
            'duration': 120,
            'hostname': 'mandarina',
            'ip': '192.168.20.53',
            'itime': start_date.timestamp(),
            'params': '/home/lcubo/.faraday/report/airbnb/nessus_report_Remote.nessus',
            'user': 'lcubo'
        }

        res = test_client.put(self.url(command, workspace=command.workspace),
                              data=raw_data)
        assert res.status_code == 200
        updated_command = self.model.query.get(command.id)
        print(updated_command.end_date)
        assert updated_command.end_date == updated_command.start_date + datetime.timedelta(seconds=120)

    def test_delete_objects_preserve_history(self, session, test_client):

        command = EmptyCommandFactory(command='test', tool='test', workspace=self.workspace)
        host = HostFactory.create(workspace=self.workspace)
        session.add(host)
        session.commit()
        CommandObjectFactory.create(
            command=command,
            object_type='host',
            object_id=host.id,
            workspace=self.workspace
        )
        session.commit()

        res = test_client.get(f'/v3/ws/{host.workspace.name}/hosts/{host.id}')
        assert res.status_code == 200

        res = test_client.delete(f'/v3/ws/{host.workspace.name}/hosts/{host.id}')
        assert res.status_code == 204

        res = test_client.get(urljoin(self.url(workspace=command.workspace), 'activity_feed'))
        assert res.status_code == 200
        command_history = list(filter(lambda hist: hist['_id'] == command.id, res.json))
        assert len(command_history)
        command_history = command_history[0]
        assert command_history['hosts_count'] == 1
        assert command_history['tool'] == 'test'

    def test_year_is_out_range(self, test_client):
        raw_data = {
            'command': 'Import Nessus:',
            'tool': 'nessus',
            'duration': None,
            'hostname': 'mandarina',
            'ip': '192.168.20.53',
            'itime': 1511387720000.048548,
            'params': '/home/lcubo/.faraday/report/airbnb/nessus_report_Remote.nessus',
            'user': 'lcubo'
        }

        res = test_client.post(self.url(), data=raw_data)

        assert res.status_code == 400