salt icon indicating copy to clipboard operation
salt copied to clipboard

salt-ssh sudo_user execution not running as sudo_user

Open ghost opened this issue 10 years ago • 11 comments

Hi,

Looking for some guidance regarding the usage of the "sudo_user" in the roster file option "minion_opts". Seems like the commands are not executed as the "sudo_user" but as the connecting user.

Salt version: 2015.8

Here is my roster file:

minionid:
  host: myhost
  user: sshuser
  priv: /secret/path/to/key/sshuser_priv
  minion_opts:
    sudo_user: appuser

I can confirm that the sudo is correct configured on "myhost" since I can run;

salt-ssh myhost cmd.run 'sudo -u appuser touch /tmp/test1'

This gives me the correct permissions as appuser, however if I run;

salt-ssh my host cmd.run 'touch /tmp/test2'

I get the permission of the "sshuser" while I was expecting that the permission of the sudo_user. Please, advice if my expectation is either incorrect or if my setup is not correct.

Kind regards,

ghost avatar Feb 10 '16 07:02 ghost

@turtletraction, thanks for reporting. I've tried to reproduce this (without setting an sudo user other than the default root) and it seems to work for me. You may need to also have the sudo: True config set in your roster data. Here's my example:

jmoney-main ~ master # cat /etc/salt/roster
# -- begin jmoney-amazon-6 --
jmoney-amazon-6: {host: 54.197.67.206, priv: /root/.ssh/id_EC2, sudo: true, tty: true,
  user: ec2-user, minion_opts: {sudo_user: other-user}}

# -- end jmoney-amazon-6 --
jmoney-main ~ master # salt-ssh 'jmoney-*' -i cmd.run 'touch /tmp/test2'
jmoney-amazon-6:
jmoney-main ~ master # salt-ssh 'jmoney-*' -i cmd.run 'ls -l /tmp'
jmoney-amazon-6:
    total 4
    drwxr-xr-x 2 root root 4096 Feb 10 19:15 hsperfdata_root
    -rw------- 1 root root    0 Feb 10 19:26 test2

jfindlay avatar Feb 10 '16 19:02 jfindlay

@jfindlay Thanks for you reply. I am not really sure what you mean by setting the user: appuser

Here is my roster:

minionid:
  host: myhost
  user: sshuser
  priv: /secret/path/to/key/sshuser_priv
  minion_opts:
    sudo_user: 
      user: appuser

But the result of whoamistill returns the sshuser

salt-ssh -l debug 'myhost' cmd.run 'whoami'
myhost:
      sshuser

Setting sudo: True attempts to run the commands as root, which is not allowed in our case.

Would it be possible to raise the priority of the bug as this feature is central to our implementation?

Grateful for your help

ghost avatar Feb 11 '16 10:02 ghost

Hello!

I am seeing the same issue on my side, my tests below if they are of any use.

Normal login, verify that NOPASSWD is setup correct for normal user:

$ cat /etc/salt/roster

munin:
 host: munin.local
 user: salt
$ salt-ssh 'munin' cmd.run 'whoami'

munin:
    salt

$ salt-ssh 'munin' cmd.run 'sudo -u nfs whoami'

munin:
    nfs

Let us configure the sudo_user minion option:

$ cat /etc/salt/roster

munin:
 host: munin.local
 user: salt
 minion_opts:
   sudo_user: nfs
$ salt-ssh 'munin' cmd.run 'whoami'

munin:
    salt

Doesn't execute command as sudo_user, let's check if sudo: True is needed:

$ cat /etc/salt/roster

munin:
 host: munin.local
 user: salt
 sudo: True
 minion_opts:
   sudo_user: nfs
$ salt-ssh 'munin' cmd.run 'whoami'

munin:
    root

Executes command as root, not as the sudo_user (nfs).

I tested this against both 2015.5.3 before upgrading, as well as 2015.8.5, and encountered the issue on both.

Version reports as follows:

                 Salt: 2015.5.3
                Python: 2.7.9 (default, Mar  1 2015, 12:57:24)
                Jinja2: 2.7.3
              M2Crypto: 0.21.1
        msgpack-python: 0.1.10
          msgpack-pure: Not Installed
              pycrypto: 2.6.1
               libnacl: Not Installed
                PyYAML: 3.11
                 ioflo: Not Installed
                 PyZMQ: 14.4.0
                  RAET: Not Installed
                   ZMQ: 4.0.5
                  Mako: 1.0.0
               Tornado: Not Installed
 Debian source package: 2015.5.3+ds-1~bpo70+2
Salt Version:
           Salt: 2015.8.5

