Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3006.x] fixes #66252 correct use of egrep to parse semanage output #66253

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/66252.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Applying `selinux.fcontext_policy_present` to a shorter path than an existing entry now works
2 changes: 1 addition & 1 deletion salt/modules/selinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ def _fcontext_add_or_delete_policy(
if "add" == action:
# need to use --modify if context for name file exists, otherwise ValueError
filespec = re.escape(name)
cmd = f"semanage fcontext -l | egrep '{filespec}'"
cmd = f"semanage fcontext -l | egrep '{filespec} '"
dmurphy18 marked this conversation as resolved.
Show resolved Hide resolved
current_entry_text = __salt__["cmd.shell"](cmd, ignore_retcode=True)
if current_entry_text != "":
action = "modify"
Expand Down
34 changes: 34 additions & 0 deletions tests/pytests/unit/modules/test_selinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from salt.exceptions import SaltInvocationError
from tests.support.mock import MagicMock, mock_open, patch

pytestmark = [pytest.mark.skip_unless_on_linux]


@pytest.fixture
def configure_loader_modules():
Expand Down Expand Up @@ -410,3 +412,35 @@ def test_selinux_add_policy_regex(name, sel_type):
mock_cmd_run_all.assert_called_once_with(
expected_cmd_run_all,
)


@pytest.mark.parametrize(
"name,sel_type",
(
("/usr/share/munin/plugins/mysql_queries", "services_munin_plugin_exec_t"),
("/usr/share/munin/plugins/mysql_", "unconfined_munin_plugin_exec_t"),
),
)
def test_selinux_add_policy_shorter_path(name, sel_type):
"""
Test adding policy with a shorter path than an existing entry
"""
mock_cmd_shell = MagicMock(return_value={"retcode": 0})
mock_cmd_run_all = MagicMock(return_value={"retcode": 0})

with patch.dict(selinux.__salt__, {"cmd.shell": mock_cmd_shell}), patch.dict(
selinux.__salt__, {"cmd.run_all": mock_cmd_run_all}
):
selinux.fcontext_add_policy(name, sel_type=sel_type)
filespec = re.escape(name)
expected_cmd_shell = f"semanage fcontext -l | egrep '{filespec}'"
mock_cmd_shell.assert_called_once_with(
expected_cmd_shell,
ignore_retcode=True,
)
expected_cmd_run_all = (
f"semanage fcontext --modify --type {sel_type} {filespec}"
)
mock_cmd_run_all.assert_called_once_with(
expected_cmd_run_all,
)
Loading