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 authored and dwoz committed Jul 11, 2024
1 parent 2aa2131 commit f96ecd1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions salt/states/firewalld.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,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 @@ -767,6 +788,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 f96ecd1

Please sign in to comment.