Dependency Versions:
         Jinja2: 2.7.3
       M2Crypto: 0.21.1
           Mako: 1.0.0
         PyYAML: 3.11
          PyZMQ: 14.4.0
         Python: 2.7.9 (default, Mar  1 2015, 12:57:24)
           RAET: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5
           cffi: 0.8.6
       cherrypy: Not Installed
       dateutil: 2.2
          gitdb: 0.5.4
      gitpython: 0.3.2 RC1
          ioflo: Not Installed
        libgit2: Not Installed
        libnacl: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.2
   mysql-python: 1.2.3
      pycparser: 2.10
       pycrypto: 2.6.1
         pygit2: Not Installed
   python-gnupg: Not Installed
          smmap: 0.8.2
        timelib: Not Installed

System Versions:
           dist: debian 8.3 
        machine: x86_64
        release: 4.2.3-2-pve
         system: debian 8.3 

adrianhj avatar Feb 11 '16 20:02 adrianhj

@turtletraction, I'm not sure about user: appuser either, I don't think I mentioned that. What is important that you're missing is sudo: True in your roster:

minionid:
  host: myhost
  user: sshuser
  priv: /secret/path/to/key/sshuser_priv
  sudo: True
  minion_opts:
    sudo_user: 
      user: appuser

However, @adrianhj has demonstrated that this doesn't work anyway.

jfindlay avatar Feb 12 '16 00:02 jfindlay

@jfindlay, another affect of this issue is of course that our custom grains which requires execution as user: appuser does not run. Additionally, do you have any indication when this may be solved?

@adrianhj, thanks for reproducing this on your side.

Many thanks

ghost avatar Feb 12 '16 08:02 ghost

I am not 100% comfortable with the codebase yet, so please bear with the rambling as I am not sure if my analysis/speculation is correct.. but I am guessing that the sudo_user minion option is only picked up by a minion in a master/minion setup as part of calling out to salt-call on the box (see the SudoExecutor).

As salt-ssh executes salt-call directly the sudo_user option in the minion config that is copied over as part of the tarball is never part of the code path as we bypass the setup of the salt-call execution.

I am guessing to achieve the desired functionality (executing command as a user other than the ssh login one via sudo) the sudo_user setup for salt-ssh needs to be part of the shim command that calls salt-call.

Currently the sudo: True gets picked up (and simply appends sudo before calling the base64 encoded shim that manages the unpacking/etc of the thin and then executing it via salt-call).

I am not sure then if sudo_user should be a 'first-class' option in the roster (i.e. not through minion_opts) and the sudo command then amended to sudo -u <sudo_user> in the case that this is set.

adrianhj avatar Feb 12 '16 09:02 adrianhj

@turtletraction, I am unsure, but I would like to see it solved sooner than later as well.

@adrianhj, it is very likely that salt-ssh is unaware of the relatively new executor plugin layer of salt, whereas before it was refactored into an executor, sudo_user was handled in a special, internal execution module that was called with some custom minion logic. I have not tested the executor version, which is 2016.3 branch and newer, however, since I ran my tests against 2015.8.3, so the old style is broken as well.

jfindlay avatar Feb 12 '16 19:02 jfindlay

This would be a great feature to have in salt-ssh and is a big argument in favour of Ansible (cf. http://docs.ansible.com/ansible/become.html) for users that require it.

wwentland avatar Jul 11 '16 11:07 wwentland

ZD-855

meggiebot avatar Jul 11 '16 16:07 meggiebot

I did not get a chance to properly investigate at the time, but I noticed that salt-ssh calls salt-call directly and as such bypasses the SudoExecutor used by minions.

I do not have access to the code changes right now as they are on locked-down systems (and it was some time ago), however the logic for my workaround for the PoC that was being implemented in case it is of help was as follows:

  • Add a sudo_user key in my roster specifying the sudo_user for the relevant entry
  • When preparing the cmd to call salt-call in salt/client/ssh/init.py check whether sudo was set to True for the roster entry
    • If it was check if sudo_user was set as well for the roster entry
      • If sudo_user is set, set sudo command to 'sudo -u {0}'.format(self.target['sudo_user'])
      • If not set, set sudo command to sudo
  • In the ssh sh shim, update the sudo check to set the SUDO variable to the correct sudo command which gets passed in to the formatter as opposed to having it hardcoded to sudo to root.

The chmod of the trans_tar that gets sent had to be changed to 640 to make sure it was readable by the sudo user after transfer, as well as ensuring that the sudo_user user was a member of the ssh users group (as the thin directory is deployed as the ssh user if I remember correctly and the sudo_user need to be able to read these files).

I originally tried using the sudo_user entry from the minion_opts set in the roster but this would not work when executing states (minion_opts would not contain the sudo_user entry), I see that there have been some changes in state.py to the way that salt.client.ssh.Single gets passed minion_opts which may mean that this is now possible but I have not verified.

adrianhj avatar Jul 11 '16 19:07 adrianhj

Hi all. Please see #35211.

cachedout avatar Aug 04 '16 18:08 cachedout