Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_pacman_key.py

raw
1 # Copyright (c) 2019, George Rawlinson <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import json
8
9 import pytest
10 from ansible.module_utils.basic import AnsibleModule
11
12 from ansible_collections.community.general.plugins.modules import pacman_key
13
14 # path used for mocking get_bin_path()
15 MOCK_BIN_PATH = "/mocked/path"
16
17 # Key ID used for tests
18 TESTING_KEYID = "14F26682D0916CDD81E37B6D61B7B526D98F0353"
19 TESTING_KEYFILE_PATH = "/tmp/pubkey.asc"
20
21 # gpg --{show,list}-key output (key present, but expired)
22 GPG_SHOWKEY_OUTPUT_EXPIRED = """
23 tru::1:1616373715:0:3:1:5
24 pub:-:4096:1:61B7B526D98F0353:1437155332:::-:::scSC::::::23::0:
25 fpr:::::::::14F26682D0916CDD81E37B6D61B7B526D98F0353:
26 uid:-::::1437155332::E57D1F9BFF3B404F9F30333629369B08DF5E2161::Mozilla Software Releases <[email protected]>::::::::::0:
27 sub:e:4096:1:1C69C4E55E9905DB:1437155572:1500227572:::::s::::::23:
28 fpr:::::::::F2EF4E6E6AE75B95F11F1EB51C69C4E55E9905DB:
29 sub:e:4096:1:BBBEBDBB24C6F355:1498143157:1561215157:::::s::::::23:
30 fpr:::::::::DCEAC5D96135B91C4EA672ABBBBEBDBB24C6F355:
31 sub:e:4096:1:F1A6668FBB7D572E:1559247338:1622319338:::::s::::::23:
32 fpr:::::::::097B313077AE62A02F84DA4DF1A6668FBB7D572E:
33 """.strip()
34
35 # gpg --{show,list}-key output (key present and trusted)
36 GPG_SHOWKEY_OUTPUT_TRUSTED = """
37 tru::1:1616373715:0:3:1:5
38 pub:f:4096:1:61B7B526D98F0353:1437155332:::-:::scSC::::::23::0:
39 fpr:::::::::14F26682D0916CDD81E37B6D61B7B526D98F0353:
40 uid:f::::1437155332::E57D1F9BFF3B404F9F30333629369B08DF5E2161::Mozilla Software Releases <[email protected]>::::::::::0:
41 sub:e:4096:1:1C69C4E55E9905DB:1437155572:1500227572:::::s::::::23:
42 fpr:::::::::F2EF4E6E6AE75B95F11F1EB51C69C4E55E9905DB:
43 sub:e:4096:1:BBBEBDBB24C6F355:1498143157:1561215157:::::s::::::23:
44 fpr:::::::::DCEAC5D96135B91C4EA672ABBBBEBDBB24C6F355:
45 sub:e:4096:1:F1A6668FBB7D572E:1559247338:1622319338:::::s::::::23:
46 fpr:::::::::097B313077AE62A02F84DA4DF1A6668FBB7D572E:
47 """.strip()
48
49 GPG_LIST_SECRET_KEY_OUTPUT = """
50 sec:u:2048:1:58FCCBCC131FCCAB:1406639814:::u:::scSC:::+:::23::0:
51 fpr:::::::::AC0F357BE07F1493C34DCAB258FCCBCC131FCCAB:
52 grp:::::::::C1227FFDD039AD942F777EA0639E1F1EAA96AB12:
53 uid:u::::1406639814::79311EDEA01302E0DBBB2F33AE799F8BB677652F::Pacman Keyring Master Key <pacman@localhost>::::::::::0:
54 """.lstrip()
55
56 GPG_CHECK_SIGNATURES_OUTPUT = """
57 tru::1:1742507906:1750096255:3:1:5
58 pub:f:4096:1:61B7B526D98F0353:1437155332:::-:::scSC::::::23:1742507897:1 https\x3a//185.125.188.26\x3a443:
59 fpr:::::::::14F26682D0916CDD81E37B6D61B7B526D98F0353:
60 uid:f::::1437155332::E57D1F9BFF3B404F9F30333629369B08DF5E2161::Mozilla Software Releases <[email protected]>:::::::::1742507897:1:
61 sig:!::1:61B7B526D98F0353:1437155332::::Mozilla Software Releases <[email protected]>:13x:::::2:
62 sig:!::1:58FCCBCC131FCCAB:1742507905::::Pacman Keyring Master Key <pacman@localhost>:10l::AC0F357BE07F1493C34DCAB258FCCBCC131FCCAB:::8:
63 sub:f:4096:1:E36D3B13F3D93274:1683308659:1746380659:::::s::::::23:
64 fpr:::::::::ADD7079479700DCADFDD5337E36D3B13F3D93274:
65 sig:!::1:61B7B526D98F0353:1683308659::::Mozilla Software Releases <[email protected]>:18x::14F26682D0916CDD81E37B6D61B7B526D98F0353:::10:
66 sub:e:4096:1:1C69C4E55E9905DB:1437155572:1500227572:::::s::::::23:
67 fpr:::::::::F2EF4E6E6AE75B95F11F1EB51C69C4E55E9905DB:
68 sig:!::1:61B7B526D98F0353:1437155572::::Mozilla Software Releases <[email protected]>:18x:::::2:
69 sub:e:4096:1:BBBEBDBB24C6F355:1498143157:1561215157:::::s::::::23:
70 fpr:::::::::DCEAC5D96135B91C4EA672ABBBBEBDBB24C6F355:
71 sig:!::1:61B7B526D98F0353:1498143157::::Mozilla Software Releases <[email protected]>:18x::14F26682D0916CDD81E37B6D61B7B526D98F0353:::8:
72 sub:e:4096:1:F1A6668FBB7D572E:1559247338:1622319338:::::s::::::23:
73 fpr:::::::::097B313077AE62A02F84DA4DF1A6668FBB7D572E:
74 sig:!::1:61B7B526D98F0353:1559247338::::Mozilla Software Releases <[email protected]>:18x::14F26682D0916CDD81E37B6D61B7B526D98F0353:::10:
75 sub:e:4096:1:EBE41E90F6F12F6D:1621282261:1684354261:::::s::::::23:
76 fpr:::::::::4360FE2109C49763186F8E21EBE41E90F6F12F6D:
77 sig:!::1:61B7B526D98F0353:1621282261::::Mozilla Software Releases <[email protected]>:18x::14F26682D0916CDD81E37B6D61B7B526D98F0353:::10:
78 """.strip()
79
80 # gpg --{show,list}-key output (key absent)
81 GPG_NOKEY_OUTPUT = """
82 gpg: error reading key: No public key
83 tru::1:1616373715:0:3:1:5
84 """.strip()
85
86 # pacman-key output (successful invocation)
87 PACMAN_KEY_SUCCESS = """
88 ==> Updating trust database...
89 gpg: next trustdb check due at 2021-08-02
90 """.strip()
91
92 # expected command for gpg --list-keys KEYID
93 RUN_CMD_LISTKEYS = [
94 MOCK_BIN_PATH,
95 "--homedir=/etc/pacman.d/gnupg",
96 "--no-permission-warning",
97 "--with-colons",
98 "--quiet",
99 "--batch",
100 "--no-tty",
101 "--no-default-keyring",
102 "--list-keys",
103 TESTING_KEYID,
104 ]
105
106 # expected command for gpg --show-keys KEYFILE
107 RUN_CMD_SHOW_KEYFILE = [
108 MOCK_BIN_PATH,
109 "--no-permission-warning",
110 "--with-colons",
111 "--quiet",
112 "--batch",
113 "--no-tty",
114 "--with-fingerprint",
115 "--show-keys",
116 TESTING_KEYFILE_PATH,
117 ]
118
119 # expected command for pacman-key --lsign-key KEYID
120 RUN_CMD_LSIGN_KEY = [
121 MOCK_BIN_PATH,
122 "--gpgdir",
123 "/etc/pacman.d/gnupg",
124 "--lsign-key",
125 TESTING_KEYID,
126 ]
127
128 RUN_CMD_LIST_SECRET_KEY = [
129 MOCK_BIN_PATH,
130 "--homedir=/etc/pacman.d/gnupg",
131 "--no-permission-warning",
132 "--with-colons",
133 "--quiet",
134 "--batch",
135 "--no-tty",
136 "--list-secret-key",
137 ]
138
139 # expected command for gpg --check-signatures
140 RUN_CMD_CHECK_SIGNATURES = [
141 MOCK_BIN_PATH,
142 "--homedir=/etc/pacman.d/gnupg",
143 "--no-permission-warning",
144 "--with-colons",
145 "--quiet",
146 "--batch",
147 "--no-tty",
148 "--check-signatures",
149 TESTING_KEYID,
150 ]
151
152 TESTCASES = [
153 #
154 # invalid user input
155 #
156 # state: present, id: absent
157 [
158 {
159 "state": "present",
160 },
161 {
162 "id": "param_missing_id",
163 "msg": "missing required arguments: id",
164 "failed": True,
165 },
166 ],
167 # state: present, required parameters: missing
168 [
169 {
170 "state": "present",
171 "id": "0xDOESNTMATTER",
172 },
173 {
174 "id": "param_missing_method",
175 "msg": "state is present but any of the following are missing: data, file, url, keyserver",
176 "failed": True,
177 },
178 ],
179 # state: present, id: invalid (not full-length)
180 [
181 {
182 "id": "0xDOESNTMATTER",
183 "data": "FAKEDATA",
184 },
185 {
186 "id": "param_id_not_full",
187 "msg": "key ID is not full-length: DOESNTMATTER",
188 "failed": True,
189 },
190 ],
191 # state: present, id: invalid (not hexadecimal)
192 [
193 {
194 "state": "present",
195 "id": "01234567890ABCDE01234567890ABCDE1234567M",
196 "data": "FAKEDATA",
197 },
198 {
199 "id": "param_id_not_hex",
200 "msg": "key ID is not hexadecimal: 01234567890ABCDE01234567890ABCDE1234567M",
201 "failed": True,
202 },
203 ],
204 # state: absent, id: absent
205 [
206 {
207 "state": "absent",
208 },
209 {
210 "id": "param_absent_state_missing_id",
211 "msg": "missing required arguments: id",
212 "failed": True,
213 },
214 ],
215 #
216 # check mode
217 #
218 # state & key present
219 [
220 {
221 "state": "present",
222 "id": TESTING_KEYID,
223 "data": "FAKEDATA",
224 "_ansible_check_mode": True,
225 },
226 {
227 "id": "checkmode_state_and_key_present",
228 "run_command.calls": [
229 (
230 RUN_CMD_LISTKEYS,
231 {"check_rc": False},
232 (
233 0,
234 GPG_SHOWKEY_OUTPUT_EXPIRED,
235 "",
236 ),
237 ),
238 ],
239 "changed": False,
240 },
241 ],
242 # state present, key absent
243 [
244 {
245 "state": "present",
246 "id": TESTING_KEYID,
247 "data": "FAKEDATA",
248 "_ansible_check_mode": True,
249 },
250 {
251 "id": "checkmode_state_present_key_absent",
252 "run_command.calls": [
253 (
254 RUN_CMD_LISTKEYS,
255 {"check_rc": False},
256 (
257 2,
258 "",
259 GPG_NOKEY_OUTPUT,
260 ),
261 ),
262 ],
263 "changed": True,
264 },
265 ],
266 # state & key absent
267 [
268 {
269 "state": "absent",
270 "id": TESTING_KEYID,
271 "_ansible_check_mode": True,
272 },
273 {
274 "id": "checkmode_state_and_key_absent",
275 "run_command.calls": [
276 (
277 RUN_CMD_LISTKEYS,
278 {"check_rc": False},
279 (
280 2,
281 "",
282 GPG_NOKEY_OUTPUT,
283 ),
284 ),
285 ],
286 "changed": False,
287 },
288 ],
289 # state absent, key present
290 [
291 {
292 "state": "absent",
293 "id": TESTING_KEYID,
294 "_ansible_check_mode": True,
295 },
296 {
297 "id": "check_mode_state_absent_key_present",
298 "run_command.calls": [
299 (
300 RUN_CMD_LISTKEYS,
301 {"check_rc": False},
302 (
303 0,
304 GPG_SHOWKEY_OUTPUT_EXPIRED,
305 "",
306 ),
307 ),
308 ],
309 "changed": True,
310 },
311 ],
312 #
313 # normal operation
314 #
315 # state & key present
316 [
317 {
318 "state": "present",
319 "id": TESTING_KEYID,
320 "data": "FAKEDATA",
321 },
322 {
323 "id": "state_and_key_present",
324 "run_command.calls": [
325 (
326 RUN_CMD_LISTKEYS,
327 {"check_rc": False},
328 (
329 0,
330 GPG_SHOWKEY_OUTPUT_EXPIRED,
331 "",
332 ),
333 ),
334 ],
335 "changed": False,
336 },
337 ],
338 # state present, ensure_trusted & key expired
339 [
340 {
341 "state": "present",
342 "ensure_trusted": True,
343 "id": TESTING_KEYID,
344 "data": "FAKEDATA",
345 "_ansible_check_mode": True,
346 },
347 {
348 "id": "state_present_trusted_key_expired",
349 "run_command.calls": [
350 (
351 RUN_CMD_LISTKEYS,
352 {
353 "check_rc": False,
354 },
355 (
356 0,
357 GPG_SHOWKEY_OUTPUT_EXPIRED,
358 "",
359 ),
360 ),
361 ],
362 "changed": True,
363 },
364 ],
365 # state present & key trusted
366 [
367 {
368 "state": "present",
369 "ensure_trusted": True,
370 "id": TESTING_KEYID,
371 "data": "FAKEDATA",
372 "_ansible_check_mode": True,
373 },
374 {
375 "id": "state_present_and_key_trusted",
376 "run_command.calls": [
377 (
378 RUN_CMD_LISTKEYS,
379 {
380 "check_rc": False,
381 },
382 (
383 0,
384 GPG_SHOWKEY_OUTPUT_TRUSTED,
385 "",
386 ),
387 ),
388 (
389 RUN_CMD_CHECK_SIGNATURES,
390 {},
391 (
392 0,
393 GPG_CHECK_SIGNATURES_OUTPUT,
394 "",
395 ),
396 ),
397 (
398 RUN_CMD_LIST_SECRET_KEY,
399 {},
400 (
401 0,
402 GPG_LIST_SECRET_KEY_OUTPUT,
403 "",
404 ),
405 ),
406 ],
407 "changed": False,
408 },
409 ],
410 # state absent, key present
411 [
412 {
413 "state": "absent",
414 "id": TESTING_KEYID,
415 },
416 {
417 "id": "state_absent_key_present",
418 "run_command.calls": [
419 (
420 RUN_CMD_LISTKEYS,
421 {"check_rc": False},
422 (
423 0,
424 GPG_SHOWKEY_OUTPUT_EXPIRED,
425 "",
426 ),
427 ),
428 (
429 [
430 MOCK_BIN_PATH,
431 "--gpgdir",
432 "/etc/pacman.d/gnupg",
433 "--delete",
434 TESTING_KEYID,
435 ],
436 {"check_rc": True},
437 (
438 0,
439 PACMAN_KEY_SUCCESS,
440 "",
441 ),
442 ),
443 ],
444 "changed": True,
445 },
446 ],
447 # state & key absent
448 [
449 {
450 "state": "absent",
451 "id": TESTING_KEYID,
452 },
453 {
454 "id": "state_and_key_absent",
455 "run_command.calls": [
456 (
457 RUN_CMD_LISTKEYS,
458 {"check_rc": False},
459 (
460 2,
461 "",
462 GPG_NOKEY_OUTPUT,
463 ),
464 ),
465 ],
466 "changed": False,
467 },
468 ],
469 # state: present, key: absent, method: file
470 [
471 {
472 "state": "present",
473 "id": TESTING_KEYID,
474 "file": TESTING_KEYFILE_PATH,
475 },
476 {
477 "id": "state_present_key_absent_method_file",
478 "run_command.calls": [
479 (
480 RUN_CMD_LISTKEYS,
481 {"check_rc": False},
482 (
483 2,
484 "",
485 GPG_NOKEY_OUTPUT,
486 ),
487 ),
488 (
489 RUN_CMD_SHOW_KEYFILE,
490 {"check_rc": True},
491 (
492 0,
493 GPG_SHOWKEY_OUTPUT_EXPIRED,
494 "",
495 ),
496 ),
497 (
498 [
499 MOCK_BIN_PATH,
500 "--gpgdir",
501 "/etc/pacman.d/gnupg",
502 "--add",
503 "/tmp/pubkey.asc",
504 ],
505 {"check_rc": True},
506 (
507 0,
508 PACMAN_KEY_SUCCESS,
509 "",
510 ),
511 ),
512 (
513 RUN_CMD_LSIGN_KEY,
514 {"check_rc": True},
515 (
516 0,
517 PACMAN_KEY_SUCCESS,
518 "",
519 ),
520 ),
521 ],
522 "changed": True,
523 },
524 ],
525 # state: present, key: absent, method: file
526 # failure: keyid & keyfile don't match
527 [
528 {
529 "state": "present",
530 "id": TESTING_KEYID,
531 "file": TESTING_KEYFILE_PATH,
532 },
533 {
534 "id": "state_present_key_absent_verify_failed",
535 "msg": "key ID does not match. expected 14F26682D0916CDD81E37B6D61B7B526D98F0353, got 14F26682D0916CDD81E37B6D61B7B526D98F0354",
536 "run_command.calls": [
537 (
538 RUN_CMD_LISTKEYS,
539 {"check_rc": False},
540 (
541 2,
542 "",
543 GPG_NOKEY_OUTPUT,
544 ),
545 ),
546 (
547 RUN_CMD_SHOW_KEYFILE,
548 {"check_rc": True},
549 (
550 0,
551 GPG_SHOWKEY_OUTPUT_EXPIRED.replace("61B7B526D98F0353", "61B7B526D98F0354"),
552 "",
553 ),
554 ),
555 ],
556 "failed": True,
557 },
558 ],
559 # state: present, key: absent, method: keyserver
560 [
561 {
562 "state": "present",
563 "id": TESTING_KEYID,
564 "keyserver": "pgp.mit.edu",
565 },
566 {
567 "id": "state_present_key_absent_method_keyserver",
568 "run_command.calls": [
569 (
570 RUN_CMD_LISTKEYS,
571 {"check_rc": False},
572 (
573 2,
574 "",
575 GPG_NOKEY_OUTPUT,
576 ),
577 ),
578 (
579 [
580 MOCK_BIN_PATH,
581 "--gpgdir",
582 "/etc/pacman.d/gnupg",
583 "--keyserver",
584 "pgp.mit.edu",
585 "--recv-keys",
586 TESTING_KEYID,
587 ],
588 {"check_rc": True},
589 (
590 0,
591 """
592 gpg: key 0x61B7B526D98F0353: 32 signatures not checked due to missing keys
593 gpg: key 0x61B7B526D98F0353: public key "Mozilla Software Releases <[email protected]>" imported
594 gpg: marginals needed: 3 completes needed: 1 trust model: pgp
595 gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
596 gpg: Total number processed: 1
597 gpg: imported: 1
598 """,
599 "",
600 ),
601 ),
602 (
603 RUN_CMD_LSIGN_KEY,
604 {"check_rc": True},
605 (
606 0,
607 PACMAN_KEY_SUCCESS,
608 "",
609 ),
610 ),
611 ],
612 "changed": True,
613 },
614 ],
615 # state: present, key: absent, method: data
616 [
617 {
618 "state": "present",
619 "id": TESTING_KEYID,
620 "data": "PGP_DATA",
621 },
622 {
623 "id": "state_present_key_absent_method_data",
624 "run_command.calls": [
625 (
626 RUN_CMD_LISTKEYS,
627 {"check_rc": False},
628 (
629 2,
630 "",
631 GPG_NOKEY_OUTPUT,
632 ),
633 ),
634 (
635 RUN_CMD_SHOW_KEYFILE,
636 {"check_rc": True},
637 (
638 0,
639 GPG_SHOWKEY_OUTPUT_EXPIRED,
640 "",
641 ),
642 ),
643 (
644 [
645 MOCK_BIN_PATH,
646 "--gpgdir",
647 "/etc/pacman.d/gnupg",
648 "--add",
649 "/tmp/pubkey.asc",
650 ],
651 {"check_rc": True},
652 (
653 0,
654 PACMAN_KEY_SUCCESS,
655 "",
656 ),
657 ),
658 (
659 RUN_CMD_LSIGN_KEY,
660 {"check_rc": True},
661 (
662 0,
663 PACMAN_KEY_SUCCESS,
664 "",
665 ),
666 ),
667 ],
668 "save_key_output": TESTING_KEYFILE_PATH,
669 "changed": True,
670 },
671 ],
672 ]
673
674
675 @pytest.fixture
676 def patch_get_bin_path(mocker):
677 mocker.patch.object(
678 AnsibleModule,
679 "get_bin_path",
680 return_value=MOCK_BIN_PATH,
681 )
682
683
684 @pytest.mark.parametrize(
685 "patch_ansible_module, expected",
686 TESTCASES,
687 ids=[item[1]["id"] for item in TESTCASES], # type: ignore
688 indirect=["patch_ansible_module"],
689 )
690 @pytest.mark.usefixtures("patch_ansible_module")
691 def test_operation(mocker, capfd, patch_get_bin_path, expected):
692 # patch run_command invocations with mock data
693 if "run_command.calls" in expected:
694 mocker.patch.object(
695 AnsibleModule,
696 "run_command",
697 side_effect=[item[2] for item in expected["run_command.calls"]],
698 )
699
700 # patch save_key invocations with mock data
701 if "save_key_output" in expected:
702 mocker.patch.object(
703 pacman_key.PacmanKey,
704 "save_key",
705 return_value=expected["save_key_output"],
706 )
707
708 # invoke module
709 with pytest.raises(SystemExit):
710 pacman_key.main()
711
712 # capture std{out,err}
713 out, err = capfd.readouterr()
714 results = json.loads(out)
715
716 # assertion time!
717 if "msg" in expected:
718 assert results["msg"] == expected["msg"]
719 if "changed" in expected:
720 assert results["changed"] == expected["changed"]
721 if "failed" in expected:
722 assert results["failed"] == expected["failed"]
723
724 if "run_command.calls" in expected:
725 assert AnsibleModule.run_command.call_count == len(expected["run_command.calls"])
726 call_args_list = [(item[0][0], item[1]) for item in AnsibleModule.run_command.call_args_list]
727 expected_call_args_list = [(item[0], item[1]) for item in expected["run_command.calls"]]
728 assert call_args_list == expected_call_args_list