mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-16 17:53:08 +09:00
feature #203: Added rsa authentication
This commit is contained in:
1 parent
d5d6d717da
commit
0dc421eabf
6 files changed
+255
-3
No files matched your search
+2
-1
@@ -288,7 +288,8 @@ RUBY_LIB_FILES="src/mad/ruby/one_mad.rb \
|
||||
src/authm_mad/one_usage.rb \
|
||||
src/authm_mad/quota.rb \
|
||||
src/authm_mad/simple_auth.rb \
|
||||
src/authm_mad/simple_permissions.rb"
|
||||
src/authm_mad/simple_permissions.rb \
|
||||
src/authm_mad/ssh_auth.rb"
|
||||
|
||||
RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \
|
||||
src/oca/ruby/OpenNebula/HostPool.rb \
|
||||
|
||||
@@ -36,6 +36,7 @@ require 'simple_auth'
|
||||
require 'simple_permissions'
|
||||
require 'yaml'
|
||||
require 'sequel'
|
||||
require 'ssh_auth'
|
||||
|
||||
class AuthorizationManager < OpenNebulaDriver
|
||||
def initialize
|
||||
@@ -50,7 +51,8 @@ class AuthorizationManager < OpenNebulaDriver
|
||||
database_url=@config[:database]
|
||||
@db=Sequel.connect(database_url)
|
||||
|
||||
@authenticate=SimpleAuth.new
|
||||
#@authenticate=SimpleAuth.new
|
||||
@authenticate=SshAuth.new
|
||||
@permissions=SimplePermissions.new(@db, OpenNebula::Client.new,
|
||||
@config)
|
||||
|
||||
@@ -59,6 +61,7 @@ class AuthorizationManager < OpenNebulaDriver
|
||||
end
|
||||
|
||||
def action_authenticate(request_id, user_id, user, password, token)
|
||||
STDERR.puts [user_id, user, password, token].inspect
|
||||
auth=@authenticate.auth(user_id, user, password, token)
|
||||
if auth==true
|
||||
send_message('AUTHENTICATE', RESULT[:success],
|
||||
|
||||
Executable
+153
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2010, 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. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
ONE_LOCATION=ENV["ONE_LOCATION"]
|
||||
|
||||
if !ONE_LOCATION
|
||||
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
|
||||
ETC_LOCATION="/etc/one/"
|
||||
VAR_LOCATION="/var/lib/one"
|
||||
else
|
||||
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
|
||||
ETC_LOCATION=ONE_LOCATION+"/etc/"
|
||||
VAR_LOCATION="#{ONE_LOCATION}/var"
|
||||
end
|
||||
|
||||
$: << RUBY_LIB_LOCATION
|
||||
|
||||
|
||||
require 'OpenNebula'
|
||||
require 'client_utilities'
|
||||
require 'command_parse'
|
||||
|
||||
require 'rubygems'
|
||||
require 'sequel'
|
||||
require 'quota'
|
||||
require 'ssh_auth'
|
||||
|
||||
class OneAuthCli < CommandParse
|
||||
|
||||
COMMANDS_HELP=<<-EOT
|
||||
Commands:
|
||||
|
||||
* create (Creates a new user)
|
||||
oneuser create username password
|
||||
|
||||
* delete (Removes a user)
|
||||
oneuser delete <id>
|
||||
|
||||
* list (Lists all the users in the pool)
|
||||
oneuser list
|
||||
|
||||
EOT
|
||||
|
||||
def text_commands
|
||||
COMMANDS_HELP
|
||||
end
|
||||
|
||||
def text_command_name
|
||||
"oneauth"
|
||||
end
|
||||
|
||||
def list_options
|
||||
table=ShowTable.new(ShowTableUP)
|
||||
table.print_help
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def get_database
|
||||
config_data=File.read(ETC_LOCATION+'/auth/auth.conf')
|
||||
config=YAML::load(config_data)
|
||||
|
||||
database_url=config[:database]
|
||||
db=Sequel.connect(database_url)
|
||||
end
|
||||
|
||||
def add_quota(uid, cpu, memory)
|
||||
db=get_database
|
||||
quota=Quota.new(db, OpenNebula::Client.new)
|
||||
quota.set(uid.to_i, cpu.to_f, memory.to_i, nil)
|
||||
end
|
||||
|
||||
oneauth_opts=OneAuthCli.new
|
||||
oneauth_opts.parse(ARGV)
|
||||
ops=oneauth_opts.options
|
||||
|
||||
result=[false, "Unknown error"]
|
||||
|
||||
command=ARGV.shift
|
||||
|
||||
case command
|
||||
when "quota"
|
||||
check_parameters("quota", 1)
|
||||
|
||||
case ARGV[0].downcase
|
||||
when 'set'
|
||||
check_parameters("quota set", 3)
|
||||
Dir.chdir VAR_LOCATION
|
||||
add_quota(*ARGV[1..3])
|
||||
else
|
||||
#default
|
||||
end
|
||||
|
||||
exit 0
|
||||
|
||||
when "login"
|
||||
check_parameters("login", 1)
|
||||
|
||||
user=ARGV[0]
|
||||
time=ARGV[1]
|
||||
|
||||
if time
|
||||
time=time.to_i
|
||||
else
|
||||
time=3600
|
||||
end
|
||||
|
||||
ssh=SshAuth.new
|
||||
ssh.login(user, time)
|
||||
|
||||
when "create"
|
||||
user=OpenNebula::User.new(
|
||||
OpenNebula::User.build_xml, OpenNebula::Client.new)
|
||||
password = ARGV[1]
|
||||
|
||||
pp [ARGV[0], ARGV[1]]
|
||||
|
||||
result=user.allocate(ARGV[0], password)
|
||||
if !OpenNebula.is_error?(result)
|
||||
puts "ID: " + user.id.to_s if ops[:verbose]
|
||||
exit 0
|
||||
end
|
||||
|
||||
when "key"
|
||||
ssh=SshAuth.new
|
||||
puts ssh.extract_public_key
|
||||
|
||||
exit 0
|
||||
|
||||
else
|
||||
oneauth_opts.print_help
|
||||
exit -1
|
||||
end
|
||||
|
||||
if OpenNebula.is_error?(result)
|
||||
puts "Error: " + result.message
|
||||
exit -1
|
||||
end
|
||||
@@ -68,10 +68,13 @@ class Quota
|
||||
}
|
||||
|
||||
quotas=@table.filter(:uid => uid)
|
||||
pp quotas.first
|
||||
|
||||
if quotas.first
|
||||
STDERR.puts "updating"
|
||||
quotas.update(data)
|
||||
else
|
||||
STDERR.puts "inserting"
|
||||
@table.insert(data.merge!(:uid => uid))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
|
||||
require 'pp'
|
||||
require 'openssl'
|
||||
require 'base64'
|
||||
require 'fileutils'
|
||||
|
||||
class SshAuth
|
||||
|
||||
def get_priv_key
|
||||
path=ENV['HOME']+'/.ssh/id_rsa'
|
||||
File.read(path)
|
||||
end
|
||||
|
||||
def get_proxy_file
|
||||
proxy_dir=ENV['HOME']+'/.one'
|
||||
begin
|
||||
FileUtils.mkdir_p(proxy_dir)
|
||||
rescue Errno::EEXIST
|
||||
end
|
||||
|
||||
File.open(proxy_dir+'/one_ssh', "w")
|
||||
end
|
||||
|
||||
def encrypt(data)
|
||||
rsa=OpenSSL::PKey::RSA.new(get_priv_key)
|
||||
Base64::encode64(rsa.private_encrypt(data)).gsub!(/\n/, '').strip
|
||||
end
|
||||
|
||||
def decrypt(data, pub_key)
|
||||
rsa=OpenSSL::PKey::RSA.new(Base64::decode64(pub_key))
|
||||
rsa.public_decrypt(Base64::decode64(data))
|
||||
end
|
||||
|
||||
def extract_public_key
|
||||
key=OpenSSL::PKey::RSA.new(get_priv_key)
|
||||
public_key=key.public_key.to_pem.split("\n")
|
||||
public_key.reject {|l| l.match(/RSA PUBLIC KEY/) }.join('')
|
||||
end
|
||||
|
||||
def login(user, expire=3600)
|
||||
time=Time.now.to_i+expire
|
||||
proxy_text="#{user}:#{time}"
|
||||
proxy_crypted=encrypt(proxy_text)
|
||||
proxy="#{user}:ssh:#{proxy_crypted}"
|
||||
file=get_proxy_file
|
||||
file.write(proxy)
|
||||
file.close
|
||||
|
||||
puts "export ONE_AUTH=#{ENV['HOME']}/.one/one_ssh"
|
||||
|
||||
proxy_crypted
|
||||
end
|
||||
|
||||
def auth(user_id, user, password, token)
|
||||
begin
|
||||
decrypted=decrypt(token, password)
|
||||
|
||||
username, time=decrypted.split(':')
|
||||
|
||||
pp [username, time]
|
||||
|
||||
if user==username
|
||||
if Time.now.to_i>=time.to_i
|
||||
"proxy expired, login again"
|
||||
else
|
||||
true
|
||||
end
|
||||
else
|
||||
"invalid credentials"
|
||||
end
|
||||
rescue
|
||||
"error"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
#pub_key="MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAyspEH4KiKRX625j4knzQueD2IRxkJlvT7O777Q18105kqDhhPYW5RtvxKWFqhUAQPPd2cjLkJ1bmk4JJuDk4rJWN0KEmB14JWB68u+YAv+u3NWCw0StDf25hRf+iN9dBf+WOt9brTpLGBF1BPtHY0+XkC/DnVhPbCxz5dvptSW8ajmpRS+u1qQIyyv9/bucDIoBvHmiA10ydBoQYwMOHk3U+ONJrgniph01tYfeQqLIngG86yaauadp8CqScRhPZdvtbBIhbxghrE/AfhXhWNti0cPbTZTWc2teXHkiwq//JIyIl29oZjmr3jcAZT8j2e5kJzKSS6RxrGdmR66fSkwIBIw=="
|
||||
|
||||
#ssh=SshAuth.new
|
||||
#pp data=ssh.encrypt("hola cucucucucuc #{Time.now.to_i}")
|
||||
#pp ssh.decrypt(data, pub_key)
|
||||
|
||||
#pp ssh.decrypt(ssh.login("jfontan", 0), pub_key)
|
||||
|
||||
@@ -84,7 +84,14 @@ module OpenNebula
|
||||
|
||||
|
||||
one_secret=~/^(.+?):(.+)$/
|
||||
@one_auth = "#{$1}:#{Digest::SHA1.hexdigest($2)}"
|
||||
user=$1
|
||||
password=$2
|
||||
|
||||
if password.match(/^ssh:/)
|
||||
@one_auth = "#{user}:#{password.split(':').last}"
|
||||
else
|
||||
@one_auth = "#{user}:#{Digest::SHA1.hexdigest(password)}"
|
||||
end
|
||||
|
||||
if endpoint
|
||||
@one_endpoint=endpoint
|
||||
|
||||
Reference in new issue
Block a user