Sha256: 222deede2a8bb7716fb401f190ef51c3c9239ae7d0f1262af98d0199f8bbda2a

Contents?: true

Size: 1.93 KB

Versions: 15

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    class SELinux < BaseResolver
      @semaphore = Mutex.new
      @fact_list ||= {}

      class << self
        private

        def post_resolve(fact_name)
          @fact_list.fetch(fact_name) { retrieve_facts(fact_name) }
        end

        def retrieve_facts(fact_name)
          mountpoint = read_mounts_file if @fact_list[:enabled].nil?
          read_other_selinux_facts(mountpoint) if @fact_list[:enabled]

          @fact_list[fact_name]
        end

        def read_mounts_file
          output = Facter::Core::Execution.execute('cat /proc/self/mounts', logger: log)
          @fact_list[:enabled] = false
          mountpoint = ''

          output.each_line do |line|
            next unless line =~ /selinuxfs/

            @fact_list[:enabled] = true
            mountpoint = line.split("\s")[1]
            break
          end
          mountpoint
        end

        def read_other_selinux_facts(mountpoint)
          return unless read_selinux_config

          enforce_file = "#{mountpoint}/enforce"
          policy_file = "#{mountpoint}/policyvers"

          @fact_list[:policy_version] = Util::FileHelper.safe_read(policy_file, nil)

          enforce = Util::FileHelper.safe_read(enforce_file)
          if enforce.eql?('1')
            @fact_list[:enforced] = true
            @fact_list[:current_mode] = 'enforcing'
          else
            @fact_list[:enforced] = false
            @fact_list[:current_mode] = 'permissive'
          end
        end

        def read_selinux_config
          file_lines = Util::FileHelper.safe_readlines('/etc/selinux/config')

          file_lines.map do |line|
            @fact_list[:config_mode] = line.split('=').last.strip if line =~ /^SELINUX=/
            @fact_list[:config_policy] = line.split('=').last.strip if line =~ /^SELINUXTYPE=/
          end

          true unless file_lines.empty?
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
facter-4.0.36 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.35 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.34 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.33 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.32 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.31 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.30 lib/facter/resolvers/selinux_resolver.rb
facter-4.0.29 lib/resolvers/selinux_resolver.rb
facter-4.0.28 lib/resolvers/selinux_resolver.rb
facter-4.0.26 lib/resolvers/selinux_resolver.rb
facter-4.0.25 lib/resolvers/selinux_resolver.rb
facter-4.0.24 lib/resolvers/selinux_resolver.rb
facter-4.0.23 lib/resolvers/selinux_resolver.rb
facter-4.0.22 lib/resolvers/selinux_resolver.rb
facter-4.0.21 lib/resolvers/selinux_resolver.rb