Compare commits

...

7 Commits

View File

@@ -31,7 +31,7 @@ import difflib
# 2 : Config file invalid, sections must define at least CONST.CFG_MANDATORY # 2 : Config file invalid, sections must define at least CONST.CFG_MANDATORY
# 3 : Performing a firewalld rules check failed # 3 : Performing a firewalld rules check failed
# 4 : Performing a firewalld rules encountered a FileNotFoundError # 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 # 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 # 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 # 8 : Exception while adding a chain XML element to firewalld direct rules
@@ -127,6 +127,10 @@ internal_empty_ok = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if d
config = ConfigParser(defaults=internal_defaults, config = ConfigParser(defaults=internal_defaults,
converters={'list': lambda x: [i.strip() for i in x.split(',') if len(x) > 0]}) converters={'list': lambda x: [i.strip() for i in x.split(',') if len(x) > 0]})
config.read(CONST.CFG_DEFAULT_ABS_PATH) config.read(CONST.CFG_DEFAULT_ABS_PATH)
exit_code_desc = {
5: "Unable to open firewalld direct rules file for reading",
9: "Unable to open firewalld direct rules file for updating"
}
def print_section_header( def print_section_header(
@@ -287,7 +291,7 @@ def add_rule_elem(
prio: int, prio: int,
target: str, target: str,
/, *, /, *,
arg_section_name: str = None, arg_section: str = None,
arg_proto: str = None, arg_proto: str = None,
arg_state: str = None, arg_state: str = None,
arg_ports: list = None, arg_ports: list = None,
@@ -309,8 +313,7 @@ def add_rule_elem(
f"""{"--match multiport --destination-ports " + ",".join(arg_ports) + " " if arg_ports else ""}""" \ f"""{"--match multiport --destination-ports " + ",".join(arg_ports) + " " if arg_ports else ""}""" \
f"""{"--source " + arg_address + " " if arg_address else ""}""" \ f"""{"--source " + arg_address + " " if arg_address else ""}""" \
f"""--jump {target}""" \ f"""--jump {target}""" \
f""" f"""{" --match comment --comment " + chr(34) + arg_section[:256] + chr(34) if arg_section else ""}"""
{" --match comment --comment " + chr(34) + arg_section_name[:256] + chr(34) if arg_section_name else ""}"""
except lxml.etree.LxmlError as le: except lxml.etree.LxmlError as le:
log.error(f"""Failed to add XML '<rule ipv=f"{address_family}" .../>'\n""" log.error(f"""Failed to add XML '<rule ipv=f"{address_family}" .../>'\n"""
f"Verbatim exception was:\n" f"Verbatim exception was:\n"
@@ -384,7 +387,7 @@ def add_fw_rule_to_xml(
address_family, address_family,
rules_already_added[address_family], rules_already_added[address_family],
target, target,
arg_section_name=section_name, arg_section=section_name,
arg_proto=proto, arg_proto=proto,
arg_state=config_obj.get(section_name, "state"), arg_state=config_obj.get(section_name, "state"),
arg_ports=ports, arg_ports=ports,
@@ -400,7 +403,7 @@ def add_fw_rule_to_xml(
address_family, address_family,
rules_already_added[address_family], rules_already_added[address_family],
target, target,
arg_section_name=section_name, arg_section=section_name,
arg_proto=proto, arg_proto=proto,
arg_state=config_obj.get(section_name, "state"), arg_state=config_obj.get(section_name, "state"),
arg_ports=ports) arg_ports=ports)
@@ -475,14 +478,34 @@ def gen_fwd_direct_scaffolding() -> lxml.builder.ElementMaker:
return fw_rule_data return fw_rule_data
def write_new_fwd_direct_xml( def ose_handler(
config_obj: configparser.ConfigParser()) -> bool: os_error: OSError,
human_text: str = None,
exit_code: int = None) -> None:
nl = "\n"
log.error(f"{human_text if human_text else exit_code_desc.get(exit_code)}"
f"{nl}Verbatim exception was:\n"
f"{os_error}"
f"""{nl + "Exiting " + str(exit_code) + " ..." if exit_code else ""}""")
def get_xml_str_repr() -> str:
global arg_fw_rule_data global arg_fw_rule_data
fwd_direct_xml_str = lxml.etree.tostring(arg_fw_rule_data, fwd_direct_xml_str = lxml.etree.tostring(arg_fw_rule_data,
pretty_print=True, pretty_print=True,
encoding="UTF-8", encoding="UTF-8",
xml_declaration=True).decode() xml_declaration=True).decode()
return fwd_direct_xml_str
def write_new_fwd_direct_xml(
config_obj: configparser.ConfigParser()) -> bool:
global arg_fw_rule_data
fwd_direct_xml_str = get_xml_str_repr()
try: try:
with open(config_obj.get(configparser.DEFAULTSECT, "firewalld_direct_abs"), "r+") as fwd_file_handle: with open(config_obj.get(configparser.DEFAULTSECT, "firewalld_direct_abs"), "r+") as fwd_file_handle:
log.info(f"Writing new firewalld direct config ...") log.info(f"Writing new firewalld direct config ...")
@@ -492,10 +515,7 @@ def write_new_fwd_direct_xml(
fwd_file_handle.write(fwd_direct_xml_str) fwd_file_handle.write(fwd_direct_xml_str)
fwd_file_handle.truncate() fwd_file_handle.truncate()
except OSError as ose: except OSError as ose:
log.error(f"Unable to open firewalld direct rules file for updating.\n" ose_handler(os_error=ose, exit_code=9)
f"Verbatim exception was:\n"
f"f{ose}\n"
f"Exiting 9 ...")
sys.exit(9) sys.exit(9)
else: else:
return True return True