Class: ApacheConf

Inherits:
Object
  • Object
show all
Includes:
FindFiles
Defined in:
lib/resources/apache_conf.rb

Constant Summary

Constants included from FindFiles

FindFiles::TYPES

Instance Method Summary collapse

Methods included from FindFiles

#find_files, #find_files_or_error

Constructor Details

#initialize(conf_path = nil) ⇒ ApacheConf

Returns a new instance of ApacheConf.



21
22
23
24
25
26
27
28
# File 'lib/resources/apache_conf.rb', line 21

def initialize(conf_path = nil)
  @conf_path = conf_path || inspec.apache.conf_path
  @conf_dir = File.dirname(@conf_path)
  @files_contents = {}
  @content = nil
  @params = nil
  read_content
end

Instance Method Details

#contentObject



30
31
32
# File 'lib/resources/apache_conf.rb', line 30

def content
  @content ||= read_content
end

#filter_comments(data) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/resources/apache_conf.rb', line 43

def filter_comments(data)
  content = ''
  data.each_line do |line|
    if !line.match(/^\s*#/)
      content << line
    end
  end
  content
end

#include_files(params) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/resources/apache_conf.rb', line 92

def include_files(params)
  # see if there is more config files to include
  include_files = params['Include'] || []
  include_files_optional = params['IncludeOptional'] || []

  includes = []
  (include_files + include_files_optional).each do |f|
    id = File.join(@conf_dir, f)
    files = find_files(id, depth: 1, type: 'file')

    includes.push(files) if files
  end

  # [].flatten! == nil
  includes.flatten! || []
end

#params(*opts) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/resources/apache_conf.rb', line 34

def params(*opts)
  @params || read_content
  res = @params
  opts.each do |opt|
    res = res[opt] unless res.nil?
  end
  res
end

#read_contentObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/resources/apache_conf.rb', line 53

def read_content
  @content = ''
  @params = {}

  # skip if the main configuration file doesn't exist
  file = inspec.file(@conf_path)
  if !file.file?
    return skip_resource "Can't find file \"#{@conf_path}\""
  end

  raw_conf = file.content
  if raw_conf.empty? && file.size > 0
    return skip_resource("Can't read file \"#{@conf_path}\"")
  end

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

    # parse include file parameters
    params = SimpleConfig.new(
      raw_conf,
      assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
      multiple_values: true,
    ).params
    @params.merge!(params)

    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) ⇒ Object



109
110
111
# File 'lib/resources/apache_conf.rb', line 109

def read_file(path)
  @files_contents[path] ||= inspec.file(path).content
end

#to_sObject



113
114
115
# File 'lib/resources/apache_conf.rb', line 113

def to_s
  "Apache Config #{@conf_path}"
end