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

Constructor Details

#initialize(conf_path = nil) ⇒ ApacheConf

Returns a new instance of ApacheConf.



15
16
17
18
19
20
21
22
# File 'lib/resources/apache_conf.rb', line 15

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



24
25
26
# File 'lib/resources/apache_conf.rb', line 24

def content
  @content ||= read_content
end

#filter_comments(data) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/resources/apache_conf.rb', line 37

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

#include_files(params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/resources/apache_conf.rb', line 86

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

  required = []
  include_files.each do |f|
    id = File.join(@conf_dir, f)
    required.push(find_files(id, depth: 1, type: 'file'))
  end

  optional = []
  include_files_optional.each do |f|
    id = File.join(@conf_dir, f)
    optional.push(find_files(id, depth: 1, type: 'file'))
  end

  required.flatten! + optional.flatten!
end

#params(*opts) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/resources/apache_conf.rb', line 28

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

#read_contentObject



47
48
49
50
51
52
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
# File 'lib/resources/apache_conf.rb', line 47

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



106
107
108
# File 'lib/resources/apache_conf.rb', line 106

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

#to_sObject



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

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