Class: Inspec::Resources::ApacheConf
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- Inspec::Resources::ApacheConf
 show all
      - Includes:
- FileReader, FindFiles
    - Defined in:
- lib/inspec/resources/apache_conf.rb
 
  Constant Summary
  
  Constants included
     from FindFiles
  FindFiles::TYPES
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods included from FileReader
  #read_file_content
  
  
  
  
  
  
  
  
  Methods included from FindFiles
  #find_files, #find_files_or_warn
  Constructor Details
  
    
  
  
    #initialize(conf_path = nil)  ⇒ ApacheConf 
  
  
  
  
    
Returns a new instance of ApacheConf.
   
 
  
  
    | 
24
25
26
27
28
29
30 | # 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 | 
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(name)  ⇒ Object 
  
  
  
  
    | 
45
46
47
48
49
50
51 | # File 'lib/inspec/resources/apache_conf.rb', line 45
def method_missing(name)
    @params || read_content
    @params[name.to_s] unless @params.nil?
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #conf_path  ⇒ Object  
  
  
  
  
    
Returns the value of attribute conf_path.
   
 
  
  
    | 
22
23
24 | # File 'lib/inspec/resources/apache_conf.rb', line 22
def conf_path
  @conf_path
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #conf_dir  ⇒ Object 
  
  
  
  
    | 
130
131
132
133
134
135
136
137
138
139
140 | # File 'lib/inspec/resources/apache_conf.rb', line 130
def conf_dir
  if inspec.os.debian?
    File.dirname(conf_path)
  else
                    Pathname.new(File.dirname(conf_path)).parent.to_s
  end
end | 
 
    
      
  
  
    #content  ⇒ Object 
  
  
  
  
    | 
32
33
34 | # File 'lib/inspec/resources/apache_conf.rb', line 32
def content
  @content ||= read_content
end | 
 
    
      
  
  
    | 
53
54
55
56
57
58
59
60
61 | # File 'lib/inspec/resources/apache_conf.rb', line 53
def (data)
  content = ""
  data.each_line do |line|
    unless line.match(/^\s*#/)
      content << line
    end
  end
  content
end | 
 
    
      
  
  
    #include_files(params)  ⇒ Object 
  
  
  
  
    | 
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 | # File 'lib/inspec/resources/apache_conf.rb', line 108
def include_files(params)
    include_files = params["Include"] || []
  include_files_optional = params["IncludeOptional"] || []
  includes = []
  (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
    includes.flatten! || []
end | 
 
    
      
  
  
    #params(*opts)  ⇒ Object 
  
  
  
  
    | 
36
37
38
39
40
41
42
43 | # File 'lib/inspec/resources/apache_conf.rb', line 36
def params(*opts)
  @params || read_content
  res = @params
  opts.each do |opt|
    res = res[opt] unless res.nil?
  end
  res
end | 
 
    
      
  
  
    #read_content  ⇒ Object 
  
  
  
  
    | 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 | # File 'lib/inspec/resources/apache_conf.rb', line 63
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
                                    params = SimpleConfig.new(
      raw_conf,
      assignment_regex: /^\s*(\S+)\s+['"]*((?=.*\s+$).*?|.*?)['"]*\s*$/,
      multiple_values: true
    ).params
        params.values.map! do |value|
      value.map! do |sub_value|
        sub_value[/(?<=["|'])(?:\\.|[^"'\\])*(?=["|'])/] || sub_value
      end
    end
    @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
    @content =  @content
  @content
end | 
 
    
      
  
  
    #read_file(path)  ⇒ Object 
  
  
  
  
    | 
126
127
128 | # File 'lib/inspec/resources/apache_conf.rb', line 126
def read_file(path)
  @files_contents[path] ||= read_file_content(path, true)
end | 
 
    
      
  
  
    #to_s  ⇒ Object 
  
  
  
  
    | 
142
143
144 | # File 'lib/inspec/resources/apache_conf.rb', line 142
def to_s
  "Apache Config #{conf_path}"
end |