From 6c9771c94ee0d798aae88e942056808013929bc3 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 17 Oct 2014 12:31:00 +0200 Subject: [PATCH 1/4] feature #3180: options for SSL certs in OCA ONE_CERT_DIR: adds an extra directory with trusted CA certificates ONE_DISABLE_SSL_VERIFY: disable certificate verification Both of these options make the calls change from asynchronous (one http connection per call) to synchronous (same http connection for all calls). XMLRPC library creates a new HTTP object per asynchronous connection and there is no way of passing configuration options to it. --- src/oca/ruby/opennebula/client.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/oca/ruby/opennebula/client.rb b/src/oca/ruby/opennebula/client.rb index f202fc1f9a..2eb906911a 100644 --- a/src/oca/ruby/opennebula/client.rb +++ b/src/oca/ruby/opennebula/client.rb @@ -17,6 +17,7 @@ require 'xmlrpc/client' require 'bigdecimal' require 'stringio' +require 'openssl' module OpenNebula @@ -135,6 +136,8 @@ module OpenNebula @one_endpoint = "http://localhost:2633/RPC2" end + @async = true + timeout=nil timeout=options[:timeout] if options[:timeout] @@ -144,6 +147,25 @@ module OpenNebula @server = XMLRPC::Client.new2(@one_endpoint, http_proxy, timeout) @server.http_header_extra = {'accept-encoding' => 'identity'} + http = @server.instance_variable_get("@http") + + if options['cert_dir'] || ENV['ONE_CERT_DIR'] + @async = false + cert_dir = options['cert_dir'] || ENV['ONE_CERT_DIR'] + cert_files = Dir["#{cert_dir}/*"] + + cert_store = OpenSSL::X509::Store.new + cert_store.set_default_paths + cert_files.each {|cert| cert_store.add_file(cert) } + + http.cert_store = cert_store + end + + if options['disable_ssl_verify'] || ENV['ONE_DISABLE_SSL_VERIFY'] + @async = false + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + if defined?(OxStreamParser) @server.set_parser(OxStreamParser.new) elsif OpenNebula::NOKOGIRI @@ -155,7 +177,11 @@ module OpenNebula def call(action, *args) begin - response = @server.call_async("one."+action, @one_auth, *args) + if @async + response = @server.call_async("one."+action, @one_auth, *args) + else + response = @server.call("one."+action, @one_auth, *args) + end if response[0] == false Error.new(response[1], response[2]) From 3d3a8d2487f17c2e8fc40aea02da502d69556c46 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 21 Oct 2014 11:45:56 +0200 Subject: [PATCH 2/4] feature #3180: add option to make xmlrpc calls synchronous Using SSL options in Ruby OCA no longer disable async calls automatically. The sync call must be specified when creating a client: client = OpenNebula::Client.new(secret, proxy, :sync => true) --- src/oca/ruby/opennebula/client.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/oca/ruby/opennebula/client.rb b/src/oca/ruby/opennebula/client.rb index 2eb906911a..42cd9f79b5 100644 --- a/src/oca/ruby/opennebula/client.rb +++ b/src/oca/ruby/opennebula/client.rb @@ -106,6 +106,14 @@ module OpenNebula # defaults to 30 # @option params [String] :http_proxy HTTP proxy string used for # connecting to the endpoint; defaults to no proxy + # @option params [Boolean] :sync Use only one http connection for + # all calls. It should not be used for multithreaded programs. + # It's the only mode that can be used with :cert_dir and + # :disable_ssl_verify + # @option params [String] :cert_dir Extra directory where to import + # trusted issuer certificates. Use with :sync = true + # @option params [String] :disable_ssl_verify Disable SSL certificate + # verification. Use only for testing and with :sync = true # # @return [OpenNebula::Client] def initialize(secret=nil, endpoint=nil, options={}) @@ -136,7 +144,7 @@ module OpenNebula @one_endpoint = "http://localhost:2633/RPC2" end - @async = true + @async = !options[:sync] timeout=nil timeout=options[:timeout] if options[:timeout] @@ -149,9 +157,10 @@ module OpenNebula http = @server.instance_variable_get("@http") - if options['cert_dir'] || ENV['ONE_CERT_DIR'] - @async = false - cert_dir = options['cert_dir'] || ENV['ONE_CERT_DIR'] + if options[:cert_dir] || ENV['ONE_CERT_DIR'] + STDERR.puts "SSL options don't work in async mode" if @async + + cert_dir = options[:cert_dir] || ENV['ONE_CERT_DIR'] cert_files = Dir["#{cert_dir}/*"] cert_store = OpenSSL::X509::Store.new @@ -161,8 +170,9 @@ module OpenNebula http.cert_store = cert_store end - if options['disable_ssl_verify'] || ENV['ONE_DISABLE_SSL_VERIFY'] - @async = false + if options[:disable_ssl_verify] || ENV['ONE_DISABLE_SSL_VERIFY'] + STDERR.puts "SSL options don't work in async mode" if @async + http.verify_mode = OpenSSL::SSL::VERIFY_NONE end From 7520c11555e79093194e8288290fc7d0e328615a Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 21 Oct 2014 11:50:01 +0200 Subject: [PATCH 3/4] feature #3180: make CLI use sync calls to honor SSL parameters --- src/cli/one_helper.rb | 2 +- src/cli/one_helper/oneuser_helper.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index c9a2ffebe4..cc3eaa22d2 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -370,7 +370,7 @@ EOT endpoint=options[:endpoint] end - @@client=OpenNebula::Client.new(secret, endpoint) + @@client=OpenNebula::Client.new(secret, endpoint, :sync => true) end end diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index 463b253e18..d53e190c10 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -163,7 +163,9 @@ class OneUserHelper < OpenNebulaHelper::OneHelper # authentication token for the user #----------------------------------------------------------------------- token = auth.login_token(username, options[:time]) - login_client = OpenNebula::Client.new("#{username}:#{token}") + login_client = OpenNebula::Client.new("#{username}:#{token}", + nil, + :sync => true) user = OpenNebula::User.new(User.build_xml, login_client) From f970ed011d3ef92018a7f5c8e0b60112e15451e6 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sun, 26 Oct 2014 22:21:33 +0100 Subject: [PATCH 4/4] feature #3180: Raise an exception for not compatible client options --- src/oca/ruby/opennebula/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/oca/ruby/opennebula/client.rb b/src/oca/ruby/opennebula/client.rb index 42cd9f79b5..c4cf6458f4 100644 --- a/src/oca/ruby/opennebula/client.rb +++ b/src/oca/ruby/opennebula/client.rb @@ -158,7 +158,7 @@ module OpenNebula http = @server.instance_variable_get("@http") if options[:cert_dir] || ENV['ONE_CERT_DIR'] - STDERR.puts "SSL options don't work in async mode" if @async + raise "SSL options don't work in async mode" if @async cert_dir = options[:cert_dir] || ENV['ONE_CERT_DIR'] cert_files = Dir["#{cert_dir}/*"] @@ -171,7 +171,7 @@ module OpenNebula end if options[:disable_ssl_verify] || ENV['ONE_DISABLE_SSL_VERIFY'] - STDERR.puts "SSL options don't work in async mode" if @async + raise "SSL options don't work in async mode" if @async http.verify_mode = OpenSSL::SSL::VERIFY_NONE end