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 gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
20
|
|
|
|
21
|
|
|
from gvm.protocols.gmpv208 import AlertCondition, AlertEvent, AlertMethod |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class GmpCreateAlertTestMixin: |
25
|
|
|
def test_missing_name(self): |
26
|
|
|
with self.assertRaises(RequiredArgument): |
27
|
|
|
self.gmp.create_alert( |
28
|
|
|
name='', |
29
|
|
|
condition=AlertCondition.ALWAYS, |
30
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
31
|
|
|
method=AlertMethod.EMAIL, |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
with self.assertRaises(RequiredArgument): |
35
|
|
|
self.gmp.create_alert( |
36
|
|
|
name=None, |
37
|
|
|
condition=AlertCondition.ALWAYS, |
38
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
39
|
|
|
method=AlertMethod.EMAIL, |
40
|
|
|
) |
41
|
|
|
|
42
|
|
|
def test_missing_condition(self): |
43
|
|
|
with self.assertRaises(RequiredArgument): |
44
|
|
|
self.gmp.create_alert( |
45
|
|
|
name='foo', condition='', event='bar', method='lorem' |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
with self.assertRaises(RequiredArgument): |
49
|
|
|
self.gmp.create_alert( |
50
|
|
|
name='foo', condition=None, event='bar', method='lorem' |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
def test_missing_event(self): |
54
|
|
|
with self.assertRaises(RequiredArgument): |
55
|
|
|
self.gmp.create_alert( |
56
|
|
|
name='foo', condition='bar', event='', method='lorem' |
57
|
|
|
) |
58
|
|
|
|
59
|
|
|
with self.assertRaises(RequiredArgument): |
60
|
|
|
self.gmp.create_alert( |
61
|
|
|
name='foo', condition='bar', event=None, method='lorem' |
62
|
|
|
) |
63
|
|
|
|
64
|
|
|
def test_missing_method(self): |
65
|
|
|
with self.assertRaises(RequiredArgument): |
66
|
|
|
self.gmp.create_alert( |
67
|
|
|
name='foo', condition='bar', event='lorem', method='' |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
with self.assertRaises(RequiredArgument): |
71
|
|
|
self.gmp.create_alert( |
72
|
|
|
name='foo', condition='bar', event='lorem', method=None |
73
|
|
|
) |
74
|
|
|
|
75
|
|
|
def test_invalid_condition(self): |
76
|
|
|
with self.assertRaises(InvalidArgumentType): |
77
|
|
|
self.gmp.create_alert( |
78
|
|
|
name='foo', |
79
|
|
|
condition='bar', |
80
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
81
|
|
|
method=AlertMethod.EMAIL, |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
def test_invalid_event(self): |
85
|
|
|
with self.assertRaises(InvalidArgumentType): |
86
|
|
|
self.gmp.create_alert( |
87
|
|
|
name='foo', |
88
|
|
|
condition=AlertCondition.ALWAYS, |
89
|
|
|
event='lorem', |
90
|
|
|
method=AlertMethod.EMAIL, |
91
|
|
|
) |
92
|
|
|
|
93
|
|
|
def test_invalid_method(self): |
94
|
|
|
with self.assertRaises(InvalidArgumentType): |
95
|
|
|
self.gmp.create_alert( |
96
|
|
|
name='foo', |
97
|
|
|
condition=AlertCondition.ALWAYS, |
98
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
99
|
|
|
method='ipsum', |
100
|
|
|
) |
101
|
|
|
|
102
|
|
|
def test_invalid_condition_for_secinfo(self): |
103
|
|
|
with self.assertRaises(InvalidArgument): |
104
|
|
|
self.gmp.create_alert( |
105
|
|
|
name='foo', |
106
|
|
|
condition=AlertCondition.SEVERITY_AT_LEAST, |
107
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
108
|
|
|
method=AlertMethod.EMAIL, |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
def test_invalid_method_for_secinfo(self): |
112
|
|
|
with self.assertRaises(InvalidArgument): |
113
|
|
|
self.gmp.create_alert( |
114
|
|
|
name='foo', |
115
|
|
|
condition=AlertCondition.ALWAYS, |
116
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
117
|
|
|
method=AlertMethod.HTTP_GET, |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
with self.assertRaises(InvalidArgument): |
121
|
|
|
self.gmp.create_alert( |
122
|
|
|
name='foo', |
123
|
|
|
condition=AlertCondition.ALWAYS, |
124
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
125
|
|
|
method=AlertMethod.ALEMBA_VFIRE, |
126
|
|
|
) |
127
|
|
|
|
128
|
|
|
with self.assertRaises(InvalidArgument): |
129
|
|
|
self.gmp.create_alert( |
130
|
|
|
name='foo', |
131
|
|
|
condition=AlertCondition.ALWAYS, |
132
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
133
|
|
|
method=AlertMethod.SOURCEFIRE_CONNECTOR, |
134
|
|
|
) |
135
|
|
|
|
136
|
|
|
with self.assertRaises(InvalidArgument): |
137
|
|
|
self.gmp.create_alert( |
138
|
|
|
name='foo', |
139
|
|
|
condition=AlertCondition.ALWAYS, |
140
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
141
|
|
|
method=AlertMethod.START_TASK, |
142
|
|
|
) |
143
|
|
|
|
144
|
|
|
with self.assertRaises(InvalidArgument): |
145
|
|
|
self.gmp.create_alert( |
146
|
|
|
name='foo', |
147
|
|
|
condition=AlertCondition.ALWAYS, |
148
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
149
|
|
|
method=AlertMethod.TIPPINGPOINT_SMS, |
150
|
|
|
) |
151
|
|
|
|
152
|
|
|
with self.assertRaises(InvalidArgument): |
153
|
|
|
self.gmp.create_alert( |
154
|
|
|
name='foo', |
155
|
|
|
condition=AlertCondition.ALWAYS, |
156
|
|
|
event=AlertEvent.UPDATED_SECINFO_ARRIVED, |
157
|
|
|
method=AlertMethod.VERINICE_CONNECTOR, |
158
|
|
|
) |
159
|
|
|
|
160
|
|
|
with self.assertRaises(InvalidArgument): |
161
|
|
|
self.gmp.create_alert( |
162
|
|
|
name='foo', |
163
|
|
|
condition=AlertCondition.ALWAYS, |
164
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
165
|
|
|
method=AlertMethod.HTTP_GET, |
166
|
|
|
) |
167
|
|
|
|
168
|
|
|
with self.assertRaises(InvalidArgument): |
169
|
|
|
self.gmp.create_alert( |
170
|
|
|
name='foo', |
171
|
|
|
condition=AlertCondition.ALWAYS, |
172
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
173
|
|
|
method=AlertMethod.ALEMBA_VFIRE, |
174
|
|
|
) |
175
|
|
|
|
176
|
|
|
with self.assertRaises(InvalidArgument): |
177
|
|
|
self.gmp.create_alert( |
178
|
|
|
name='foo', |
179
|
|
|
condition=AlertCondition.ALWAYS, |
180
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
181
|
|
|
method=AlertMethod.SOURCEFIRE_CONNECTOR, |
182
|
|
|
) |
183
|
|
|
|
184
|
|
|
with self.assertRaises(InvalidArgument): |
185
|
|
|
self.gmp.create_alert( |
186
|
|
|
name='foo', |
187
|
|
|
condition=AlertCondition.ALWAYS, |
188
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
189
|
|
|
method=AlertMethod.START_TASK, |
190
|
|
|
) |
191
|
|
|
|
192
|
|
|
with self.assertRaises(InvalidArgument): |
193
|
|
|
self.gmp.create_alert( |
194
|
|
|
name='foo', |
195
|
|
|
condition=AlertCondition.ALWAYS, |
196
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
197
|
|
|
method=AlertMethod.TIPPINGPOINT_SMS, |
198
|
|
|
) |
199
|
|
|
|
200
|
|
|
with self.assertRaises(InvalidArgument): |
201
|
|
|
self.gmp.create_alert( |
202
|
|
|
name='foo', |
203
|
|
|
condition=AlertCondition.ALWAYS, |
204
|
|
|
event=AlertEvent.NEW_SECINFO_ARRIVED, |
205
|
|
|
method=AlertMethod.VERINICE_CONNECTOR, |
206
|
|
|
) |
207
|
|
|
|
208
|
|
|
def test_missing_method_for_ticket_received(self): |
209
|
|
|
with self.assertRaises(RequiredArgument): |
210
|
|
|
self.gmp.create_alert( |
211
|
|
|
name='foo', |
212
|
|
|
condition=AlertCondition.ALWAYS, |
213
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
214
|
|
|
method=None, |
215
|
|
|
) |
216
|
|
|
|
217
|
|
|
def test_missing_condition_for_ticket_received(self): |
218
|
|
|
with self.assertRaises(RequiredArgument): |
219
|
|
|
self.gmp.create_alert( |
220
|
|
|
name='foo', |
221
|
|
|
condition=None, |
222
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
223
|
|
|
method=AlertMethod.EMAIL, |
224
|
|
|
) |
225
|
|
|
|
226
|
|
|
def test_invalid_method_for_ticket_received(self): |
227
|
|
|
with self.assertRaises(InvalidArgument): |
228
|
|
|
self.gmp.create_alert( |
229
|
|
|
name='foo', |
230
|
|
|
condition=AlertCondition.ALWAYS, |
231
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
232
|
|
|
method=AlertMethod.HTTP_GET, |
233
|
|
|
) |
234
|
|
|
|
235
|
|
|
with self.assertRaises(InvalidArgument): |
236
|
|
|
self.gmp.create_alert( |
237
|
|
|
name='foo', |
238
|
|
|
condition=AlertCondition.ALWAYS, |
239
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
240
|
|
|
method=AlertMethod.SCP, |
241
|
|
|
) |
242
|
|
|
|
243
|
|
|
with self.assertRaises(InvalidArgument): |
244
|
|
|
self.gmp.create_alert( |
245
|
|
|
name='foo', |
246
|
|
|
condition=AlertCondition.ALWAYS, |
247
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
248
|
|
|
method=AlertMethod.SEND, |
249
|
|
|
) |
250
|
|
|
|
251
|
|
|
with self.assertRaises(InvalidArgument): |
252
|
|
|
self.gmp.create_alert( |
253
|
|
|
name='foo', |
254
|
|
|
condition=AlertCondition.ALWAYS, |
255
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
256
|
|
|
method=AlertMethod.SMB, |
257
|
|
|
) |
258
|
|
|
|
259
|
|
|
with self.assertRaises(InvalidArgument): |
260
|
|
|
self.gmp.create_alert( |
261
|
|
|
name='foo', |
262
|
|
|
condition=AlertCondition.ALWAYS, |
263
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
264
|
|
|
method=AlertMethod.SNMP, |
265
|
|
|
) |
266
|
|
|
|
267
|
|
|
with self.assertRaises(InvalidArgument): |
268
|
|
|
self.gmp.create_alert( |
269
|
|
|
name='foo', |
270
|
|
|
condition=AlertCondition.ALWAYS, |
271
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
272
|
|
|
method=AlertMethod.ALEMBA_VFIRE, |
273
|
|
|
) |
274
|
|
|
|
275
|
|
|
with self.assertRaises(InvalidArgument): |
276
|
|
|
self.gmp.create_alert( |
277
|
|
|
name='foo', |
278
|
|
|
condition=AlertCondition.ALWAYS, |
279
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
280
|
|
|
method=AlertMethod.VERINICE_CONNECTOR, |
281
|
|
|
) |
282
|
|
|
|
283
|
|
|
with self.assertRaises(InvalidArgument): |
284
|
|
|
self.gmp.create_alert( |
285
|
|
|
name='foo', |
286
|
|
|
condition=AlertCondition.ALWAYS, |
287
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
288
|
|
|
method=AlertMethod.TIPPINGPOINT_SMS, |
289
|
|
|
) |
290
|
|
|
|
291
|
|
|
with self.assertRaises(InvalidArgument): |
292
|
|
|
self.gmp.create_alert( |
293
|
|
|
name='foo', |
294
|
|
|
condition=AlertCondition.ALWAYS, |
295
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
296
|
|
|
method=AlertMethod.SOURCEFIRE_CONNECTOR, |
297
|
|
|
) |
298
|
|
|
|
299
|
|
|
def test_invalid_condition_for_task_run_status_changed(self): |
300
|
|
|
with self.assertRaises(InvalidArgument): |
301
|
|
|
self.gmp.create_alert( |
302
|
|
|
name='foo', |
303
|
|
|
condition=AlertCondition.ERROR, |
304
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
305
|
|
|
method=AlertMethod.EMAIL, |
306
|
|
|
) |
307
|
|
|
|
308
|
|
|
def test_invalid_condition_for_ticket_received(self): |
309
|
|
|
with self.assertRaises(InvalidArgument): |
310
|
|
|
self.gmp.create_alert( |
311
|
|
|
name='foo', |
312
|
|
|
condition=AlertCondition.FILTER_COUNT_AT_LEAST, |
313
|
|
|
event=AlertEvent.TICKET_RECEIVED, |
314
|
|
|
method=AlertMethod.EMAIL, |
315
|
|
|
) |
316
|
|
|
|
317
|
|
|
def test_create_alert(self): |
318
|
|
|
self.gmp.create_alert( |
319
|
|
|
name='foo', |
320
|
|
|
condition=AlertCondition.ALWAYS, |
321
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
322
|
|
|
method=AlertMethod.EMAIL, |
323
|
|
|
) |
324
|
|
|
|
325
|
|
|
self.connection.send.has_been_called_with( |
326
|
|
|
'<create_alert>' |
327
|
|
|
'<name>foo</name>' |
328
|
|
|
'<condition>Always</condition>' |
329
|
|
|
'<event>Task run status changed</event>' |
330
|
|
|
'<method>Email</method>' |
331
|
|
|
'</create_alert>' |
332
|
|
|
) |
333
|
|
|
|
334
|
|
|
def test_create_alert_with_filter_id(self): |
335
|
|
|
self.gmp.create_alert( |
336
|
|
|
name='foo', |
337
|
|
|
condition=AlertCondition.ALWAYS, |
338
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
339
|
|
|
method=AlertMethod.EMAIL, |
340
|
|
|
filter_id='f1', |
341
|
|
|
) |
342
|
|
|
|
343
|
|
|
self.connection.send.has_been_called_with( |
344
|
|
|
'<create_alert>' |
345
|
|
|
'<name>foo</name>' |
346
|
|
|
'<condition>Always</condition>' |
347
|
|
|
'<event>Task run status changed</event>' |
348
|
|
|
'<method>Email</method>' |
349
|
|
|
'<filter id="f1"/>' |
350
|
|
|
'</create_alert>' |
351
|
|
|
) |
352
|
|
|
|
353
|
|
|
def test_create_alert_with_comment(self): |
354
|
|
|
self.gmp.create_alert( |
355
|
|
|
name='foo', |
356
|
|
|
condition=AlertCondition.ALWAYS, |
357
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
358
|
|
|
method=AlertMethod.EMAIL, |
359
|
|
|
comment='hello', |
360
|
|
|
) |
361
|
|
|
|
362
|
|
|
self.connection.send.has_been_called_with( |
363
|
|
|
'<create_alert>' |
364
|
|
|
'<name>foo</name>' |
365
|
|
|
'<condition>Always</condition>' |
366
|
|
|
'<event>Task run status changed</event>' |
367
|
|
|
'<method>Email</method>' |
368
|
|
|
'<comment>hello</comment>' |
369
|
|
|
'</create_alert>' |
370
|
|
|
) |
371
|
|
|
|
372
|
|
|
def test_create_alert_with_condition_data(self): |
373
|
|
|
self.gmp.create_alert( |
374
|
|
|
name='foo', |
375
|
|
|
condition=AlertCondition.ALWAYS, |
376
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
377
|
|
|
method=AlertMethod.EMAIL, |
378
|
|
|
condition_data={'foo': 'bar'}, |
379
|
|
|
) |
380
|
|
|
|
381
|
|
|
self.connection.send.has_been_called_with( |
382
|
|
|
'<create_alert>' |
383
|
|
|
'<name>foo</name>' |
384
|
|
|
'<condition>Always<data>bar<name>foo</name></data></condition>' |
385
|
|
|
'<event>Task run status changed</event>' |
386
|
|
|
'<method>Email</method>' |
387
|
|
|
'</create_alert>' |
388
|
|
|
) |
389
|
|
|
|
390
|
|
|
def test_create_alert_with_event_data(self): |
391
|
|
|
self.gmp.create_alert( |
392
|
|
|
name='foo', |
393
|
|
|
condition=AlertCondition.ALWAYS, |
394
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
395
|
|
|
method=AlertMethod.EMAIL, |
396
|
|
|
event_data={'foo': 'bar'}, |
397
|
|
|
) |
398
|
|
|
|
399
|
|
|
self.connection.send.has_been_called_with( |
400
|
|
|
'<create_alert>' |
401
|
|
|
'<name>foo</name>' |
402
|
|
|
'<condition>Always</condition>' |
403
|
|
|
'<event>Task run status changed' |
404
|
|
|
'<data>bar<name>foo</name></data>' |
405
|
|
|
'</event>' |
406
|
|
|
'<method>Email</method>' |
407
|
|
|
'</create_alert>' |
408
|
|
|
) |
409
|
|
|
|
410
|
|
|
def test_create_alert_with_method_data(self): |
411
|
|
|
self.gmp.create_alert( |
412
|
|
|
name='foo', |
413
|
|
|
condition=AlertCondition.ALWAYS, |
414
|
|
|
event=AlertEvent.TASK_RUN_STATUS_CHANGED, |
415
|
|
|
method=AlertMethod.EMAIL, |
416
|
|
|
method_data={'foo': 'bar'}, |
417
|
|
|
) |
418
|
|
|
|
419
|
|
|
self.connection.send.has_been_called_with( |
420
|
|
|
'<create_alert>' |
421
|
|
|
'<name>foo</name>' |
422
|
|
|
'<condition>Always</condition>' |
423
|
|
|
'<event>Task run status changed</event>' |
424
|
|
|
'<method>Email<data>bar<name>foo</name></data></method>' |
425
|
|
|
'</create_alert>' |
426
|
|
|
) |
427
|
|
|
|