From fa1b8d8eb7f10a723b21e8400bc01f42e0702daf Mon Sep 17 00:00:00 2001 From: Gerlof Fokkema Date: Tue, 11 Jun 2024 11:34:48 +0200 Subject: [PATCH] Firewalld: Add functionality to set forwarding. Fixes #529. --- plugins/modules/firewalld.py | 82 ++++++++++++++++++- .../firewalld/tasks/source_test_cases.yml | 5 +- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/plugins/modules/firewalld.py b/plugins/modules/firewalld.py index 146990f46b..06f43fbfb1 100644 --- a/plugins/modules/firewalld.py +++ b/plugins/modules/firewalld.py @@ -108,6 +108,10 @@ - The amount of time in seconds the rule should be in effect for when non-permanent. type: int default: 0 + forward: + description: + - The forward setting you would like to enable/disable to/from zones within firewalld. + type: str masquerade: description: - The masquerade setting you would like to enable/disable to/from zones within firewalld. @@ -198,6 +202,12 @@ permanent: true state: enabled +- ansible.posix.firewalld: + forward: true + state: enabled + permanent: true + zone: internal + - ansible.posix.firewalld: masquerade: true state: enabled @@ -405,6 +415,49 @@ def set_disabled_permanent(self, protocol, timeout): self.update_fw_settings(fw_zone, fw_settings) +class ForwardTransaction(FirewallTransaction): + """ + ForwardTransaction + """ + + def __init__(self, module, action_args=None, zone=None, desired_state=None, permanent=False, immediate=False): + super(ForwardTransaction, self).__init__( + module, action_args=action_args, desired_state=desired_state, zone=zone, permanent=permanent, immediate=immediate + ) + + self.enabled_msg = "Added forward to zone %s" % self.zone + self.disabled_msg = "Removed forward from zone %s" % self.zone + + def get_enabled_immediate(self): + if self.fw.queryForward(self.zone) is True: + return True + else: + return False + + def get_enabled_permanent(self): + fw_zone, fw_settings = self.get_fw_zone_settings() + if fw_settings.queryForward() is True: + return True + else: + return False + + def set_enabled_immediate(self): + self.fw.addForward(self.zone) + + def set_enabled_permanent(self): + fw_zone, fw_settings = self.get_fw_zone_settings() + fw_settings.setForward(True) + self.update_fw_settings(fw_zone, fw_settings) + + def set_disabled_immediate(self): + self.fw.removeForward(self.zone) + + def set_disabled_permanent(self): + fw_zone, fw_settings = self.get_fw_zone_settings() + fw_settings.setForward(False) + self.update_fw_settings(fw_zone, fw_settings) + + class MasqueradeTransaction(FirewallTransaction): """ MasqueradeTransaction @@ -821,6 +874,7 @@ def main(): state=dict(type='str', required=True, choices=['absent', 'disabled', 'enabled', 'present']), timeout=dict(type='int', default=0), interface=dict(type='str'), + forward=dict(type='str'), masquerade=dict(type='str'), offline=dict(type='bool', default=False), target=dict(type='str', choices=['default', 'ACCEPT', 'DROP', '%%REJECT%%']), @@ -833,7 +887,7 @@ def main(): ), mutually_exclusive=[ ['icmp_block', 'icmp_block_inversion', 'service', 'protocol', 'port', 'port_forward', 'rich_rule', - 'interface', 'masquerade', 'source', 'target'] + 'interface', 'forward', 'masquerade', 'source', 'target'] ], ) @@ -842,6 +896,7 @@ def main(): immediate = module.params['immediate'] timeout = module.params['timeout'] interface = module.params['interface'] + forward = module.params['forward'] masquerade = module.params['masquerade'] offline = module.params['offline'] @@ -905,7 +960,7 @@ def main(): modification = False if any([icmp_block, icmp_block_inversion, service, protocol, port, port_forward, rich_rule, - interface, masquerade, source, target]): + interface, forward, masquerade, source, target]): modification = True if modification and desired_state in ['absent', 'present'] and target is None: module.fail_json( @@ -1072,6 +1127,29 @@ def main(): changed, transaction_msgs = transaction.run() msgs = msgs + transaction_msgs + if forward is not None: + # Type of forward will be changed to boolean in a future release. + forward_status = True + try: + forward_status = boolean(forward, True) + except TypeError: + module.warn('The value of the forward option is "%s". ' + 'The type of the option will be changed from string to boolean in a future release. ' + 'To avoid unexpected behavior, please change the value to boolean.' % forward) + + expected_state = 'enabled' if (desired_state == 'enabled') == forward_status else 'disabled' + transaction = ForwardTransaction( + module, + action_args=(), + zone=zone, + desired_state=expected_state, + permanent=permanent, + immediate=immediate, + ) + + changed, transaction_msgs = transaction.run() + msgs = msgs + transaction_msgs + if masquerade is not None: # Type of masquerade will be changed to boolean in a future release. masquerade_status = True diff --git a/tests/integration/targets/firewalld/tasks/source_test_cases.yml b/tests/integration/targets/firewalld/tasks/source_test_cases.yml index a2552d55f5..8b76521d94 100644 --- a/tests/integration/targets/firewalld/tasks/source_test_cases.yml +++ b/tests/integration/targets/firewalld/tasks/source_test_cases.yml @@ -83,5 +83,6 @@ ansible.builtin.assert: that: - result is not changed - - "result.msg == - 'parameters are mutually exclusive: icmp_block|icmp_block_inversion|service|protocol|port|port_forward|rich_rule|interface|masquerade|source|target'" + - > + result.msg == 'parameters are mutually exclusive: + icmp_block|icmp_block_inversion|service|protocol|port|port_forward|rich_rule|interface|forward|masquerade|source|target'