From 7b6103be72acb47b45539aafbac7eb36c7443c5c Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Tue, 5 Jul 2022 17:26:05 +0200 Subject: [PATCH 1/3] refactor(debug): Clearly identify exit code 5 reason --- update-firewall-source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-firewall-source.py b/update-firewall-source.py index 9f36171..dc89c60 100644 --- a/update-firewall-source.py +++ b/update-firewall-source.py @@ -29,7 +29,7 @@ import subprocess # 2 : Config file invalid, sections must define at least CONST.CFG_MANDATORY # 3 : Performing a firewalld rules check failed # 4 : Performing a firewalld rules encountered a FileNotFoundError -# 5 : Unable to open firewalld direct rules file +# 5 : Unable to open firewalld direct rules file for reading # 6 : Source and destination are identical when attempting to back up firewalld direct rules file # 7 : An option that must have a non-null value is either unset or null # 8 : Exception while adding a chain XML element to firewalld direct rules From 1bbf75d3ddeca1c6313005d161331238ce20f0b8 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Tue, 5 Jul 2022 17:26:10 +0200 Subject: [PATCH 2/3] refactor(debug): Dedicated function to generate stringified XML repr --- update-firewall-source.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/update-firewall-source.py b/update-firewall-source.py index dc89c60..0be4fbe 100644 --- a/update-firewall-source.py +++ b/update-firewall-source.py @@ -477,10 +477,8 @@ def write_new_fwd_direct_xml( config_obj: configparser.ConfigParser()) -> bool: global arg_fw_rule_data - fwd_direct_xml_str = lxml.etree.tostring(arg_fw_rule_data, - pretty_print=True, - encoding="UTF-8", - xml_declaration=True).decode() + fwd_direct_xml_str = get_xml_str_repr() + try: with open(config_obj.get(configparser.DEFAULTSECT, "firewalld_direct_abs"), "r+") as fwd_file_handle: log.info(f"Writing new firewalld direct config ...") From c4781aa615ea63aa17763ae1331deada501ee116 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Tue, 5 Jul 2022 17:26:24 +0200 Subject: [PATCH 3/3] refactor(xml): Shorten section name var --- update-firewall-source.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/update-firewall-source.py b/update-firewall-source.py index 0be4fbe..14c7005 100644 --- a/update-firewall-source.py +++ b/update-firewall-source.py @@ -285,7 +285,7 @@ def add_rule_elem( prio: int, target: str, /, *, - arg_section_name: str = None, + arg_section: str = None, arg_proto: str = None, arg_state: str = None, arg_ports: list = None, @@ -307,8 +307,7 @@ def add_rule_elem( f"""{"--match multiport --destination-ports " + ",".join(arg_ports) + " " if arg_ports else ""}""" \ f"""{"--source " + arg_address + " " if arg_address else ""}""" \ f"""--jump {target}""" \ - f""" - {" --match comment --comment " + chr(34) + arg_section_name[:256] + chr(34) if arg_section_name else ""}""" + f"""{" --match comment --comment " + chr(34) + arg_section[:256] + chr(34) if arg_section else ""}""" except lxml.etree.LxmlError as le: log.error(f"""Failed to add XML ''\n""" f"Verbatim exception was:\n" @@ -382,7 +381,7 @@ def add_fw_rule_to_xml( address_family, rules_already_added[address_family], target, - arg_section_name=section_name, + arg_section=section_name, arg_proto=proto, arg_state=config_obj.get(section_name, "state"), arg_ports=ports, @@ -398,7 +397,7 @@ def add_fw_rule_to_xml( address_family, rules_already_added[address_family], target, - arg_section_name=section_name, + arg_section=section_name, arg_proto=proto, arg_state=config_obj.get(section_name, "state"), arg_ports=ports)