saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Get access to the Salt software package repository here:
https://repo.saltproject.io/
Apache License 2.0
14.16k stars 5.48k forks source link

[BUG] ssh_known_hosts.present excpetion when specified user not present #62049

Open boltronics opened 2 years ago

boltronics commented 2 years ago

Description Using the ssh_known_hosts.present state module with the - user: argument blows up with an unhelpful TypeError: string indices must be integers exception when the supplied user does not actually exist.

Setup No special setup required. Just specify a user that doesn't actually exist, and you'll get the following:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/salt/state.py", line 2154, in call
    *cdata["args"], **cdata["kwargs"]
  File "/usr/lib/python3/dist-packages/salt/loader.py", line 2106, in wrapper
    return f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/salt/states/ssh_known_hosts.py", line 177, in present
    fingerprint_hash_type=fingerprint_hash_type,
  File "/usr/lib/python3/dist-packages/salt/modules/ssh.py", line 1114, in set_known_host
    stored_keys = [h["key"] for h in stored_host_entries] if stored_host_entries else []
  File "/usr/lib/python3/dist-packages/salt/modules/ssh.py", line 1114, in <listcomp>
    stored_keys = [h["key"] for h in stored_host_entries] if stored_host_entries else []
TypeError: string indices must be integers

Steps to Reproduce the behavior

Add {{ host }} ({{ host_fqdn }}) SSH host-key to baduser known_hosts:
  ssh_known_hosts.present:
    - name: {{ host_fqdn }}
    - user: baduser
    - enc: ecdsa
    - fingerprint: '{{ fingerprint }}'
    - port: 22
    - fingerprint_hash_type: sha256

Everything works as expected if I replace "baduser" with the valid user "root", for example.

Expected behavior An error that the user does not exist.

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.) ```yaml Salt Version: Salt: 3002.8 Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: 2.7.3 docker-py: Not Installed gitdb: 2.0.5 gitpython: 2.1.11 Jinja2: 2.10 libgit2: Not Installed M2Crypto: Not Installed Mako: Not Installed msgpack: 0.5.6 msgpack-pure: Not Installed mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pycryptodome: 3.6.1 pygit2: Not Installed Python: 3.7.3 (default, Jan 22 2021, 20:04:44) python-gnupg: 0.4.4 PyYAML: 3.13 PyZMQ: 17.1.2 smmap: 2.0.5 timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.3.1 System Versions: dist: debian 10 buster locale: UTF-8 machine: x86_64 release: 4.19.0-17-cloud-amd64 system: Linux version: Debian GNU/Linux 10 buster ```

Additional context I could not tell what the issue was when I ran into this exception without some investigation, which are additional steps that should not be necessary.

Thanks.

Ch3LL commented 1 year ago

I'm able to confirm this behavior on the head of master.