1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2020-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, InvalidArgumentType |
20
|
|
|
from gvm.protocols.gmpv208 import ( |
21
|
|
|
CredentialType, |
22
|
|
|
SnmpAuthAlgorithm, |
23
|
|
|
SnmpPrivacyAlgorithm, |
24
|
|
|
) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class GmpCreateCredentialTestMixin: |
28
|
|
|
def test_create_up_credential_missing_name(self): |
29
|
|
|
with self.assertRaises(RequiredArgument): |
30
|
|
|
self.gmp.create_credential( |
31
|
|
|
name='', |
32
|
|
|
credential_type=CredentialType.USERNAME_PASSWORD, |
33
|
|
|
login='foo', |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
with self.assertRaises(RequiredArgument): |
37
|
|
|
self.gmp.create_credential( |
38
|
|
|
name=None, |
39
|
|
|
credential_type=CredentialType.USERNAME_PASSWORD, |
40
|
|
|
login='foo', |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
def test_create_up_credential(self): |
44
|
|
|
self.gmp.create_credential( |
45
|
|
|
name='foo', |
46
|
|
|
credential_type=CredentialType.USERNAME_PASSWORD, |
47
|
|
|
comment='bar', |
48
|
|
|
login='Max', |
49
|
|
|
password='123', |
50
|
|
|
) |
51
|
|
|
|
52
|
|
|
self.connection.send.has_been_called_with( |
53
|
|
|
'<create_credential>' |
54
|
|
|
'<name>foo</name>' |
55
|
|
|
'<type>up</type>' |
56
|
|
|
'<comment>bar</comment>' |
57
|
|
|
'<login>Max</login>' |
58
|
|
|
'<password>123</password>' |
59
|
|
|
'</create_credential>' |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
def test_create_up_credential_with_allow_insecure(self): |
63
|
|
|
self.gmp.create_credential( |
64
|
|
|
name='foo', |
65
|
|
|
credential_type=CredentialType.USERNAME_PASSWORD, |
66
|
|
|
comment='bar', |
67
|
|
|
login='Max', |
68
|
|
|
password='123', |
69
|
|
|
allow_insecure=True, |
70
|
|
|
) |
71
|
|
|
|
72
|
|
|
self.connection.send.has_been_called_with( |
73
|
|
|
'<create_credential>' |
74
|
|
|
'<name>foo</name>' |
75
|
|
|
'<type>up</type>' |
76
|
|
|
'<comment>bar</comment>' |
77
|
|
|
'<allow_insecure>1</allow_insecure>' |
78
|
|
|
'<login>Max</login>' |
79
|
|
|
'<password>123</password>' |
80
|
|
|
'</create_credential>' |
81
|
|
|
) |
82
|
|
|
|
83
|
|
|
self.gmp.create_credential( |
84
|
|
|
name='foo', |
85
|
|
|
credential_type=CredentialType.USERNAME_PASSWORD, |
86
|
|
|
comment='bar', |
87
|
|
|
login='Max', |
88
|
|
|
password='123', |
89
|
|
|
allow_insecure=False, |
90
|
|
|
) |
91
|
|
|
|
92
|
|
|
self.connection.send.has_been_called_with( |
93
|
|
|
'<create_credential>' |
94
|
|
|
'<name>foo</name>' |
95
|
|
|
'<type>up</type>' |
96
|
|
|
'<comment>bar</comment>' |
97
|
|
|
'<allow_insecure>0</allow_insecure>' |
98
|
|
|
'<login>Max</login>' |
99
|
|
|
'<password>123</password>' |
100
|
|
|
'</create_credential>' |
101
|
|
|
) |
102
|
|
|
|
103
|
|
|
def test_create_cc_credential_missing_certificate(self): |
104
|
|
|
with self.assertRaises(RequiredArgument): |
105
|
|
|
self.gmp.create_credential( |
106
|
|
|
name='foo', credential_type=CredentialType.CLIENT_CERTIFICATE |
107
|
|
|
) |
108
|
|
|
|
109
|
|
|
def test_create_cc_credential(self): |
110
|
|
|
self.gmp.create_credential( |
111
|
|
|
name='foo', |
112
|
|
|
credential_type=CredentialType.CLIENT_CERTIFICATE, |
113
|
|
|
certificate='abcdef', |
114
|
|
|
) |
115
|
|
|
|
116
|
|
|
self.connection.send.has_been_called_with( |
117
|
|
|
'<create_credential>' |
118
|
|
|
'<name>foo</name>' |
119
|
|
|
'<type>cc</type>' |
120
|
|
|
'<certificate>abcdef</certificate>' |
121
|
|
|
'</create_credential>' |
122
|
|
|
) |
123
|
|
|
|
124
|
|
|
def test_create_cc_credential_with_private_key(self): |
125
|
|
|
self.gmp.create_credential( |
126
|
|
|
name='foo', |
127
|
|
|
credential_type=CredentialType.CLIENT_CERTIFICATE, |
128
|
|
|
certificate='abcdef', |
129
|
|
|
private_key='123456', |
130
|
|
|
) |
131
|
|
|
|
132
|
|
|
self.connection.send.has_been_called_with( |
133
|
|
|
'<create_credential>' |
134
|
|
|
'<name>foo</name>' |
135
|
|
|
'<type>cc</type>' |
136
|
|
|
'<certificate>abcdef</certificate>' |
137
|
|
|
'<key>' |
138
|
|
|
'<private>123456</private>' |
139
|
|
|
'</key>' |
140
|
|
|
'</create_credential>' |
141
|
|
|
) |
142
|
|
|
|
143
|
|
|
def test_create_usk_credential_missing_private_key(self): |
144
|
|
|
with self.assertRaises(RequiredArgument): |
145
|
|
|
self.gmp.create_credential( |
146
|
|
|
name='foo', |
147
|
|
|
credential_type=CredentialType.USERNAME_SSH_KEY, |
148
|
|
|
login='foo', |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
def test_create_usk_credential_missing_login(self): |
152
|
|
|
with self.assertRaises(RequiredArgument): |
153
|
|
|
self.gmp.create_credential( |
154
|
|
|
name='foo', |
155
|
|
|
credential_type=CredentialType.USERNAME_SSH_KEY, |
156
|
|
|
private_key='123456', |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
def test_create_usk_credential(self): |
160
|
|
|
self.gmp.create_credential( |
161
|
|
|
name='foo', |
162
|
|
|
credential_type=CredentialType.USERNAME_SSH_KEY, |
163
|
|
|
private_key='123456', |
164
|
|
|
login='foo', |
165
|
|
|
) |
166
|
|
|
|
167
|
|
|
self.connection.send.has_been_called_with( |
168
|
|
|
'<create_credential>' |
169
|
|
|
'<name>foo</name>' |
170
|
|
|
'<type>usk</type>' |
171
|
|
|
'<login>foo</login>' |
172
|
|
|
'<key>' |
173
|
|
|
'<private>123456</private>' |
174
|
|
|
'</key>' |
175
|
|
|
'</create_credential>' |
176
|
|
|
) |
177
|
|
|
|
178
|
|
|
def test_create_usk_credential_with_key_phrase(self): |
179
|
|
|
self.gmp.create_credential( |
180
|
|
|
name='foo', |
181
|
|
|
credential_type=CredentialType.USERNAME_SSH_KEY, |
182
|
|
|
private_key='123456', |
183
|
|
|
login='foo', |
184
|
|
|
key_phrase='abcdef', |
185
|
|
|
) |
186
|
|
|
|
187
|
|
|
self.connection.send.has_been_called_with( |
188
|
|
|
'<create_credential>' |
189
|
|
|
'<name>foo</name>' |
190
|
|
|
'<type>usk</type>' |
191
|
|
|
'<login>foo</login>' |
192
|
|
|
'<key>' |
193
|
|
|
'<private>123456</private>' |
194
|
|
|
'<phrase>abcdef</phrase>' |
195
|
|
|
'</key>' |
196
|
|
|
'</create_credential>' |
197
|
|
|
) |
198
|
|
|
|
199
|
|
|
def test_create_snmp_credential_invalid_auth_algorithm(self): |
200
|
|
|
with self.assertRaises(InvalidArgumentType): |
201
|
|
|
self.gmp.create_credential( |
202
|
|
|
name='foo', credential_type=CredentialType.SNMP, login='foo' |
203
|
|
|
) |
204
|
|
|
|
205
|
|
|
with self.assertRaises(InvalidArgumentType): |
206
|
|
|
self.gmp.create_credential( |
207
|
|
|
name='foo', |
208
|
|
|
credential_type=CredentialType.SNMP, |
209
|
|
|
login='foo', |
210
|
|
|
auth_algorithm='', |
211
|
|
|
) |
212
|
|
|
|
213
|
|
|
with self.assertRaises(InvalidArgumentType): |
214
|
|
|
self.gmp.create_credential( |
215
|
|
|
name='foo', |
216
|
|
|
credential_type=CredentialType.SNMP, |
217
|
|
|
login='foo', |
218
|
|
|
auth_algorithm='bar', |
219
|
|
|
) |
220
|
|
|
|
221
|
|
|
def test_create_snmp_credential_auth_algorithm_md5(self): |
222
|
|
|
self.gmp.create_credential( |
223
|
|
|
name='foo', |
224
|
|
|
credential_type=CredentialType.SNMP, |
225
|
|
|
login='foo', |
226
|
|
|
auth_algorithm=SnmpAuthAlgorithm.MD5, |
227
|
|
|
) |
228
|
|
|
|
229
|
|
|
self.connection.send.has_been_called_with( |
230
|
|
|
'<create_credential>' |
231
|
|
|
'<name>foo</name>' |
232
|
|
|
'<type>snmp</type>' |
233
|
|
|
'<login>foo</login>' |
234
|
|
|
'<auth_algorithm>md5</auth_algorithm>' |
235
|
|
|
'</create_credential>' |
236
|
|
|
) |
237
|
|
|
|
238
|
|
|
def test_create_snmp_credential_auth_algorithm_sha1(self): |
239
|
|
|
self.gmp.create_credential( |
240
|
|
|
name='foo', |
241
|
|
|
credential_type=CredentialType.SNMP, |
242
|
|
|
login='foo', |
243
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
244
|
|
|
) |
245
|
|
|
|
246
|
|
|
self.connection.send.has_been_called_with( |
247
|
|
|
'<create_credential>' |
248
|
|
|
'<name>foo</name>' |
249
|
|
|
'<type>snmp</type>' |
250
|
|
|
'<login>foo</login>' |
251
|
|
|
'<auth_algorithm>sha1</auth_algorithm>' |
252
|
|
|
'</create_credential>' |
253
|
|
|
) |
254
|
|
|
|
255
|
|
|
def test_create_snmp_credential_with_community(self): |
256
|
|
|
self.gmp.create_credential( |
257
|
|
|
name='foo', |
258
|
|
|
credential_type=CredentialType.SNMP, |
259
|
|
|
login='foo', |
260
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
261
|
|
|
community='ipsum', |
262
|
|
|
) |
263
|
|
|
|
264
|
|
|
self.connection.send.has_been_called_with( |
265
|
|
|
'<create_credential>' |
266
|
|
|
'<name>foo</name>' |
267
|
|
|
'<type>snmp</type>' |
268
|
|
|
'<login>foo</login>' |
269
|
|
|
'<auth_algorithm>sha1</auth_algorithm>' |
270
|
|
|
'<community>ipsum</community>' |
271
|
|
|
'</create_credential>' |
272
|
|
|
) |
273
|
|
|
|
274
|
|
|
def test_create_snmp_credential_invalid_privacy_algorithm(self): |
275
|
|
|
with self.assertRaises(InvalidArgumentType): |
276
|
|
|
self.gmp.create_credential( |
277
|
|
|
name='foo', |
278
|
|
|
credential_type=CredentialType.SNMP, |
279
|
|
|
login='foo', |
280
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
281
|
|
|
privacy_algorithm='', |
282
|
|
|
) |
283
|
|
|
|
284
|
|
|
with self.assertRaises(InvalidArgumentType): |
285
|
|
|
self.gmp.create_credential( |
286
|
|
|
name='foo', |
287
|
|
|
credential_type=CredentialType.SNMP, |
288
|
|
|
login='foo', |
289
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
290
|
|
|
privacy_algorithm='foo', |
291
|
|
|
) |
292
|
|
|
|
293
|
|
|
def test_create_snmp_credential_with_privacy_algorithm_aes(self): |
294
|
|
|
self.gmp.create_credential( |
295
|
|
|
name='foo', |
296
|
|
|
credential_type=CredentialType.SNMP, |
297
|
|
|
login='foo', |
298
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
299
|
|
|
privacy_algorithm=SnmpPrivacyAlgorithm.AES, |
300
|
|
|
) |
301
|
|
|
|
302
|
|
|
self.connection.send.has_been_called_with( |
303
|
|
|
'<create_credential>' |
304
|
|
|
'<name>foo</name>' |
305
|
|
|
'<type>snmp</type>' |
306
|
|
|
'<login>foo</login>' |
307
|
|
|
'<auth_algorithm>sha1</auth_algorithm>' |
308
|
|
|
'<privacy>' |
309
|
|
|
'<algorithm>aes</algorithm>' |
310
|
|
|
'</privacy>' |
311
|
|
|
'</create_credential>' |
312
|
|
|
) |
313
|
|
|
|
314
|
|
|
def test_create_snmp_credential_with_privacy_algorithm_des(self): |
315
|
|
|
self.gmp.create_credential( |
316
|
|
|
name='foo', |
317
|
|
|
credential_type=CredentialType.SNMP, |
318
|
|
|
login='foo', |
319
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
320
|
|
|
privacy_algorithm=SnmpPrivacyAlgorithm.DES, |
321
|
|
|
) |
322
|
|
|
|
323
|
|
|
self.connection.send.has_been_called_with( |
324
|
|
|
'<create_credential>' |
325
|
|
|
'<name>foo</name>' |
326
|
|
|
'<type>snmp</type>' |
327
|
|
|
'<login>foo</login>' |
328
|
|
|
'<auth_algorithm>sha1</auth_algorithm>' |
329
|
|
|
'<privacy>' |
330
|
|
|
'<algorithm>des</algorithm>' |
331
|
|
|
'</privacy>' |
332
|
|
|
'</create_credential>' |
333
|
|
|
) |
334
|
|
|
|
335
|
|
|
def test_create_snmp_credential_with_privacy_password(self): |
336
|
|
|
self.gmp.create_credential( |
337
|
|
|
name='foo', |
338
|
|
|
credential_type=CredentialType.SNMP, |
339
|
|
|
login='foo', |
340
|
|
|
auth_algorithm=SnmpAuthAlgorithm.SHA1, |
341
|
|
|
privacy_password='123', |
342
|
|
|
) |
343
|
|
|
|
344
|
|
|
self.connection.send.has_been_called_with( |
345
|
|
|
'<create_credential>' |
346
|
|
|
'<name>foo</name>' |
347
|
|
|
'<type>snmp</type>' |
348
|
|
|
'<login>foo</login>' |
349
|
|
|
'<auth_algorithm>sha1</auth_algorithm>' |
350
|
|
|
'<privacy>' |
351
|
|
|
'<password>123</password>' |
352
|
|
|
'</privacy>' |
353
|
|
|
'</create_credential>' |
354
|
|
|
) |
355
|
|
|
|
356
|
|
|
def test_create_smime_credential(self): |
357
|
|
|
self.gmp.create_credential( |
358
|
|
|
name='foo', |
359
|
|
|
credential_type=CredentialType.SMIME_CERTIFICATE, |
360
|
|
|
certificate='ipsum', |
361
|
|
|
) |
362
|
|
|
|
363
|
|
|
self.connection.send.has_been_called_with( |
364
|
|
|
'<create_credential>' |
365
|
|
|
'<name>foo</name>' |
366
|
|
|
'<type>smime</type>' |
367
|
|
|
'<certificate>ipsum</certificate>' |
368
|
|
|
'</create_credential>' |
369
|
|
|
) |
370
|
|
|
|
371
|
|
|
def test_create_smime_credential_missing_certificate(self): |
372
|
|
|
with self.assertRaises(RequiredArgument): |
373
|
|
|
self.gmp.create_credential( |
374
|
|
|
name='foo', credential_type=CredentialType.SMIME_CERTIFICATE |
375
|
|
|
) |
376
|
|
|
|
377
|
|
|
def test_create_pgp_credential(self): |
378
|
|
|
self.gmp.create_credential( |
379
|
|
|
name='foo', |
380
|
|
|
credential_type=CredentialType.PGP_ENCRYPTION_KEY, |
381
|
|
|
public_key='ipsum', |
382
|
|
|
) |
383
|
|
|
|
384
|
|
|
self.connection.send.has_been_called_with( |
385
|
|
|
'<create_credential>' |
386
|
|
|
'<name>foo</name>' |
387
|
|
|
'<type>pgp</type>' |
388
|
|
|
'<key>' |
389
|
|
|
'<public>ipsum</public>' |
390
|
|
|
'</key>' |
391
|
|
|
'</create_credential>' |
392
|
|
|
) |
393
|
|
|
|
394
|
|
|
def test_create_pgp_credential_missing_public_key(self): |
395
|
|
|
with self.assertRaises(RequiredArgument): |
396
|
|
|
self.gmp.create_credential( |
397
|
|
|
name='foo', credential_type=CredentialType.PGP_ENCRYPTION_KEY |
398
|
|
|
) |
399
|
|
|
|
400
|
|
|
def test_create_credential_invalid_credential_type(self): |
401
|
|
|
with self.assertRaises(InvalidArgumentType): |
402
|
|
|
self.gmp.create_credential(name='foo', credential_type=None) |
403
|
|
|
|
404
|
|
|
with self.assertRaises(InvalidArgumentType): |
405
|
|
|
self.gmp.create_credential(name='foo', credential_type='') |
406
|
|
|
|
407
|
|
|
with self.assertRaises(InvalidArgumentType): |
408
|
|
|
self.gmp.create_credential(name='foo', credential_type='bar') |
409
|
|
|
|
410
|
|
|
def test_create_pw_credential_missing_password(self): |
411
|
|
|
with self.assertRaises(RequiredArgument): |
412
|
|
|
self.gmp.create_credential( |
413
|
|
|
name='foo', credential_type=CredentialType.PASSWORD_ONLY |
414
|
|
|
) |
415
|
|
|
|
416
|
|
|
def test_create_pw_credential(self): |
417
|
|
|
self.gmp.create_credential( |
418
|
|
|
name='foo', |
419
|
|
|
credential_type=CredentialType.PASSWORD_ONLY, |
420
|
|
|
password='foo', |
421
|
|
|
) |
422
|
|
|
self.connection.send.has_been_called_with( |
423
|
|
|
'<create_credential>' |
424
|
|
|
'<name>foo</name>' |
425
|
|
|
'<type>pw</type>' |
426
|
|
|
'<password>foo</password>' |
427
|
|
|
'</create_credential>' |
428
|
|
|
) |
429
|
|
|
|