Debian Codegraph @

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

raw
1 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
2 # SPDX-License-Identifier: GPL-3.0-or-later
3
4 from __future__ import annotations
5
6 import unittest
7 from unittest.mock import patch
8
9 from ansible.module_utils import basic
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 AnsibleExitJson,
12 AnsibleFailJson,
13 exit_json,
14 fail_json,
15 set_module_args,
16 )
17
18 import ansible_collections.community.general.plugins.modules.ufw as module
19
20 # mock ufw messages
21
22 ufw_version_35 = """ufw 0.35\nCopyright 2008-2015 Canonical Ltd.\n"""
23
24 ufw_verbose_header = """Status: active
25 Logging: on (low)
26 Default: deny (incoming), allow (outgoing), deny (routed)
27 New profiles: skip
28
29 To Action From
30 -- ------ ----"""
31
32
33 ufw_status_verbose_with_port_7000 = (
34 f"{ufw_verbose_header}\n7000/tcp ALLOW IN Anywhere\n"
35 "7000/tcp (v6) ALLOW IN Anywhere (v6)\n"
36 )
37
38 user_rules_with_port_7000 = """### tuple ### allow tcp 7000 0.0.0.0/0 any 0.0.0.0/0 in
39 ### tuple ### allow tcp 7000 ::/0 any ::/0 in
40 """
41
42 user_rules_with_ipv6 = """### tuple ### allow udp 5353 0.0.0.0/0 any 224.0.0.251 in
43 ### tuple ### allow udp 5353 ::/0 any ff02::fb in
44 """
45
46 ufw_status_verbose_with_ipv6 = (
47 f"{ufw_verbose_header}\n5353/udp ALLOW IN 224.0.0.251\n"
48 "5353/udp ALLOW IN ff02::fb\n"
49 )
50
51 ufw_status_verbose_nothing = ufw_verbose_header
52
53 skippg_adding_existing_rules = "Skipping adding existing rule\nSkipping adding existing rule (v6)\n"
54
55 grep_config_cli = (
56 "grep -h '^### tuple' /lib/ufw/user.rules /lib/ufw/user6.rules /etc/ufw/user.rules /etc/ufw/user6.rules "
57 )
58 grep_config_cli += "/var/lib/ufw/user.rules /var/lib/ufw/user6.rules"
59
60 dry_mode_cmd_with_port_700 = {
61 "ufw status verbose": ufw_status_verbose_with_port_7000,
62 "ufw --version": ufw_version_35,
63 "ufw --dry-run allow from any to any port 7000 proto tcp": skippg_adding_existing_rules,
64 "ufw --dry-run insert 1 allow from any to any port 7000 proto tcp": skippg_adding_existing_rules,
65 "ufw --dry-run delete allow from any to any port 7000 proto tcp": "",
66 "ufw --dry-run delete allow from any to any port 7001 proto tcp": user_rules_with_port_7000,
67 "ufw --dry-run route allow in on foo out on bar from 1.1.1.1 port 7000 to 8.8.8.8 port 7001 proto tcp": "",
68 "ufw --dry-run allow in on foo from any to any port 7003 proto tcp": "",
69 "ufw --dry-run allow in on foo from 1.1.1.1 port 7002 to 8.8.8.8 port 7003 proto tcp": "",
70 "ufw --dry-run allow out on foo from any to any port 7004 proto tcp": "",
71 "ufw --dry-run allow out on foo from 1.1.1.1 port 7003 to 8.8.8.8 port 7004 proto tcp": "",
72 grep_config_cli: user_rules_with_port_7000,
73 }
74
75 # setup configuration :
76 # ufw reset
77 # ufw enable
78 # ufw allow proto udp to any port 5353 from 224.0.0.251
79 # ufw allow proto udp to any port 5353 from ff02::fb
80 dry_mode_cmd_with_ipv6 = {
81 "ufw status verbose": ufw_status_verbose_with_ipv6,
82 "ufw --version": ufw_version_35,
83 # CONTENT of the command sudo ufw --dry-run delete allow in from ff02::fb port 5353 proto udp | grep -E "^### tupple"
84 "ufw --dry-run delete allow from ff02::fb to any port 5353 proto udp": "### tuple ### allow udp any ::/0 5353 ff02::fb in",
85 grep_config_cli: user_rules_with_ipv6,
86 "ufw --dry-run allow from ff02::fb to any port 5353 proto udp": skippg_adding_existing_rules,
87 "ufw --dry-run allow from 224.0.0.252 to any port 5353 proto udp": """### tuple ### allow udp 5353 0.0.0.0/0 any 224.0.0.251 in
88 ### tuple ### allow udp 5353 0.0.0.0/0 any 224.0.0.252 in
89 """,
90 "ufw --dry-run allow from 10.0.0.0/24 to any port 1577 proto udp": "### tuple ### allow udp 1577 0.0.0.0/0 any 10.0.0.0/24 in",
91 }
92
93 dry_mode_cmd_nothing = {
94 "ufw status verbose": ufw_status_verbose_nothing,
95 "ufw --version": ufw_version_35,
96 grep_config_cli: "",
97 "ufw --dry-run allow from any to :: port 23": "### tuple ### allow any 23 :: any ::/0 in",
98 }
99
100
101 def do_nothing_func_nothing(*args, **kwarg):
102 return 0, dry_mode_cmd_nothing[args[0]], ""
103
104
105 def do_nothing_func_ipv6(*args, **kwarg):
106 return 0, dry_mode_cmd_with_ipv6[args[0]], ""
107
108
109 def do_nothing_func_port_7000(*args, **kwarg):
110 return 0, dry_mode_cmd_with_port_700[args[0]], ""
111
112
113 def get_bin_path(self, arg, required=False):
114 """Mock AnsibleModule.get_bin_path"""
115 return arg
116
117
118 class TestUFW(unittest.TestCase):
119 def setUp(self):
120 self.mock_module_helper = patch.multiple(
121 basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json, get_bin_path=get_bin_path
122 )
123 self.mock_module_helper.start()
124 self.addCleanup(self.mock_module_helper.stop)
125
126 def test_filter_line_that_contains_ipv4(self):
127 reg = module.compile_ipv4_regexp()
128
129 self.assertTrue(reg.search("### tuple ### allow udp 5353 ::/0 any ff02::fb in") is None)
130 self.assertTrue(reg.search("### tuple ### allow udp 5353 0.0.0.0/0 any 224.0.0.251 in") is not None)
131
132 self.assertTrue(reg.match("ff02::fb") is None)
133 self.assertTrue(reg.match("224.0.0.251") is not None)
134 self.assertTrue(reg.match("10.0.0.0/8") is not None)
135 self.assertTrue(reg.match("somethingElse") is None)
136 self.assertTrue(reg.match("::") is None)
137 self.assertTrue(reg.match("any") is None)
138
139 def test_filter_line_that_contains_ipv6(self):
140 reg = module.compile_ipv6_regexp()
141 self.assertTrue(reg.search("### tuple ### allow udp 5353 ::/0 any ff02::fb in") is not None)
142 self.assertTrue(reg.search("### tuple ### allow udp 5353 0.0.0.0/0 any 224.0.0.251 in") is None)
143 self.assertTrue(reg.search("### tuple ### allow any 23 :: any ::/0 in") is not None)
144 self.assertTrue(reg.match("ff02::fb") is not None)
145 self.assertTrue(reg.match("224.0.0.251") is None)
146 self.assertTrue(reg.match("::") is not None)
147
148 def test_check_mode_add_rules(self):
149 with set_module_args({"rule": "allow", "proto": "tcp", "port": "7000", "_ansible_check_mode": True}):
150 result = self.__getResult(do_nothing_func_port_7000)
151 self.assertFalse(result.exception.args[0]["changed"])
152
153 def test_check_mode_add_insert_rules(self):
154 with set_module_args(
155 {"insert": "1", "rule": "allow", "proto": "tcp", "port": "7000", "_ansible_check_mode": True}
156 ):
157 result = self.__getResult(do_nothing_func_port_7000)
158 self.assertFalse(result.exception.args[0]["changed"])
159
160 def test_check_mode_add_detailed_route(self):
161 with set_module_args(
162 {
163 "rule": "allow",
164 "route": "yes",
165 "interface_in": "foo",
166 "interface_out": "bar",
167 "proto": "tcp",
168 "from_ip": "1.1.1.1",
169 "to_ip": "8.8.8.8",
170 "from_port": "7000",
171 "to_port": "7001",
172 "_ansible_check_mode": True,
173 }
174 ):
175 result = self.__getResult(do_nothing_func_port_7000)
176 self.assertTrue(result.exception.args[0]["changed"])
177
178 def test_check_mode_add_ambiguous_route(self):
179 with set_module_args(
180 {
181 "rule": "allow",
182 "route": "yes",
183 "interface_in": "foo",
184 "interface_out": "bar",
185 "direction": "in",
186 "interface": "baz",
187 "_ansible_check_mode": True,
188 }
189 ):
190 with self.assertRaises(AnsibleFailJson) as result:
191 self.__getResult(do_nothing_func_port_7000)
192
193 exc = result.exception.args[0]
194 self.assertTrue(exc["failed"])
195 self.assertIn("mutually exclusive", exc["msg"])
196
197 def test_check_mode_add_interface_in(self):
198 with set_module_args(
199 {"rule": "allow", "proto": "tcp", "port": "7003", "interface_in": "foo", "_ansible_check_mode": True}
200 ):
201 result = self.__getResult(do_nothing_func_port_7000)
202 self.assertTrue(result.exception.args[0]["changed"])
203
204 def test_check_mode_add_interface_out(self):
205 with set_module_args(
206 {"rule": "allow", "proto": "tcp", "port": "7004", "interface_out": "foo", "_ansible_check_mode": True}
207 ):
208 result = self.__getResult(do_nothing_func_port_7000)
209 self.assertTrue(result.exception.args[0]["changed"])
210
211 def test_check_mode_add_non_route_interface_both(self):
212 with set_module_args(
213 {
214 "rule": "allow",
215 "proto": "tcp",
216 "port": "7004",
217 "interface_in": "foo",
218 "interface_out": "bar",
219 "_ansible_check_mode": True,
220 }
221 ):
222 with self.assertRaises(AnsibleFailJson) as result:
223 self.__getResult(do_nothing_func_port_7000)
224
225 exc = result.exception.args[0]
226 self.assertTrue(exc["failed"])
227 self.assertIn("combine", exc["msg"])
228
229 def test_check_mode_add_direction_in(self):
230 with set_module_args(
231 {
232 "rule": "allow",
233 "proto": "tcp",
234 "port": "7003",
235 "direction": "in",
236 "interface": "foo",
237 "_ansible_check_mode": True,
238 }
239 ):
240 result = self.__getResult(do_nothing_func_port_7000)
241 self.assertTrue(result.exception.args[0]["changed"])
242
243 def test_check_mode_add_direction_in_with_ip(self):
244 with set_module_args(
245 {
246 "rule": "allow",
247 "proto": "tcp",
248 "from_ip": "1.1.1.1",
249 "from_port": "7002",
250 "to_ip": "8.8.8.8",
251 "to_port": "7003",
252 "direction": "in",
253 "interface": "foo",
254 "_ansible_check_mode": True,
255 }
256 ):
257 result = self.__getResult(do_nothing_func_port_7000)
258 self.assertTrue(result.exception.args[0]["changed"])
259
260 def test_check_mode_add_direction_out(self):
261 with set_module_args(
262 {
263 "rule": "allow",
264 "proto": "tcp",
265 "port": "7004",
266 "direction": "out",
267 "interface": "foo",
268 "_ansible_check_mode": True,
269 }
270 ):
271 result = self.__getResult(do_nothing_func_port_7000)
272 self.assertTrue(result.exception.args[0]["changed"])
273
274 def test_check_mode_add_direction_out_with_ip(self):
275 with set_module_args(
276 {
277 "rule": "allow",
278 "proto": "tcp",
279 "from_ip": "1.1.1.1",
280 "from_port": "7003",
281 "to_ip": "8.8.8.8",
282 "to_port": "7004",
283 "direction": "out",
284 "interface": "foo",
285 "_ansible_check_mode": True,
286 }
287 ):
288 result = self.__getResult(do_nothing_func_port_7000)
289 self.assertTrue(result.exception.args[0]["changed"])
290
291 def test_check_mode_delete_existing_rules(self):
292 with set_module_args(
293 {
294 "rule": "allow",
295 "proto": "tcp",
296 "port": "7000",
297 "delete": "yes",
298 "_ansible_check_mode": True,
299 }
300 ):
301 self.assertTrue(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
302
303 def test_check_mode_delete_existing_insert_rules(self):
304 with set_module_args(
305 {
306 "insert": "1",
307 "rule": "allow",
308 "proto": "tcp",
309 "port": "7000",
310 "delete": "yes",
311 "_ansible_check_mode": True,
312 }
313 ):
314 self.assertTrue(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
315
316 def test_check_mode_delete_not_existing_rules(self):
317 with set_module_args(
318 {
319 "rule": "allow",
320 "proto": "tcp",
321 "port": "7001",
322 "delete": "yes",
323 "_ansible_check_mode": True,
324 }
325 ):
326 self.assertFalse(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
327
328 def test_check_mode_delete_not_existing_insert_rules(self):
329 with set_module_args(
330 {
331 "insert": "1",
332 "rule": "allow",
333 "proto": "tcp",
334 "port": "7001",
335 "delete": "yes",
336 "_ansible_check_mode": True,
337 }
338 ):
339 self.assertFalse(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
340
341 def test_enable_mode(self):
342 with set_module_args({"state": "enabled", "_ansible_check_mode": True}):
343 self.assertFalse(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
344
345 def test_disable_mode(self):
346 with set_module_args({"state": "disabled", "_ansible_check_mode": True}):
347 self.assertTrue(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
348
349 def test_logging_off(self):
350 with set_module_args({"logging": "off", "_ansible_check_mode": True}):
351 self.assertTrue(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
352
353 def test_logging_on(self):
354 with set_module_args({"logging": "on", "_ansible_check_mode": True}):
355 self.assertFalse(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
356
357 def test_default_changed(self):
358 with set_module_args({"default": "allow", "direction": "incoming", "_ansible_check_mode": True}):
359 self.assertTrue(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
360
361 def test_default_not_changed(self):
362 with set_module_args({"default": "deny", "direction": "incoming", "_ansible_check_mode": True}):
363 self.assertFalse(self.__getResult(do_nothing_func_port_7000).exception.args[0]["changed"])
364
365 def test_ipv6_remove(self):
366 with set_module_args(
367 {
368 "rule": "allow",
369 "proto": "udp",
370 "port": "5353",
371 "from": "ff02::fb",
372 "delete": "yes",
373 "_ansible_check_mode": True,
374 }
375 ):
376 self.assertTrue(self.__getResult(do_nothing_func_ipv6).exception.args[0]["changed"])
377
378 def test_ipv6_add_existing(self):
379 with set_module_args(
380 {
381 "rule": "allow",
382 "proto": "udp",
383 "port": "5353",
384 "from": "ff02::fb",
385 "_ansible_check_mode": True,
386 }
387 ):
388 self.assertFalse(self.__getResult(do_nothing_func_ipv6).exception.args[0]["changed"])
389
390 def test_add_not_existing_ipv4_submask(self):
391 with set_module_args(
392 {
393 "rule": "allow",
394 "proto": "udp",
395 "port": "1577",
396 "from": "10.0.0.0/24",
397 "_ansible_check_mode": True,
398 }
399 ):
400 self.assertTrue(self.__getResult(do_nothing_func_ipv6).exception.args[0]["changed"])
401
402 def test_ipv4_add_with_existing_ipv6(self):
403 with set_module_args(
404 {
405 "rule": "allow",
406 "proto": "udp",
407 "port": "5353",
408 "from": "224.0.0.252",
409 "_ansible_check_mode": True,
410 }
411 ):
412 self.assertTrue(self.__getResult(do_nothing_func_ipv6).exception.args[0]["changed"])
413
414 def test_ipv6_add_from_nothing(self):
415 with set_module_args(
416 {
417 "rule": "allow",
418 "port": "23",
419 "to": "::",
420 "_ansible_check_mode": True,
421 }
422 ):
423 result = self.__getResult(do_nothing_func_nothing).exception.args[0]
424 print(result)
425 self.assertTrue(result["changed"])
426
427 def __getResult(self, cmd_fun):
428 with patch.object(basic.AnsibleModule, "run_command") as mock_run_command:
429 mock_run_command.side_effect = cmd_fun
430 with self.assertRaises(AnsibleExitJson) as result:
431 module.main()
432 return result