salt icon indicating copy to clipboard operation
salt copied to clipboard

Salt-SSH JSON render failed - dumping password as string in output?

Open Xiol opened this issue 11 years ago • 16 comments

Hi,

Bit of a strange one here and I've not been able to get to the bottom of it - or figured out how to reliably replicate it.

Salt-SSH occasionally gets itself into a state where it will randomly output the password used for SSHing to the server into the JSON output it's trying to render, causing a render failure. When it's doing this it will always output the password at the end of the JSON output, but I've also found that it appears random in the output. For example:

[ERROR   ] JSON Render failed for: {
    "local": {
        "fun_args": [
            "/var/lib/acme/salt//salt_state.tgz", 
            "test=None", 
            "pkg_sum=953bb413acfa01c1e9a65c7e5258f449", 
            "hash_type=md5"
        ], 
        "jid": "20151204095708560039", 
        "return": {
   ... snip ...
            "file_|-/var/www/vhosts_|-/var/www/vhosts_|-directory": {
                "comment": "Directory /var/www/vhosts is in the correct state", THEPASSWORD

                "name": "/var/www/vhosts", 
                "start_time": "09:57:34.040038", 
                "result": true, 
                "duration": 0.823, 
                "__run_num__": 52, 
                "changes": {}
            }, 
    ... snip ...
        }, 
        "retcode": 0, 
        "fun": "state.pkg", 
        "id": "SID1000000", 
        "out": "highstate"
    }
}
THEPASSWORD

I've tried removing the thin dir on the remote, and also disabling keeping the thin dir just in case, but it still happens. I'm not sure what triggers it but I'm unable to make it stop doing this! There's nothing else that stands out in the debug output that I believe it related to this.

Versions info:

Salt Version:
           Salt: 2015.8.3

Dependency Versions:
         Jinja2: 2.8
       M2Crypto: Not Installed
           Mako: 1.0.2
         PyYAML: 3.11
          PyZMQ: Not Installed
         Python: 2.7.10 (default, Oct 14 2015, 16:09:02)
           RAET: Not Installed
        Tornado: 4.2.1
            ZMQ: Not Installed
           cffi: 1.1.2
       cherrypy: Not Installed
       dateutil: 2.2
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
        libnacl: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: 1.3.4
      pycparser: 2.14
       pycrypto: 2.6.1
         pygit2: Not Installed
   python-gnupg: Not Installed
          smmap: Not Installed
        timelib: Not Installed

System Versions:
           dist: Ubuntu 15.10 wily
        machine: x86_64
        release: 4.2.0-19-generic
         system: Ubuntu 15.10 wily

Xiol avatar Dec 04 '15 10:12 Xiol

@Xiol, thanks for the report. Is there any other information you can provide to help narrow this down? Did it only start to happen after upgrading to 2015.8.3? Does it happen only with the json outputter? Does it only output passwords and not other auth tokens, such as the name of a private key file? This may be the result of a stray print statement, but a quick search shows nothing obvious.

jfindlay avatar Dec 04 '15 20:12 jfindlay

I've seen this happen on all 2015.8.x versions. It doesn't matter what outputter I'm using as I'll still get JSON render failures, even when forcing the YAML outputter - using JSON output results in a JSON string encoded inside JSON, YAML output results in JSON-encoded YAML strings and no selected outputter gives JSON-like output, with the exception that the passwords are dumped in the output, preventing it from being true JSON.

I've not tried with pubkeys as some of our other legacy systems don't support pubkey authentication (yet) so we're stuck with passwords I'm afraid.

I managed to fix this problem by fixing some states that were failing. This is the complete diff between the formula that was outputting the passwords and the working one which doesn't, hopefully it'll narrow down where to look?

  • The segfault_fix command that was run was failing on a missing file (as we target CentOS7 and that was assuming SysV not systemd).
  • php-pear pkg install was also failing as the package was installed from a different repo and using a different version (e.g. not CentOS7 php-pear but php56u-pear from IUS and as php56u-pear was already installed, attempting to install php-pear was causing a yum conflict error).
  • The MySQL user and name options were wrong.

Also, no pillar data containing the password for the server (as outputted) was used in any states.

Xiol avatar Dec 08 '15 15:12 Xiol

