Class: Inspec::Resources::WindowsUser

Inherits:
UserInfo
  • Object
show all
Defined in:
lib/resources/users.rb

Overview

For now, we stick with WMI Win32_UserAccount using Get-AdUser would be the best command for domain machines, but it will not be installed on client machines by default Just for reference, we could also use ADSI (Active Directory Service Interfaces)

Instance Attribute Summary

Attributes inherited from UserInfo

#inspec

Instance Method Summary collapse

Methods inherited from UserInfo

#collect_user_details, #credentials, #initialize, #user_details

Methods included from Converter

#convert_to_i

Constructor Details

This class inherits a constructor from Inspec::Resources::UserInfo

Instance Method Details

#identity(username) ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/resources/users.rb', line 571

def identity(username)
  # extract domain/user information
  , domain = (username)

  # TODO: escape content
  if !domain.nil?
    filter = "Name = '#{}' and Domain = '#{domain}'"
  else
    filter = "Name = '#{}' and LocalAccount = true"
  end

  script = <<-EOH
    # find user
    $user = Get-WmiObject Win32_UserAccount -filter "#{filter}"
    # get related groups
    $groups = $user.GetRelated('Win32_Group') | Select-Object -Property Caption, Domain, Name, LocalAccount, SID, SIDType, Status
    # filter user information
    $user = $user | Select-Object -Property Caption, Description, Domain, Name, LocalAccount, Lockout, PasswordChangeable, PasswordExpires, PasswordRequired, SID, SIDType, Status, Disabled
    # build response object
    New-Object -Type PSObject | `
    Add-Member -MemberType NoteProperty -Name User -Value ($user) -PassThru | `
    Add-Member -MemberType NoteProperty -Name Groups -Value ($groups) -PassThru | `
    ConvertTo-Json
  EOH

  cmd = inspec.powershell(script)

  # cannot rely on exit code for now, successful command returns exit code 1
  # return nil if cmd.exit_status != 0, try to parse json
  begin
    params = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  user_hash = params['User'] || {}
  group_hashes = params['Groups'] || []
  # if groups is no array, generate one
  group_hashes = [group_hashes] unless group_hashes.is_a?(Array)
  group_names = group_hashes.map { |grp| grp['Caption'] }
  {
    uid: user_hash['SID'],
    username: user_hash['Caption'],
    gid: nil,
    group: nil,
    groups: group_names,
    disabled: user_hash['Disabled'],
  }
end

#list_usersObject



629
630
631
632
633
# File 'lib/resources/users.rb', line 629

def list_users
  script = 'Get-WmiObject Win32_UserAccount | Select-Object -ExpandProperty Caption'
  cmd = inspec.powershell(script)
  cmd.stdout.chomp.lines
end

#meta_info(_username) ⇒ Object

not implemented yet



622
623
624
625
626
627
# File 'lib/resources/users.rb', line 622

def meta_info(_username)
  {
    home: nil,
    shell: nil,
  }
end

#parse_windows_account(username) ⇒ Object

parse windows account name



564
565
566
567
568
569
# File 'lib/resources/users.rb', line 564

def (username)
   = username.split('\\')
  name = .pop
  domain = .pop if .size > 0
  [name, domain]
end