-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
firewalld: normalize new rich rules before comparing to old (bsc#1222…
…684) (#648) * Normalize new rich rules before comparing to old 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. * Add changelog entry * Enhance documentation for normalization function * Add unit tests to cover rich rules normalization --------- Co-authored-by: Pablo Suárez Hernández <[email protected]>
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- firewalld: normalize new rich rules before comparing to old ones |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
:codeauthor: Hristo Voyvodov <[email protected]> | ||
""" | ||
|
||
import pytest | ||
|
||
import salt.states.firewalld as firewalld | ||
from tests.support.mock import MagicMock, patch | ||
|
||
|
||
@pytest.fixture | ||
def configure_loader_modules(): | ||
return {firewalld: {"__opts__": {"test": False}}} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"rich_rule", | ||
[ | ||
( | ||
[ | ||
'rule family="ipv4" source address="192.168.0.0/16" port port=22 protocol=tcp accept' | ||
] | ||
), | ||
( | ||
[ | ||
'rule family="ipv4" source address="192.168.0.0/16" port port=\'22\' protocol=tcp accept' | ||
] | ||
), | ||
( | ||
[ | ||
"rule family='ipv4' source address='192.168.0.0/16' port port='22' protocol=tcp accept" | ||
] | ||
), | ||
], | ||
) | ||
def test_present_rich_rules_normalized(rich_rule): | ||
firewalld_reload_rules = MagicMock(return_value={}) | ||
firewalld_rich_rules = [ | ||
'rule family="ipv4" source address="192.168.0.0/16" port port="22" protocol="tcp" accept', | ||
] | ||
|
||
firewalld_get_zones = MagicMock( | ||
return_value=[ | ||
"block", | ||
"public", | ||
] | ||
) | ||
firewalld_get_masquerade = MagicMock(return_value=False) | ||
firewalld_get_rich_rules = MagicMock(return_value=firewalld_rich_rules) | ||
|
||
__salt__ = { | ||
"firewalld.reload_rules": firewalld_reload_rules, | ||
"firewalld.get_zones": firewalld_get_zones, | ||
"firewalld.get_masquerade": firewalld_get_masquerade, | ||
"firewalld.get_rich_rules": firewalld_get_rich_rules, | ||
} | ||
with patch.dict(firewalld.__dict__, {"__salt__": __salt__}): | ||
ret = firewalld.present("public", rich_rules=rich_rule) | ||
assert ret == { | ||
"changes": {}, | ||
"result": True, | ||
"comment": "'public' is already in the desired state.", | ||
"name": "public", | ||
} |