From 0dc421eabf38ba4ce8702e9d30cce417cced58ff Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 16 Jul 2010 18:48:20 +0200 Subject: [PATCH] feature #203: Added rsa authentication --- install.sh | 3 +- src/authm_mad/one_auth_mad.rb | 5 +- src/authm_mad/oneauth | 153 ++++++++++++++++++++++++++++++++++ src/authm_mad/quota.rb | 3 + src/authm_mad/ssh_auth.rb | 85 +++++++++++++++++++ src/oca/ruby/OpenNebula.rb | 9 +- 6 files changed, 255 insertions(+), 3 deletions(-) create mode 100755 src/authm_mad/oneauth create mode 100644 src/authm_mad/ssh_auth.rb diff --git a/install.sh b/install.sh index 733ab6bb53..be49f69277 100755 --- a/install.sh +++ b/install.sh @@ -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 \ diff --git a/src/authm_mad/one_auth_mad.rb b/src/authm_mad/one_auth_mad.rb index 9c5e87878f..dc45082935 100755 --- a/src/authm_mad/one_auth_mad.rb +++ b/src/authm_mad/one_auth_mad.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], diff --git a/src/authm_mad/oneauth b/src/authm_mad/oneauth new file mode 100755 index 0000000000..194b126439 --- /dev/null +++ b/src/authm_mad/oneauth @@ -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 + +* 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 diff --git a/src/authm_mad/quota.rb b/src/authm_mad/quota.rb index 5e029c879c..1a7f8e004d 100644 --- a/src/authm_mad/quota.rb +++ b/src/authm_mad/quota.rb @@ -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 diff --git a/src/authm_mad/ssh_auth.rb b/src/authm_mad/ssh_auth.rb new file mode 100644 index 0000000000..c3a2b6f8a9 --- /dev/null +++ b/src/authm_mad/ssh_auth.rb @@ -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) + diff --git a/src/oca/ruby/OpenNebula.rb b/src/oca/ruby/OpenNebula.rb index 34360f0832..1e8412b8be 100644 --- a/src/oca/ruby/OpenNebula.rb +++ b/src/oca/ruby/OpenNebula.rb @@ -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