mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-21 04:12:37 +09:00
appflow: Use opennebula auth instead of external auth
This commit is contained in:
1 parent
ba3f7bd198
commit
352cb0f2ab
4 files changed
+27
-140
No files matched your search
@@ -235,27 +235,23 @@ module Service
|
||||
@username = opts[:username]
|
||||
@password = opts[:password]
|
||||
|
||||
if @username.nil? && @password.nil?
|
||||
if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"])
|
||||
one_auth = File.read(ENV["ONE_AUTH"])
|
||||
elsif File.file?(ENV["HOME"]+"/.one/one_auth")
|
||||
one_auth = File.read(ENV["HOME"]+"/.one/one_auth")
|
||||
end
|
||||
|
||||
one_auth.rstrip!
|
||||
|
||||
@username, @password = one_auth.split(':')
|
||||
end
|
||||
|
||||
opts[:url] ||= 'http://localhost:2474'
|
||||
@uri = URI.parse(opts[:url])
|
||||
|
||||
@cookie = opts[:cookie]
|
||||
|
||||
@user_agent = "OpenNebula #{CloudClient::VERSION} " <<
|
||||
"(#{opts[:user_agent]||"Ruby"})"
|
||||
|
||||
if @cookie.nil?
|
||||
if @username.nil? && @password.nil?
|
||||
if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"])
|
||||
one_auth = File.read(ENV["ONE_AUTH"])
|
||||
elsif File.file?(ENV["HOME"]+"/.one/one_auth")
|
||||
one_auth = File.read(ENV["HOME"]+"/.one/one_auth")
|
||||
end
|
||||
|
||||
one_auth.rstrip!
|
||||
|
||||
@username, @password = one_auth.split(':')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def get(path)
|
||||
@@ -292,16 +288,10 @@ module Service
|
||||
private
|
||||
|
||||
def do_request(req)
|
||||
if @username && @password
|
||||
req.basic_auth @username, @password
|
||||
end
|
||||
req.basic_auth @username, @password
|
||||
|
||||
req['User-Agent'] = @user_agent
|
||||
|
||||
if @cookie
|
||||
req['Cookie'] = @cookie
|
||||
end
|
||||
|
||||
res = CloudClient::http_start(@uri, @timeout) do |http|
|
||||
http.request(req)
|
||||
end
|
||||
|
||||
@@ -18,12 +18,6 @@
|
||||
# Auth
|
||||
#############################################################
|
||||
|
||||
# Authentication driver for incomming requests
|
||||
# - sunstone, for OpenNebula's user-password scheme
|
||||
# - opennebula, the authentication will be done by the opennebula core using
|
||||
# the driver defined for the user
|
||||
:auth: sunstone
|
||||
|
||||
# Authentication driver to communicate with OpenNebula core
|
||||
# - cipher, for symmetric cipher encryption of tokens
|
||||
# - x509, for x509 certificate encryption of tokens
|
||||
|
||||
@@ -78,67 +78,21 @@ set :cloud_auth, cloud_auth
|
||||
##############################################################################
|
||||
# Helpers
|
||||
##############################################################################
|
||||
helpers do
|
||||
def authorized?
|
||||
session[:ip] && session[:ip]==request.ip ? true : false
|
||||
end
|
||||
|
||||
def build_session
|
||||
username = authenticate(request.env, params)
|
||||
if username
|
||||
session[:ip] = request.ip
|
||||
session[:user] = username
|
||||
|
||||
return [204, ""]
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_session
|
||||
session.clear
|
||||
return [204, ""]
|
||||
end
|
||||
|
||||
def authenticate(env, params)
|
||||
begin
|
||||
username = settings.cloud_auth.auth(request.env, params)
|
||||
rescue Exception => e
|
||||
Log.error LOG_COMP, e.message
|
||||
error 500, "Server error"
|
||||
end
|
||||
|
||||
if username.nil? #unable to authenticate
|
||||
Log.error LOG_COMP, "User not authorized"
|
||||
error 401, "User not authorized"
|
||||
else
|
||||
return username
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
unless request.path=='/login'
|
||||
if authorized?
|
||||
@client = settings.cloud_auth.client(session[:user])
|
||||
else
|
||||
username = authenticate(request.env, params)
|
||||
if username
|
||||
@client = settings.cloud_auth.client(username)
|
||||
end
|
||||
end
|
||||
auth = Rack::Auth::Basic::Request.new(request.env)
|
||||
|
||||
if auth.provided? && auth.basic?
|
||||
username, password = auth.credentials
|
||||
|
||||
@client = OpenNebula::Client.new("#{username}:#{password}")
|
||||
else
|
||||
error 401, "A username and password must be provided"
|
||||
end
|
||||
end
|
||||
|
||||
after do
|
||||
unless request.path=='/login' || request.path=='/'
|
||||
unless session[:remember] == "true"
|
||||
if params[:timeout] == "true"
|
||||
env['rack.session.options'][:defer] = true
|
||||
else
|
||||
env['rack.session.options'][:expire_after] = 60*10
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
##############################################################################
|
||||
# LCM thread
|
||||
@@ -151,16 +105,6 @@ t = Thread.new {
|
||||
}
|
||||
t.abort_on_exception = true
|
||||
|
||||
##############################################################################
|
||||
# Login
|
||||
##############################################################################
|
||||
post '/login' do
|
||||
build_session
|
||||
end
|
||||
|
||||
post '/logout' do
|
||||
destroy_session
|
||||
end
|
||||
|
||||
##############################################################################
|
||||
# Service
|
||||
|
||||
@@ -2,57 +2,16 @@ $: << RUBY_LIB_LOCATION+"/apptools/flow"
|
||||
|
||||
require 'appflow_client'
|
||||
|
||||
##############################################################################
|
||||
# Login
|
||||
##############################################################################
|
||||
post '/login' do
|
||||
code, body = build_session
|
||||
if code == 204
|
||||
auth = Rack::Auth::Basic::Request.new(request.env)
|
||||
|
||||
if auth.provided? && auth.basic?
|
||||
username, password = auth.credentials
|
||||
|
||||
client = Service::Client.new(
|
||||
:username => username,
|
||||
:password => password,
|
||||
:url => settings.config[:appflow_server],
|
||||
:user_agent => "Sunstone")
|
||||
|
||||
response = client.login
|
||||
if CloudClient::is_error?(response)
|
||||
settings.logger "[AppFlow] " + response.to_s
|
||||
else
|
||||
session[:appflow_cookie] = response['set-cookie'].split('; ')[0]
|
||||
end
|
||||
else
|
||||
settings.logger "[AppFlow] " + "Unauthorized AppFlow login attempt"
|
||||
end
|
||||
|
||||
[204, ""]
|
||||
else
|
||||
[code, body]
|
||||
end
|
||||
end
|
||||
|
||||
post '/logout' do
|
||||
client = Service::Client.new(
|
||||
:url => settings.config[:appflow_server],
|
||||
:user_agent => "Sunstone",
|
||||
:cookie => session[:appflow_cookie])
|
||||
|
||||
client.logout
|
||||
# TBD Check http code
|
||||
|
||||
destroy_session
|
||||
end
|
||||
|
||||
helpers do
|
||||
def build_client
|
||||
flow_client = settings.cloud_auth.client(session[:user])
|
||||
split_array = flow_client.one_auth.split(':')
|
||||
|
||||
Service::Client.new(
|
||||
:url => settings.config[:appflow_server],
|
||||
:user_agent => "Sunstone",
|
||||
:cookie => session[:appflow_cookie])
|
||||
:username => split_array.shift,
|
||||
:password => split_array.join(':'))
|
||||
end
|
||||
|
||||
def format_response(resp)
|
||||
|
||||
Reference in new issue
Block a user