-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix cmd.run on MacOS -- wrong environment variables #54079
Changes from all commits
cb4ce13
92c1801
3cfaa35
cbdb8b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -407,16 +407,27 @@ def _get_stripped(cmd): | |
return win_runas(cmd, runas, password, cwd) | ||
|
||
if runas and salt.utils.platform.is_darwin(): | ||
# we need to insert the user simulation into the command itself and not | ||
# just run it from the environment on macOS as that | ||
# method doesn't work properly when run as root for certain commands. | ||
# We need to insert the user simulation into the command itself and not | ||
# just run it from the environment on macOS as that method doesn't work | ||
# properly when run as root for certain commands. | ||
if isinstance(cmd, (list, tuple)): | ||
cmd = ' '.join(map(_cmd_quote, cmd)) | ||
|
||
cmd = 'su -l {0} -c "{1}"'.format(runas, cmd) | ||
# set runas to None, because if you try to run `su -l` as well as | ||
# simulate the environment macOS will prompt for the password of the | ||
# user and will cause salt to hang. | ||
# Ensure directory is correct before running command | ||
cmd = 'cd -- {dir} && {{ {cmd}\n }}'.format(dir=_cmd_quote(cwd), cmd=cmd) | ||
|
||
# Ensure environment is correct for a newly logged-in user by running | ||
# the command under bash as a login shell | ||
cmd = '/bin/bash -l -c {cmd}'.format(cmd=_cmd_quote(cmd)) | ||
|
||
# Ensure the login is simulated correctly (note: su runs sh, not bash, | ||
# which causes the environment to be initialised incorrectly, which is | ||
# fixed by the previous line of code) | ||
cmd = 'su -l {0} -c {1}'.format(_cmd_quote(runas), _cmd_quote(cmd)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cmd = [ 'su', '-l', runas, '-c', 'cd '{0}' && {1}'.format(_cmd_quote(cwd), cmd) ] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The command is coerced to a string before executing, so either case is the same. I have covered this specific exploit in the integration test |
||
|
||
# Set runas to None, because if you try to run `su -l` after changing | ||
# user, su will prompt for the password of the user and cause salt to | ||
# hang. | ||
runas = None | ||
|
||
if runas: | ||
|
@@ -459,7 +470,7 @@ def _get_stripped(cmd): | |
'sys.stdout.write(\"' + marker + '\");' | ||
) | ||
|
||
if use_sudo or __grains__['os'] in ['MacOS', 'Darwin']: | ||
if use_sudo: | ||
env_cmd = ['sudo'] | ||
# runas is optional if use_sudo is set. | ||
if runas: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, great work!
I have a question related with this line. What does happen if my default shell is
zsh
for example and all my settings are provided in the~/.zshrc
file instead of the~/.bash_profile
one?Will this change load my
PATH
or any otherexports
?This is an important point to take into account since starting with macOS Catalina, the default shell for new users will be
zsh
. More info at: https://support.apple.com/en-us/HT208050There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an example, when the following command is executed:
this is the result with line
cmd = '/bin/bash -l -c {cmd}'.format(cmd=_cmd_quote(cmd))
uncommentedand this is the result with the same line commented:
As you can see, with that line commented
zsh
stills getting the right environment, so a more specific solution is required forbash
shells instead of making it the default solution.