Fix issue when policy dirs are missing

This commit is contained in:
twangboy 2023-10-16 15:43:04 -06:00 committed by Gareth J. Greenaway
parent 84d6c962f6
commit 134ea53a43
2 changed files with 243 additions and 19 deletions

View file

@ -16,8 +16,10 @@ import stat
import sys
import tempfile
import salt.utils.files
import salt.utils.path
import salt.utils.platform
import salt.utils.user
from salt.exceptions import CommandExecutionError, SaltInvocationError
from salt.modules.file import (
__clean_tmp,
@ -107,6 +109,15 @@ try:
except ImportError:
HAS_WINDOWS_MODULES = False
HAS_WIN_DACL = False
try:
if salt.utils.platform.is_windows():
import salt.utils.win_dacl
HAS_WIN_DACL = True
except ImportError:
HAS_WIN_DACL = False
if salt.utils.platform.is_windows():
if HAS_WINDOWS_MODULES:
# namespace functions from file.py
@ -194,6 +205,8 @@ def __virtual__():
"""
if not salt.utils.platform.is_windows() or not HAS_WINDOWS_MODULES:
return False, "Module win_file: Missing Win32 modules"
if not HAS_WIN_DACL:
return False, "Module win_file: Unable to load salt.utils.win_dacl"
return __virtualname__
@ -305,7 +318,7 @@ def group_to_gid(group):
if group is None:
return ""
return __utils__["dacl.get_sid_string"](group)
return salt.utils.win_dacl.get_sid_string(group)
def get_pgid(path, follow_symlinks=True):
@ -346,8 +359,8 @@ def get_pgid(path, follow_symlinks=True):
if follow_symlinks and sys.getwindowsversion().major >= 6:
path = _resolve_symlink(path)
group_name = __utils__["dacl.get_primary_group"](path)
return __utils__["dacl.get_sid_string"](group_name)
group_name = salt.utils.win_dacl.get_primary_group(path)
return salt.utils.win_dacl.get_sid_string(group_name)
def get_pgroup(path, follow_symlinks=True):
@ -498,7 +511,7 @@ def uid_to_user(uid):
if uid is None or uid == "":
return ""
return __utils__["dacl.get_name"](uid)
return salt.utils.win_dacl.get_name(uid)
def user_to_uid(user):
@ -518,9 +531,9 @@ def user_to_uid(user):
salt '*' file.user_to_uid myusername
"""
if user is None:
user = __utils__["user.get_user"]()
user = salt.utils.user.get_user()
return __utils__["dacl.get_sid_string"](user)
return salt.utils.win_dacl.get_sid_string(user)
def get_uid(path, follow_symlinks=True):
@ -558,8 +571,8 @@ def get_uid(path, follow_symlinks=True):
if follow_symlinks and sys.getwindowsversion().major >= 6:
path = _resolve_symlink(path)
owner_sid = __utils__["dacl.get_owner"](path)
return __utils__["dacl.get_sid_string"](owner_sid)
owner_sid = salt.utils.win_dacl.get_owner(path)
return salt.utils.win_dacl.get_sid_string(owner_sid)
def get_user(path, follow_symlinks=True):
@ -597,7 +610,7 @@ def get_user(path, follow_symlinks=True):
if follow_symlinks and sys.getwindowsversion().major >= 6:
path = _resolve_symlink(path)
return __utils__["dacl.get_owner"](path)
return salt.utils.win_dacl.get_owner(path)
def get_mode(path):
@ -735,9 +748,9 @@ def chown(path, user, group=None, pgroup=None, follow_symlinks=True):
if not os.path.exists(path):
raise CommandExecutionError("Path not found: {}".format(path))
__utils__["dacl.set_owner"](path, user)
salt.utils.win_dacl.set_owner(path, user)
if pgroup:
__utils__["dacl.set_primary_group"](path, pgroup)
salt.utils.win_dacl.set_primary_group(path, pgroup)
return True
@ -767,7 +780,7 @@ def chpgrp(path, group):
salt '*' file.chpgrp c:\\temp\\test.txt Administrators
salt '*' file.chpgrp c:\\temp\\test.txt "'None'"
"""
return __utils__["dacl.set_primary_group"](path, group)
return salt.utils.win_dacl.set_primary_group(path, group)
def chgrp(path, group):
@ -802,7 +815,7 @@ def chgrp(path, group):
.. code-block:: bash
salt '*' file.chpgrp c:\\temp\\test.txt administrators
salt '*' file.chgrp c:\\temp\\test.txt administrators
"""
func_name = "{}.chgrp".format(__virtualname__)
if __opts__.get("fun", "") == func_name:
@ -871,7 +884,7 @@ def stats(path, hash_type="sha256", follow_symlinks=True):
ret["mtime"] = pstat.st_mtime
ret["ctime"] = pstat.st_ctime
ret["size"] = pstat.st_size
ret["mode"] = __utils__["files.normalize_mode"](oct(stat.S_IMODE(pstat.st_mode)))
ret["mode"] = salt.utils.files.normalize_mode(oct(stat.S_IMODE(pstat.st_mode)))
if hash_type:
ret["sum"] = get_sum(path, hash_type)
ret["type"] = "file"
@ -1503,7 +1516,7 @@ def is_link(path):
)
try:
return __utils__["path.islink"](path)
return salt.utils.path.islink(path)
except Exception as exc: # pylint: disable=broad-except
raise CommandExecutionError(exc)
@ -1594,10 +1607,10 @@ def mkdir(
# Set owner
if owner:
__utils__["dacl.set_owner"](obj_name=path, principal=owner)
salt.utils.win_dacl.set_owner(obj_name=path, principal=owner)
# Set permissions
__utils__["dacl.set_perms"](
salt.utils.win_dacl.set_perms(
obj_name=path,
obj_type="file",
grant_perms=grant_perms,
@ -1916,7 +1929,7 @@ def check_perms(
path = os.path.expanduser(path)
return __utils__["dacl.check_perms"](
return salt.utils.win_dacl.check_perms(
obj_name=path,
obj_type="file",
ret=ret,
@ -2002,7 +2015,7 @@ def set_perms(path, grant_perms=None, deny_perms=None, inheritance=True, reset=F
# Specify advanced attributes with a list
salt '*' file.set_perms C:\\Temp\\ "{'jsnuffy': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder_only'}}"
"""
return __utils__["dacl.set_perms"](
return salt.utils.win_dacl.set_perms(
obj_name=path,
obj_type="file",
grant_perms=grant_perms,

View file

@ -1,13 +1,43 @@
import os
import re
import pytest
import salt.modules.win_file as win_file
import salt.utils.user
import salt.utils.win_dacl
from salt.exceptions import CommandExecutionError
from tests.support.mock import patch
pytestmark = [pytest.mark.windows_whitelisted, pytest.mark.skip_unless_on_windows]
@pytest.fixture
def configure_loader_modules():
return {
win_file: {},
salt.utils.win_dacl: {},
}
def test__virtual__not_windows():
with patch("salt.utils.platform.is_windows", autospec=True, return_value=False):
expected = (False, "Module win_file: Missing Win32 modules")
result = win_file.__virtual__()
assert result == expected
with patch("salt.modules.win_file.HAS_WINDOWS_MODULES", False):
expected = (False, "Module win_file: Missing Win32 modules")
result = win_file.__virtual__()
assert result == expected
def test__virtual__no_dacl():
with patch("salt.modules.win_file.HAS_WIN_DACL", False):
expected = (False, "Module win_file: Unable to load salt.utils.win_dacl")
result = win_file.__virtual__()
assert result == expected
def test__get_version_os():
expected = ["32-bit Windows", "Windows NT"]
result = win_file._get_version_os(0x00040004)
@ -56,6 +86,187 @@ def test__get_version_sys():
assert regex.search(result)
def test_get_pgid_error():
with pytest.raises(CommandExecutionError):
win_file.get_pgid("C:\\Path\\That\\Does\\Not\\Exist.txt")
def test_get_pgid():
"""
We can't know what this value is, so we're just making sure it found
something
"""
result = win_file.get_pgid(os.getenv("COMSPEC"))
assert not result == ""
def test_group_to_gid():
with patch.dict(win_file.__opts__, {}):
result = win_file.group_to_gid("Administrators")
expected = "S-1-5-32-544"
assert result == expected
def test_group_to_gid_empty():
with patch.dict(win_file.__opts__, {}):
result = win_file.group_to_gid("")
expected = "S-1-5-32"
assert result == expected
def test_uid_to_user():
result = win_file.uid_to_user("S-1-5-32-544")
expected = "Administrators"
assert result == expected
def test_uid_to_user_empty():
result = win_file.uid_to_user("")
expected = ""
assert result == expected
def test_user_to_uid():
result = win_file.user_to_uid("Administrator")
expected = salt.utils.win_dacl.get_sid_string("Administrator")
assert result == expected
def test_user_to_uid_none():
result = win_file.user_to_uid(None)
expected = salt.utils.win_dacl.get_sid_string(salt.utils.user.get_user())
assert result == expected
def test_get_uid():
"""
We can't know what this value is, so we're just making sure it found
something
"""
result = win_file.get_uid(os.getenv("WINDIR"))
assert not result == ""
def test_get_uid_error():
with pytest.raises(CommandExecutionError):
win_file.get_uid("C:\\fake\\path")
def test_chown(tmp_path):
test_file = tmp_path / "test_file.txt"
test_file.touch()
win_file.chown(path=str(test_file), user="Administrators", pgroup="Guests")
assert win_file.get_user(str(test_file)) == "Administrators"
assert win_file.get_pgroup(str(test_file)) == "Guests"
def test_chpgrp(tmp_path):
test_file = tmp_path / "test_file.txt"
test_file.touch()
win_file.chown(path=str(test_file), user="Administrators", pgroup="Guests")
win_file.chpgrp(path=str(test_file), group="Administrators")
assert win_file.get_pgroup(str(test_file)) == "Administrators"
def test_stats_mode(tmp_path):
test_file = tmp_path / "test_file.txt"
test_file.touch()
results = win_file.stats(str(test_file))
assert results["mode"] == "0666"
def test_is_link_true(tmp_path):
test_source = tmp_path / "test_source.txt"
test_link = tmp_path / "test_link.txt"
test_source.touch()
test_link.symlink_to(test_source)
results = win_file.is_link(str(test_link))
expected = True
assert results == expected
def test_is_link_false(tmp_path):
test_file = tmp_path / "test_not_link.txt"
test_file.touch()
results = win_file.is_link(str(test_file))
expected = False
assert results == expected
def test_mkdir(tmp_path):
test_dir = tmp_path / "test_dir"
grant_perms = {"Guests": {"perms": "full_control"}}
win_file.mkdir(
path=str(test_dir),
owner="Administrators",
grant_perms=grant_perms,
)
owner = win_file.get_user(str(test_dir))
assert owner == "Administrators"
perms = salt.utils.win_dacl.get_permissions(str(test_dir))
assert perms["Not Inherited"]["Guests"]["grant"]["permissions"] == "Full control"
def test_check_perms(tmp_path):
test_dir = tmp_path / "test_dir"
test_dir.mkdir()
grant_perms = {"Guests": {"perms": "full_control"}}
ret = {}
with patch.dict(salt.utils.win_dacl.__opts__, {"test": False}):
result = win_file.check_perms(
path=str(test_dir),
ret=ret,
owner="Guests",
grant_perms=grant_perms,
)
expected = {
"changes": {
"grant_perms": {
"Guests": {
"permissions": "full_control",
},
},
"owner": "Guests",
},
"comment": "",
"name": str(test_dir),
"result": True,
}
assert result == expected
owner = win_file.get_user(str(test_dir))
assert owner == "Guests"
perms = salt.utils.win_dacl.get_permissions(str(test_dir))
assert perms["Not Inherited"]["Guests"]["grant"]["permissions"] == "Full control"
def test_set_perms(tmp_path):
test_dir = tmp_path / "test_dir"
test_dir.mkdir()
grant_perms = {"Guests": {"perms": "full_control"}}
win_file.set_perms(
path=str(test_dir),
grant_perms=grant_perms,
)
perms = salt.utils.win_dacl.get_permissions(str(test_dir))
assert perms["Not Inherited"]["Guests"]["grant"]["permissions"] == "Full control"
def test_get_user():
"""
We can't know what this value is, so we're just making sure it found
something
"""
result = win_file.get_user(os.getenv("WINDIR"))
assert not result == ""
def test_get_user_error():
with pytest.raises(CommandExecutionError):
win_file.get_user("C:\\fake\\path")
def test_version_missing_file():
with pytest.raises(CommandExecutionError):
win_file.version("C:\\Windows\\bogus.exe")