class Chef::Resource::SelinuxFcontext

Public Instance Methods

current_file_context() click to toggle source
# File lib/chef/resource/selinux_fcontext.rb, line 61
def current_file_context
  file_hash = {
    "a" => "all files",
    "f" => "regular file",
    "d" => "directory",
    "c" => "character device",
    "b" => "block device",
    "s" => "socket",
    "l" => "symbolic link",
    "p" => "named pipe",
  }

  contexts = shell_out!("semanage fcontext -l").stdout.split("\n")
  # pull out file label from user:role:type:level context string
  contexts.grep(/^#{Regexp.escape(new_resource.file_spec)}\s+#{file_hash[new_resource.file_type]}/) do |c|
    c.match(/.+ (?<user>.+):(?<role>.+):(?<type>.+):(?<level>.+)$/)[:type]
    # match returns ['foo'] or [], shift converts that to 'foo' or nil
  end.shift
end
relabel_files() click to toggle source

Run restorecon to fix label github.com/sous-chefs/selinux_policy/pull/72#issuecomment-338718721

# File lib/chef/resource/selinux_fcontext.rb, line 83
def relabel_files
  spec = new_resource.file_spec
  escaped = Regexp.escape spec

  # find common path between regex and string
  common = if spec == escaped
             spec
           else
             index = spec.size.times { |i| break i if spec[i] != escaped[i] }
             ::File.dirname spec[0...index]
           end

  # if path is not absolute, ignore it and search everything
  common = "/" if common[0] != "/"

  if ::TargetIO::File.exist? common
    shell_out!("find #{common.shellescape} -ignore_readdir_race -regextype posix-egrep -regex #{spec.shellescape} -prune -print0 | xargs -0 restorecon -iRv")
  end
end