Skip to content

Commit

Permalink
Merge pull request #65457 from s0undt3ch/hotfix/merge-forward
Browse files Browse the repository at this point in the history
[master] Merge 3006.x into master
  • Loading branch information
s0undt3ch authored Oct 27, 2023
2 parents 0cf0214 + 90666c8 commit 93b6405
Show file tree
Hide file tree
Showing 20 changed files with 528 additions and 249 deletions.
2 changes: 2 additions & 0 deletions changelog/65411.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes an issue setting user or machine policy on Windows when the Group Policy
directory is missing
2 changes: 2 additions & 0 deletions salt/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# pylint: disable=unused-import
import sys

# pragma: no cover

# The ipaddress module included in Salt is from Python 3.9.5.
# When running from Py3.9.5+ use the standard library module, use ours otherwise
if sys.version_info >= (3, 9, 5):
Expand Down
52 changes: 33 additions & 19 deletions salt/modules/win_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
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,
Expand Down Expand Up @@ -107,6 +109,15 @@
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
Expand Down Expand Up @@ -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__


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -735,9 +748,9 @@ def chown(path, user, group=None, pgroup=None, follow_symlinks=True):
if not os.path.exists(path):
raise CommandExecutionError(f"Path not found: {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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 = f"{__virtualname__}.chgrp"
if __opts__.get("fun", "") == func_name:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -1513,7 +1526,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)

Expand Down Expand Up @@ -1604,10 +1617,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,
Expand Down Expand Up @@ -1926,7 +1939,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,
Expand All @@ -1935,6 +1948,7 @@ def check_perms(
deny_perms=deny_perms,
inheritance=inheritance,
reset=reset,
test_mode=__opts__["test"],
)


Expand Down Expand Up @@ -2012,7 +2026,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,
Expand Down
Loading

0 comments on commit 93b6405

Please sign in to comment.