Class: OneUserHelper
- Inherits:
-
OpenNebulaHelper::OneHelper
- Object
- OpenNebulaHelper::OneHelper
- OneUserHelper
- Defined in:
- lib/one_helper/oneuser_helper.rb
Constant Summary collapse
- ONE_AUTH =
"/var/lib/one/.one/one_auth"
Instance Attribute Summary
Attributes inherited from OpenNebulaHelper::OneHelper
Class Method Summary collapse
- .conf_file ⇒ Object
- .password_to_str(arg, options) ⇒ Object
- .password_to_str_desc ⇒ Object
- .rname ⇒ Object
Instance Method Summary collapse
- #auth_file(auth_string) ⇒ Object
- #find_token(user, token, group = nil, show_expired = false) ⇒ Object
- #format_pool(options) ⇒ Object
- #get_client_user ⇒ Object
- #get_login_client(username, options) ⇒ Object
-
#login(username, options, use_client = false) ⇒ Object
Generates a token and stores it in ONE_AUTH path as defined in this class.
- #password(options) ⇒ Object
- #token_create(args, options) ⇒ Object
Methods inherited from OpenNebulaHelper::OneHelper
#check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, #to_id, to_id_desc, #user_name
Constructor Details
This class inherits a constructor from OpenNebulaHelper::OneHelper
Class Method Details
.conf_file ⇒ Object
44 45 46 |
# File 'lib/one_helper/oneuser_helper.rb', line 44 def self.conf_file "oneuser.yaml" end |
.password_to_str(arg, options) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/one_helper/oneuser_helper.rb', line 52 def self.password_to_str(arg, ) if [:read_file] begin password = File.read(arg).split("\n").first rescue return -1, "Cannot read file: #{arg}" end else password = arg.dup end if [:driver] == OpenNebula::User::X509_AUTH require 'opennebula/x509_auth' password = OpenNebula::X509Auth.escape_dn(password) end if [:sha256] || [:driver] == OpenNebula::User::CIPHER_AUTH require 'digest/sha2' password = Digest::SHA256.hexdigest(password) end return 0, password end |
.password_to_str_desc ⇒ Object
48 49 50 |
# File 'lib/one_helper/oneuser_helper.rb', line 48 def self.password_to_str_desc "User password" end |
.rname ⇒ Object
40 41 42 |
# File 'lib/one_helper/oneuser_helper.rb', line 40 def self.rname "USER" end |
Instance Method Details
#auth_file(auth_string) ⇒ Object
112 113 114 115 |
# File 'lib/one_helper/oneuser_helper.rb', line 112 def auth_file(auth_string) auth_filename = Digest::MD5.hexdigest(auth_string) ENV['HOME'] + "/.one/#{auth_filename}.token" end |
#find_token(user, token, group = nil, show_expired = false) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/one_helper/oneuser_helper.rb', line 451 def find_token(user, token, group=nil, show_expired=false) user_hash = user.to_hash valid_tokens = [user_hash["USER"]["LOGIN_TOKEN"]].flatten.compact return [] if valid_tokens.empty? valid_tokens.map! do |e| if token next if !e["TOKEN"].start_with?(token) end exp_time = e["EXPIRATION_TIME"].to_i if group next if e["EGID"].to_i != group.to_i end if !show_expired next if exp_time != -1 && Time.now > Time.at(exp_time) end e end.compact! # Sort the tokens so it returns first the one that will expire the # latest. valid_tokens.sort! do |a,b| a_exp = a["EXPIRATION_TIME"].to_i b_exp = b["EXPIRATION_TIME"].to_i if a_exp == -1 || b_exp == -1 a_exp <=> b_exp else b_exp <=> a_exp end end valid_tokens end |
#format_pool(options) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/one_helper/oneuser_helper.rb', line 255 def format_pool() config_file = self.class.table_conf table = CLIHelper::ShowTable.new(config_file, self) do def pool_default_quotas(path) @data.dsearch('/USER_POOL/DEFAULT_USER_QUOTAS/'+path) end def quotas if !defined?(@quotas) quotas = @data.dsearch('USER_POOL/QUOTAS') @quotas = Hash.new if (!quotas.nil?) quotas = [quotas].flatten quotas.each do |q| @quotas[q['ID']] = q end end end @quotas end column :ID, "ONE identifier for the User", :size=>4 do |d| d["ID"] end column :NAME, "Name of the User", :left, :size=>15 do |d| d["NAME"] end column :ENABLED, "User is enabled", :left, :size=>4 do |d| if d["ENABLED"] == "1" "yes" else "no" end end column :GROUP, "Group of the User", :left, :size=>10 do |d| helper.group_name(d, ) end column :AUTH, "Auth driver of the User", :left, :size=>8 do |d| d["AUTH_DRIVER"] end column :VMS , "Number of VMS", :size=>9 do |d| begin q = quotas[d['ID']] if q['VM_QUOTA']['VM'].nil? && d["ID"].to_i != 0 q['VM_QUOTA']['VM'] = { "VMS" => OneQuotaHelper::LIMIT_DEFAULT, "VMS_USED" => "0", "CPU" => OneQuotaHelper::LIMIT_DEFAULT, "CPU_USED" => "0", "MEMORY" => OneQuotaHelper::LIMIT_DEFAULT, "MEMORY_USED" => "0", "RUNNING_VMS" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_VMS_USED" => "0", "RUNNING_CPU" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_CPU_USED" => "0", "RUNNING_MEMORY" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_MEMORY_USED" => "0", "SYSTEM_DISK_SIZE" => OneQuotaHelper::LIMIT_DEFAULT, "SYSTEM_DISK_SIZE_USED" => "0" } quotas[d['ID']] = q end limit = q['VM_QUOTA']['VM']["VMS"] if limit == OneQuotaHelper::LIMIT_DEFAULT limit = pool_default_quotas("VM_QUOTA/VM/VMS") if limit.nil? || limit == "" limit = OneQuotaHelper::LIMIT_UNLIMITED end end if limit == OneQuotaHelper::LIMIT_UNLIMITED "%3d / -" % [q['VM_QUOTA']['VM']["VMS_USED"]] else "%3d / %3d" % [q['VM_QUOTA']['VM']["VMS_USED"], limit] end rescue NoMethodError "-" end end column :MEMORY, "Total memory allocated to user VMs", :size=>17 do |d| begin q = quotas[d['ID']] if q['VM_QUOTA']['VM'].nil? && d["ID"].to_i != 0 q['VM_QUOTA']['VM'] = { "VMS" => OneQuotaHelper::LIMIT_DEFAULT, "VMS_USED" => "0", "CPU" => OneQuotaHelper::LIMIT_DEFAULT, "CPU_USED" => "0", "MEMORY" => OneQuotaHelper::LIMIT_DEFAULT, "MEMORY_USED" => "0", "RUNNING_VMS" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_VMS_USED" => "0", "RUNNING_CPU" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_CPU_USED" => "0", "RUNNING_MEMORY" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_MEMORY_USED" => "0", "SYSTEM_DISK_SIZE" => OneQuotaHelper::LIMIT_DEFAULT, "SYSTEM_DISK_SIZE_USED" => "0" } quotas[d['ID']] = q end limit = q['VM_QUOTA']['VM']["MEMORY"] if limit == OneQuotaHelper::LIMIT_DEFAULT limit = pool_default_quotas("VM_QUOTA/VM/MEMORY") if limit.nil? || limit == "" limit = OneQuotaHelper::LIMIT_UNLIMITED end end if limit == OneQuotaHelper::LIMIT_UNLIMITED "%7s / -" % [ OpenNebulaHelper.unit_to_str(q['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M")] else "%7s / %7s" % [ OpenNebulaHelper.unit_to_str(q['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M"), OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")] end rescue NoMethodError "-" end end column :CPU, "Total CPU allocated to user VMs", :size=>11 do |d| begin q = quotas[d['ID']] if q['VM_QUOTA']['VM'].nil? && d["ID"].to_i != 0 q['VM_QUOTA']['VM'] = { "VMS" => OneQuotaHelper::LIMIT_DEFAULT, "VMS_USED" => "0", "CPU" => OneQuotaHelper::LIMIT_DEFAULT, "CPU_USED" => "0", "MEMORY" => OneQuotaHelper::LIMIT_DEFAULT, "MEMORY_USED" => "0", "RUNNING_VMS" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_VMS_USED" => "0", "RUNNING_CPU" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_CPU_USED" => "0", "RUNNING_MEMORY" => OneQuotaHelper::LIMIT_DEFAULT, "RUNNING_MEMORY_USED" => "0", "SYSTEM_DISK_SIZE" => OneQuotaHelper::LIMIT_DEFAULT, "SYSTEM_DISK_SIZE_USED" => "0" } quotas[d['ID']] = q end limit = q['VM_QUOTA']['VM']["CPU"] if limit == OneQuotaHelper::LIMIT_DEFAULT limit = pool_default_quotas("VM_QUOTA/VM/CPU") if limit.nil? || limit == "" limit = OneQuotaHelper::LIMIT_UNLIMITED end end if limit == OneQuotaHelper::LIMIT_UNLIMITED "%3.1f / -" % [q['VM_QUOTA']['VM']["CPU_USED"]] else "%3.1f / %3.1f" % [q['VM_QUOTA']['VM']["CPU_USED"], limit] end rescue NoMethodError "-" end end column :PASSWORD, "Password of the User", :size=>50 do |d| d['PASSWORD'] end default :ID, :NAME, :ENABLED, :GROUP, :AUTH, :VMS, :MEMORY, :CPU end table end |
#get_client_user ⇒ Object
492 493 494 495 496 497 498 499 500 |
# File 'lib/one_helper/oneuser_helper.rb', line 492 def get_client_user user = self.retrieve_resource(OpenNebula::User::SELF) rc = user.info if OpenNebula.is_error?(rc) puts rc. exit 1 end user end |
#get_login_client(username, options) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/one_helper/oneuser_helper.rb', line 117 def get_login_client(username, ) #----------------------------------------------------------------------- # Init the associated Authentication class to generate the token. #----------------------------------------------------------------------- case [:driver] when OpenNebula::User::SSH_AUTH require 'opennebula/ssh_auth' [:key] ||= ENV['HOME']+'/.ssh/id_rsa' begin auth = OpenNebula::SshAuth.new(:private_key=>[:key]) rescue Exception => e return -1, e. end when OpenNebula::User::X509_AUTH require 'opennebula/x509_auth' [:cert] ||= ENV['X509_USER_CERT'] [:key] ||= ENV['X509_USER_KEY'] begin certs = [File.read([:cert])] key = File.read([:key]) auth = OpenNebula::X509Auth.new(:certs_pem=>certs, :key_pem=>key) rescue Exception => e return -1, e. end when OpenNebula::User::X509_PROXY_AUTH require 'opennebula/x509_auth' [:proxy] ||= ENV['X509_PROXY_CERT'] begin proxy = File.read([:proxy]) certs = proxy.scan(/(-+BEGIN CERTIFICATE-+\n[^-]*\n-+END CERTIFICATE-+)/) certs.flatten! rc = proxy.match(/(-+BEGIN RSA PRIVATE KEY-+\n[^-]*\n-+END RSA PRIVATE KEY-+)/) key= rc[1] auth = OpenNebula::X509Auth.new(:certs_pem=>certs, :key_pem=>key) rescue => e return -1, e. end else auth = TokenAuth.new() #oned generated token end #----------------------------------------------------------------------- # Authenticate with oned using the token/passwd and set/generate the # authentication token for the user #----------------------------------------------------------------------- # This breaks the CLI SSL support for Ruby 1.8.7, but is necessary # in order to do template updates, otherwise you get the broken pipe # error (bug #3341) if RUBY_VERSION < '1.9' sync = false else sync = true end if [:stdin_password] token = STDIN.read.strip else token = auth.login_token(username, [:time]) end OpenNebula::Client.new("#{username}:#{token}", nil, :sync => sync) end |
#login(username, options, use_client = false) ⇒ Object
Generates a token and stores it in ONE_AUTH path as defined in this class
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/one_helper/oneuser_helper.rb', line 197 def login(username, , use_client=false) if use_client login_client = OpenNebulaHelper::OneHelper.get_client else login_client = self.get_login_client(username, ) end if (login_client.is_a? Array) && login_client[0] == -1 return login_client end user = OpenNebula::User.new(User.build_xml, login_client) egid = [:group] || -1 token_oned = user.login(username, "", [:time], egid) return -1, token_oned. if OpenNebula.is_error?(token_oned) token_info = "Authentication Token is:\n#{username}:#{token_oned}" #----------------------------------------------------------------------- # Check that ONE_AUTH target can be written #----------------------------------------------------------------------- if File.file?(ONE_AUTH) && ![:force] puts " * Do you want to overwrite the file #{ONE_AUTH}? (Y|N): " answer = STDIN.readline.chop case answer when 'Y', 'y', 'yes', 'YES', 'Yes' puts "overwriting #{ONE_AUTH} ..." when 'N', 'n', 'no', 'NO', 'No' return 0, "File #{ONE_AUTH} exists, use --force to overwrite."\ "\n#{token_info}" else puts "Not valid option." return -1 end end #----------------------------------------------------------------------- # Store the token in ONE_AUTH. #----------------------------------------------------------------------- begin FileUtils.mkdir_p(File.dirname(ONE_AUTH)) rescue Errno::EEXIST end file = File.open(ONE_AUTH, "w") file.write("#{username}:#{token_oned}") file.close File.chmod(0600, ONE_AUTH) return 0, token_info end |
#password(options) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/one_helper/oneuser_helper.rb', line 76 def password() case [:driver] when OpenNebula::User::SSH_AUTH if ![:key] return -1, "You have to specify the --key option" end require 'opennebula/ssh_auth' begin auth = OpenNebula::SshAuth.new(:private_key=>[:key]) rescue Exception => e return -1, e. end when OpenNebula::User::X509_AUTH [:cert] ||= ENV['X509_USER_CERT'] if ![:cert] return -1, "You have to specify the --cert option" end require 'opennebula/x509_auth' begin cert = [File.read([:cert])] auth = OpenNebula::X509Auth.new(:certs_pem=>cert) rescue Exception => e return -1, e. end else return 0, '' end return 0, auth.password end |
#token_create(args, options) ⇒ Object
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/one_helper/oneuser_helper.rb', line 502 def token_create(args, ) [:time] ||= 36000 if args[0] username = args[0] use_client = false else if !defined?(@@client) return -1, "No username in the argument or valid ONE_AUTH found." end user = self.get_client_user username = user['NAME'] use_client = true end self.login(username, , use_client) end |