From e7e96a9fab5984bab8102ed553959f32b1a66c8a Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 23 Aug 2011 16:22:04 +0200 Subject: [PATCH] feature #754: Change initialize parameters for SshAuth --- src/authm_mad/remotes/ssh/authenticate | 8 ++++-- src/authm_mad/remotes/ssh/ssh_auth.rb | 35 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/authm_mad/remotes/ssh/authenticate b/src/authm_mad/remotes/ssh/authenticate index 0892917738..517d9033d2 100755 --- a/src/authm_mad/remotes/ssh/authenticate +++ b/src/authm_mad/remotes/ssh/authenticate @@ -36,8 +36,12 @@ pass = ARGV[1] secret = ARGV[2] #OpenNebula.log_debug("Authenticating #{user}, with password #{pass} (#{secret})") - -ssh_auth = SshAuth.new(pass) +begin + ssh_auth = SshAuth.new(:public_key=>pass) +rescue Exception => e + OpenNebula.error_message e.message + exit -1 +end rc = ssh_auth.authenticate(user,secret) diff --git a/src/authm_mad/remotes/ssh/ssh_auth.rb b/src/authm_mad/remotes/ssh/ssh_auth.rb index f6f9f8f704..85d38f313e 100644 --- a/src/authm_mad/remotes/ssh/ssh_auth.rb +++ b/src/authm_mad/remotes/ssh/ssh_auth.rb @@ -25,21 +25,38 @@ require 'fileutils' # by oneauth command class SshAuth attr_reader :public_key - - def initialize(pub_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 - @private_key = File.read(ENV['HOME']+'/.ssh/id_rsa') + # Initialize SshAuth object + # + # @param [Hash] default options for path + # @option options [String] :public_key public key for the user + # @option options [String] :private_key key private key for the user. + def initialize(options={}) + @private_key = nil + @public_key = nil - if pub_key == nil + if options[:private_key] + begin + @private_key = File.read(options[:private_key]) + rescue Exception => e + raise "Cannot read #{options[:private_key]}" + end + end + + 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") @public_key = @public_key.reject {|l| l.match(/RSA PUBLIC KEY/) }.join('') - else - @public_key = pub_key + end + + if @private_key.nil? && @public_key.nil? + raise "You have to define at least one of the keys" end end