class Inspec::Resources::ApacheConf

Constants

PATHS

Attributes

conf_path[R]

Public Class Methods

new(conf_path = nil) click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 24
def initialize(conf_path = nil)
  @conf_path = conf_path || default_conf_path
  @files_contents = {}
  @content = nil
  @params = nil

  read_content
end

Public Instance Methods

conf_dir() click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 122
def conf_dir
  # apparently apache conf keys are case insensitive
  @params["ServerRoot"] || @params["serverroot"]
end
content() click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 33
def content
  @content ||= read_content
end
filter_comments(data) click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 49
def filter_comments(data)
  data.lines.grep_v(/^\s*#/).join
end
include_files(params) click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 98
def include_files(params)
  # see if there is more config files to include
  include_files = params["Include"] || []
  include_files_optional = params["IncludeOptional"] || []

  includes = []
  unless conf_dir.nil?
    (include_files + include_files_optional).each do |f|
      id    = Pathname.new(f).absolute? ? f : File.join(conf_dir, f)
      files = find_files(id, depth: 1, type: "file")
      files += find_files(id, depth: 1, type: "link")

      includes.push(files) if files
    end
  end

  # [].flatten! == nil
  includes.flatten! || []
end
method_missing(name) click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 45
def method_missing(name)
  @params[name.to_s]
end
params(*opts) click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 37
def params(*opts)
  res = @params
  opts.each do |opt|
    res = res[opt] unless res.nil?
  end
  res
end
read_content() click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 53
def read_content
  @content = ""
  @params = {}

  read_file_content(conf_path)

  to_read = [conf_path]
  until to_read.empty?
    raw_conf = read_file(to_read[0])
    @content += raw_conf

    # An explaination of the below regular expression.
    # Creates two capture groups.
    # The first group captures the first group of non-whitespace character
    # surrounded whitespace characters.
    # The second group contains a conditional with a positive lookahead
    # (does the line end with one or more spaces?). If the lookahead succeeds
    # a non-greedy capture takes place, if it fails then a greedy capture takes place.
    # The regex is terminated by an expression that matches zero or more spaces.
    params = SimpleConfig.new(
      raw_conf,
      assignment_regex: /^\s*(\S+)\s+['"]*((?=.*\s+$).*?|.*?)['"]*\s*$/,
      multiple_values: true
    ).params

    # Capture any characters between quotes that are not escaped in values
    params.values.each do |value|
      value.map! do |sub_value|
        sub_value[/(?<=["|'])(?:\\.|[^"'\\])*(?=["|'])/] || sub_value
      end
    end

    @params.merge!(params) { |key, current_val, new_val| [*current_val].to_a + [*new_val].to_a }

    to_read = to_read.drop(1)
    to_read += include_files(params).find_all do |fp|
      not @files_contents.key? fp
    end
  end

  # fiter comments
  @content = filter_comments @content
  @content
end
read_file(path) click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 118
def read_file(path)
  @files_contents[path] ||= read_file_content(path, true)
end
to_s() click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 127
def to_s
  "Apache Config #{conf_path}"
end

Private Instance Methods

default_conf_path() click to toggle source
# File lib/inspec/resources/apache_conf.rb, line 138
def default_conf_path
  @default ||= PATHS.find { |path| inspec.file(path).exist? }
end