Skip to content

Commit

Permalink
Normalize new rich rules before comparing to old
Browse files Browse the repository at this point in the history
Firewallcmd rich rule output quotes each
assigned part of the rich rule, for example:
rule family="ipv4" source port port="161" ...
The firewalld module must first normalize
the user defined rich rules to match the
firewallcmd output before comparison to
ensure idempotency.
  • Loading branch information
m-czernek committed May 13, 2024
1 parent 4ec5c8b commit c3f0607
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion salt/states/firewalld.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ def present(
rich_rules=None,
prune_rich_rules=False,
):

"""
Ensure a zone has specific attributes.
Expand Down Expand Up @@ -378,6 +377,27 @@ def service(name, ports=None, protocols=None):
return ret


def _normalize_rich_rules(rich_rules):
normalized_rules = []
for rich_rule in rich_rules:
normalized_rule = ""
for cmd in rich_rule.split(" "):
cmd_components = cmd.split("=", 1)
if len(cmd_components) == 2:
assigned_component = cmd_components[1]
if not assigned_component.startswith(
'"'
) and not assigned_component.endswith('"'):
if assigned_component.startswith(
"'"
) and assigned_component.endswith("'"):
assigned_component = assigned_component[1:-1]
cmd_components[1] = f'"{assigned_component}"'
normalized_rule = f"{normalized_rule} {'='.join(cmd_components)}"
normalized_rules.append(normalized_rule.lstrip())
return normalized_rules


def _present(
name,
block_icmp=None,
Expand Down Expand Up @@ -761,6 +781,7 @@ def _present(

if rich_rules or prune_rich_rules:
rich_rules = rich_rules or []
rich_rules = _normalize_rich_rules(rich_rules)
try:
_current_rich_rules = __salt__["firewalld.get_rich_rules"](
name, permanent=True
Expand Down

0 comments on commit c3f0607

Please sign in to comment.