@Xiol, that is exceedingly strange. Thanks for the extra information.

jfindlay avatar Dec 08 '15 22:12 jfindlay

I've been banging my head against this problem again and I've managed to get a bit further with debugging it, although I'm no closer to finding the root cause.

I've narrowed the problem down to the following state. If I comment this state out, it works fine. With this state active, it fails and dumps out the root SSH password (which is different to any passwords used in this state!)

{% for host in ['127.0.0.1', 'localhost', '::1'] %}
setup_mysql_root_user_for_{{ host | replace(':', 'c') }}:
  mysql_user.present:
    - name: root
    - password: {{ pillar['magento_stack_mysql_root_password'] }}
    - host: {{ host }}
    - connection_host: 127.0.0.1
    - connection_user: root
{% if not host == "127.0.0.1" or salt['helpers.is_path_exists']('/tmp/.acme_db_user_created') %}
    - connection_password: {{ pillar['magento_stack_mysql_root_password'] }}
{% endif %}
    - require:
      - cmd: mysql_upgrade
{% endfor %}

Looking at the debug output, this is templated correctly into the following states:

# This one works if run alone
setup_mysql_root_user_for_127.0.0.1:
  mysql_user.present:
    - name: root
    - password: RootPassw0rd1
    - host: 127.0.0.1
    - connection_host: 127.0.0.1
    - connection_user: root
    - connection_password: RootPassw0rd1

# This one works if run alone
setup_mysql_root_user_for_localhost:
  mysql_user.present:
    - name: root
    - password: RootPassw0rd1
    - host: localhost
    - connection_host: 127.0.0.1
    - connection_user: root
    - connection_password: RootPassw0rd1

# This does not work
setup_mysql_root_user_for_cc1:
  mysql_user.present:
    - name: root
    - password: RootPassw0rd1
    - host: ::1
    - connection_host: 127.0.0.1
    - connection_user: root
    - connection_password: RootPassw0rd1

So, the next step was to run each of those generated states individually. I've commented above on which ones worked and which ones didn't. It looks like when we're trying to create the user root@::1, it is unable to complete. I've also tried it by quoting the host entry ('::1') but that doesn't help either. Something dumps out the root password into the JSON stream when this fails:

[ERROR   ] JSON Render failed for: {
    "local": {
        "fun_args": [
            "/tmp/.root_becb82_salt/salt_state.tgz", 
            "test=None", 
            "pkg_sum=9644255a180f3b334569dd6943a5fe76", 
            "hash_type=md5"
        ], 
        "jid": "20160120162936477217", 
        "return": {
            "mysql_user_|-setup_mysql_root_user_for_127.0.0.1_|-root_|-present": {
                "comment": "User [email protected] is already present with the desired password", 
                "name": "root", 
                "start_time": "16:29:36.524645", 
                "result": true, 
                "duration": 4.945, 
                "__run_num__": 0, 
                "changes": {}
            }, 
            "mysql_user_|-setup_mysql_root_user_for_cc1_|-root_|-present": {
                "comment": "MySQL Error 1045: Access denied for user 'root'@'localhost' (using password: NO)", 
                "name": "root", 
                "start_time": "16:29:36.531760", 
                "result": false, 
                "duration": 35.487, 
                "__run_num__": 2, 
                "changes": {}
            }, 
            "mysql_user_|-setup_mysql_root_user_for_localhost_|-root_|-present": {
                "comment": "User root@localhost is already present with the desired password", 
                "name": "root", 
                "start_time": "16:29:36.529726", 
                "result": true, 
                "duration": 1.896, 
                "__run_num__": 1, 
                "changes": {}
            }
        }, 
        "retcode": 0, 
        "fun": "state.pkg", 
        "id": "SID1000000", 
        "out": "highstate"
    }
}
THESSHROOTPASSWORD

Thanks

Xiol avatar Jan 20 '16 16:01 Xiol

@Xiol, thanks for your investigations on this issue.

jfindlay avatar Jan 20 '16 18:01 jfindlay

Any further findings on this? I've seen similar behaviour when using salt ssh and having the plaintext passwords inadvertently dumped out is causing me some PCI related headaches

zyio avatar Apr 05 '16 09:04 zyio

