File: test_create_credential.py

package info (click to toggle)
python-gvm 26.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,132 kB
  • sloc: python: 44,662; makefile: 18
file content (446 lines) | stat: -rw-r--r-- 14,372 bytes parent folder | download
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
444
445
446
# SPDX-FileCopyrightText: 2020-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

from gvm.errors import InvalidArgument, RequiredArgument
from gvm.protocols.gmp.requests.v224 import (
    CredentialType,
    SnmpAuthAlgorithm,
    SnmpPrivacyAlgorithm,
)


class GmpCreateCredentialTestMixin:
    def test_create_up_credential_missing_name(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="",
                credential_type=CredentialType.USERNAME_PASSWORD,
                login="foo",
            )

        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name=None,
                credential_type=CredentialType.USERNAME_PASSWORD,
                login="foo",
            )

    def test_create_up_credential(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_PASSWORD,
            comment="bar",
            login="Max",
            password="123",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>up</type>"
            b"<comment>bar</comment>"
            b"<login>Max</login>"
            b"<password>123</password>"
            b"</create_credential>"
        )

    def test_create_up_credential_with_allow_insecure(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_PASSWORD,
            comment="bar",
            login="Max",
            password="123",
            allow_insecure=True,
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>up</type>"
            b"<comment>bar</comment>"
            b"<allow_insecure>1</allow_insecure>"
            b"<login>Max</login>"
            b"<password>123</password>"
            b"</create_credential>"
        )

        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_PASSWORD,
            comment="bar",
            login="Max",
            password="123",
            allow_insecure=False,
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>up</type>"
            b"<comment>bar</comment>"
            b"<allow_insecure>0</allow_insecure>"
            b"<login>Max</login>"
            b"<password>123</password>"
            b"</create_credential>"
        )

    def test_create_up_credential_auto_generate_password(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_PASSWORD,
            login="Max",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>up</type>"
            b"<login>Max</login>"
            b"</create_credential>"
        )

    def test_create_cc_credential_missing_certificate(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo", credential_type=CredentialType.CLIENT_CERTIFICATE
            )

    def test_create_cc_credential(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.CLIENT_CERTIFICATE,
            certificate="abcdef",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>cc</type>"
            b"<certificate>abcdef</certificate>"
            b"</create_credential>"
        )

    def test_create_cc_credential_with_private_key(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.CLIENT_CERTIFICATE,
            certificate="abcdef",
            private_key="123456",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>cc</type>"
            b"<certificate>abcdef</certificate>"
            b"<key>"
            b"<private>123456</private>"
            b"</key>"
            b"</create_credential>"
        )

    def test_create_usk_credential_missing_private_key(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo",
                credential_type=CredentialType.USERNAME_SSH_KEY,
                login="foo",
                private_key="",
            )

    def test_create_usk_credential_missing_login(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo",
                credential_type=CredentialType.USERNAME_SSH_KEY,
                private_key="123456",
            )

    def test_create_usk_credential(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_SSH_KEY,
            private_key="123456",
            login="foo",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>usk</type>"
            b"<login>foo</login>"
            b"<key>"
            b"<private>123456</private>"
            b"</key>"
            b"</create_credential>"
        )

    def test_create_usk_credential_with_key_phrase(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_SSH_KEY,
            private_key="123456",
            login="foo",
            key_phrase="abcdef",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>usk</type>"
            b"<login>foo</login>"
            b"<key>"
            b"<private>123456</private>"
            b"<phrase>abcdef</phrase>"
            b"</key>"
            b"</create_credential>"
        )

    def test_create_usk_credential_auto_generate_ssh_key(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.USERNAME_SSH_KEY,
            login="foo",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>usk</type>"
            b"<login>foo</login>"
            b"</create_credential>"
        )

    def test_create_snmp_credential_invalid_auth_algorithm(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo", credential_type=CredentialType.SNMP, login="foo"
            )

        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo",
                credential_type=CredentialType.SNMP,
                login="foo",
                auth_algorithm="",
            )

        with self.assertRaises(InvalidArgument):
            self.gmp.create_credential(
                name="foo",
                credential_type=CredentialType.SNMP,
                login="foo",
                auth_algorithm="bar",
            )

    def test_create_snmp_credential_auth_algorithm_md5(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SNMP,
            login="foo",
            auth_algorithm=SnmpAuthAlgorithm.MD5,
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>snmp</type>"
            b"<login>foo</login>"
            b"<auth_algorithm>md5</auth_algorithm>"
            b"</create_credential>"
        )

    def test_create_snmp_credential_auth_algorithm_sha1(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SNMP,
            login="foo",
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>snmp</type>"
            b"<login>foo</login>"
            b"<auth_algorithm>sha1</auth_algorithm>"
            b"</create_credential>"
        )

    def test_create_snmp_credential_with_community(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SNMP,
            login="foo",
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
            community="ipsum",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>snmp</type>"
            b"<login>foo</login>"
            b"<auth_algorithm>sha1</auth_algorithm>"
            b"<community>ipsum</community>"
            b"</create_credential>"
        )

    def test_create_snmp_credential_invalid_privacy_algorithm(self):
        with self.assertRaises(ValueError):
            self.gmp.create_credential(
                name="foo",
                credential_type=CredentialType.SNMP,
                login="foo",
                auth_algorithm=SnmpAuthAlgorithm.SHA1,
                privacy_algorithm="",
            )

        with self.assertRaises(InvalidArgument):
            self.gmp.create_credential(
                name="foo",
                credential_type=CredentialType.SNMP,
                login="foo",
                auth_algorithm=SnmpAuthAlgorithm.SHA1,
                privacy_algorithm="foo",
            )

    def test_create_snmp_credential_with_privacy_algorithm_aes(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SNMP,
            login="foo",
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
            privacy_algorithm=SnmpPrivacyAlgorithm.AES,
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>snmp</type>"
            b"<login>foo</login>"
            b"<auth_algorithm>sha1</auth_algorithm>"
            b"<privacy>"
            b"<algorithm>aes</algorithm>"
            b"</privacy>"
            b"</create_credential>"
        )

    def test_create_snmp_credential_with_privacy_algorithm_des(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SNMP,
            login="foo",
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
            privacy_algorithm=SnmpPrivacyAlgorithm.DES,
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>snmp</type>"
            b"<login>foo</login>"
            b"<auth_algorithm>sha1</auth_algorithm>"
            b"<privacy>"
            b"<algorithm>des</algorithm>"
            b"</privacy>"
            b"</create_credential>"
        )

    def test_create_snmp_credential_with_privacy_password(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SNMP,
            login="foo",
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
            privacy_password="123",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>snmp</type>"
            b"<login>foo</login>"
            b"<auth_algorithm>sha1</auth_algorithm>"
            b"<privacy>"
            b"<password>123</password>"
            b"</privacy>"
            b"</create_credential>"
        )

    def test_create_smime_credential(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.SMIME_CERTIFICATE,
            certificate="ipsum",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>smime</type>"
            b"<certificate>ipsum</certificate>"
            b"</create_credential>"
        )

    def test_create_smime_credential_missing_certificate(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo", credential_type=CredentialType.SMIME_CERTIFICATE
            )

    def test_create_pgp_credential(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.PGP_ENCRYPTION_KEY,
            public_key="ipsum",
        )

        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>pgp</type>"
            b"<key>"
            b"<public>ipsum</public>"
            b"</key>"
            b"</create_credential>"
        )

    def test_create_pgp_credential_missing_public_key(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo", credential_type=CredentialType.PGP_ENCRYPTION_KEY
            )

    def test_create_credential_invalid_credential_type(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(name="foo", credential_type=None)

        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(name="foo", credential_type="")

        with self.assertRaises(InvalidArgument):
            self.gmp.create_credential(name="foo", credential_type="bar")

    def test_create_pw_credential_missing_password(self):
        with self.assertRaises(RequiredArgument):
            self.gmp.create_credential(
                name="foo", credential_type=CredentialType.PASSWORD_ONLY
            )

    def test_create_pw_credential(self):
        self.gmp.create_credential(
            name="foo",
            credential_type=CredentialType.PASSWORD_ONLY,
            password="foo",
        )
        self.connection.send.has_been_called_with(
            b"<create_credential>"
            b"<name>foo</name>"
            b"<type>pw</type>"
            b"<password>foo</password>"
            b"</create_credential>"
        )