From a8ce2e91e7940980874088def07f2124f1e92daf Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 21 Mar 2012 16:12:55 +0100 Subject: [PATCH 01/10] feature-967: Add OpenNebulaCloudAuth --- src/cloud/common/CloudAuth.rb | 9 +++-- .../common/CloudAuth/OpenNebulaCloudAuth.rb | 39 +++++++++++++++++++ src/cloud/occi/etc/occi-server.conf | 1 + 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index b6203a11ea..203b7cd928 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -17,10 +17,11 @@ class CloudAuth # These are the authentication methods for the user requests AUTH_MODULES = { - "occi" => 'OCCICloudAuth', - "sunstone" => 'SunstoneCloudAuth' , - "ec2" => 'EC2CloudAuth', - "x509" => 'X509CloudAuth' + "occi" => 'OCCICloudAuth', + "sunstone" => 'SunstoneCloudAuth' , + "ec2" => 'EC2CloudAuth', + "x509" => 'X509CloudAuth', + "opennebula" => 'OpenNebulaCloudAuth' } # These are the authentication modules for the OpenNebula requests diff --git a/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb b/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb new file mode 100644 index 0000000000..d29acfee05 --- /dev/null +++ b/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb @@ -0,0 +1,39 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module CoreCloudAuth + def do_auth(env, params={}) + auth = Rack::Auth::Basic::Request.new(env) + + if auth.provided? && auth.basic? + username, password = auth.credentials + + client = OpenNebula::Client.new("#{username}:#{password}") + user = OpenNebula::User.new_with_id(OpenNebula::User::SELF, client) + + rc = user.info + if OpenNebula.is_error?(rc) + logger.error { "User #{username} could not be authenticated"} + logger.error { rc.message } + return nil + end + + return username + end + + return nil + end +end \ No newline at end of file diff --git a/src/cloud/occi/etc/occi-server.conf b/src/cloud/occi/etc/occi-server.conf index 971326cd20..0d97e3bbfa 100644 --- a/src/cloud/occi/etc/occi-server.conf +++ b/src/cloud/occi/etc/occi-server.conf @@ -27,6 +27,7 @@ # Authentication driver for incomming requests # occi, for OpenNebula's user-password scheme # x509, for x509 certificates based authentication +# opennebula, use the driver defined for the user in OpenNebula :auth: occi # Authentication driver to communicate with OpenNebula core From 284d512916001b0288cb3dc5ae5db44f97ea0698 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 21 Mar 2012 16:28:58 +0100 Subject: [PATCH 02/10] Add OpenNebulaCloudAuth to install.sh --- install.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 69a3fc669e..013da7e361 100755 --- a/install.sh +++ b/install.sh @@ -987,7 +987,8 @@ COMMON_CLOUD_CLIENT_LIB_FILES="src/cloud/common/CloudClient.rb" CLOUD_AUTH_LIB_FILES="src/cloud/common/CloudAuth/OCCICloudAuth.rb \ src/cloud/common/CloudAuth/SunstoneCloudAuth.rb \ src/cloud/common/CloudAuth/EC2CloudAuth.rb \ - src/cloud/common/CloudAuth/X509CloudAuth.rb" + src/cloud/common/CloudAuth/X509CloudAuth.rb \ + src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb" #------------------------------------------------------------------------------- # EC2 Query for OpenNebula From cbc622f8b56560593d2b4bb57e71bd04a5b9c520 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 21 Mar 2012 17:18:15 +0100 Subject: [PATCH 03/10] bug #1142: Check user driver in CloudAuth --- src/cloud/common/CloudAuth.rb | 6 +++--- src/cloud/common/CloudAuth/EC2CloudAuth.rb | 2 +- src/cloud/common/CloudAuth/OCCICloudAuth.rb | 2 +- src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb | 4 ++-- src/cloud/common/CloudAuth/SunstoneCloudAuth.rb | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index 203b7cd928..d33a1a9f6a 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -118,9 +118,9 @@ class CloudAuth @user_pool end - def get_password(username, non_public_user=false) - if non_public_user == true - xp="USER[NAME=\"#{username}\" and AUTH_DRIVER!=\"public\"]/PASSWORD" + def get_password(username, driver=nil) + if driver + xp="USER[NAME=\"#{username}\" and AUTH_DRIVER=\"#{driver}\"]/PASSWORD" else xp="USER[NAME=\"#{username}\"]/PASSWORD" end diff --git a/src/cloud/common/CloudAuth/EC2CloudAuth.rb b/src/cloud/common/CloudAuth/EC2CloudAuth.rb index 38afacd64f..9f76421023 100644 --- a/src/cloud/common/CloudAuth/EC2CloudAuth.rb +++ b/src/cloud/common/CloudAuth/EC2CloudAuth.rb @@ -17,7 +17,7 @@ module EC2CloudAuth def do_auth(env, params={}) username = params['AWSAccessKeyId'] - one_pass = get_password(username) + one_pass = get_password(username, 'core') return nil unless one_pass signature = case params['SignatureVersion'] diff --git a/src/cloud/common/CloudAuth/OCCICloudAuth.rb b/src/cloud/common/CloudAuth/OCCICloudAuth.rb index 69831163ac..5dff6ec0f7 100644 --- a/src/cloud/common/CloudAuth/OCCICloudAuth.rb +++ b/src/cloud/common/CloudAuth/OCCICloudAuth.rb @@ -21,7 +21,7 @@ module OCCICloudAuth if auth.provided? && auth.basic? username, password = auth.credentials - one_pass = get_password(username) + one_pass = get_password(username, 'core') if one_pass && one_pass == password return username diff --git a/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb b/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb index d29acfee05..bcf54ea99e 100644 --- a/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb +++ b/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb @@ -14,7 +14,7 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -module CoreCloudAuth +module OpenNebulaCloudAuth def do_auth(env, params={}) auth = Rack::Auth::Basic::Request.new(env) @@ -26,7 +26,7 @@ module CoreCloudAuth rc = user.info if OpenNebula.is_error?(rc) - logger.error { "User #{username} could not be authenticated"} + logger.error { "User #{username} could not be authenticated" } logger.error { rc.message } return nil end diff --git a/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb b/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb index ef3cf0f2e3..1ac866ef6d 100644 --- a/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb +++ b/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb @@ -21,7 +21,7 @@ module SunstoneCloudAuth if auth.provided? && auth.basic? username, password = auth.credentials - one_pass = get_password(username, true) + one_pass = get_password(username, 'core') if one_pass && one_pass == Digest::SHA1.hexdigest(password) return username From 1efef61128acf68ce9e29e1dd608e1c6e3439d17 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Wed, 21 Mar 2012 18:38:49 +0100 Subject: [PATCH 04/10] bug #1142: Check public driver --- src/cloud/common/CloudAuth/EC2CloudAuth.rb | 2 +- src/cloud/common/CloudAuth/OCCICloudAuth.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cloud/common/CloudAuth/EC2CloudAuth.rb b/src/cloud/common/CloudAuth/EC2CloudAuth.rb index 9f76421023..8bae4cd704 100644 --- a/src/cloud/common/CloudAuth/EC2CloudAuth.rb +++ b/src/cloud/common/CloudAuth/EC2CloudAuth.rb @@ -17,7 +17,7 @@ module EC2CloudAuth def do_auth(env, params={}) username = params['AWSAccessKeyId'] - one_pass = get_password(username, 'core') + one_pass = get_password(username, 'core|public') return nil unless one_pass signature = case params['SignatureVersion'] diff --git a/src/cloud/common/CloudAuth/OCCICloudAuth.rb b/src/cloud/common/CloudAuth/OCCICloudAuth.rb index 5dff6ec0f7..8ad6fa07a6 100644 --- a/src/cloud/common/CloudAuth/OCCICloudAuth.rb +++ b/src/cloud/common/CloudAuth/OCCICloudAuth.rb @@ -21,7 +21,7 @@ module OCCICloudAuth if auth.provided? && auth.basic? username, password = auth.credentials - one_pass = get_password(username, 'core') + one_pass = get_password(username, 'core|public') if one_pass && one_pass == password return username From 2d471abdc9d1d8d98dff10e14bf85d03620abec4 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 23 Mar 2012 13:00:09 +0100 Subject: [PATCH 05/10] feature #967: CloudAuth thread safe --- src/cloud/common/CloudAuth.rb | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index d33a1a9f6a..778afb0121 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -14,6 +14,8 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'thread' + class CloudAuth # These are the authentication methods for the user requests AUTH_MODULES = { @@ -47,6 +49,8 @@ class CloudAuth @conf = conf @logger = logger + @mutex = Mutex.new + @token_expiration_time = Time.now.to_i + EXPIRE_DELTA if AUTH_MODULES.include?(@conf[:auth]) @@ -56,7 +60,6 @@ class CloudAuth raise "Auth module not specified" end - if AUTH_CORE_MODULES.include?(@conf[:core_auth]) core_auth = AUTH_CORE_MODULES[@conf[:core_auth]] else @@ -69,6 +72,8 @@ class CloudAuth rescue => e raise e.message end + + update_userpool_cache end # Generate a new OpenNebula client for the target User, if the username @@ -81,12 +86,12 @@ class CloudAuth end def update_userpool_cache - @user_pool = OpenNebula::UserPool.new(client) + @mutex.synchronize { + @user_pool = OpenNebula::UserPool.new(client) - rc = @user_pool.info - if OpenNebula.is_error?(rc) - raise rc.message - end + rc = @user_pool.info + raise rc.message if OpenNebula.is_error?(rc) + } end def auth(env, params={}) @@ -106,18 +111,14 @@ class CloudAuth time_now = Time.now.to_i if time_now > @token_expiration_time - EXPIRE_MARGIN - @token_expiration_time = time_now + EXPIRE_DELTA + @mutex.synchronize { + @token_expiration_time = time_now + EXPIRE_DELTA + } end @token_expiration_time end - # If @user_pool is not defined it will retrieve it from OpenNebula - def get_userpool - update_userpool_cache if @user_pool.nil? - @user_pool - end - def get_password(username, driver=nil) if driver xp="USER[NAME=\"#{username}\" and AUTH_DRIVER=\"#{driver}\"]/PASSWORD" @@ -125,13 +126,13 @@ class CloudAuth xp="USER[NAME=\"#{username}\"]/PASSWORD" end - return get_userpool[xp] + @user_pool[xp] end # Gets the username associated with a password # password:: _String_ the password # [return] _Hash_ with the username def get_username(password) - return get_userpool["USER[contains(PASSWORD, \"#{password}\")]/NAME"] + @user_pool["USER[contains(PASSWORD, \"#{password}\")]/NAME"] end end \ No newline at end of file From 133a14135f5f0bce5c2c607c617a9c417ff77b4e Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 23 Mar 2012 15:48:20 +0100 Subject: [PATCH 06/10] feature #967: Fix retrieve from userpool --- src/cloud/common/CloudAuth.rb | 43 +++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index 778afb0121..0feae1edbe 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -49,7 +49,7 @@ class CloudAuth @conf = conf @logger = logger - @mutex = Mutex.new + @lock = Mutex.new @token_expiration_time = Time.now.to_i + EXPIRE_DELTA @@ -73,7 +73,10 @@ class CloudAuth raise e.message end - update_userpool_cache + @user_pool = OpenNebula::UserPool.new(client) + + rc = @user_pool.info + raise rc.message if OpenNebula.is_error?(rc) end # Generate a new OpenNebula client for the target User, if the username @@ -81,12 +84,15 @@ class CloudAuth # ussername:: _String_ Name of the User # [return] _Client_ def client(username=nil) - token = @server_auth.login_token(expiration_time,username) + token = @lock.synchronize { + @server_auth.login_token(expiration_time,username) + } + Client.new(token,@conf[:one_xmlrpc]) end def update_userpool_cache - @mutex.synchronize { + @lock.synchronize { @user_pool = OpenNebula::UserPool.new(client) rc = @user_pool.info @@ -111,28 +117,35 @@ class CloudAuth time_now = Time.now.to_i if time_now > @token_expiration_time - EXPIRE_MARGIN - @mutex.synchronize { - @token_expiration_time = time_now + EXPIRE_DELTA - } + @token_expiration_time = time_now + EXPIRE_DELTA end @token_expiration_time end - def get_password(username, driver=nil) - if driver - xp="USER[NAME=\"#{username}\" and AUTH_DRIVER=\"#{driver}\"]/PASSWORD" - else - xp="USER[NAME=\"#{username}\"]/PASSWORD" - end + def retrieve_from_userpool(xpath) + @lock.synchronize { + @user_pool[xpath] + } + end - @user_pool[xp] + def get_password(username, driver=nil) + xpath = "USER[NAME=\"#{username}\"" + if driver + xpath = "\" and (AUTH_DRIVER=\"" + xpath << driver.split('|').join("or AUTH_DRIVER=\"") << '")' + end + xpath << "]/PASSWORD" + + retrieve_from_userpool(xpath) end # Gets the username associated with a password # password:: _String_ the password # [return] _Hash_ with the username def get_username(password) - @user_pool["USER[contains(PASSWORD, \"#{password}\")]/NAME"] + xpath = "USER[contains(PASSWORD, \"#{password}\")]/NAME" + + retrieve_from_userpool(xpath) end end \ No newline at end of file From b87cce74d911681cb0c147717a8508bc765c819c Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 23 Mar 2012 16:03:53 +0100 Subject: [PATCH 07/10] feature #967: Fix xpath expression --- src/cloud/common/CloudAuth.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index 0feae1edbe..689cbc6c29 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -132,8 +132,8 @@ class CloudAuth def get_password(username, driver=nil) xpath = "USER[NAME=\"#{username}\"" if driver - xpath = "\" and (AUTH_DRIVER=\"" - xpath << driver.split('|').join("or AUTH_DRIVER=\"") << '")' + xpath << " and (AUTH_DRIVER=\"" + xpath << driver.split('|').join("\" or AUTH_DRIVER=\"") << '")' end xpath << "]/PASSWORD" From 244afde3f9bae93c1654243779e4b7dbe42dc944 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 23 Mar 2012 18:23:33 +0100 Subject: [PATCH 08/10] feature #967: Fix sync in CloudAuth --- src/cloud/common/CloudAuth.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index 689cbc6c29..0c9aad4e1f 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -84,16 +84,16 @@ class CloudAuth # ussername:: _String_ Name of the User # [return] _Client_ def client(username=nil) - token = @lock.synchronize { - @server_auth.login_token(expiration_time,username) - } - + @server_auth.login_token(expiration_time,username) + Client.new(token,@conf[:one_xmlrpc]) end def update_userpool_cache + oneadmin_client = client + @lock.synchronize { - @user_pool = OpenNebula::UserPool.new(client) + @user_pool = OpenNebula::UserPool.new(oneadmin_client) rc = @user_pool.info raise rc.message if OpenNebula.is_error?(rc) @@ -114,13 +114,15 @@ class CloudAuth protected def expiration_time - time_now = Time.now.to_i + @lock.synchronize { + time_now = Time.now.to_i + + if time_now > @token_expiration_time - EXPIRE_MARGIN + @token_expiration_time = time_now + EXPIRE_DELTA + end - if time_now > @token_expiration_time - EXPIRE_MARGIN - @token_expiration_time = time_now + EXPIRE_DELTA - end - - @token_expiration_time + @token_expiration_time + } end def retrieve_from_userpool(xpath) From 0931fdaa2527c17720cda31070335d6eff85c964 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 23 Mar 2012 19:56:01 +0100 Subject: [PATCH 09/10] feature #967: Fix token generation --- src/cloud/common/CloudAuth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index 0c9aad4e1f..ea8adfe256 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -84,7 +84,7 @@ class CloudAuth # ussername:: _String_ Name of the User # [return] _Client_ def client(username=nil) - @server_auth.login_token(expiration_time,username) + token = @server_auth.login_token(expiration_time,username) Client.new(token,@conf[:one_xmlrpc]) end From f433e6f0aeecf8191e9356e169dc14e60459af2f Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 24 Mar 2012 00:47:36 +0100 Subject: [PATCH 10/10] feature #967: Fix method scope --- src/cloud/common/CloudAuth.rb | 72 ++++++++++++++++++--------------- src/sunstone/sunstone-server.rb | 1 - 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index ea8adfe256..9006b58183 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -46,10 +46,10 @@ class CloudAuth # conf a hash with the configuration attributes as symbols def initialize(conf, logger=nil) - @conf = conf + @conf = conf @logger = logger - @lock = Mutex.new + @lock = Mutex.new @token_expiration_time = Time.now.to_i + EXPIRE_DELTA @@ -63,7 +63,7 @@ class CloudAuth if AUTH_CORE_MODULES.include?(@conf[:core_auth]) core_auth = AUTH_CORE_MODULES[@conf[:core_auth]] else - core_auth =AUTH_CORE_MODULES["cipher"] + core_auth = AUTH_CORE_MODULES["cipher"] end begin @@ -84,22 +84,24 @@ class CloudAuth # ussername:: _String_ Name of the User # [return] _Client_ def client(username=nil) + expiration_time = @lock.synchronize { + time_now = Time.now.to_i + + if time_now > @token_expiration_time - EXPIRE_MARGIN + @token_expiration_time = time_now + EXPIRE_DELTA + end + + @token_expiration_time + } + token = @server_auth.login_token(expiration_time,username) Client.new(token,@conf[:one_xmlrpc]) end - def update_userpool_cache - oneadmin_client = client - - @lock.synchronize { - @user_pool = OpenNebula::UserPool.new(oneadmin_client) - - rc = @user_pool.info - raise rc.message if OpenNebula.is_error?(rc) - } - end - + # + # + # def auth(env, params={}) username = do_auth(env, params) @@ -113,24 +115,10 @@ class CloudAuth protected - def expiration_time - @lock.synchronize { - time_now = Time.now.to_i - - if time_now > @token_expiration_time - EXPIRE_MARGIN - @token_expiration_time = time_now + EXPIRE_DELTA - end - - @token_expiration_time - } - end - - def retrieve_from_userpool(xpath) - @lock.synchronize { - @user_pool[xpath] - } - end - + # Gets the password associated with a username + # username:: _String_ the username + # driver:: _String_ list of valid drivers for the user, | separated + # [return] _Hash_ with the username def get_password(username, driver=nil) xpath = "USER[NAME=\"#{username}\"" if driver @@ -150,4 +138,24 @@ class CloudAuth retrieve_from_userpool(xpath) end + + private + + def retrieve_from_userpool(xpath) + @lock.synchronize { + @user_pool[xpath] + } + end + + + def update_userpool_cache + oneadmin_client = client + + @lock.synchronize { + @user_pool = OpenNebula::UserPool.new(oneadmin_client) + + rc = @user_pool.info + raise rc.message if OpenNebula.is_error?(rc) + } + end end \ No newline at end of file diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 21230aa0f5..de1414a584 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -107,7 +107,6 @@ helpers do def build_session begin - settings.cloud_auth.update_userpool_cache result = settings.cloud_auth.auth(request.env, params) rescue Exception => e error 500, ""