diff --git a/install.sh b/install.sh index b751ac47a5..451f79e350 100755 --- a/install.sh +++ b/install.sh @@ -998,7 +998,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 diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index b6203a11ea..9006b58183 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -14,13 +14,16 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'thread' + 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 @@ -43,9 +46,11 @@ 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 + @token_expiration_time = Time.now.to_i + EXPIRE_DELTA if AUTH_MODULES.include?(@conf[:auth]) @@ -55,11 +60,10 @@ 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 - core_auth =AUTH_CORE_MODULES["cipher"] + core_auth = AUTH_CORE_MODULES["cipher"] end begin @@ -68,6 +72,11 @@ class CloudAuth rescue => e raise e.message end + + @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 @@ -75,19 +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 - @user_pool = OpenNebula::UserPool.new(client) - - rc = @user_pool.info - if OpenNebula.is_error?(rc) - raise rc.message - end - end - + # + # + # def auth(env, params={}) username = do_auth(env, params) @@ -101,36 +115,47 @@ class CloudAuth protected - def expiration_time - time_now = Time.now.to_i - - if time_now > @token_expiration_time - EXPIRE_MARGIN - @token_expiration_time = time_now + EXPIRE_DELTA + # 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 + xpath << " and (AUTH_DRIVER=\"" + xpath << driver.split('|').join("\" or AUTH_DRIVER=\"") << '")' end + xpath << "]/PASSWORD" - @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, non_public_user=false) - if non_public_user == true - xp="USER[NAME=\"#{username}\" and AUTH_DRIVER!=\"public\"]/PASSWORD" - else - xp="USER[NAME=\"#{username}\"]/PASSWORD" - end - - return get_userpool[xp] + 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) - return get_userpool["USER[contains(PASSWORD, \"#{password}\")]/NAME"] + xpath = "USER[contains(PASSWORD, \"#{password}\")]/NAME" + + 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/cloud/common/CloudAuth/EC2CloudAuth.rb b/src/cloud/common/CloudAuth/EC2CloudAuth.rb index 38afacd64f..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) + 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 69831163ac..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) + one_pass = get_password(username, 'core|public') if one_pass && one_pass == password return username diff --git a/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb b/src/cloud/common/CloudAuth/OpenNebulaCloudAuth.rb new file mode 100644 index 0000000000..bcf54ea99e --- /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 OpenNebulaCloudAuth + 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/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 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 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, ""