From c0adc81af648e015c8eafc753478613fab1bf2eb Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 23 Apr 2013 11:56:09 +0200 Subject: [PATCH] bug #1919: Do not ask ssh password twice Patch by Jean-Philippe Garcia Ballester Conflicts: src/authm_mad/remotes/ssh/ssh_auth.rb --- src/authm_mad/remotes/ssh/ssh_auth.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/authm_mad/remotes/ssh/ssh_auth.rb b/src/authm_mad/remotes/ssh/ssh_auth.rb index 7b1e740128..c0d95c6ef5 100644 --- a/src/authm_mad/remotes/ssh/ssh_auth.rb +++ b/src/authm_mad/remotes/ssh/ssh_auth.rb @@ -44,18 +44,18 @@ class OpenNebula::SshAuth raise "Cannot read #{options[:private_key]}" end end + @private_key_rsa = OpenSSL::PKey::RSA.new(@private_key) unless @private_key.nil? if options[:public_key] @public_key = options[:public_key] elsif @private_key != nil # Init ssh keys using private key. public key is extracted in a # format compatible with openssl. The public key does not contain - # "---- BEGIN/END RSA PUBLIC KEY ----" and is in a single line - key = OpenSSL::PKey::RSA.new(@private_key) - - @public_key = key.public_key.to_pem.split("\n") + # "---- BEGIN/END PUBLIC KEY ----" and is in a single line + @public_key = @private_key_rsa.public_key.to_pem.split("\n") @public_key = @public_key.reject {|l| l.match(/PUBLIC KEY/) }.join('') end + @public_key_rsa = OpenSSL::PKey::RSA.new(Base64::decode64(@public_key)) unless @public_key.nil? if @private_key.nil? && @public_key.nil? raise "You have to define at least one of the keys" @@ -127,13 +127,11 @@ class OpenNebula::SshAuth # Encrypts data with the private key of the user and returns # base 64 encoded output in a single line def encrypt(data) - rsa=OpenSSL::PKey::RSA.new(@private_key) - Base64::encode64(rsa.private_encrypt(data)).gsub!(/\n/, '').strip + Base64::encode64(@private_key_rsa.private_encrypt(data)).gsub!(/\n/, '').strip end # Decrypts base 64 encoded data with pub_key (public key) def decrypt(data) - rsa=OpenSSL::PKey::RSA.new(Base64::decode64(@public_key)) - rsa.public_decrypt(Base64::decode64(data)) + @public_key_rsa.public_decrypt(Base64::decode64(data)) end end