bug #847: Added Sunstone Authorization. Updated server classes

This commit is contained in:
Ruben S. Montero committed 2011-10-25 14:44:43 +02:00
1 parent 21b3753c41
commit 4f8a1c2f9d
8 files changed
+118 -23

No files matched your search

+1
View File
@@ -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"
+2 -2
View File
@@ -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
@@ -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 (<user>:<passwd>)"
else
srv_user = rc[1]
srv_passwd = rc[2]
end
rescue => e
raise e.message
end
end
self.new(srv_user, srv_passwd)
end
@@ -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
+33 -5
View File
@@ -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?
@@ -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
+2 -2
View File
@@ -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
############################################################################
+12 -13
View File
@@ -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