mirror of
https://github.com/inspec/inspec
synced 2024-11-27 15:10:44 +00:00
improvement: file resource check precondition and add file permission check
This commit is contained in:
parent
98d5a40686
commit
d75e16546c
1 changed files with 53 additions and 9 deletions
|
@ -30,25 +30,69 @@ module Vulcano::Resources
|
|||
end
|
||||
|
||||
def readable?(by_owner, by_user)
|
||||
m = unix_mode_mask(by_owner, 'r') ||
|
||||
by_owner, by_user = check_preconditions(by_owner, by_user)
|
||||
|
||||
if by_user.nil?
|
||||
m = @file.unix_mode_mask(by_owner, 'r') ||
|
||||
fail("#{by_owner} is not a valid unix owner.")
|
||||
(@file.mask & m) != 0
|
||||
(@file.mode & m) != 0
|
||||
else
|
||||
check_user_access(by_user, @path, 'r')
|
||||
end
|
||||
end
|
||||
|
||||
def writable?(by_owner, by_user)
|
||||
m = unix_mode_mask(by_owner, 'w') ||
|
||||
by_owner, by_user = check_preconditions(by_owner, by_user)
|
||||
|
||||
if by_user.nil?
|
||||
m = @file.unix_mode_mask(by_owner, 'w') ||
|
||||
fail("#{by_owner} is not a valid unix owner.")
|
||||
(@file.mask & m) != 0
|
||||
(@file.mode & m) != 0
|
||||
else
|
||||
check_user_access(by_user, @path, 'w')
|
||||
end
|
||||
end
|
||||
|
||||
def executable?(by_owner, by_user)
|
||||
m = unix_mode_mask(by_owner, 'x') ||
|
||||
by_owner, by_user = check_preconditions(by_owner, by_user)
|
||||
|
||||
if by_user.nil?
|
||||
m = @file.unix_mode_mask(by_owner, 'x') ||
|
||||
fail("#{by_owner} is not a valid unix owner.")
|
||||
(@file.mask & m) != 0
|
||||
(@file.mode & m) != 0
|
||||
else
|
||||
check_user_access(by_user, @path, 'x')
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
"File #{@path}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_preconditions(by_owner, by_user)
|
||||
by_owner = 'other' if by_owner == 'others'
|
||||
by_owner = 'all' if (by_owner.nil? || by_owner.empty?) && (by_user.nil?)
|
||||
[by_owner, by_user]
|
||||
end
|
||||
|
||||
# check permissions on linux
|
||||
def check_user_access(user, file, flag)
|
||||
if vulcano.os.linux? == true
|
||||
# use sh on linux
|
||||
perm_cmd = "su -s /bin/sh -c \"test -#{flag} #{file}\" #{user}"
|
||||
elsif vulcano.os[:family] == 'freebsd'
|
||||
# use sudo on freebsd
|
||||
perm_cmd = "sudo -u #{user} test -#{flag} #{file}"
|
||||
end
|
||||
|
||||
if !perm_cmd.nil?
|
||||
cmd = vulcano.command(perm_cmd)
|
||||
cmd.exit_status == 0 ? true : false
|
||||
else
|
||||
return skip_resource 'The `file` resource does not support `by_user` on your OS.'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue