From 4f8a1c2f9ddabcc0e08e6358a24426a07e2ae3b7 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 25 Oct 2011 14:44:43 +0200 Subject: [PATCH] bug #847: Added Sunstone Authorization. Updated server classes --- install.sh | 1 + share/etc/oned.conf | 4 +- .../server_cipher/server_cipher_auth.rb | 25 +++++++++++- .../remotes/server_x509/server_x509_auth.rb | 11 ++++++ src/cloud/common/CloudAuth.rb | 38 ++++++++++++++++--- .../common/CloudAuth/SunstoneCloudAuth.rb | 33 ++++++++++++++++ src/sunstone/models/SunstoneServer.rb | 4 +- src/sunstone/sunstone-server.rb | 25 ++++++------ 8 files changed, 118 insertions(+), 23 deletions(-) create mode 100644 src/cloud/common/CloudAuth/SunstoneCloudAuth.rb diff --git a/install.sh b/install.sh index 35d023fc1a..46a9fe7e56 100755 --- a/install.sh +++ b/install.sh @@ -809,6 +809,7 @@ COMMON_CLOUD_LIB_FILES="src/cloud/common/CloudServer.rb \ COMMON_CLOUD_CLIENT_LIB_FILES="src/cloud/common/CloudClient.rb" CLOUD_AUTH_LIB_FILES="src/cloud/common/CloudAuth/BasicCloudAuth.rb \ + src/cloud/common/CloudAuth/SunstoneCloudAuth.rb \ src/cloud/common/CloudAuth/EC2CloudAuth.rb \ src/cloud/common/CloudAuth/X509CloudAuth.rb" diff --git a/share/etc/oned.conf b/share/etc/oned.conf index 42a82437ff..03168786e1 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -558,8 +558,8 @@ HM_MAD = [ #AUTH_MAD = [ # executable = "one_auth_mad", -# arguments = "--authn ssh, x509, server_cipher, server_x509" -# arguments = "--authz quota --authn plain,ssh,x509"ssh, x509, server_cipher, server_x509 +# arguments = "--authn ssh,x509,server_cipher,server_x509" +# arguments = "--authz quota --authn ssh,x509,server_cipher,server_x509" #] SESSION_EXPIRATION_TIME = 900 \ No newline at end of file diff --git a/src/authm_mad/remotes/server_cipher/server_cipher_auth.rb b/src/authm_mad/remotes/server_cipher/server_cipher_auth.rb index e25bb3e74e..cac1227736 100644 --- a/src/authm_mad/remotes/server_cipher/server_cipher_auth.rb +++ b/src/authm_mad/remotes/server_cipher/server_cipher_auth.rb @@ -50,7 +50,30 @@ class ServerCipherAuth ########################################################################### # Creates a ServerCipher for client usage - def self.new_client(srv_user, srv_passwd) + def self.new_client(srv_user=nil, srv_passwd=nil) + if ( srv_user == nil || srv_passwd == nil ) + begin + if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? + one_auth = File.read(ENV["ONE_AUTH"]) + else + one_auth = File.read(ENV["HOME"]+"/.one/one_auth") + end + + one_auth.rstrip! + + rc = one_auth.match(/(.*?):(.*)/) + + if rc.nil? + raise "Bad format for one_auth token (:)" + else + srv_user = rc[1] + srv_passwd = rc[2] + end + rescue => e + raise e.message + end + end + self.new(srv_user, srv_passwd) end diff --git a/src/authm_mad/remotes/server_x509/server_x509_auth.rb b/src/authm_mad/remotes/server_x509/server_x509_auth.rb index 6e7050e3c4..44e31cb4d5 100644 --- a/src/authm_mad/remotes/server_x509/server_x509_auth.rb +++ b/src/authm_mad/remotes/server_x509/server_x509_auth.rb @@ -56,6 +56,13 @@ class ServerX509Auth < X509Auth end end + ########################################################################### + # Client side + ########################################################################### + + # Creates a ServerCipher for client usage + alias :new_client :new + # Generates a login token in the form: # - server_user:target_user:time_expires def login_token(expire, target_user=nil) @@ -71,6 +78,10 @@ class ServerX509Auth < X509Auth ########################################################################### # Server side ########################################################################### + + # Creates a ServerCipher for driver usage + alias :new_driver :new + # auth method for auth_mad def authenticate(server_user, server_pass, signed_text) begin diff --git a/src/cloud/common/CloudAuth.rb b/src/cloud/common/CloudAuth.rb index b11024a3f8..1b1eb3a27d 100644 --- a/src/cloud/common/CloudAuth.rb +++ b/src/cloud/common/CloudAuth.rb @@ -17,12 +17,22 @@ require 'server_cipher_auth' class CloudAuth + # These are the authentication methods for the user requests AUTH_MODULES = { - "basic" => 'BasicCloudAuth', - "ec2" => 'EC2CloudAuth', - "x509" => 'X509CloudAuth' + "basic" => 'BasicCloudAuth', + "sunstone" => 'SunstoneCloudAuth' , + "ec2" => 'EC2CloudAuth', + "x509" => 'X509CloudAuth' } + # These are the authentication modules for the OpenNebula requests + # Each entry is an array with the filename for require and class name + # to instantiate the object. + AUTH_CORE_MODULES = { + "cipher" => [ 'server_cipher_auth', 'ServerCipherAuth' ], + "x509" => [ 'server_x509_auth', 'ServerX509Auth' ] + } + # Default interval for timestamps. Tokens will be generated using the same # timestamp for this interval of time. EXPIRE_DELTA = 36000 @@ -32,6 +42,7 @@ class CloudAuth attr_reader :client, :token + # conf a hash with the configuration attributes as symbols def initialize(conf) @conf = conf @@ -45,7 +56,24 @@ class CloudAuth raise "Auth module not specified" end - @server_auth = ServerCipherAuth.new + + if AUTH_CORE_MODULES.include?(@conf[:core_auth]) + core_auth = AUTH_CORE_MODULES[@conf[:core_auth]] + else + core_auth =AUTH_CORE_MODULES["cipher"] + end + + begin + require core_auth[0] + @server_auth = Kernel.const_get(core_auth[1]).new_client + rescue => e + raise e.message + end + end + + def client(username) + token = @server_auth.login_token(expiration_time,username) + Client.new(token,@conf[:one_xmlrpc]) end protected @@ -62,7 +90,7 @@ class CloudAuth end def get_password(username) - token = @server_auth.login_token(nil, expiration_time) + token = @server_auth.login_token(expiration_time) @oneadmin_client ||= OpenNebula::Client.new(token, @conf[:one_xmlrpc]) if @user_pool.nil? diff --git a/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb b/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb new file mode 100644 index 0000000000..9122e78b63 --- /dev/null +++ b/src/cloud/common/CloudAuth/SunstoneCloudAuth.rb @@ -0,0 +1,33 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, 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 SunstoneCloudAuth + def auth(env, params={}) + auth = Rack::Auth::Basic::Request.new(env) + + if auth.provided? && auth.basic? + username, password = auth.credentials + + one_pass = get_password(username) + + if one_pass && one_pass == Digest::SHA1.hexdigest(password) + return username + end + end + + return nil + end +end \ No newline at end of file diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index ece69b836f..66d53709ac 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -23,8 +23,8 @@ class SunstoneServer # FLAG that will filter the elements retrieved from the Pools POOL_FILTER = Pool::INFO_GROUP - def initialize(token, xmlrpc) - @client = Client.new(token, xmlrpc) + def initialize(client) + @client = client end ############################################################################ diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index a75f9af50d..a9a9169c9d 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -66,6 +66,8 @@ set :config, conf set :host, settings.config[:host] set :port, settings.config[:port] +set :cloud_auth, CloudAuth.new(settings.config) + ############################################################################## # Helpers ############################################################################## @@ -75,20 +77,19 @@ helpers do end def build_session - cloud_auth = CloudAuth.new(settings.config) + # begin + result = settings.cloud_auth.auth(request.env, params) + # rescue Exception => e + # error 500, e.message + # end - begin - result = cloud_auth.auth(request.env, params) - rescue Exception => e - error 500, e.message - end - - if result + if result.nil? return [401, ""] else + client = settings.cloud_auth.client(result) user_id = OpenNebula::User::SELF - user = OpenNebula::User.new_with_id(user_id, cloud_auth.client) - + + user = OpenNebula::User.new_with_id(user_id, client) rc = user.info if OpenNebula.is_error?(rc) # Add a log message @@ -99,7 +100,6 @@ helpers do session[:user_id] = user['ID'] session[:user_gid] = user['GID'] session[:user_gname] = user['GNAME'] - session[:token] = cloud_auth.token session[:ip] = request.ip session[:remember] = params[:remember] @@ -122,8 +122,7 @@ before do halt 401 unless authorized? @SunstoneServer = SunstoneServer.new( - session[:token], - settings.config[:one_xmlrpc]) + settings.cloud_auth.client(session[:user])) end end