Class: Inspec::Resources::UnixUser
- Defined in:
- lib/resources/users.rb
Overview
implements generic unix id handling
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id_cmd ⇒ Object
readonly
Returns the value of attribute id_cmd.
-
#inspec ⇒ Object
readonly
Returns the value of attribute inspec.
-
#list_users_cmd ⇒ Object
readonly
Returns the value of attribute list_users_cmd.
Instance Method Summary collapse
-
#identity(username) ⇒ Object
extracts the identity.
-
#initialize(inspec) ⇒ UnixUser
constructor
A new instance of UnixUser.
-
#list_users ⇒ Object
returns a list of all local users on a system.
-
#parse_id_entries(raw) ⇒ Object
splits the results of id into seperate lines.
-
#parse_value(line) ⇒ Object
parse one id entry like ‘0(wheel)”.
Methods inherited from UserInfo
#collect_user_details, #credentials, #meta_info, #user_details
Methods included from Converter
Constructor Details
#initialize(inspec) ⇒ UnixUser
Returns a new instance of UnixUser.
338 339 340 341 342 343 |
# File 'lib/resources/users.rb', line 338 def initialize(inspec) @inspec = inspec @id_cmd ||= 'id' @list_users_cmd ||= 'cut -d: -f1 /etc/passwd | grep -v "^#"' super end |
Instance Attribute Details
#id_cmd ⇒ Object (readonly)
Returns the value of attribute id_cmd.
337 338 339 |
# File 'lib/resources/users.rb', line 337 def id_cmd @id_cmd end |
#inspec ⇒ Object (readonly)
Returns the value of attribute inspec.
337 338 339 |
# File 'lib/resources/users.rb', line 337 def inspec @inspec end |
#list_users_cmd ⇒ Object (readonly)
Returns the value of attribute list_users_cmd.
337 338 339 |
# File 'lib/resources/users.rb', line 337 def list_users_cmd @list_users_cmd end |
Instance Method Details
#identity(username) ⇒ Object
extracts the identity
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/resources/users.rb', line 364 def identity(username) cmd = inspec.command("#{id_cmd} #{username}") return nil if cmd.exit_status != 0 # parse words params = SimpleConfig.new( parse_id_entries(cmd.stdout.chomp), assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/, group_re: nil, multiple_values: false, ).params { uid: convert_to_i(parse_value(params['uid']).keys[0]), username: parse_value(params['uid']).values[0], gid: convert_to_i(parse_value(params['gid']).keys[0]), groupname: parse_value(params['gid']).values[0], groups: parse_value(params['groups']).values, } end |
#list_users ⇒ Object
returns a list of all local users on a system
346 347 348 349 350 |
# File 'lib/resources/users.rb', line 346 def list_users cmd = inspec.command(list_users_cmd) return [] if cmd.exit_status != 0 cmd.stdout.chomp.lines end |
#parse_id_entries(raw) ⇒ Object
splits the results of id into seperate lines
386 387 388 389 390 391 392 393 394 |
# File 'lib/resources/users.rb', line 386 def parse_id_entries(raw) data = [] until (index = raw.index(/\)\s{1}/)).nil? data.push(raw[0, index+1]) # inclue closing ) raw = raw[index+2, raw.length-index-2] end data.push(raw) if !raw.nil? data.join("\n") end |
#parse_value(line) ⇒ Object
parse one id entry like ‘0(wheel)”
353 354 355 356 357 358 359 360 361 |
# File 'lib/resources/users.rb', line 353 def parse_value(line) SimpleConfig.new( line, line_separator: ',', assignment_regex: /^\s*([^\(]*?)\s*\(\s*(.*?)\)*$/, group_re: nil, multiple_values: false, ).params end |