From a7ad08d22db13edcc00bf21d708facd5eb4259e7 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 22 Apr 2024 15:00:45 +0100 Subject: [PATCH] Revert "Preserve target permissions when setting link ownership" This reverts commit 7a1a558cfc3c256446fd6ed0df3e3369b8e4db4a. --- salt/modules/file.py | 3 +- tests/pytests/integration/states/test_file.py | 44 ------------------- 2 files changed, 2 insertions(+), 45 deletions(-) diff --git a/salt/modules/file.py b/salt/modules/file.py index e7514091363..09364804fe6 100644 --- a/salt/modules/file.py +++ b/salt/modules/file.py @@ -5147,7 +5147,7 @@ def check_perms( if err: ret["result"] = False ret["comment"].append(err) - elif not is_link: + else: # Python os.chown() resets the suid and sgid, hence we # setting the previous mode again. Pending mode changes # will be applied later. @@ -5198,6 +5198,7 @@ def check_perms( ret["comment"].append(f"Failed to change group to {group}") elif "cgroup" in perms: ret["changes"]["group"] = group + if mode is not None: # File is a symlink, ignore the mode setting # if follow_symlinks is False diff --git a/tests/pytests/integration/states/test_file.py b/tests/pytests/integration/states/test_file.py index 4c01387de9a..673676b2502 100644 --- a/tests/pytests/integration/states/test_file.py +++ b/tests/pytests/integration/states/test_file.py @@ -6,8 +6,6 @@ import logging import os import pathlib import re -import stat -import subprocess import textwrap import pytest @@ -1208,45 +1206,3 @@ def test_contents_file(salt_master, salt_call_cli, tmp_path): assert state_run["result"] is True # Check to make sure the file was created assert target_path.is_file() - - -def test_directory_recurse(salt_master, salt_call_cli, tmp_path): - """ - Test modifying ownership of symlink without affecting the link target's - permissions. - """ - target_path = tmp_path / "test" - target_path.mkdir() - - (target_path / "target").write_text("this is a test file") - (target_path / "link").symlink_to(target_path / "target") - # Change the ownership of the sybolic link to 'nobody' - subprocess.run(["chown", "-h", "nobody", str(target_path / "link")], check=True) - file_perms = ( - stat.S_IFREG | stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH - ) - # The permissions of the file should be 644. - mode = (target_path / "target").stat().st_mode - assert file_perms == mode - - sls_name = "test" - sls_contents = f""" - {target_path}: - file.directory: - - user: root - - recurse: - - user - """ - sls_tempfile = salt_master.state_tree.base.temp_file( - f"{sls_name}.sls", sls_contents - ) - with sls_tempfile: - ret = salt_call_cli.run("state.sls", sls_name) - key = f"file_|-{target_path}_|-{target_path}_|-directory" - assert key in ret.json - result = ret.json[key] - assert "changes" in result and result["changes"] - - # Permissions of file should not have changed. - mode = (target_path / "target").stat().st_mode - assert file_perms == mode