|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2018-2021 Greenbone Networks GmbH |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
|
5
|
|
|
# |
|
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
# it under the terms of the GNU General Public License as published by |
|
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
# (at your option) any later version. |
|
10
|
|
|
# |
|
11
|
|
|
# This program is distributed in the hope that it will be useful, |
|
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
# GNU General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the GNU General Public License |
|
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
|
|
19
|
|
|
from collections import OrderedDict |
|
20
|
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
|
22
|
|
|
|
|
23
|
|
|
from gvm.protocols.gmpv208 import HostsOrdering |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class GmpCreateAuditTestMixin: |
|
27
|
|
|
def test_create_task(self): |
|
28
|
|
|
self.gmp.create_audit( |
|
29
|
|
|
name='foo', policy_id='c1', target_id='t1', scanner_id='s1' |
|
30
|
|
|
) |
|
31
|
|
|
|
|
32
|
|
|
self.connection.send.has_been_called_with( |
|
33
|
|
|
'<create_task>' |
|
34
|
|
|
'<name>foo</name>' |
|
35
|
|
|
'<usage_type>audit</usage_type>' |
|
36
|
|
|
'<config id="c1"/>' |
|
37
|
|
|
'<target id="t1"/>' |
|
38
|
|
|
'<scanner id="s1"/>' |
|
39
|
|
|
'</create_task>' |
|
40
|
|
|
) |
|
41
|
|
|
|
|
42
|
|
|
def test_create_audit_missing_name(self): |
|
43
|
|
|
with self.assertRaises(RequiredArgument): |
|
44
|
|
|
self.gmp.create_audit( |
|
45
|
|
|
name=None, policy_id='c1', target_id='t1', scanner_id='s1' |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
with self.assertRaises(RequiredArgument): |
|
49
|
|
|
self.gmp.create_audit( |
|
50
|
|
|
name='', policy_id='c1', target_id='t1', scanner_id='s1' |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
|
|
def test_create_audit_missing_policy_id(self): |
|
54
|
|
|
with self.assertRaises(RequiredArgument): |
|
55
|
|
|
self.gmp.create_audit( |
|
56
|
|
|
name='foo', policy_id=None, target_id='t1', scanner_id='s1' |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
with self.assertRaises(RequiredArgument): |
|
60
|
|
|
self.gmp.create_audit( |
|
61
|
|
|
name='foo', policy_id='', target_id='t1', scanner_id='s1' |
|
62
|
|
|
) |
|
63
|
|
|
|
|
64
|
|
|
def test_create_audit_missing_target_id(self): |
|
65
|
|
|
with self.assertRaises(RequiredArgument): |
|
66
|
|
|
self.gmp.create_audit( |
|
67
|
|
|
name='foo', policy_id='c1', target_id=None, scanner_id='s1' |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
with self.assertRaises(RequiredArgument): |
|
71
|
|
|
self.gmp.create_audit( |
|
72
|
|
|
name='foo', policy_id='c1', target_id='', scanner_id='s1' |
|
73
|
|
|
) |
|
74
|
|
|
|
|
75
|
|
|
def test_create_audit_missing_scanner_id(self): |
|
76
|
|
|
with self.assertRaises(RequiredArgument): |
|
77
|
|
|
self.gmp.create_audit( |
|
78
|
|
|
name='foo', policy_id='c1', target_id='t1', scanner_id=None |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
with self.assertRaises(RequiredArgument): |
|
82
|
|
|
self.gmp.create_audit( |
|
83
|
|
|
name='foo', policy_id='c1', target_id='t1', scanner_id='' |
|
84
|
|
|
) |
|
85
|
|
|
|
|
86
|
|
|
def test_create_audit_with_comment(self): |
|
87
|
|
|
self.gmp.create_audit( |
|
88
|
|
|
name='foo', |
|
89
|
|
|
policy_id='c1', |
|
90
|
|
|
target_id='t1', |
|
91
|
|
|
scanner_id='s1', |
|
92
|
|
|
comment='bar', |
|
93
|
|
|
) |
|
94
|
|
|
|
|
95
|
|
|
self.connection.send.has_been_called_with( |
|
96
|
|
|
'<create_task>' |
|
97
|
|
|
'<name>foo</name>' |
|
98
|
|
|
'<usage_type>audit</usage_type>' |
|
99
|
|
|
'<config id="c1"/>' |
|
100
|
|
|
'<target id="t1"/>' |
|
101
|
|
|
'<scanner id="s1"/>' |
|
102
|
|
|
'<comment>bar</comment>' |
|
103
|
|
|
'</create_task>' |
|
104
|
|
|
) |
|
105
|
|
|
|
|
106
|
|
|
def test_create_audit_single_alert(self): |
|
107
|
|
|
# pylint: disable=invalid-name |
|
108
|
|
|
self.gmp.create_audit( |
|
109
|
|
|
name='foo', |
|
110
|
|
|
policy_id='c1', |
|
111
|
|
|
target_id='t1', |
|
112
|
|
|
scanner_id='s1', |
|
113
|
|
|
alert_ids=['a1'], |
|
114
|
|
|
) |
|
115
|
|
|
|
|
116
|
|
|
self.connection.send.has_been_called_with( |
|
117
|
|
|
'<create_task>' |
|
118
|
|
|
'<name>foo</name>' |
|
119
|
|
|
'<usage_type>audit</usage_type>' |
|
120
|
|
|
'<config id="c1"/>' |
|
121
|
|
|
'<target id="t1"/>' |
|
122
|
|
|
'<scanner id="s1"/>' |
|
123
|
|
|
'<alert id="a1"/>' |
|
124
|
|
|
'</create_task>' |
|
125
|
|
|
) |
|
126
|
|
|
|
|
127
|
|
|
def test_create_audit_multiple_alerts(self): |
|
128
|
|
|
self.gmp.create_audit( |
|
129
|
|
|
name='foo', |
|
130
|
|
|
policy_id='c1', |
|
131
|
|
|
target_id='t1', |
|
132
|
|
|
scanner_id='s1', |
|
133
|
|
|
alert_ids=['a1', 'a2', 'a3'], |
|
134
|
|
|
) |
|
135
|
|
|
|
|
136
|
|
|
self.connection.send.has_been_called_with( |
|
137
|
|
|
'<create_task>' |
|
138
|
|
|
'<name>foo</name>' |
|
139
|
|
|
'<usage_type>audit</usage_type>' |
|
140
|
|
|
'<config id="c1"/>' |
|
141
|
|
|
'<target id="t1"/>' |
|
142
|
|
|
'<scanner id="s1"/>' |
|
143
|
|
|
'<alert id="a1"/>' |
|
144
|
|
|
'<alert id="a2"/>' |
|
145
|
|
|
'<alert id="a3"/>' |
|
146
|
|
|
'</create_task>' |
|
147
|
|
|
) |
|
148
|
|
|
|
|
149
|
|
|
def test_create_audit_with_alterable(self): |
|
150
|
|
|
self.gmp.create_audit( |
|
151
|
|
|
name='foo', |
|
152
|
|
|
policy_id='c1', |
|
153
|
|
|
target_id='t1', |
|
154
|
|
|
scanner_id='s1', |
|
155
|
|
|
alterable=True, |
|
156
|
|
|
) |
|
157
|
|
|
|
|
158
|
|
|
self.connection.send.has_been_called_with( |
|
159
|
|
|
'<create_task>' |
|
160
|
|
|
'<name>foo</name>' |
|
161
|
|
|
'<usage_type>audit</usage_type>' |
|
162
|
|
|
'<config id="c1"/>' |
|
163
|
|
|
'<target id="t1"/>' |
|
164
|
|
|
'<scanner id="s1"/>' |
|
165
|
|
|
'<alterable>1</alterable>' |
|
166
|
|
|
'</create_task>' |
|
167
|
|
|
) |
|
168
|
|
|
|
|
169
|
|
|
self.gmp.create_audit( |
|
170
|
|
|
name='foo', |
|
171
|
|
|
policy_id='c1', |
|
172
|
|
|
target_id='t1', |
|
173
|
|
|
scanner_id='s1', |
|
174
|
|
|
alterable=False, |
|
175
|
|
|
) |
|
176
|
|
|
|
|
177
|
|
|
self.connection.send.has_been_called_with( |
|
178
|
|
|
'<create_task>' |
|
179
|
|
|
'<name>foo</name>' |
|
180
|
|
|
'<usage_type>audit</usage_type>' |
|
181
|
|
|
'<config id="c1"/>' |
|
182
|
|
|
'<target id="t1"/>' |
|
183
|
|
|
'<scanner id="s1"/>' |
|
184
|
|
|
'<alterable>0</alterable>' |
|
185
|
|
|
'</create_task>' |
|
186
|
|
|
) |
|
187
|
|
|
|
|
188
|
|
|
def test_create_audit_with_hosts_ordering(self): |
|
189
|
|
|
self.gmp.create_audit( |
|
190
|
|
|
name='foo', |
|
191
|
|
|
policy_id='c1', |
|
192
|
|
|
target_id='t1', |
|
193
|
|
|
scanner_id='s1', |
|
194
|
|
|
hosts_ordering=HostsOrdering.REVERSE, |
|
195
|
|
|
) |
|
196
|
|
|
|
|
197
|
|
|
self.connection.send.has_been_called_with( |
|
198
|
|
|
'<create_task>' |
|
199
|
|
|
'<name>foo</name>' |
|
200
|
|
|
'<usage_type>audit</usage_type>' |
|
201
|
|
|
'<config id="c1"/>' |
|
202
|
|
|
'<target id="t1"/>' |
|
203
|
|
|
'<scanner id="s1"/>' |
|
204
|
|
|
'<hosts_ordering>reverse</hosts_ordering>' |
|
205
|
|
|
'</create_task>' |
|
206
|
|
|
) |
|
207
|
|
|
|
|
208
|
|
|
def test_create_audit_invalid_hosts_ordering(self): |
|
209
|
|
|
with self.assertRaises(InvalidArgumentType): |
|
210
|
|
|
self.gmp.create_audit( |
|
211
|
|
|
name='foo', |
|
212
|
|
|
policy_id='c1', |
|
213
|
|
|
target_id='t1', |
|
214
|
|
|
scanner_id='s1', |
|
215
|
|
|
hosts_ordering='foo', |
|
216
|
|
|
) |
|
217
|
|
|
|
|
218
|
|
|
def test_create_audit_with_schedule(self): |
|
219
|
|
|
self.gmp.create_audit( |
|
220
|
|
|
name='foo', |
|
221
|
|
|
policy_id='c1', |
|
222
|
|
|
target_id='t1', |
|
223
|
|
|
scanner_id='s1', |
|
224
|
|
|
schedule_id='s1', |
|
225
|
|
|
) |
|
226
|
|
|
|
|
227
|
|
|
self.connection.send.has_been_called_with( |
|
228
|
|
|
'<create_task>' |
|
229
|
|
|
'<name>foo</name>' |
|
230
|
|
|
'<usage_type>audit</usage_type>' |
|
231
|
|
|
'<config id="c1"/>' |
|
232
|
|
|
'<target id="t1"/>' |
|
233
|
|
|
'<scanner id="s1"/>' |
|
234
|
|
|
'<schedule id="s1"/>' |
|
235
|
|
|
'</create_task>' |
|
236
|
|
|
) |
|
237
|
|
|
|
|
238
|
|
|
def test_create_audit_with_schedule_and_schedule_periods(self): |
|
239
|
|
|
self.gmp.create_audit( |
|
240
|
|
|
name='foo', |
|
241
|
|
|
policy_id='c1', |
|
242
|
|
|
target_id='t1', |
|
243
|
|
|
scanner_id='s1', |
|
244
|
|
|
schedule_id='s1', |
|
245
|
|
|
schedule_periods=0, |
|
246
|
|
|
) |
|
247
|
|
|
|
|
248
|
|
|
self.connection.send.has_been_called_with( |
|
249
|
|
|
'<create_task>' |
|
250
|
|
|
'<name>foo</name>' |
|
251
|
|
|
'<usage_type>audit</usage_type>' |
|
252
|
|
|
'<config id="c1"/>' |
|
253
|
|
|
'<target id="t1"/>' |
|
254
|
|
|
'<scanner id="s1"/>' |
|
255
|
|
|
'<schedule id="s1"/>' |
|
256
|
|
|
'<schedule_periods>0</schedule_periods>' |
|
257
|
|
|
'</create_task>' |
|
258
|
|
|
) |
|
259
|
|
|
|
|
260
|
|
|
self.gmp.create_audit( |
|
261
|
|
|
name='foo', |
|
262
|
|
|
policy_id='c1', |
|
263
|
|
|
target_id='t1', |
|
264
|
|
|
scanner_id='s1', |
|
265
|
|
|
schedule_id='s1', |
|
266
|
|
|
schedule_periods=5, |
|
267
|
|
|
) |
|
268
|
|
|
|
|
269
|
|
|
self.connection.send.has_been_called_with( |
|
270
|
|
|
'<create_task>' |
|
271
|
|
|
'<name>foo</name>' |
|
272
|
|
|
'<usage_type>audit</usage_type>' |
|
273
|
|
|
'<config id="c1"/>' |
|
274
|
|
|
'<target id="t1"/>' |
|
275
|
|
|
'<scanner id="s1"/>' |
|
276
|
|
|
'<schedule id="s1"/>' |
|
277
|
|
|
'<schedule_periods>5</schedule_periods>' |
|
278
|
|
|
'</create_task>' |
|
279
|
|
|
) |
|
280
|
|
|
|
|
281
|
|
|
def test_create_audit_with_schedule_and_invalid_schedule_periods(self): |
|
282
|
|
|
with self.assertRaises(InvalidArgument): |
|
283
|
|
|
self.gmp.create_audit( |
|
284
|
|
|
name='foo', |
|
285
|
|
|
policy_id='c1', |
|
286
|
|
|
target_id='t1', |
|
287
|
|
|
scanner_id='s1', |
|
288
|
|
|
schedule_id='s1', |
|
289
|
|
|
schedule_periods='foo', |
|
290
|
|
|
) |
|
291
|
|
|
|
|
292
|
|
|
with self.assertRaises(InvalidArgument): |
|
293
|
|
|
self.gmp.create_audit( |
|
294
|
|
|
name='foo', |
|
295
|
|
|
policy_id='c1', |
|
296
|
|
|
target_id='t1', |
|
297
|
|
|
scanner_id='s1', |
|
298
|
|
|
schedule_id='s1', |
|
299
|
|
|
schedule_periods=-1, |
|
300
|
|
|
) |
|
301
|
|
|
|
|
302
|
|
|
def test_create_audit_with_observers(self): |
|
303
|
|
|
self.gmp.create_audit( |
|
304
|
|
|
name='foo', |
|
305
|
|
|
policy_id='c1', |
|
306
|
|
|
target_id='t1', |
|
307
|
|
|
scanner_id='s1', |
|
308
|
|
|
observers=['u1', 'u2'], |
|
309
|
|
|
) |
|
310
|
|
|
|
|
311
|
|
|
self.connection.send.has_been_called_with( |
|
312
|
|
|
'<create_task>' |
|
313
|
|
|
'<name>foo</name>' |
|
314
|
|
|
'<usage_type>audit</usage_type>' |
|
315
|
|
|
'<config id="c1"/>' |
|
316
|
|
|
'<target id="t1"/>' |
|
317
|
|
|
'<scanner id="s1"/>' |
|
318
|
|
|
'<observers>u1,u2</observers>' |
|
319
|
|
|
'</create_task>' |
|
320
|
|
|
) |
|
321
|
|
|
|
|
322
|
|
|
def test_create_audit_invalid_observers(self): |
|
323
|
|
|
with self.assertRaises(InvalidArgumentType): |
|
324
|
|
|
self.gmp.create_audit( |
|
325
|
|
|
name='foo', |
|
326
|
|
|
policy_id='c1', |
|
327
|
|
|
target_id='t1', |
|
328
|
|
|
scanner_id='s1', |
|
329
|
|
|
observers='', |
|
330
|
|
|
) |
|
331
|
|
|
|
|
332
|
|
|
with self.assertRaises(InvalidArgumentType): |
|
333
|
|
|
self.gmp.create_audit( |
|
334
|
|
|
name='foo', |
|
335
|
|
|
policy_id='c1', |
|
336
|
|
|
target_id='t1', |
|
337
|
|
|
scanner_id='s1', |
|
338
|
|
|
observers='foo', |
|
339
|
|
|
) |
|
340
|
|
|
|
|
341
|
|
|
def test_create_audit_with_preferences(self): |
|
342
|
|
|
self.gmp.create_audit( |
|
343
|
|
|
name='foo', |
|
344
|
|
|
policy_id='c1', |
|
345
|
|
|
target_id='t1', |
|
346
|
|
|
scanner_id='s1', |
|
347
|
|
|
preferences=OrderedDict([('foo', 'bar'), ('lorem', 'ipsum')]), |
|
348
|
|
|
) |
|
349
|
|
|
|
|
350
|
|
|
self.connection.send.has_been_called_with( |
|
351
|
|
|
'<create_task>' |
|
352
|
|
|
'<name>foo</name>' |
|
353
|
|
|
'<usage_type>audit</usage_type>' |
|
354
|
|
|
'<config id="c1"/>' |
|
355
|
|
|
'<target id="t1"/>' |
|
356
|
|
|
'<scanner id="s1"/>' |
|
357
|
|
|
'<preferences>' |
|
358
|
|
|
'<preference>' |
|
359
|
|
|
'<scanner_name>foo</scanner_name>' |
|
360
|
|
|
'<value>bar</value>' |
|
361
|
|
|
'</preference>' |
|
362
|
|
|
'<preference>' |
|
363
|
|
|
'<scanner_name>lorem</scanner_name>' |
|
364
|
|
|
'<value>ipsum</value>' |
|
365
|
|
|
'</preference>' |
|
366
|
|
|
'</preferences>' |
|
367
|
|
|
'</create_task>' |
|
368
|
|
|
) |
|
369
|
|
|
|
|
370
|
|
|
def test_create_audit_invalid_preferences(self): |
|
371
|
|
|
with self.assertRaises(InvalidArgumentType): |
|
372
|
|
|
self.gmp.create_audit( |
|
373
|
|
|
name='foo', |
|
374
|
|
|
policy_id='c1', |
|
375
|
|
|
target_id='t1', |
|
376
|
|
|
scanner_id='s1', |
|
377
|
|
|
preferences='', |
|
378
|
|
|
) |
|
379
|
|
|
|
|
380
|
|
|
with self.assertRaises(InvalidArgumentType): |
|
381
|
|
|
self.gmp.create_audit( |
|
382
|
|
|
name='foo', |
|
383
|
|
|
policy_id='c1', |
|
384
|
|
|
target_id='t1', |
|
385
|
|
|
scanner_id='s1', |
|
386
|
|
|
preferences=['foo', 'bar'], |
|
387
|
|
|
) |
|
388
|
|
|
|
|
389
|
|
|
def test_create_audit_don_t_allow_container_task(self): |
|
390
|
|
|
with self.assertRaises(InvalidArgument): |
|
391
|
|
|
self.gmp.create_audit( |
|
392
|
|
|
name='foo', |
|
393
|
|
|
policy_id='c1', |
|
394
|
|
|
target_id='0', |
|
395
|
|
|
scanner_id='s1', |
|
396
|
|
|
observers='', |
|
397
|
|
|
) |
|
398
|
|
|
|
|
399
|
|
|
# target_id=0 is considered as False |
|
400
|
|
|
with self.assertRaises(RequiredArgument): |
|
401
|
|
|
self.gmp.create_audit( |
|
402
|
|
|
name='foo', |
|
403
|
|
|
policy_id='c1', |
|
404
|
|
|
target_id=0, |
|
405
|
|
|
scanner_id='s1', |
|
406
|
|
|
observers='', |
|
407
|
|
|
) |
|
408
|
|
|
|