Class: Inspec::Resources::WindowsFilePermissions

Inherits:
FilePermissions show all
Defined in:
lib/inspec/resources/file.rb

Instance Attribute Summary

Attributes inherited from FilePermissions

#inspec

Instance Method Summary collapse

Methods inherited from FilePermissions

#initialize

Constructor Details

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

Instance Method Details

#check_file_permission_by_mask(_file, _access_type, _usergroup, _specific_user) ⇒ Object



318
319
320
# File 'lib/inspec/resources/file.rb', line 318

def check_file_permission_by_mask(_file, _access_type, _usergroup, _specific_user)
  raise "`check_file_permission_by_mask` is not supported on Windows"
end

#check_file_permission_by_user(access_type, user, path) ⇒ Object



326
327
328
329
330
331
332
# File 'lib/inspec/resources/file.rb', line 326

def check_file_permission_by_user(access_type, user, path)
  access_rule = translate_perm_names(access_type)
  access_rule = convert_to_powershell_array(access_rule)

  cmd = inspec.command("@(@((Get-Acl '#{path}').access | Where-Object {$_.AccessControlType -eq 'Allow' -and $_.IdentityReference -eq '#{user}' }) | Where-Object {($_.FileSystemRights.ToString().Split(',') | % {$_.trim()} | ? {#{access_rule} -contains $_}) -ne $null}) | measure | % { $_.Count }")
  cmd.stdout.chomp == "0" ? false : true
end

#inherited?(file) ⇒ Boolean

Returns:

  • (Boolean)


313
314
315
316
# File 'lib/inspec/resources/file.rb', line 313

def inherited?(file)
  cmd = inspec.command("(Get-Acl -Path #{file.path}).access| Where-Object {$_.IsInherited -eq $true} | measure | % { $_.Count }")
  cmd.stdout.chomp == "0" ? false : true
end

#more_permissive_than?Boolean

Returns:

  • (Boolean)

Raises:



322
323
324
# File 'lib/inspec/resources/file.rb', line 322

def more_permissive_than?(*)
  raise Inspec::Exceptions::ResourceSkipped, "The `more_permissive_than?` matcher is not supported on your OS yet."
end

#user_permissions(file) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/inspec/resources/file.rb', line 299

def user_permissions(file)
  script = <<-EOH
  $Acl = Get-Acl -Path #{file.path}
  $Result = foreach ($Access in $acl.Access) {
    [PSCustomObject]@{
      $Access.IdentityReference.Value  = $Access.FileSystemRights.ToString()
    }
  }
  $Result | ConvertTo-Json
  EOH
  result = inspec.powershell(script)
  JSON.load(result.stdout).inject(&:merge) unless result.stdout.empty?
end