diff options
-rw-r--r-- | lib/net/ssh/key_factory.rb | 8 | ||||
-rw-r--r-- | test/test_key_factory.rb | 22 |
2 files changed, 27 insertions, 3 deletions
diff --git a/lib/net/ssh/key_factory.rb b/lib/net/ssh/key_factory.rb index fdc7eda..94b4633 100644 --- a/lib/net/ssh/key_factory.rb +++ b/lib/net/ssh/key_factory.rb @@ -105,7 +105,13 @@ module Net; module SSH # the file describes an RSA or DSA key, and will load it # appropriately. The new public key is returned. def load_data_public_key(data, filename="") - _, blob = data.split(/ /) + fields = data.split(/ /) + + blob = nil + begin + blob = fields.shift + end while !blob.nil? && !/^(ssh-(rsa|dss)|ecdsa-sha2-nistp\d+)$/.match(blob) + blob = fields.shift raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil? diff --git a/test/test_key_factory.rb b/test/test_key_factory.rb index d5c6416..5bc90b2 100644 --- a/test/test_key_factory.rb +++ b/test/test_key_factory.rb @@ -65,6 +65,20 @@ class TestKeyFactory < Test::Unit::TestCase assert_equal rsa_key.to_blob, Net::SSH::KeyFactory.load_public_key(@key_file).to_blob end + def test_load_public_rsa_key_with_comment_should_return_key + File.expects(:read).with(@key_file).returns(public(rsa_key) + " key_comment") + assert_equal rsa_key.to_blob, Net::SSH::KeyFactory.load_public_key(@key_file).to_blob + end + + def test_load_public_rsa_key_with_options_should_return_key + File.expects(:read).with(@key_file).returns(public(rsa_key, 'environment="FOO=bar"')) + assert_equal rsa_key.to_blob, Net::SSH::KeyFactory.load_public_key(@key_file).to_blob + end + + def test_load_public_rsa_key_with_options_and_comment_should_return_key + File.expects(:read).with(@key_file).returns(public(rsa_key, 'environment="FOO=bar"') + " key_comment") + assert_equal rsa_key.to_blob, Net::SSH::KeyFactory.load_public_key(@key_file).to_blob + end if defined?(OpenSSL::PKey::EC) def test_load_unencrypted_private_ecdsa_sha2_nistp256_key_should_return_key File.expects(:read).with("/key-file").returns(ecdsa_sha2_nistp256_key.to_pem) @@ -165,8 +179,12 @@ WEKt5v3QsUEgVrzkM4K9UbI= key.export(OpenSSL::Cipher::Cipher.new("des-ede3-cbc"), password) end - def public(key) - result = "#{key.ssh_type} " + def public(key, args = nil) + result = "" + if !args.nil? + result << "#{args} " + end + result << "#{key.ssh_type} " result << [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").strip.tr("\n\r\t ", "") result << " joe@host.test" end |