forked from net-ssh/net-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
class TestCertHostAuth < NetSSHTest | ||
include IntegrationTestHelpers | ||
|
||
def setup_ssh_env(&block) | ||
def setup_ssh_env(principals: "one.hosts.netssh", validity: "+30d", &block) | ||
tmpdir do |dir| | ||
cert_type = "rsa" | ||
# cert_type = "ssh-ed25519" | ||
|
@@ -24,7 +24,8 @@ def setup_ssh_env(&block) | |
sh "ssh-keygen -t #{cert_type} -N '' -C '[email protected]' -f #{@cert} #{debug ? '' : '-q'}" | ||
FileUtils.cp "/etc/ssh/ssh_host_#{host_key_type}_key.pub", "#{dir}/one.hosts.netssh.pub" | ||
Dir.chdir(dir) do | ||
sh "ssh-keygen -s #{@cert} -h -I one.hosts.netssh -n one.hosts.netssh #{debug ? '' : '-q'} #{dir}/one.hosts.netssh.pub" | ||
principals_arg = principals.to_s.empty? ? "" : "-n #{principals}" | ||
sh "ssh-keygen -s #{@cert} -h -I one.hosts.netssh -V #{validity} #{principals_arg} #{debug ? '' : '-q'} #{dir}/one.hosts.netssh.pub" | ||
sh "ssh-keygen -L -f one.hosts.netssh-cert.pub" if debug | ||
end | ||
signed_host_key = "/etc/ssh/ssh_host_#{host_key_type}_key-cert.pub" | ||
|
@@ -92,6 +93,32 @@ def test_with_other_pub_key_host_key_should_not_match | |
end | ||
end | ||
|
||
def test_with_expired_certificate_should_fail | ||
Tempfile.open('cert_kh') do |f| | ||
setup_ssh_env(validity: "-30d:-1d") do |params| | ||
data = File.read(params[:cert_pub]) | ||
f.write("@cert-authority [*.hosts.netssh]:2200 #{data}") | ||
f.close | ||
|
||
config_lines = ["HostCertificate #{params[:signed_host_key]}"] | ||
start_sshd_7_or_later(config: config_lines) do |_pid, port| | ||
Timeout.timeout(500) do | ||
# sleep 0.2 | ||
# sh "ssh -v -i ~/.ssh/id_ed25519 one.hosts.netssh -o UserKnownHostsFile=#{f.path} -p 2200" | ||
assert_raises(Net::SSH::HostKeyMismatch) do | ||
Net::SSH.start("one.hosts.netssh", "net_ssh_1", password: 'foopwd', port: port, verify_host_key: :always, user_known_hosts_file: [f.path]) do |ssh| | ||
ssh.exec! "echo 'foo'" | ||
end | ||
end | ||
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH | ||
sleep 0.25 | ||
retry | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def test_host_should_match_when_host_key_was_signed_by_key_and_matching_principal | ||
Tempfile.open('cert_kh') do |f| | ||
setup_ssh_env do |params| | ||
|
@@ -117,6 +144,31 @@ def test_host_should_match_when_host_key_was_signed_by_key_and_matching_principa | |
end | ||
end | ||
|
||
def test_host_should_match_when_host_key_was_signed_by_key_and_no_principal_in_certificate | ||
Tempfile.open('cert_kh') do |f| | ||
setup_ssh_env(principals: "") do |params| | ||
data = File.read(params[:cert_pub]) | ||
f.write("@cert-authority [*.hosts.netssh]:2200 #{data}") | ||
f.close | ||
|
||
config_lines = ["HostCertificate #{params[:signed_host_key]}"] | ||
start_sshd_7_or_later(config: config_lines) do |_pid, port| | ||
Timeout.timeout(500) do | ||
# sleep 0.2 | ||
# sh "ssh -v -i ~/.ssh/id_ed25519 one.hosts.netssh -o UserKnownHostsFile=#{f.path} -p 2200" | ||
ret = Net::SSH.start("one.hosts.netssh", "net_ssh_1", password: 'foopwd', port: port, verify_host_key: :always, user_known_hosts_file: [f.path]) do |ssh| | ||
ssh.exec! "echo 'foo'" | ||
end | ||
assert_equal "foo\n", ret | ||
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH | ||
sleep 0.25 | ||
retry | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def test_host_should_not_match_when_host_key_was_signed_by_key_not_not_matching_principal | ||
Tempfile.open('cert_kh') do |f| | ||
setup_ssh_env do |params| | ||
|