Skip to content

Commit 76d2aae

Browse files
committed
Fix KnownHostsKeys#keys_for to match on the entire hostlist
See #362 (comment)
1 parent 283b1dc commit 76d2aae

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

lib/sshkit/backends/netssh/known_hosts.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ def keys_for(hostlist)
1818
parse_file unless keys && hashes
1919
keys, hashes = hosts_keys, hosts_hashes
2020

21-
hostlist.split(',').each do |host|
22-
key_list = keys[host]
23-
return key_list if key_list
21+
host_names = hostlist.split(',')
2422

23+
keys_found = host_names.map { |h| keys[h] || [] }.compact.inject(:&)
24+
return keys_found unless keys_found.empty?
25+
26+
host_names.each do |host|
2527
hashes.each do |(hmac, salt), hash_keys|
2628
if OpenSSL::HMAC.digest(sha1, salt, host) == hmac
2729
return hash_keys

test/known_hosts/github_ip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com,192.30.252.123 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==

test/unit/backends/test_netssh.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,28 @@ def test_transfer_summarizer
5656

5757
if Net::SSH::Version::CURRENT >= Net::SSH::Version[3, 1, 0]
5858
def test_known_hosts_for_when_all_hosts_are_recognized
59-
perform_known_hosts_test("github")
59+
perform_known_hosts_test('github', 'github.com')
6060
end
6161

6262
def test_known_hosts_for_when_an_host_hash_is_recognized
63-
perform_known_hosts_test("github_hash")
63+
perform_known_hosts_test('github_hash', 'github.com')
64+
end
65+
66+
def test_known_hosts_for_with_multiple_hosts
67+
perform_known_hosts_test('github', '192.30.252.123,github.com', 0)
68+
perform_known_hosts_test('github_ip', '192.30.252.123,github.com', 1)
6469
end
6570
end
6671

6772
private
6873

69-
def perform_known_hosts_test(hostfile)
74+
def perform_known_hosts_test(hostfile, hostlist, keys_count = 1)
7075
source = File.join(File.dirname(__FILE__), '../../known_hosts', hostfile)
7176
kh = Netssh::KnownHosts.new
72-
keys = kh.search_for('github.com', user_known_hosts_file: source, global_known_hosts_file: Tempfile.new('sshkit-test').path)
77+
keys = kh.search_for(hostlist, user_known_hosts_file: source, global_known_hosts_file: Tempfile.new('sshkit-test').path)
7378

7479
assert_instance_of ::Net::SSH::HostKeys, keys
75-
assert_equal(1, keys.count)
80+
assert_equal(keys_count, keys.count)
7681
keys.each do |key|
7782
assert_equal("ssh-rsa", key.ssh_type)
7883
end

0 commit comments

Comments
 (0)