Skip to content

Conversation

@boltronics
Copy link
Contributor

@boltronics boltronics commented Dec 15, 2025

What does this PR do?

Fixes the exception described in issue #62049, triggered when the ssh_known_hosts.present state module is used with the user argument, but the user account specified is not present.

What issues does this PR fix or reference?

Fixes #62049

Previous Behavior

Here is an example state sls file:

Add github.com SSH host-key to baduser known_hosts:
  ssh_known_hosts.present:
    - name: github.com
    - user: baduser
    - enc: ecdsa
    - fingerprint: 'a7:64:00:31:73:48:0b:54:c9:61:67:88:3a:db:6b:55:cf:7c:fd:1d:41:50:55:ae:df:f2:e2:c8:a8:14:7d:03'
    - port: 22
    - fingerprint_hash_type: sha256

If the user baduser does not exist, this will happen:

$ salt-call -c local/etc/salt state.sls ssh_known_hosts
[ERROR   ] An exception occurred in this state: Traceback (most recent call last):
  File "/home/abolte/salt/salt/state.py", line 2231, in call
    ret = self.states[cdata["full"]](
  File "/home/abolte/salt/salt/loader/lazy.py", line 162, in __call__
    ret = self.loader.run(run_func, *args, **kwargs)
  File "/home/abolte/salt/salt/loader/lazy.py", line 1291, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/home/abolte/salt/salt/loader/lazy.py", line 1306, in _run_as
    ret = _func_or_method(*args, **kwargs)
  File "/home/abolte/salt/salt/loader/lazy.py", line 1342, in wrapper
    return f(*args, **kwargs)
  File "/home/abolte/salt/salt/states/ssh_known_hosts.py", line 165, in present
    result = __salt__["ssh.set_known_host"](
  File "/home/abolte/salt/salt/loader/lazy.py", line 162, in __call__
    ret = self.loader.run(run_func, *args, **kwargs)
  File "/home/abolte/salt/salt/loader/lazy.py", line 1291, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/home/abolte/salt/salt/loader/lazy.py", line 1306, in _run_as
    ret = _func_or_method(*args, **kwargs)
  File "/home/abolte/salt/salt/modules/ssh.py", line 1119, in set_known_host
    [h["key"] for h in stored_host_entries if enc is None or h["enc"] == enc]
  File "/home/abolte/salt/salt/modules/ssh.py", line 1119, in <listcomp>
    [h["key"] for h in stored_host_entries if enc is None or h["enc"] == enc]
TypeError: string indices must be integers

local:
----------
          ID: Add github.com SSH host-key to baduser known_hosts
    Function: ssh_known_hosts.present
        Name: github.com
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/home/abolte/salt/salt/state.py", line 2231, in call
                  ret = self.states[cdata["full"]](
                File "/home/abolte/salt/salt/loader/lazy.py", line 162, in __call__
                  ret = self.loader.run(run_func, *args, **kwargs)
                File "/home/abolte/salt/salt/loader/lazy.py", line 1291, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "/home/abolte/salt/salt/loader/lazy.py", line 1306, in _run_as
                  ret = _func_or_method(*args, **kwargs)
                File "/home/abolte/salt/salt/loader/lazy.py", line 1342, in wrapper
                  return f(*args, **kwargs)
                File "/home/abolte/salt/salt/states/ssh_known_hosts.py", line 165, in present
                  result = __salt__["ssh.set_known_host"](
                File "/home/abolte/salt/salt/loader/lazy.py", line 162, in __call__
                  ret = self.loader.run(run_func, *args, **kwargs)
                File "/home/abolte/salt/salt/loader/lazy.py", line 1291, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "/home/abolte/salt/salt/loader/lazy.py", line 1306, in _run_as
                  ret = _func_or_method(*args, **kwargs)
                File "/home/abolte/salt/salt/modules/ssh.py", line 1119, in set_known_host
                  [h["key"] for h in stored_host_entries if enc is None or h["enc"] == enc]
                File "/home/abolte/salt/salt/modules/ssh.py", line 1119, in <listcomp>
                  [h["key"] for h in stored_host_entries if enc is None or h["enc"] == enc]
              TypeError: string indices must be integers
     Started: 14:41:16.140516
    Duration: 16.238 ms
     Changes:   

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:  16.238 ms

New Behavior

$ salt-call -c local/etc/salt state.sls ssh_known_hosts
[ERROR   ] User baduser does not exist
local:
----------
          ID: Add github.com SSH host-key to baduser known_hosts
    Function: ssh_known_hosts.present
        Name: github.com
      Result: False
     Comment: User baduser does not exist
     Started: 14:38:13.350626
    Duration: 4.048 ms
     Changes:   

Summary for local
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   4.048 ms

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

twangboy
twangboy previously approved these changes Dec 16, 2025
@twangboy twangboy added the test:full Run the full test suite label Dec 16, 2025
@twangboy twangboy added this to the Sulfur v3006.18 milestone Dec 16, 2025
@dwoz dwoz merged commit 08c6e2e into saltstack:3006.x Dec 18, 2025
796 of 799 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants