Class: OneUserHelper
- Inherits:
-
OpenNebulaHelper::OneHelper
- Object
- OpenNebulaHelper::OneHelper
- OneUserHelper
- Defined in:
- lib/one_helper/oneuser_helper.rb
Overview
CLI helper for oneuser command
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_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
- #read_user ⇒ Object
- #token_create(args, options) ⇒ Object
Methods inherited from OpenNebulaHelper::OneHelper
#backup_mode_valid?, #check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, list_layout_help, #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, template_input_help, #to_id, to_id_desc, #user_name
Constructor Details
This class inherits a constructor from OpenNebulaHelper::OneHelper
Class Method Details
.conf_file ⇒ Object
51 52 53 |
# File 'lib/one_helper/oneuser_helper.rb', line 51 def self.conf_file 'oneuser.yaml' end |
.password_to_str(arg, options) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/one_helper/oneuser_helper.rb', line 59 def self.password_to_str(arg, ) if [:read_file] begin password = File.read(arg).split("\n").first rescue StandardError 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
55 56 57 |
# File 'lib/one_helper/oneuser_helper.rb', line 55 def self.password_to_str_desc 'User password' end |
.rname ⇒ Object
47 48 49 |
# File 'lib/one_helper/oneuser_helper.rb', line 47 def self.rname 'USER' end |
Instance Method Details
#auth_file(auth_string) ⇒ Object
119 120 121 122 |
# File 'lib/one_helper/oneuser_helper.rb', line 119 def auth_file(auth_string) auth_filename = Digest::MD5.hexdigest(auth_string) Dir.home + "/.one/#{auth_filename}.token" end |
#find_token(user, token, group = nil, show_expired = false) ⇒ Object
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/one_helper/oneuser_helper.rb', line 439 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| next if token && !e['TOKEN'].start_with?(token) next if group && e['EGID'].to_i != group.to_i exp_time = e['EXPIRATION_TIME'].to_i next if !show_expired && exp_time != -1 && Time.now > Time.at(exp_time) 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
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 |
# File 'lib/one_helper/oneuser_helper.rb', line 262 def format_pool() config_file = self.class.table_conf # rubocop:disable Style/FormatStringToken CLIHelper::ShowTable.new(config_file, self) do pool_default_quotas = lambda do |path| limit = @data.dsearch('/USER_POOL/DEFAULT_USER_QUOTAS/'+path) limit = OneQuotaHelper::LIMIT_UNLIMITED if limit.nil? || limit.empty? limit end quotas_proc = lambda do if !defined?(@quotas) quotas = @data.dsearch('USER_POOL/QUOTAS') @quotas = {} if !quotas.nil? quotas = [quotas].flatten quotas.each do |q| # Fix rare bug, when there are multiple VM_QUOTA values vm_quota = q['VM_QUOTA'] if vm_quota.is_a?(Array) q['VM_QUOTA'] = vm_quota.max_by {|h| h.size } end @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=>5 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_proc.call[d['ID']]['VM_QUOTA']['VM'] if q.nil? && d['ID'].to_i != 0 q = OneQuotaHelper::DEFAULT_VM_QUOTA end # In case of multiple quotas, use the global quota or the first if q.is_a?(Array) global_q = q.find {|h| h['CLUSTER_IDS'].nil? || h['CLUSTER_IDS'].empty? } q = global_q || q[0] end limit = q['VMS'] limit = pool_default_quotas.call('VM_QUOTA/VM/VMS') if limit == OneQuotaHelper::LIMIT_DEFAULT if limit == OneQuotaHelper::LIMIT_UNLIMITED format('%3d / -', q['VMS_USED']) else format('%3d / %3d', q['VMS_USED'], limit) end rescue NoMethodError '-' end end column :MEMORY, 'Total memory allocated to user VMs', :size=>15 do |d| begin q = quotas_proc.call[d['ID']]['VM_QUOTA']['VM'] if q.nil? && d['ID'].to_i != 0 q = OneQuotaHelper::DEFAULT_VM_QUOTA end # In case of multiple quotas, use the global quota or the first if q.is_a?(Array) global_q = q.find {|h| h['CLUSTER_IDS'].nil? || h['CLUSTER_IDS'].empty? } q = global_q || q[0] end limit = q['MEMORY'] limit = pool_default_quotas.call('VM_QUOTA/VM/MEMORY') if limit == OneQuotaHelper::LIMIT_DEFAULT if limit == OneQuotaHelper::LIMIT_UNLIMITED format('%6s / -', OpenNebulaHelper.unit_to_str(q['MEMORY_USED'].to_i, {}, 'M')) else format('%6s / %6s', OpenNebulaHelper.unit_to_str(q['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_proc.call[d['ID']]['VM_QUOTA']['VM'] if q.nil? && d['ID'].to_i != 0 q = OneQuotaHelper::DEFAULT_VM_QUOTA end # In case of multiple quotas, use the global quota or the first if q.is_a?(Array) global_q = q.find {|h| h['CLUSTER_IDS'].nil? || h['CLUSTER_IDS'].empty? } q = global_q || q[0] end limit = q['CPU'] limit = pool_default_quotas.call('VM_QUOTA/VM/CPU') if limit == OneQuotaHelper::LIMIT_DEFAULT if limit == OneQuotaHelper::LIMIT_UNLIMITED format('%3.1f / -', q['CPU_USED']) else format('%3.1f / %3.1f', q['CPU_USED'], limit) end rescue NoMethodError '-' end end column :PCI, 'Total PCIs allocated to user VMs', :size=>9 do |d| begin q = quotas_proc.call[d['ID']]['VM_QUOTA']['VM'] if q.nil? && d['ID'].to_i != 0 q = OneQuotaHelper::DEFAULT_VM_QUOTA end # In case of multiple quotas, use the global quota or the first if q.is_a?(Array) global_q = q.find {|h| h['CLUSTER_IDS'].nil? || h['CLUSTER_IDS'].empty? } q = global_q || q[0] end limit = q['PCI_DEV'] limit = pool_default_quotas.call('VM_QUOTA/VM/PCI_DEV') if limit == OneQuotaHelper::LIMIT_DEFAULT if limit == OneQuotaHelper::LIMIT_UNLIMITED format('%3s / -', q['PCI_DEV_USED']) else format('%3s / %3s', q['PCI_DEV_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, :PCI end # rubocop:enable Style/FormatStringToken end |
#get_login_client(username, options) ⇒ Object
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 193 194 195 196 197 198 199 |
# File 'lib/one_helper/oneuser_helper.rb', line 124 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] ||= Dir.home + '/.ssh/id_rsa' begin auth = OpenNebula::SshAuth.new(:private_key=>[:key]) rescue StandardError => 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 StandardError => 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 StandardError => 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
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 254 255 256 257 258 259 260 |
# File 'lib/one_helper/oneuser_helper.rb', line 204 def login(username, , use_client = false) if use_client login_client = OpenNebulaHelper::OneHelper.get_client else login_client = 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(0o0600, ONE_AUTH) return 0, token_info end |
#password(options) ⇒ Object
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 111 112 113 114 115 116 117 |
# File 'lib/one_helper/oneuser_helper.rb', line 83 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 StandardError => 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 StandardError => e return -1, e. end else return 0, '' end return 0, auth.password end |
#read_user ⇒ Object
474 475 476 477 478 479 480 481 482 |
# File 'lib/one_helper/oneuser_helper.rb', line 474 def read_user user = retrieve_resource(OpenNebula::User::SELF) rc = user.info if OpenNebula.is_error?(rc) puts rc. exit 1 end user end |
#token_create(args, options) ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/one_helper/oneuser_helper.rb', line 484 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 = read_user username = user['NAME'] use_client = true end login(username, , use_client) end |