Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_node_firewall.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2026, Clément Cruau (@PendaGTP) <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 DOCUMENTATION = r"""
8 module: proxmox_node_firewall
9 short_description: Node-level firewall options management for Proxmox VE cluster
10 version_added: "2.0.0"
11 author:
12 - Clément Cruau (@PendaGTP)
13 description:
14 - Manage firewall options at the node level in Proxmox VE.
15 attributes:
16 check_mode:
17 support: full
18 diff_mode:
19 support: none
20 options:
21 state:
22 description:
23 - Enable or disable the firewall node-wide.
24 choices:
25 - enabled
26 - disabled
27 type: str
28 default: enabled
29 node_name:
30 description:
31 - Name of the node to configure the firewall on.
32 type: str
33 aliases: ["node"]
34 required: true
35 log_level_in:
36 description:
37 - Log level for incoming traffic.
38 type: str
39 choices:
40 - emerg
41 - alert
42 - crit
43 - err
44 - warning
45 - notice
46 - info
47 - debug
48 - nolog
49 default: nolog
50 log_level_out:
51 description:
52 - Log level for outgoing traffic.
53 type: str
54 choices:
55 - emerg
56 - alert
57 - crit
58 - err
59 - warning
60 - notice
61 - info
62 - debug
63 - nolog
64 default: nolog
65 log_level_forward:
66 description:
67 - Log level for fowarded traffic.
68 type: str
69 choices:
70 - emerg
71 - alert
72 - crit
73 - err
74 - warning
75 - notice
76 - info
77 - debug
78 - nolog
79 default: nolog
80 ndp:
81 description:
82 - Enable NDP (Neighbor Discovery Protocol).
83 type: bool
84 default: true
85 nftables:
86 description:
87 - Enable nftables based firewall.
88 type: bool
89 default: false
90 nosmurfs:
91 description:
92 - Enable SMURFS filter.
93 type: bool
94 default: true
95 smurf_log_level:
96 description:
97 - Log level for SMURFS filter.
98 type: str
99 choices:
100 - emerg
101 - alert
102 - crit
103 - err
104 - warning
105 - notice
106 - info
107 - debug
108 - nolog
109 default: nolog
110 tcp_flags_log_level:
111 description:
112 - Log level for illegal TCP flags filter.
113 type: str
114 choices:
115 - emerg
116 - alert
117 - crit
118 - err
119 - warning
120 - notice
121 - info
122 - debug
123 - nolog
124 default: nolog
125 tcpflags:
126 description:
127 - Filter illegal combinations of TCP flags.
128 type: bool
129 default: false
130 nf_conntrack_allow_invalid:
131 description: Allow invalid packets on connection tracking.
132 type: bool
133 default: false
134 nf_conntrack_helpers:
135 description:
136 - Enable conntrack helpers for specific protocols.
137 type: str
138 nf_conntrack_max:
139 description:
140 - Maximum number of tracked connections.
141 - Minimum value is 32768.
142 type: int
143 default: 262144
144 nf_conntrack_tcp_timeout_established:
145 description:
146 - Conntrack established timeout in seconds.
147 - Minimum value is 7875.
148 type: int
149 default: 432000
150 nf_conntrack_tcp_timeout_syn_recv:
151 description:
152 - Conntrack syn receive timeout.
153 - Values between 30 - 60.
154 type: int
155 default: 60
156 protection_synflood:
157 description:
158 - Enable synflood protection.
159 type: bool
160 default: false
161 protection_synflood_burst:
162 description:
163 - Synflood protection rate burst by IP source address.
164 type: int
165 default: 1000
166 protection_synflood_rate:
167 description:
168 - Synflood protection rate syn/sec by IP source address.
169 type: int
170 default: 200
171
172
173 seealso:
174 - name: Proxmox VE Firewall configuration
175 description: Complete reference of Proxmox VE Firewall
176 link: https://pve.proxmox.com/pve-docs/chapter-pve-firewall.html
177 - name: Proxmox VE node-wide configuration
178 description: Complete reference of Proxmox VE Firewall host configuration
179 link: https://pve.proxmox.com/pve-docs/chapter-pve-firewall.html#pve_firewall_host_specific_configuration
180 - module: community.proxmox.proxmox_node_firewall_info
181 - module: community.proxmox.proxmox_cluster_firewall
182
183 extends_documentation_fragment:
184 - community.proxmox.proxmox.actiongroup_proxmox
185 - community.proxmox.proxmox.documentation
186 - community.proxmox.attributes
187 """
188
189 EXAMPLES = r"""
190 - name: Configure traffic log level
191 community.proxmox.proxmox_node_firewall:
192 state: enabled
193 node_name: pve-001
194 log_level_in: alert
195 log_level_out: alert
196 log_level_forward: alert
197
198 - name: Disable node-wide firewall
199 community.proxmox.proxmox_node_firewall:
200 state: disabled
201 node_name: pve-001
202 """
203
204 RETURN = r"""
205 enabled:
206 description: Whether the firewall is enabled node-wide.
207 returned: on success
208 type: bool
209 sample: true
210 node_name:
211 description: The name of the node.
212 returned: on success
213 type: str
214 sample: pve-001
215 log_level_in:
216 description: Log level setting for incoming traffic.
217 returned: on success
218 type: str
219 sample: no_log
220 log_level_out:
221 description: Log level setting for outgoing traffic.
222 returned: on success
223 type: str
224 sample: no_log
225 log_level_forward:
226 description: Log level setting for fowarded traffic.
227 returned: on success
228 type: str
229 sample: no_log
230 ndp:
231 description: Whether NDP (Neighbor Discovery Protocol) is enabled.
232 returned: on success
233 type: bool
234 sample: true
235 nftables:
236 description: Whether nftables based firewall is enabled.
237 returned: on success
238 type: bool
239 sample: false
240 nosmurfs:
241 description: Whether SMURFS filter is enabled.
242 returned: on success
243 type: bool
244 sample: true
245 smurf_log_level:
246 description: Log level setting for SMURFS filter.
247 returned: on success
248 type: str
249 tcp_flags_log_level:
250 description: Log level setting for illegal TCP flags filter.
251 returned: on success
252 type: str
253 tcpflags:
254 description: Whether illegal combinations of TCP flags are filtered.
255 returned: on success
256 type: bool
257 sample: false
258 nf_conntrack_allow_invalid:
259 description: Whether invalid packets are allowed on connection tracking.
260 returned: on success
261 type: bool
262 sample: false
263 nf_conntrack_helpers:
264 description: Conntrack helpers for specific protocols.
265 returned: on success
266 type: str
267 nf_conntrack_max:
268 description: Maximum number of tracked connections.
269 returned: on success
270 type: int
271 sample: 262144
272 nf_conntrack_tcp_timeout_established:
273 description: Conntrack established timeout in seconds.
274 returned: on success
275 type: int
276 sample: 432000
277 nf_conntrack_tcp_timeout_syn_recv:
278 description: Conntrack syn recv timeout in seconds.
279 returned: on success
280 type: int
281 sample: 60
282 protection_synflood:
283 description: Whether synflood protection is enabled.
284 returned: on success
285 type: bool
286 sample: false
287 protection_synflood_burst:
288 description: Synflood protection rate burst by IP source address.
289 returned: on success
290 type: int
291 sample: 1000
292 protection_synflood_rate:
293 description: Synflood protection rate syn/sec by IP source address.
294 returned: on success
295 type: int
296 sample: 200
297
298 msg:
299 description: A short message on what the module did.
300 returned: always
301 type: str
302 sample: "Node firewall options updated"
303 """
304
305 from ansible.module_utils.common.text.converters import to_native
306 from ansible.module_utils.errors import AnsibleValidationError
307
308 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
309 ProxmoxAnsible,
310 create_proxmox_module,
311 )
312 from ansible_collections.community.proxmox.plugins.module_utils.proxmox_node_firewall import (
313 SCHEMA,
314 node_firewall_options_to_ansible_result,
315 )
316
317
318 def _validate_nf_conntrack_max(value):
319 MIN_VALUE = 32768
320 if value is not None and value < MIN_VALUE:
321 raise AnsibleValidationError(f"nf_conntrack_max must be greater than {MIN_VALUE}")
322
323
324 def _validate_nf_conntrack_tcp_timeout_syn_recv(value):
325 MIN_VALUE = 30
326 MAX_VALUE = 60
327 if value is not None and (value < MIN_VALUE or value > MAX_VALUE):
328 raise AnsibleValidationError(f"nf_conntrack_tcp_timeout_syn_recv must be between {MIN_VALUE} and {MAX_VALUE}")
329
330
331 def _validate_protection_synflood_burst(value):
332 MIN_VALUE = 1000
333 if value is not None and value < MIN_VALUE:
334 raise AnsibleValidationError(f"protection_synflood_burst must be greater than {MIN_VALUE}")
335
336
337 def _validate_protection_synflood_rate(value):
338 MIN_VALUE = 200
339 if value is not None and value < MIN_VALUE:
340 raise AnsibleValidationError(f"protection_synflood_rate must be greater than {MIN_VALUE}")
341
342
343 def module_args():
344 return dict(
345 state=dict(choices=["enabled", "disabled"], default="enabled"),
346 node_name=dict(aliases=["node"], type="str", required=True),
347 log_level_in=dict(
348 type="str",
349 choices=["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug", "nolog"],
350 default="nolog",
351 ),
352 log_level_out=dict(
353 type="str",
354 choices=["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug", "nolog"],
355 default="nolog",
356 ),
357 log_level_forward=dict(
358 type="str",
359 choices=["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug", "nolog"],
360 default="nolog",
361 ),
362 ndp=dict(type="bool", default=True),
363 nftables=dict(type="bool", default=False),
364 nosmurfs=dict(type="bool", default=True),
365 smurf_log_level=dict(
366 type="str",
367 choices=["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug", "nolog"],
368 default="nolog",
369 ),
370 tcp_flags_log_level=dict(
371 type="str",
372 choices=["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug", "nolog"],
373 default="nolog",
374 ),
375 tcpflags=dict(type="bool", default=False),
376 nf_conntrack_allow_invalid=dict(type="bool", default=False),
377 nf_conntrack_helpers=dict(type="str"),
378 nf_conntrack_max=dict(type="int", default=262144),
379 nf_conntrack_tcp_timeout_established=dict(type="int", default=432000),
380 nf_conntrack_tcp_timeout_syn_recv=dict(type="int", default=60),
381 protection_synflood=dict(type="bool", default=False),
382 protection_synflood_burst=dict(type="int", default=1000),
383 protection_synflood_rate=dict(type="int", default=200),
384 )
385
386
387 def module_options():
388 return {}
389
390
391 class ProxmoxNodeFirewallAnsible(ProxmoxAnsible):
392 def __init__(self, module):
393 super().__init__(module)
394 self.params = module.params
395
396 def run(self):
397 self._ensure_state()
398
399 def validate_params(self):
400 try:
401 _validate_nf_conntrack_max(self.params["nf_conntrack_max"])
402 _validate_nf_conntrack_tcp_timeout_syn_recv(self.params["nf_conntrack_tcp_timeout_syn_recv"])
403 _validate_protection_synflood_burst(self.params["protection_synflood_burst"])
404 _validate_protection_synflood_rate(self.params["protection_synflood_rate"])
405 except AnsibleValidationError as e:
406 self.module.fail_json(msg=to_native(e))
407
408 def _ensure_state(self):
409 current_raw = self._fetch_firewall_options()
410 current = self._format_options(current_raw)
411 desired = self._build_desired()
412
413 if not self._is_update_required(current, desired):
414 self.module.exit_json(
415 changed=False,
416 msg="Node firewall options already match desired state",
417 **current,
418 )
419
420 if self.module.check_mode:
421 result = current.copy()
422 result.update(desired)
423 self.module.exit_json(
424 changed=True,
425 msg="Node firewall options would be updated",
426 **result,
427 )
428
429 self._update_firewall(desired)
430
431 updated_raw = self._fetch_firewall_options()
432 updated = self._format_options(updated_raw)
433
434 self.module.exit_json(
435 changed=True,
436 msg="Node firewall options updated",
437 **updated,
438 )
439
440 def _is_update_required(self, current, desired):
441 return any(current[k] != desired[k] for k in SCHEMA)
442
443 def _update_firewall(self, desired):
444 payload = self._build_params(desired)
445
446 try:
447 self.proxmox_api.nodes(self.params["node_name"]).firewall().options.put(**payload)
448 except Exception as e:
449 self.module.fail_json(msg=f"Failed to set node firewall options: {to_native(e)}")
450
451 def _fetch_firewall_options(self):
452 try:
453 return self.proxmox_api.nodes(self.params["node_name"]).firewall().options.get()
454 except Exception as e:
455 self.module.fail_json(msg=f"Failed to retrieve node firewall options: {to_native(e)}")
456
457 def _build_desired(self):
458 result = {
459 "node_name": self.params["node_name"],
460 "enabled": self.params["state"] == "enabled",
461 }
462
463 for field in SCHEMA:
464 if field == "enabled":
465 continue
466 result[field] = self.params.get(field)
467
468 return result
469
470 def _build_params(self, desired):
471 payload = {}
472
473 for field, meta in SCHEMA.items():
474 api_key = meta.get("api", field)
475 value = desired.get(field)
476
477 if value is None:
478 continue
479
480 if "to_api" in meta:
481 value = meta["to_api"](value)
482
483 payload[api_key] = value
484
485 return payload
486
487 def _format_options(self, raw):
488 return node_firewall_options_to_ansible_result(self.params["node_name"], raw)
489
490
491 def main():
492 module = create_proxmox_module(module_args(), **module_options())
493 proxmox = ProxmoxNodeFirewallAnsible(module)
494
495 proxmox.validate_params()
496 proxmox.run()
497
498
499 if __name__ == "__main__":
500 main()