Fix comment_line permissions.

In order to preserve file permissions when comment/uncomment file
we should run check_perms before and after editing files.

Fixes #28320.
This commit is contained in:
abednarik 2016-01-24 11:26:10 -03:00
parent 8e56be7f4c
commit 8b395a42cb

View file

@ -1097,7 +1097,7 @@ def comment_line(path,
path = os.path.realpath(os.path.expanduser(path))
# Make sure the file exists
if not os.path.exists(path):
if not os.path.isfile(path):
raise SaltInvocationError('File not found: {0}'.format(path))
# Make sure it is a text file
@ -1139,6 +1139,11 @@ def comment_line(path,
if not found:
return False
if not salt.utils.is_windows():
pre_user = get_user(path)
pre_group = get_group(path)
pre_mode = __salt__['config.manage_mode'](get_mode(path))
# Create a copy to read from and to use as a backup later
try:
temp_file = _mkstemp_copy(path=path, preserve_inode=False)
@ -1191,6 +1196,9 @@ def comment_line(path,
"Exception: {2}".format(path, temp_file, exc)
)
if not salt.utils.is_windows():
check_perms(path, None, pre_user, pre_group, pre_mode)
# Return a diff using the two dictionaries
return ''.join(difflib.unified_diff(orig_file, new_file))