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.09k stars 5.47k forks source link

[BUG] Include directive in sshconfig results in parse error when using salt-ssh / sshconfig roster #62084

Open robdennis opened 2 years ago

robdennis commented 2 years ago

Description Using the sshconfig roster with salt-ssh will fail with a parse error if the sshconfig uses the "Include" directive

Setup

Steps to Reproduce the behavior

rest of the host blocks here

- run any salt-ssh command with the ``sshconfig`` roster

Traceback (most recent call last): File "/usr/bin/salt-ssh", line 11, in load_entry_point('salt==3004.1', 'console_scripts', 'salt-ssh')() File "/usr/lib/python3.6/site-packages/salt/scripts.py", line 459, in salt_ssh client.run() File "/usr/lib/python3.6/site-packages/salt/cli/ssh.py", line 21, in run ssh = salt.client.ssh.SSH(self.config) File "/usr/lib/python3.6/site-packages/salt/client/ssh/init.py", line 240, in init self.targets = self.roster.targets(self.opts["tgt"], self.tgt_type) File "/usr/lib/python3.6/site-packages/salt/roster/init.py", line 100, in targets targets.update(self.rosters[f_str](tgt, tgt_type)) File "/usr/lib/python3.6/site-packages/salt/loader/lazy.py", line 149, in call return self.loader.run(run_func, *args, kwargs) File "/usr/lib/python3.6/site-packages/salt/loader/lazy.py", line 1201, in run return self._last_context.run(self._run_as, _func_or_method, *args, *kwargs) File "/usr/lib/python3.6/site-packages/contextvars/init.py", line 38, in run return callable(args, kwargs) File "/usr/lib/python3.6/site-packages/salt/loader/lazy.py", line 1216, in _run_as return _func_or_method(*args, **kwargs) File "/usr/lib/python3.6/site-packages/salt/roster/sshconfig.py", line 99, in targets all_minions = parse_ssh_config([line.rstrip() for line in fp]) File "/usr/lib/python3.6/site-packages/salt/roster/sshconfig.py", line 56, in parse_ssh_config hosts[-1].append(line) IndexError: list index out of range

The code appears to assume that all lines are either empty, start with a comment, or start with "Host", and a line that starts with "Include" would break that assumption.

**Expected behavior**

- ideally I'd like for the ssh config parsing to collect all the files listed in the include directives and include them alongside all the other host entries
  - this is similar to what paramiko did in early 2018: https://github.com/paramiko/paramiko/pull/872
  - in the absolute worst case, ignoring include directives and not bombing out is preferred to the current behavior

**Versions Report**
<details><summary>salt --versions-report</summary>
(Provided by running salt --versions-report. Please also mention any differences in master/minion versions.) 

```yaml
Salt Version:
          Salt: 3004.1

Dependency Versions:
          cffi: Not Installed
      cherrypy: unknown
      dateutil: 2.4.2
     docker-py: Not Installed
         gitdb: 0.6.4
     gitpython: 1.0.1
        Jinja2: 2.11.1
       libgit2: Not Installed
      M2Crypto: 0.35.2
          Mako: Not Installed
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: 2.6.1
  pycryptodome: Not Installed
        pygit2: Not Installed
        Python: 3.6.8 (default, Mar 30 2020, 17:04:00)
  python-gnupg: Not Installed
        PyYAML: 3.13
         PyZMQ: 17.0.0
         smmap: 0.9.0
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.1.4

System Versions:
          dist: centos 7 Core
        locale: UTF-8
       machine: x86_64
       release: 4.4.131-1.el7.centos.x86_64
        system: Linux
       version: CentOS Linux 7 Core

welcome[bot] commented 2 years ago

Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey. Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar. If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!