Bug 62940 fix cron inconsistency in _write_cron_lines

This commit is contained in:
Luděk Finstrle 2022-10-24 11:31:57 +02:00
parent 609e5e1837
commit 0fe1954b91

View file

@ -266,11 +266,8 @@ def _write_cron_lines(user, lines):
"""
lines = [salt.utils.stringutils.to_str(_l) for _l in lines]
path = salt.utils.files.mkstemp()
if _check_instance_uid_match("root") or __grains__.get("os_family") in (
"Solaris",
"AIX",
):
# In some cases crontab command should be executed as user rather than root
# Some OS' do not support specifying user via the `crontab` command
if __grains__.get("os_family") in ("Solaris", "AIX"):
with salt.utils.files.fpopen(
path, "w+", uid=__salt__["file.user_to_uid"](user), mode=0o600
) as fp_:
@ -278,10 +275,25 @@ def _write_cron_lines(user, lines):
ret = __salt__["cmd.run_all"](
_get_cron_cmdstr(path), runas=user, python_shell=False
)
else:
# If Salt is running from same user as requested in cron module we don't need any user switch
elif _check_instance_uid_match(user):
with salt.utils.files.fpopen(path, "w+", mode=0o600) as fp_:
fp_.writelines(lines)
ret = __salt__["cmd.run_all"](_get_cron_cmdstr(path), python_shell=False)
# If Salt is running from root user it could modify any user's crontab
elif _check_instance_uid_match("root"):
with salt.utils.files.fpopen(path, "w+", mode=0o600) as fp_:
fp_.writelines(lines)
ret = __salt__["cmd.run_all"](_get_cron_cmdstr(path, user), python_shell=False)
# Edge cases here, let's try do a runas
else:
with salt.utils.files.fpopen(
path, "w+", uid=__salt__["file.user_to_uid"](user), mode=0o600
) as fp_:
fp_.writelines(lines)
ret = __salt__["cmd.run_all"](
_get_cron_cmdstr(path), runas=user, python_shell=False
)
os.remove(path)
return ret