Confirmed we're still seeing this issue on the latest develop branch as well.

Happy to provide the full state we're running along with trace logs and (potentially) a server to run it against if you're interested? Would prefer to send you the details off-issue though.

Xiol avatar Apr 05 '16 10:04 Xiol

@Xiol, is the password that is being output the same one used in your roster file? Does it make a difference if you authenticate with ssh keys?

jfindlay avatar Apr 05 '16 17:04 jfindlay

@jfindlay, yeah it's the same password from the roster file. It's not used explicitly in the state itself at any point so it's not like we're echoing it out ourselves or anything.

Unfortunately our use case precludes the use of SSH keys (something we're working towards but not any time soon). I can give it a try though and report back tomorrow, if only to eliminate that line of inquiry.

Xiol avatar Apr 05 '16 20:04 Xiol

Right, I think I may have found out why this is happening.

In salt/client/ssh/shell.py on L23 you have an SSH password prompt regex for matching the prompt.

On L356 you use the regex to search the output for the prompt, then sending the password on L360 for several tries (hence why we see the password in multiple places in the output).

The reason I think it's this is because we have a similar issue where, despite being able to connect to the server fine, we're getting 'Password authentication failed' along with the password in the output, which happens on L365. I'm assuming when we get a full result back rather than that error, but with the root passwords in the output, it's just because the state has completed before the maximum retries are reached.

The reason it always seems to be the MySQL states that cause this problem is because the output from MySQL can contain the word 'password', e.g.:

"MySQL Error 1045: Access denied for user 'root'@'127.0.0.1' (using password: YES)"

This is matched by the regex, and Salt then tries to send over the root password repeatedly before timing out.

Xiol avatar Apr 27 '16 09:04 Xiol

Assume this means that MySQL execution is a command being run vs Using a Python Library to communicate to MySQL. ?????

And I assume salt runs the ssh binary and hence needs to scan for password prompt some times vs using a native ssh python library ???

damon-atkins avatar Apr 27 '16 10:04 damon-atkins

This seems to be triggering on our use of the mysql_user state.

I added a log line at L360, so when the regex matches and it sends the password over, it dumps the buff variable so we can see what it's matching on:

BUFFDUMP: s... yes\nchecking whether to build shared libraries... yes\nchecking whether to build static libraries... no\nconfigure: creating ./config.status\nconfig.
status: creating config.h\nconfig.status: executing libtool commands\nrunning: make\n/bin/sh /var/tmp/pear-build-rootUGb0Zr/memcache-3.0.8/libtool --mode=compile cc -I/usr/include/php -I. -I/var/tmp/memcach
e -DPHP_ATOM_INC -I/var/tmp/pear-build-rootUGb0Zr/memcache-3.0.8/include -I/var/tmp/pear-build-rootUGb0Zr/memcache-3.0.8/main -I/var/tmp/memcache -I/usr/include/php -I/usr/include/php/main -I/usr/include/ph
p/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /var/tmp/memcache/memcache.c -o memcache.lo\nlibtool: compile:  cc -I/usr/include/php -I. 
-I/var/tmp/memcache -DPHP_ATOM_INC -I/var/tmp/pear-build-rootUGb0Zr/memcache-3.0.8/include -I/var/tmp/pear-build-rootUGb0Zr/memcache-3.0.8/main -I/var/tmp/memcache -I/usr/include/php -I/usr/include/php/main
 -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /var/tmp/memcache/memcache.c  -fPIC -DPIC -o .libs/memcache.o\nIn file inclu
ded from /var/tmp/memcache/php_memcache.h:34:0,\n                 from /var/tmp/memcache/memcache.c:30:\n/var/tmp/memcache/memcache_pool.h:45:47: fatal error: ext/standard/php_smart_str_public.h: No such fi
le or directory\n #include \"ext/standard/php_smart_str_public.h\"\n                                               ^\ncompilation terminated.\nmake: *** [memcache.lo] Error 1\nERROR: `make' failed"
                }, 
                "__id__": "install_memcache_beta_pecl"
            }, 
            "mysql_user_|-create_mysql_user_for_web_server_|-testuser_|-present": {
                "comment": "MySQL Error 1045: Access denied for user 'root'@'127.0.0.1' (using password: YES)", 
                "name": "testuser", 
                "start_time": "12:02:53.078984", 
                "result": false, 
                "duration": 0.835, 
     ENDBUFFDUMP

It seems to match on the JSON bit in buff?

The state that's being executed here is:

create_mysql_user_for_web_server:
  mysql_user.present:
    - name: {{ pillar['magento_domain_user']}}
    - host: {{ pillar['magento_stack_web_server_ip'] }}
    - password: {{ pillar['magento_domain_mysql_password'] | yaml_encode }}
    - connection_user: root
    - connection_host: {{ pillar['magento_domain_mysql_host'] }}
{% if pillar['magento_stack_mysql_root_password'] is defined %}
    - connection_pass: {{ pillar['magento_stack_mysql_root_password'] | yaml_encode }}
{% endif %}

Obviously we have a fault in our state logic here which I'll look at, but the issue is that the regex match is triggering the root password to be sent when it's not needed.

If my understanding of Salt SSH is correct, this is the shim returning data to the server. Shouldn't the regex matching be disabled at this point as the state run is complete?

Xiol avatar Apr 27 '16 11:04 Xiol

@Xiol, excellent detective work here. I think this shouldn't be too hard to fix.

jfindlay avatar Apr 27 '16 15:04 jfindlay

@Xiol if you wish to run an experiment

/usr/bin/salt -l debug UnixBox  cmd.run stdin='hello' cmd='bash -c "read  -t 5 -p  Password: pas ;echo My \$pas"'
/usr/bin/salt -l debug UnixBox  cmd.run  cmd='bash -c "read  -t 5 -p  Password: pas ;echo My \$pas"'

Note the double followed by single quote on the end of the command

damon-atkins avatar Apr 27 '16 18:04 damon-atkins

@damon-atkins

With an additional log line on L360 in salt/client/ssh/shell.py like so:

if buff and SSH_PASSWORD_PROMPT_RE.search(buff):
    if not self.passwd:
        return '', 'Permission denied, no authentication information', 254
    if sent_passwd < passwd_retries:
        log.warning("BUFF: Sending password")    # <-----
        term.sendline(self.passwd)
        sent_passwd += 1
        continue

The output from your two commands - using salt-ssh rather than salt - is as follows. Note that I've removed some of the debug output I don't think was relevant, but if you would like it all please let me know.

Roster file:

testhost:
  host: 1.2.3.4
  port: 2020
  user: root
  passwd: RootPasswordHere
  sudo: false
  tty: true
  timeout: 60
  thin_dir: /var/lib/acmecorp/salt/

Test one:

root@ed-dev ~ # salt-ssh 'testhost' -l debug --roster-file mageroster.txt cmd.run stdin='hello' cmd='bash -c "read -t 5 -p Password: pas ;echo My \$pas"'
[DEBUG   ] LazyLoaded local_cache.prep_jid
[DEBUG   ] Adding minions for job 20160429093128246136: ['testhost']
[DEBUG   ] Could not LazyLoad cmd.run
[DEBUG   ] Performing shimmed, blocking command as follows:
cmd.run stdin=hello cmd=bash -c "read -t 5 -p Password: pas ;echo My \$pas"
[DEBUG   ] Executing command: scp   -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /tmp/shim_VcB601 1.2.3.4:.d2d2bf5dd915
[DEBUG   ] Child Forked! PID: 13985  STDOUT_FD: 9  STDERR_FD: 11
[DEBUG   ] Terminal Command: /bin/sh -c scp   -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /tmp/shim_VcB601 1.2.3.4:.d2d2bf5dd915
[WARNING ] BUFF: Sending password
[DEBUG   ] Executing command: ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /bin/sh '$HOME/.d2d2bf5dd915'
[DEBUG   ] Child Forked! PID: 13988  STDOUT_FD: 9  STDERR_FD: 11
[DEBUG   ] Terminal Command: /bin/sh -c ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /bin/sh '$HOME/.d2d2bf5dd915'
[WARNING ] BUFF: Sending password
[WARNING ] BUFF: Sending password
[WARNING ] BUFF: Sending password
[DEBUG   ] Executing command: ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  rm '$HOME/.d2d2bf5dd915'
[DEBUG   ] Child Forked! PID: 13990  STDOUT_FD: 9  STDERR_FD: 11
[DEBUG   ] Terminal Command: /bin/sh -c ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  rm '$HOME/.d2d2bf5dd915'
[WARNING ] BUFF: Sending password
[DEBUG   ] RETCODE 1.2.3.4: 0
[DEBUG   ] SHIM retcode(0) and command: RootPasswordHere
[DEBUG   ] LazyLoaded nested.output
testhost:
    ----------
    retcode:
        0
    stderr:
        Connection to 1.2.3.4 closed.
    stdout:
        RootPasswordHere
        {
            "local": {
                "fun_args": [
                    "stdin=hello", 
                    "cmd=bash -c \"read -t 5 -p Password: pas ;echo My \\$pas\""
                ], 
                "jid": "20160429092809459028", 
                "return": "My hello", 
                "retcode": 0, 
                "fun": "cmd.run", 
                "id": "testhost"
            }
        }
        RootPasswordHere

Test two:

root@ed-dev ~ # salt-ssh 'testhost' -l debug --roster-file mageroster.txt cmd.run cmd='bash -c "read -t 5 -p Password: pas ;echo My \$pas"'
[DEBUG   ] LazyLoaded local_cache.prep_jid
[DEBUG   ] Adding minions for job 20160429094007864525: ['testhost']
[DEBUG   ] Could not LazyLoad cmd.run
[DEBUG   ] Performing shimmed, blocking command as follows:
cmd.run cmd=bash -c "read -t 5 -p Password: pas ;echo My \$pas"
[DEBUG   ] Executing command: scp   -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /tmp/shim_T8aJgN 1.2.3.4:.ee1dffd05e0e
[DEBUG   ] Child Forked! PID: 14151  STDOUT_FD: 9  STDERR_FD: 11
[DEBUG   ] Terminal Command: /bin/sh -c scp   -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /tmp/shim_T8aJgN 1.2.3.4:.ee1dffd05e0e
[WARNING ] BUFF: Sending password
[DEBUG   ] Executing command: ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /bin/sh '$HOME/.ee1dffd05e0e'
[DEBUG   ] Child Forked! PID: 14154  STDOUT_FD: 9  STDERR_FD: 11
[DEBUG   ] Terminal Command: /bin/sh -c ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  /bin/sh '$HOME/.ee1dffd05e0e'
[WARNING ] BUFF: Sending password
[WARNING ] BUFF: Sending password
[WARNING ] BUFF: Sending password
[DEBUG   ] Executing command: ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  rm '$HOME/.ee1dffd05e0e'
[DEBUG   ] Child Forked! PID: 14156  STDOUT_FD: 9  STDERR_FD: 11
[DEBUG   ] Terminal Command: /bin/sh -c ssh 1.2.3.4 -t -t -o KbdInteractiveAuthentication=no -o PasswordAuthentication=yes -o GSSAPIAuthentication=no -o ConnectTimeout=60 -o Port=2020 -o IdentityFile=/etc/salt/pki/master/ssh/salt-ssh.rsa -o User=root  rm '$HOME/.ee1dffd05e0e'
[WARNING ] BUFF: Sending password
[DEBUG   ] RETCODE 1.2.3.4: 0
[DEBUG   ] SHIM retcode(0) and command: RootPasswordHere
[DEBUG   ] LazyLoaded nested.output
testhost:
    ----------
    retcode:
        0
    stderr:
        Connection to 1.2.3.4 closed.
    stdout:
        RootPasswordHere
        {
            "local": {
                "fun_args": [
                    "cmd=bash -c \"read -t 5 -p Password: pas ;echo My \\$pas\""
                ], 
                "jid": "20160429094008844089", 
                "return": "My echo \"ERROR: Unable to locate appropriate python command\" >&2", 
                "retcode": 0, 
                "fun": "cmd.run", 
                "id": "testhost"
            }
        }
        RootPasswordHere

Xiol avatar Apr 29 '16 08:04 Xiol

I've opened a pull request with a fix. It's a little naive as I've copied in the RSTR stuff from salt/client/ssh/__init__.py - wasn't sure how to go about accessing those variables from shell.py directly without a circular import.

Thanks

Xiol avatar May 11 '16 11:05 Xiol