Debian Codegraph @

test/units/modules/test_iptables.py

raw
1 # Copyright: Contributors to the Ansible project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5 from units.modules.utils import AnsibleExitJson, AnsibleFailJson, fail_json, exit_json
6 import pytest
7
8 from ansible.modules import iptables
9
10 IPTABLES_CMD = "/sbin/iptables"
11 IP6TABLES_CMD = "/sbin/ip6tables"
12 IPTABLES_VERSION = "1.8.2"
13 CONST_INPUT_FILTER = [IPTABLES_CMD, "-t", "filter", "-L", "INPUT",]
14
15
16 @pytest.fixture
17 def _mock_basic_commands(mocker):
18 """Mock basic commands like get_bin_path and get_iptables_version."""
19 mocker.patch(
20 "ansible.module_utils.basic.AnsibleModule.get_bin_path",
21 return_value=IPTABLES_CMD,
22 )
23 mocker.patch("ansible.modules.iptables.get_iptables_version", return_value=IPTABLES_VERSION)
24
25
26 @pytest.mark.usefixtures('_mock_basic_commands')
27 def test_flush_table_without_chain(mocker, set_module_args):
28 """Test flush without chain, flush the table."""
29 set_module_args(
30 {
31 "flush": True,
32 }
33 )
34 run_command = mocker.patch(
35 "ansible.module_utils.basic.AnsibleModule.run_command", return_value=(0, "", "")
36 )
37 with pytest.raises(SystemExit):
38 iptables.main()
39
40 assert run_command.call_count == 1
41 first_cmd_args_list = run_command.call_args[0][0]
42 assert first_cmd_args_list[0], IPTABLES_CMD
43 assert first_cmd_args_list[1], "-t"
44 assert first_cmd_args_list[2], "filter"
45 assert first_cmd_args_list[3], "-F"
46
47
48 @pytest.mark.usefixtures('_mock_basic_commands')
49 def test_flush_table_check_true(mocker, set_module_args):
50 """Test flush without parameters and check == true."""
51 set_module_args(
52 {
53 "flush": True,
54 "_ansible_check_mode": True,
55 }
56 )
57 run_command = mocker.patch(
58 "ansible.module_utils.basic.AnsibleModule.run_command", return_value=(0, "", "")
59 )
60 with pytest.raises(SystemExit):
61 iptables.main()
62
63 assert run_command.call_count == 0
64
65
66 @pytest.mark.usefixtures('_mock_basic_commands')
67 def test_policy_table(mocker, set_module_args):
68 """Test change policy of a chain."""
69 set_module_args(
70 {
71 "policy": "ACCEPT",
72 "chain": "INPUT",
73 }
74 )
75 commands_results = [(0, "Chain INPUT (policy DROP)\n", ""), (0, "", "")]
76 run_command = mocker.patch(
77 "ansible.module_utils.basic.AnsibleModule.run_command",
78 side_effect=commands_results,
79 )
80 with pytest.raises(SystemExit):
81 iptables.main()
82
83 assert run_command.call_count == 2
84 first_cmd_args_list = run_command.call_args_list[0]
85 second_cmd_args_list = run_command.call_args_list[1]
86 assert first_cmd_args_list[0][0] == CONST_INPUT_FILTER
87 assert second_cmd_args_list[0][0] == [
88 IPTABLES_CMD,
89 "-t",
90 "filter",
91 "-P",
92 "INPUT",
93 "ACCEPT",
94 ]
95
96
97 @pytest.mark.usefixtures('_mock_basic_commands')
98 @pytest.mark.parametrize(
99 ("test_input", "commands_results"),
100 [
101 pytest.param(
102 {
103 "policy": "ACCEPT",
104 "chain": "INPUT",
105 "_ansible_check_mode": True,
106 },
107 [
108 (0, "Chain INPUT (policy DROP)\n", ""),
109 ],
110 id="policy-table-no-change",
111 ),
112 pytest.param(
113 {
114 "policy": "ACCEPT",
115 "chain": "INPUT",
116 },
117 [
118 (0, "Chain INPUT (policy ACCEPT)\n", ""),
119 (0, "", ""),
120 ],
121 id="policy-table-change-false",
122 )
123 ]
124 )
125 def test_policy_table_flush(mocker, test_input, commands_results, set_module_args):
126 """Test flush without parameters and change == false."""
127 set_module_args(test_input)
128 run_command = mocker.patch(
129 "ansible.module_utils.basic.AnsibleModule.run_command",
130 side_effect=commands_results,
131 )
132 with pytest.raises(SystemExit):
133 iptables.main()
134
135 assert run_command.call_count == 1
136 first_cmd_args_list = run_command.call_args_list[0]
137 assert first_cmd_args_list[0][0] == CONST_INPUT_FILTER
138
139
140 # TODO ADD test policy without chain fail
141 # TODO ADD test policy with chain don't exists
142 # TODO ADD test policy with wrong choice fail
143
144 @pytest.mark.usefixtures('_mock_basic_commands')
145 def test_insert_rule_change_false(mocker, set_module_args):
146 """Test flush without parameters."""
147 set_module_args(
148 {
149 "chain": "OUTPUT",
150 "source": "1.2.3.4/32",
151 "destination": "7.8.9.10/42",
152 "jump": "ACCEPT",
153 "action": "insert",
154 "_ansible_check_mode": True,
155 }
156 )
157 commands_results = [
158 (1, "", ""), # check_rule_present
159 (0, "", ""), # check_chain_present
160 ]
161 run_command = mocker.patch(
162 "ansible.module_utils.basic.AnsibleModule.run_command",
163 side_effect=commands_results,
164 )
165
166 with pytest.raises(SystemExit):
167 iptables.main()
168
169 assert run_command.call_count == 1
170 first_cmd_args_list = run_command.call_args_list[0]
171 assert first_cmd_args_list[0][0] == [
172 IPTABLES_CMD,
173 "-t",
174 "filter",
175 "-C",
176 "OUTPUT",
177 "-s",
178 "1.2.3.4/32",
179 "-d",
180 "7.8.9.10/42",
181 "-j",
182 "ACCEPT",
183 ]
184
185
186 @pytest.mark.usefixtures('_mock_basic_commands')
187 def test_insert_rule(mocker, set_module_args):
188 """Test flush without parameters."""
189 set_module_args(
190 {
191 "chain": "OUTPUT",
192 "source": "1.2.3.4/32",
193 "destination": "7.8.9.10/42",
194 "jump": "ACCEPT",
195 "action": "insert",
196 }
197 )
198 commands_results = [
199 (1, "", ""), # check_rule_present
200 (0, "", ""),
201 ]
202 run_command = mocker.patch(
203 "ansible.module_utils.basic.AnsibleModule.run_command",
204 side_effect=commands_results,
205 )
206
207 with pytest.raises(SystemExit):
208 iptables.main()
209
210 assert run_command.call_count == 2
211 first_cmd_args_list = run_command.call_args_list[0]
212 second_cmd_args_list = run_command.call_args_list[1]
213 assert first_cmd_args_list[0][0] == [
214 IPTABLES_CMD,
215 "-t",
216 "filter",
217 "-C",
218 "OUTPUT",
219 "-s",
220 "1.2.3.4/32",
221 "-d",
222 "7.8.9.10/42",
223 "-j",
224 "ACCEPT",
225 ]
226 assert second_cmd_args_list[0][0] == [
227 IPTABLES_CMD,
228 "-t",
229 "filter",
230 "-I",
231 "OUTPUT",
232 "-s",
233 "1.2.3.4/32",
234 "-d",
235 "7.8.9.10/42",
236 "-j",
237 "ACCEPT",
238 ]
239
240
241 @pytest.mark.usefixtures('_mock_basic_commands')
242 def test_append_rule_check_mode(mocker, set_module_args):
243 """Test append a redirection rule in check mode."""
244 set_module_args(
245 {
246 "chain": "PREROUTING",
247 "source": "1.2.3.4/32",
248 "destination": "7.8.9.10/42",
249 "jump": "REDIRECT",
250 "table": "nat",
251 "to_destination": "5.5.5.5/32",
252 "protocol": "udp",
253 "destination_port": "22",
254 "to_ports": "8600",
255 "_ansible_check_mode": True,
256 }
257 )
258
259 commands_results = [
260 (1, "", ""), # check_rule_present
261 (0, "", ""), # check_chain_present
262 ]
263 run_command = mocker.patch(
264 "ansible.module_utils.basic.AnsibleModule.run_command",
265 side_effect=commands_results,
266 )
267
268 with pytest.raises(SystemExit):
269 iptables.main()
270
271 assert run_command.call_count == 1
272 first_cmd_args_list = run_command.call_args_list[0]
273 assert first_cmd_args_list[0][0] == [
274 IPTABLES_CMD,
275 "-t",
276 "nat",
277 "-C",
278 "PREROUTING",
279 "-p",
280 "udp",
281 "-s",
282 "1.2.3.4/32",
283 "-d",
284 "7.8.9.10/42",
285 "-j",
286 "REDIRECT",
287 "--to-destination",
288 "5.5.5.5/32",
289 "--destination-port",
290 "22",
291 "--to-ports",
292 "8600",
293 ]
294
295
296 @pytest.mark.usefixtures('_mock_basic_commands')
297 def test_append_rule(mocker, set_module_args):
298 """Test append a redirection rule."""
299 set_module_args(
300 {
301 "chain": "PREROUTING",
302 "source": "1.2.3.4/32",
303 "destination": "7.8.9.10/42",
304 "jump": "REDIRECT",
305 "table": "nat",
306 "to_destination": "5.5.5.5/32",
307 "protocol": "udp",
308 "destination_port": "22",
309 "to_ports": "8600",
310 }
311 )
312
313 commands_results = [
314 (1, "", ""), # check_rule_present
315 (0, "", ""), # check_chain_present
316 (0, "", ""),
317 ]
318 run_command = mocker.patch(
319 "ansible.module_utils.basic.AnsibleModule.run_command",
320 side_effect=commands_results,
321 )
322
323 with pytest.raises(SystemExit):
324 iptables.main()
325
326 assert run_command.call_count == 2
327 first_cmd_args_list = run_command.call_args_list[0]
328 second_cmd_args_list = run_command.call_args_list[1]
329 assert first_cmd_args_list[0][0] == [
330 IPTABLES_CMD,
331 "-t",
332 "nat",
333 "-C",
334 "PREROUTING",
335 "-p",
336 "udp",
337 "-s",
338 "1.2.3.4/32",
339 "-d",
340 "7.8.9.10/42",
341 "-j",
342 "REDIRECT",
343 "--to-destination",
344 "5.5.5.5/32",
345 "--destination-port",
346 "22",
347 "--to-ports",
348 "8600",
349 ]
350 assert second_cmd_args_list[0][0] == [
351 IPTABLES_CMD,
352 "-t",
353 "nat",
354 "-A",
355 "PREROUTING",
356 "-p",
357 "udp",
358 "-s",
359 "1.2.3.4/32",
360 "-d",
361 "7.8.9.10/42",
362 "-j",
363 "REDIRECT",
364 "--to-destination",
365 "5.5.5.5/32",
366 "--destination-port",
367 "22",
368 "--to-ports",
369 "8600",
370 ]
371
372
373 @pytest.mark.usefixtures('_mock_basic_commands')
374 def test_remove_rule(mocker, set_module_args):
375 """Test flush without parameters."""
376 set_module_args(
377 {
378 "chain": "PREROUTING",
379 "source": "1.2.3.4/32",
380 "destination": "7.8.9.10/42",
381 "jump": "SNAT",
382 "table": "nat",
383 "to_source": "5.5.5.5/32",
384 "protocol": "udp",
385 "source_port": "22",
386 "to_ports": "8600",
387 "state": "absent",
388 "in_interface": "eth0",
389 "out_interface": "eth1",
390 "comment": "this is a comment",
391 }
392 )
393
394 commands_results = [
395 (0, "", ""),
396 (0, "", ""),
397 ]
398 run_command = mocker.patch(
399 "ansible.module_utils.basic.AnsibleModule.run_command",
400 side_effect=commands_results,
401 )
402
403 with pytest.raises(SystemExit):
404 iptables.main()
405
406 assert run_command.call_count == 2
407 first_cmd_args_list = run_command.call_args_list[0]
408 second_cmd_args_list = run_command.call_args_list[1]
409 assert first_cmd_args_list[0][0] == [
410 IPTABLES_CMD,
411 "-t",
412 "nat",
413 "-C",
414 "PREROUTING",
415 "-p",
416 "udp",
417 "-s",
418 "1.2.3.4/32",
419 "-d",
420 "7.8.9.10/42",
421 "-j",
422 "SNAT",
423 "--to-source",
424 "5.5.5.5/32",
425 "-i",
426 "eth0",
427 "-o",
428 "eth1",
429 "--source-port",
430 "22",
431 "--to-ports",
432 "8600",
433 "-m",
434 "comment",
435 "--comment",
436 "this is a comment",
437 ]
438 assert second_cmd_args_list[0][0] == [
439 IPTABLES_CMD,
440 "-t",
441 "nat",
442 "-D",
443 "PREROUTING",
444 "-p",
445 "udp",
446 "-s",
447 "1.2.3.4/32",
448 "-d",
449 "7.8.9.10/42",
450 "-j",
451 "SNAT",
452 "--to-source",
453 "5.5.5.5/32",
454 "-i",
455 "eth0",
456 "-o",
457 "eth1",
458 "--source-port",
459 "22",
460 "--to-ports",
461 "8600",
462 "-m",
463 "comment",
464 "--comment",
465 "this is a comment",
466 ]
467
468
469 @pytest.mark.usefixtures('_mock_basic_commands')
470 def test_remove_rule_check_mode(mocker, set_module_args):
471 """Test flush without parameters check mode."""
472 set_module_args(
473 {
474 "chain": "PREROUTING",
475 "source": "1.2.3.4/32",
476 "destination": "7.8.9.10/42",
477 "jump": "SNAT",
478 "table": "nat",
479 "to_source": "5.5.5.5/32",
480 "protocol": "udp",
481 "source_port": "22",
482 "to_ports": "8600",
483 "state": "absent",
484 "in_interface": "eth0",
485 "out_interface": "eth1",
486 "comment": "this is a comment",
487 "_ansible_check_mode": True,
488 }
489 )
490
491 commands_results = [
492 (0, "", ""),
493 ]
494
495 run_command = mocker.patch(
496 "ansible.module_utils.basic.AnsibleModule.run_command",
497 side_effect=commands_results,
498 )
499
500 with pytest.raises(SystemExit):
501 iptables.main()
502
503 assert run_command.call_count == 1
504 first_cmd_args_list = run_command.call_args_list[0]
505 assert first_cmd_args_list[0][0] == [
506 IPTABLES_CMD,
507 "-t",
508 "nat",
509 "-C",
510 "PREROUTING",
511 "-p",
512 "udp",
513 "-s",
514 "1.2.3.4/32",
515 "-d",
516 "7.8.9.10/42",
517 "-j",
518 "SNAT",
519 "--to-source",
520 "5.5.5.5/32",
521 "-i",
522 "eth0",
523 "-o",
524 "eth1",
525 "--source-port",
526 "22",
527 "--to-ports",
528 "8600",
529 "-m",
530 "comment",
531 "--comment",
532 "this is a comment",
533 ]
534
535
536 @pytest.mark.usefixtures('_mock_basic_commands')
537 @pytest.mark.parametrize(
538 ("test_input", "expected"),
539 [
540 pytest.param(
541 {
542 "chain": "INPUT",
543 "protocol": "tcp",
544 "reject_with": "tcp-reset",
545 "ip_version": "ipv4",
546 },
547 [
548 IPTABLES_CMD,
549 "-t",
550 "filter",
551 "-C",
552 "INPUT",
553 "-p",
554 "tcp",
555 "-j",
556 "REJECT",
557 "--reject-with",
558 "tcp-reset",
559 ],
560 id="insert-reject-with",
561 ),
562 pytest.param(
563 {
564 "chain": "INPUT",
565 "protocol": "tcp",
566 "jump": "REJECT",
567 "reject_with": "tcp-reset",
568 "ip_version": "ipv4",
569 },
570 [
571 IPTABLES_CMD,
572 "-t",
573 "filter",
574 "-C",
575 "INPUT",
576 "-p",
577 "tcp",
578 "-j",
579 "REJECT",
580 "--reject-with",
581 "tcp-reset",
582 ],
583 id="update-reject-with",
584 ),
585 ]
586 )
587 def test_insert_with_reject(mocker, test_input, expected, set_module_args):
588 """Using reject_with with a previously defined jump: REJECT results in two Jump statements #18988."""
589 set_module_args(test_input)
590 commands_results = [
591 (0, "", ""),
592 ]
593
594 run_command = mocker.patch(
595 "ansible.module_utils.basic.AnsibleModule.run_command",
596 side_effect=commands_results,
597 )
598
599 with pytest.raises(SystemExit):
600 iptables.main()
601
602 assert run_command.call_count == 1
603 first_cmd_args_list = run_command.call_args_list[0]
604 assert first_cmd_args_list[0][0] == expected
605
606
607 @pytest.mark.usefixtures('_mock_basic_commands')
608 def test_jump_tee_gateway_negative(mocker, set_module_args):
609 """Missing gateway when JUMP is set to TEE."""
610 set_module_args(
611 {
612 "table": "mangle",
613 "chain": "PREROUTING",
614 "in_interface": "eth0",
615 "protocol": "udp",
616 "match": "state",
617 "jump": "TEE",
618 "ctstate": ["NEW"],
619 "destination_port": "9521",
620 "destination": "127.0.0.1",
621 }
622 )
623 mocker.patch(
624 "ansible.module_utils.basic.AnsibleModule.fail_json",
625 side_effect=fail_json,
626 )
627 jump_err_msg = "jump is TEE but all of the following are missing: gateway"
628 with pytest.raises(AnsibleFailJson, match=jump_err_msg) as exc:
629 iptables.main()
630 assert exc.value.args[0]["failed"]
631
632
633 @pytest.mark.usefixtures('_mock_basic_commands')
634 def test_jump_tee_gateway(mocker, set_module_args):
635 """Using gateway when JUMP is set to TEE."""
636 set_module_args(
637 {
638 "table": "mangle",
639 "chain": "PREROUTING",
640 "in_interface": "eth0",
641 "protocol": "udp",
642 "match": "state",
643 "jump": "TEE",
644 "ctstate": ["NEW"],
645 "destination_port": "9521",
646 "gateway": "192.168.10.1",
647 "destination": "127.0.0.1",
648 }
649 )
650 commands_results = [
651 (0, "", ""),
652 ]
653 run_command = mocker.patch(
654 "ansible.module_utils.basic.AnsibleModule.run_command",
655 side_effect=commands_results,
656 )
657
658 with pytest.raises(SystemExit):
659 iptables.main()
660
661 assert run_command.call_count == 1
662 first_cmd_args_list = run_command.call_args_list[0]
663 assert first_cmd_args_list[0][0] == [
664 IPTABLES_CMD,
665 "-t",
666 "mangle",
667 "-C",
668 "PREROUTING",
669 "-p",
670 "udp",
671 "-d",
672 "127.0.0.1",
673 "-m",
674 "state",
675 "-j",
676 "TEE",
677 "--gateway",
678 "192.168.10.1",
679 "-i",
680 "eth0",
681 "--destination-port",
682 "9521",
683 "--state",
684 "NEW",
685 ]
686
687
688 @pytest.mark.usefixtures('_mock_basic_commands')
689 @pytest.mark.parametrize(
690 ("test_input"),
691 [
692 pytest.param(
693 'flags=ALL flags_set="ACK,RST,SYN,FIN"',
694 id="tcp-flags-str"
695 ),
696 pytest.param(
697 {
698 "flags": "ALL", "flags_set": "ACK,RST,SYN,FIN"
699 },
700 id="tcp-flags-dict"
701 ),
702 pytest.param(
703 {
704 "flags": ["ALL"], "flags_set": ["ACK", "RST", "SYN", "FIN"]
705 },
706 id="tcp-flags-list"
707 ),
708 ],
709 )
710 def test_tcp_flags(mocker, test_input, set_module_args):
711 """Test various ways of inputting tcp_flags."""
712 rule_data = {
713 "chain": "OUTPUT",
714 "protocol": "tcp",
715 "jump": "DROP",
716 "tcp_flags": test_input,
717 }
718
719 set_module_args(rule_data)
720
721 commands_results = [
722 (0, "", ""),
723 ]
724
725 run_command = mocker.patch(
726 "ansible.module_utils.basic.AnsibleModule.run_command",
727 side_effect=commands_results,
728 )
729
730 with pytest.raises(SystemExit):
731 iptables.main()
732
733 assert run_command.call_count == 1
734 first_cmd_args_list = run_command.call_args_list[0]
735 assert first_cmd_args_list[0][0] == [
736 IPTABLES_CMD,
737 "-t",
738 "filter",
739 "-C",
740 "OUTPUT",
741 "-p",
742 "tcp",
743 "--tcp-flags",
744 "ALL",
745 "ACK,RST,SYN,FIN",
746 "-j",
747 "DROP",
748 ]
749
750
751 @pytest.mark.usefixtures('_mock_basic_commands')
752 @pytest.mark.parametrize(
753 "log_level",
754 [
755 "0",
756 "1",
757 "2",
758 "3",
759 "4",
760 "5",
761 "6",
762 "7",
763 "emerg",
764 "alert",
765 "crit",
766 "error",
767 "warning",
768 "notice",
769 "info",
770 "debug",
771 ],
772 )
773 def test_log_level(mocker, log_level, set_module_args):
774 """Test various ways of log level flag."""
775
776 set_module_args(
777 {
778 "chain": "INPUT",
779 "jump": "LOG",
780 "log_level": log_level,
781 "source": "1.2.3.4/32",
782 "log_prefix": "** DROP-this_ip **",
783 }
784 )
785 commands_results = [
786 (0, "", ""),
787 ]
788
789 run_command = mocker.patch(
790 "ansible.module_utils.basic.AnsibleModule.run_command",
791 side_effect=commands_results,
792 )
793
794 with pytest.raises(SystemExit):
795 iptables.main()
796
797 assert run_command.call_count == 1
798 first_cmd_args_list = run_command.call_args_list[0]
799 assert first_cmd_args_list[0][0] == [
800 IPTABLES_CMD,
801 "-t",
802 "filter",
803 "-C",
804 "INPUT",
805 "-s",
806 "1.2.3.4/32",
807 "-j",
808 "LOG",
809 "--log-prefix",
810 "** DROP-this_ip **",
811 "--log-level",
812 log_level,
813 ]
814
815
816 @pytest.mark.usefixtures('_mock_basic_commands')
817 @pytest.mark.parametrize(
818 ("test_input", "expected"),
819 [
820 pytest.param(
821 {
822 "chain": "INPUT",
823 "match": ["iprange"],
824 "src_range": "192.168.1.100-192.168.1.199",
825 "jump": "ACCEPT",
826 },
827 [
828 IPTABLES_CMD,
829 "-t",
830 "filter",
831 "-C",
832 "INPUT",
833 "-m",
834 "iprange",
835 "-j",
836 "ACCEPT",
837 "--src-range",
838 "192.168.1.100-192.168.1.199",
839 ],
840 id="src-range",
841 ),
842 pytest.param(
843 {
844 "chain": "INPUT",
845 "src_range": "192.168.1.100-192.168.1.199",
846 "dst_range": "10.0.0.50-10.0.0.100",
847 "jump": "ACCEPT",
848 },
849 [
850 IPTABLES_CMD,
851 "-t",
852 "filter",
853 "-C",
854 "INPUT",
855 "-j",
856 "ACCEPT",
857 "-m",
858 "iprange",
859 "--src-range",
860 "192.168.1.100-192.168.1.199",
861 "--dst-range",
862 "10.0.0.50-10.0.0.100",
863 ],
864 id="src-range-dst-range",
865 ),
866 pytest.param(
867 {
868 "chain": "INPUT",
869 "dst_range": "10.0.0.50-10.0.0.100",
870 "jump": "ACCEPT"
871 },
872 [
873 IPTABLES_CMD,
874 "-t",
875 "filter",
876 "-C",
877 "INPUT",
878 "-j",
879 "ACCEPT",
880 "-m",
881 "iprange",
882 "--dst-range",
883 "10.0.0.50-10.0.0.100",
884 ],
885 id="dst-range"
886 ),
887 ],
888 )
889 def test_iprange(mocker, test_input, expected, set_module_args):
890 """Test iprange module with its flags src_range and dst_range."""
891 set_module_args(test_input)
892
893 commands_results = [
894 (0, "", ""),
895 ]
896
897 run_command = mocker.patch(
898 "ansible.module_utils.basic.AnsibleModule.run_command",
899 side_effect=commands_results,
900 )
901
902 with pytest.raises(SystemExit):
903 iptables.main()
904
905 assert run_command.call_count == 1
906 first_cmd_args_list = run_command.call_args_list[0]
907 assert first_cmd_args_list[0][0] == expected
908
909
910 @pytest.mark.usefixtures('_mock_basic_commands')
911 def test_insert_rule_with_wait(mocker, set_module_args):
912 """Test flush without parameters."""
913 set_module_args(
914 {
915 "chain": "OUTPUT",
916 "source": "1.2.3.4/32",
917 "destination": "7.8.9.10/42",
918 "jump": "ACCEPT",
919 "action": "insert",
920 "wait": "10",
921 }
922 )
923
924 commands_results = [
925 (0, "", ""),
926 ]
927
928 run_command = mocker.patch(
929 "ansible.module_utils.basic.AnsibleModule.run_command",
930 side_effect=commands_results,
931 )
932
933 with pytest.raises(SystemExit):
934 iptables.main()
935
936 assert run_command.call_count == 1
937 first_cmd_args_list = run_command.call_args_list[0]
938 assert first_cmd_args_list[0][0] == [
939 IPTABLES_CMD,
940 "-t",
941 "filter",
942 "-C",
943 "OUTPUT",
944 "-w",
945 "10",
946 "-s",
947 "1.2.3.4/32",
948 "-d",
949 "7.8.9.10/42",
950 "-j",
951 "ACCEPT",
952 ]
953
954
955 @pytest.mark.usefixtures('_mock_basic_commands')
956 def test_comment_position_at_end(mocker, set_module_args):
957 """Test comment position to make sure it is at the end of command."""
958 set_module_args(
959 {
960 "chain": "INPUT",
961 "jump": "ACCEPT",
962 "action": "insert",
963 "ctstate": ["NEW"],
964 "comment": "this is a comment",
965 "_ansible_check_mode": True,
966 }
967 )
968
969 commands_results = [
970 (0, "", ""),
971 ]
972
973 run_command = mocker.patch(
974 "ansible.module_utils.basic.AnsibleModule.run_command",
975 side_effect=commands_results,
976 )
977
978 with pytest.raises(SystemExit):
979 iptables.main()
980
981 assert run_command.call_count == 1
982 first_cmd_args_list = run_command.call_args_list[0]
983 assert first_cmd_args_list[0][0] == [
984 IPTABLES_CMD,
985 "-t",
986 "filter",
987 "-C",
988 "INPUT",
989 "-j",
990 "ACCEPT",
991 "-m",
992 "conntrack",
993 "--ctstate",
994 "NEW",
995 "-m",
996 "comment",
997 "--comment",
998 "this is a comment",
999 ]
1000 assert run_command.call_args[0][0][14] == "this is a comment"
1001
1002
1003 @pytest.mark.usefixtures('_mock_basic_commands')
1004 def test_destination_ports(mocker, set_module_args):
1005 """Test multiport module usage with multiple ports."""
1006 set_module_args(
1007 {
1008 "chain": "INPUT",
1009 "protocol": "tcp",
1010 "in_interface": "eth0",
1011 "source": "192.168.0.1/32",
1012 "destination_ports": ["80", "443", "8081:8085"],
1013 "jump": "ACCEPT",
1014 "comment": "this is a comment",
1015 }
1016 )
1017 commands_results = [
1018 (0, "", ""),
1019 ]
1020 run_command = mocker.patch(
1021 "ansible.module_utils.basic.AnsibleModule.run_command",
1022 side_effect=commands_results,
1023 )
1024
1025 with pytest.raises(SystemExit):
1026 iptables.main()
1027
1028 assert run_command.call_count == 1
1029 first_cmd_args_list = run_command.call_args_list[0]
1030 assert first_cmd_args_list[0][0] == [
1031 IPTABLES_CMD,
1032 "-t",
1033 "filter",
1034 "-C",
1035 "INPUT",
1036 "-p",
1037 "tcp",
1038 "-s",
1039 "192.168.0.1/32",
1040 "-j",
1041 "ACCEPT",
1042 "-m",
1043 "multiport",
1044 "--dports",
1045 "80,443,8081:8085",
1046 "-i",
1047 "eth0",
1048 "-m",
1049 "comment",
1050 "--comment",
1051 "this is a comment",
1052 ]
1053
1054
1055 @pytest.mark.usefixtures('_mock_basic_commands')
1056 @pytest.mark.parametrize(
1057 ("test_input", "expected"),
1058 [
1059 pytest.param(
1060 {
1061 "chain": "INPUT",
1062 "protocol": "tcp",
1063 "match_set": "admin_hosts",
1064 "match_set_flags": "src",
1065 "destination_port": "22",
1066 "jump": "ACCEPT",
1067 "comment": "this is a comment",
1068 },
1069 [
1070 IPTABLES_CMD,
1071 "-t",
1072 "filter",
1073 "-C",
1074 "INPUT",
1075 "-p",
1076 "tcp",
1077 "-j",
1078 "ACCEPT",
1079 "--destination-port",
1080 "22",
1081 "-m",
1082 "set",
1083 "--match-set",
1084 "admin_hosts",
1085 "src",
1086 "-m",
1087 "comment",
1088 "--comment",
1089 "this is a comment",
1090 ],
1091 id="match-set-src",
1092 ),
1093 pytest.param(
1094 {
1095 "chain": "INPUT",
1096 "protocol": "udp",
1097 "match_set": "banned_hosts",
1098 "match_set_flags": "src,dst",
1099 "jump": "REJECT",
1100 },
1101 [
1102 IPTABLES_CMD,
1103 "-t",
1104 "filter",
1105 "-C",
1106 "INPUT",
1107 "-p",
1108 "udp",
1109 "-j",
1110 "REJECT",
1111 "-m",
1112 "set",
1113 "--match-set",
1114 "banned_hosts",
1115 "src,dst",
1116 ],
1117 id="match-set-src-dst",
1118 ),
1119 pytest.param(
1120 {
1121 "chain": "INPUT",
1122 "protocol": "udp",
1123 "match_set": "banned_hosts_dst",
1124 "match_set_flags": "dst,dst",
1125 "jump": "REJECT",
1126 },
1127 [
1128 IPTABLES_CMD,
1129 "-t",
1130 "filter",
1131 "-C",
1132 "INPUT",
1133 "-p",
1134 "udp",
1135 "-j",
1136 "REJECT",
1137 "-m",
1138 "set",
1139 "--match-set",
1140 "banned_hosts_dst",
1141 "dst,dst",
1142 ],
1143 id="match-set-dst-dst",
1144 ),
1145 pytest.param(
1146 {
1147 "chain": "INPUT",
1148 "protocol": "udp",
1149 "match_set": "banned_hosts",
1150 "match_set_flags": "src,src",
1151 "jump": "REJECT",
1152 },
1153 [
1154 IPTABLES_CMD,
1155 "-t",
1156 "filter",
1157 "-C",
1158 "INPUT",
1159 "-p",
1160 "udp",
1161 "-j",
1162 "REJECT",
1163 "-m",
1164 "set",
1165 "--match-set",
1166 "banned_hosts",
1167 "src,src",
1168 ],
1169 id="match-set-src-src",
1170 ),
1171 ],
1172 )
1173 def test_match_set(mocker, test_input, expected, set_module_args):
1174 """Test match_set together with match_set_flags."""
1175 set_module_args(test_input)
1176 commands_results = [
1177 (0, "", ""),
1178 ]
1179 run_command = mocker.patch(
1180 "ansible.module_utils.basic.AnsibleModule.run_command",
1181 side_effect=commands_results,
1182 )
1183
1184 with pytest.raises(SystemExit):
1185 iptables.main()
1186
1187 assert run_command.call_count == 1
1188 first_cmd_args_list = run_command.call_args_list[0]
1189 assert first_cmd_args_list[0][0] == expected
1190
1191
1192 @pytest.mark.usefixtures('_mock_basic_commands')
1193 def test_chain_creation(mocker, set_module_args):
1194 """Test chain creation when absent."""
1195 set_module_args(
1196 {
1197 "chain": "FOOBAR",
1198 "state": "present",
1199 "chain_management": True,
1200 "wait": 10,
1201 }
1202 )
1203
1204 commands_results = [
1205 (1, "", ""), # check_chain_present
1206 (0, "", ""), # create_chain
1207 ]
1208
1209 run_command = mocker.patch(
1210 "ansible.module_utils.basic.AnsibleModule.run_command",
1211 side_effect=commands_results,
1212 )
1213
1214 mocker.patch(
1215 "ansible.module_utils.basic.AnsibleModule.exit_json",
1216 side_effect=exit_json,
1217 )
1218 with pytest.raises(AnsibleExitJson):
1219 iptables.main()
1220
1221 assert run_command.call_count == 2
1222 first_cmd_args_list = run_command.call_args_list[0]
1223 assert first_cmd_args_list[0][0] == [
1224 IPTABLES_CMD,
1225 "-t",
1226 "filter",
1227 "-L",
1228 "FOOBAR",
1229 "-w",
1230 "10",
1231 ]
1232
1233 second_cmd_args_list = run_command.call_args_list[1]
1234 assert second_cmd_args_list[0][0] == [
1235 IPTABLES_CMD,
1236 "-t",
1237 "filter",
1238 "-N",
1239 "FOOBAR",
1240 "-w",
1241 "10",
1242 ]
1243
1244 commands_results = [
1245 (0, "", ""), # check_rule_present
1246 ]
1247
1248 run_command = mocker.patch(
1249 "ansible.module_utils.basic.AnsibleModule.run_command",
1250 side_effect=commands_results,
1251 )
1252
1253 with pytest.raises(AnsibleExitJson) as exc:
1254 iptables.main()
1255 assert not exc.value.args[0]["changed"]
1256
1257
1258 @pytest.mark.usefixtures('_mock_basic_commands')
1259 def test_chain_creation_check_mode(mocker, set_module_args):
1260 """Test chain creation when absent in check mode."""
1261 set_module_args(
1262 {
1263 "chain": "FOOBAR",
1264 "state": "present",
1265 "chain_management": True,
1266 "wait": 10,
1267 "_ansible_check_mode": True,
1268 }
1269 )
1270
1271 commands_results = [
1272 (1, "", ""), # check_rule_present
1273 ]
1274
1275 run_command = mocker.patch(
1276 "ansible.module_utils.basic.AnsibleModule.run_command",
1277 side_effect=commands_results,
1278 )
1279
1280 mocker.patch(
1281 "ansible.module_utils.basic.AnsibleModule.exit_json",
1282 side_effect=exit_json,
1283 )
1284 with pytest.raises(AnsibleExitJson):
1285 iptables.main()
1286
1287 assert run_command.call_count == 1
1288 first_cmd_args_list = run_command.call_args_list[0]
1289 assert first_cmd_args_list[0][0] == [
1290 IPTABLES_CMD,
1291 "-t",
1292 "filter",
1293 "-L",
1294 "FOOBAR",
1295 "-w",
1296 "10",
1297 ]
1298
1299 commands_results = [
1300 (0, "", ""), # check_rule_present
1301 ]
1302 run_command = mocker.patch(
1303 "ansible.module_utils.basic.AnsibleModule.run_command",
1304 side_effect=commands_results,
1305 )
1306 with pytest.raises(AnsibleExitJson) as exc:
1307 iptables.main()
1308
1309 assert not exc.value.args[0]["changed"]
1310
1311
1312 @pytest.mark.usefixtures('_mock_basic_commands')
1313 def test_chain_deletion(mocker, set_module_args):
1314 """Test chain deletion when present."""
1315 set_module_args(
1316 {
1317 "chain": "FOOBAR",
1318 "state": "absent",
1319 "chain_management": True,
1320 }
1321 )
1322
1323 commands_results = [
1324 (0, "", ""), # check_chain_present
1325 (0, "", ""), # delete_chain
1326 ]
1327
1328 run_command = mocker.patch(
1329 "ansible.module_utils.basic.AnsibleModule.run_command",
1330 side_effect=commands_results,
1331 )
1332
1333 mocker.patch(
1334 "ansible.module_utils.basic.AnsibleModule.exit_json",
1335 side_effect=exit_json,
1336 )
1337 with pytest.raises(AnsibleExitJson) as exc:
1338 iptables.main()
1339
1340 assert exc.value.args[0]["changed"]
1341 assert run_command.call_count == 2
1342 first_cmd_args_list = run_command.call_args_list[0]
1343 assert first_cmd_args_list[0][0] == [
1344 IPTABLES_CMD,
1345 "-t",
1346 "filter",
1347 "-L",
1348 "FOOBAR",
1349 ]
1350 second_cmd_args_list = run_command.call_args_list[1]
1351 assert second_cmd_args_list[0][0] == [
1352 IPTABLES_CMD,
1353 "-t",
1354 "filter",
1355 "-X",
1356 "FOOBAR",
1357 ]
1358
1359 commands_results = [
1360 (1, "", ""), # check_rule_present
1361 ]
1362
1363 run_command = mocker.patch(
1364 "ansible.module_utils.basic.AnsibleModule.run_command",
1365 side_effect=commands_results,
1366 )
1367
1368 with pytest.raises(AnsibleExitJson) as exc:
1369 iptables.main()
1370
1371 assert not exc.value.args[0]["changed"]
1372
1373
1374 @pytest.mark.usefixtures('_mock_basic_commands')
1375 def test_chain_deletion_check_mode(mocker, set_module_args):
1376 """Test chain deletion when present in check mode."""
1377 set_module_args(
1378 {
1379 "chain": "FOOBAR",
1380 "state": "absent",
1381 "chain_management": True,
1382 "_ansible_check_mode": True,
1383 }
1384 )
1385
1386 commands_results = [
1387 (0, "", ""), # check_chain_present
1388 ]
1389
1390 run_command = mocker.patch(
1391 "ansible.module_utils.basic.AnsibleModule.run_command",
1392 side_effect=commands_results,
1393 )
1394
1395 mocker.patch(
1396 "ansible.module_utils.basic.AnsibleModule.exit_json",
1397 side_effect=exit_json,
1398 )
1399 with pytest.raises(AnsibleExitJson) as exc:
1400 iptables.main()
1401
1402 assert exc.value.args[0]["changed"]
1403 assert run_command.call_count == 1
1404 first_cmd_args_list = run_command.call_args_list[0]
1405 assert first_cmd_args_list[0][0] == [
1406 IPTABLES_CMD,
1407 "-t",
1408 "filter",
1409 "-L",
1410 "FOOBAR",
1411 ]
1412
1413 commands_results = [
1414 (1, "", ""), # check_rule_present
1415 ]
1416
1417 run_command = mocker.patch(
1418 "ansible.module_utils.basic.AnsibleModule.run_command",
1419 side_effect=commands_results,
1420 )
1421
1422 with pytest.raises(AnsibleExitJson) as exc:
1423 iptables.main()
1424
1425 assert not exc.value.args[0]["changed"]
1426
1427
1428 @pytest.mark.parametrize("ip_version", ["both"])
1429 def test_ip_version(mocker, ip_version, set_module_args):
1430 """Test 'both' option for ip_version."""
1431
1432 set_module_args(
1433 {
1434 "chain": "INPUT",
1435 "protocol": "tcp",
1436 "destination_port": "80",
1437 "jump": "ACCEPT",
1438 "ip_version": ip_version,
1439 }
1440 )
1441
1442 commands_results = [
1443 (0, "", ""),
1444 (0, "", ""),
1445 ]
1446
1447 def get_bin_path_side_effect(arg, *args, **kwargs):
1448 if arg == "iptables":
1449 return IPTABLES_CMD
1450 elif arg == "ip6tables":
1451 return IP6TABLES_CMD
1452 return None
1453
1454 mocker.patch(
1455 "ansible.module_utils.basic.AnsibleModule.get_bin_path",
1456 side_effect=get_bin_path_side_effect,
1457 )
1458
1459 mocker.patch("ansible.modules.iptables.get_iptables_version", return_value=IPTABLES_VERSION)
1460
1461 run_command = mocker.patch(
1462 "ansible.module_utils.basic.AnsibleModule.run_command",
1463 side_effect=commands_results,
1464 )
1465
1466 with pytest.raises(SystemExit):
1467 iptables.main()
1468
1469 assert run_command.call_count == 2
1470
1471 first_cmd_args = run_command.call_args_list[0][0][0]
1472 assert first_cmd_args == [
1473 IPTABLES_CMD,
1474 "-t", "filter",
1475 "-C", "INPUT",
1476 "-p", "tcp",
1477 "-j", "ACCEPT",
1478 "--destination-port", "80",
1479 ]
1480
1481 second_cmd_args = run_command.call_args_list[1][0][0]
1482 assert second_cmd_args == [
1483 IP6TABLES_CMD,
1484 "-t", "filter",
1485 "-C", "INPUT",
1486 "-p", "tcp",
1487 "-j", "ACCEPT",
1488 "--destination-port", "80",
1489 ]