Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/62049.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a TypeError exception thrown by ssh_known_hosts.present when the specified user account does not exist
5 changes: 5 additions & 0 deletions salt/modules/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ def check_known_host(
port=port,
fingerprint_hash_type=fingerprint_hash_type,
)
if known_host_entries and "error" in known_host_entries:
return known_host_entries
known_keys = [h["key"] for h in known_host_entries] if known_host_entries else []
known_fingerprints = (
[h["fingerprint"] for h in known_host_entries] if known_host_entries else []
Expand Down Expand Up @@ -1117,6 +1119,9 @@ def set_known_host(
port=port,
fingerprint_hash_type=fingerprint_hash_type,
)
if stored_host_entries and "error" in stored_host_entries:
return stored_host_entries

stored_keys = (
[h["key"] for h in stored_host_entries if enc is None or h["enc"] == enc]
if stored_host_entries
Expand Down
4 changes: 4 additions & 0 deletions salt/states/ssh_known_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ def present(
ret["comment"] = f"ssh.check_known_host error: {err}"
return ret

if isinstance(result, dict) and "error" in result:
ret["result"] = False
ret["comment"] = result["error"]
return ret
if result == "exists":
comment = f"Host {name} is already in {config}"
ret["result"] = True
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/modules/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ def test_check_known_host_exists(self):
)
self.assertEqual(ret, "exists")

@pytest.mark.slow_test
def test_check_known_host_get_known_host_entries_error(self):
"""
Return the error from get_known_host_entries, if supplied
"""
arg = ["baduser", "github.com"]
ret = self.run_function("ssh.check_known_host", arg)
assert "error" in ret
assert "User baduser does not exist" in ret["error"]

@pytest.mark.slow_test
def test_rm_known_host(self):
"""
Expand Down
18 changes: 14 additions & 4 deletions tests/integration/states/test_ssh_known_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,22 @@ def test_present(self):
@pytest.mark.slow_test
def test_present_fail(self):
# save something wrong
kwargs = {
"name": "github.com",
"user": "root",
"enc": "ssh-rsa",
"fingerprint": "aa:bb:cc:dd",
"config": self.known_hosts,
}
ret = self.run_state("ssh_known_hosts.present", **kwargs)
self.assertSaltFalseReturn(ret)
# Missing user.
kwargs["user"] = "baduser"
kwargs["fingerprint"] = GITHUB_FINGERPRINT
del kwargs["config"]
ret = self.run_state(
"ssh_known_hosts.present",
name="github.com",
user="root",
fingerprint="aa:bb:cc:dd",
config=self.known_hosts,
**kwargs,
)
self.assertSaltFalseReturn(ret)

Expand Down