2015-07-27 21:24:46 +00:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
2015-10-06 16:55:44 +00:00
# author: Dominik Richter
# author: Christoph Hartmann
2015-07-27 21:24:46 +00:00
# license: All rights reserved
require 'utils/simpleconfig'
2015-08-03 03:11:01 +00:00
require 'utils/find_files'
2015-07-27 21:24:46 +00:00
2015-10-26 03:04:18 +00:00
class ApacheConf < Inspec . resource ( 1 )
2015-08-28 20:02:18 +00:00
name 'apache_conf'
2015-11-27 13:02:38 +00:00
desc 'Use the apache_conf InSpec audit resource to test the configuration settings for Apache. This file is typically located under /etc/apache2 on the Debian and Ubuntu platforms and under /etc/httpd on the Fedora, CentOS, Red Hat Enterprise Linux, and Arch Linux platforms. The configuration settings may vary significantly from platform to platform.'
example "
describe apache_conf do
its ( 'setting_name' ) { should eq 'value' }
end
"
2015-07-27 21:24:46 +00:00
2015-09-10 09:33:41 +00:00
include FindFiles
2015-10-26 21:41:02 +00:00
def initialize ( conf_path = nil )
@conf_path = conf_path || inspec . apache . conf_path
2015-08-28 20:02:18 +00:00
@conf_dir = File . dirname ( @conf_path )
2015-07-27 21:24:46 +00:00
@files_contents = { }
@content = nil
@params = nil
read_content
end
def content
@content || = read_content
end
2015-09-03 18:43:58 +00:00
def params ( * opts )
2015-07-27 21:24:46 +00:00
@params || read_content
res = @params
opts . each do | opt |
res = res [ opt ] unless res . nil?
end
res
end
2015-09-03 18:43:58 +00:00
def filter_comments ( data )
2015-09-03 18:35:23 +00:00
content = ''
2015-07-27 21:24:46 +00:00
data . each_line do | line |
2015-09-09 16:37:16 +00:00
if ! line . match ( / ^ \ s* # / )
2015-08-29 09:10:36 +00:00
content << line
2015-07-27 21:24:46 +00:00
end
end
2015-09-03 18:45:37 +00:00
content
2015-07-27 21:24:46 +00:00
end
def read_content
2015-09-03 18:35:23 +00:00
@content = ''
2015-07-27 21:24:46 +00:00
@params = { }
2015-08-03 03:28:32 +00:00
# skip if the main configuration file doesn't exist
2015-10-26 03:04:18 +00:00
file = inspec . file ( @conf_path )
2015-08-30 00:13:53 +00:00
if ! file . file?
2015-08-03 03:28:32 +00:00
return skip_resource " Can't find file \" #{ @conf_path } \" "
end
2015-08-28 20:02:18 +00:00
2015-08-29 09:10:36 +00:00
raw_conf = file . content
2015-08-28 20:02:18 +00:00
if raw_conf . empty? && file . size > 0
2015-08-03 03:35:18 +00:00
return skip_resource ( " Can't read file \" #{ @conf_path } \" " )
end
2015-08-03 03:28:32 +00:00
2015-07-27 21:24:46 +00:00
to_read = [ @conf_path ]
2015-09-09 16:37:16 +00:00
until to_read . empty?
2015-07-27 21:24:46 +00:00
raw_conf = read_file ( to_read [ 0 ] )
@content += raw_conf
# parse include file parameters
2015-09-05 14:07:54 +00:00
params = SimpleConfig . new (
raw_conf ,
2015-07-27 21:24:46 +00:00
assignment_re : / ^ \ s*( \ S+) \ s+(.*) \ s*$ / ,
2015-09-09 16:37:16 +00:00
multiple_values : true ,
2015-07-27 21:24:46 +00:00
) . params
@params . merge! ( params )
to_read = to_read . drop ( 1 )
2015-09-09 17:15:51 +00:00
to_read += include_files ( params ) . find_all do | fp |
2015-07-27 21:24:46 +00:00
not @files_contents . key? fp
end
end
# fiter comments
@content = filter_comments @content
@content
end
2015-09-09 17:15:51 +00:00
def include_files ( params )
# see if there is more config files to include
include_files = params [ 'Include' ] || [ ]
include_files_optional = params [ 'IncludeOptional' ] || [ ]
2015-12-04 07:59:44 +00:00
includes = [ ]
( include_files + include_files_optional ) . each do | f |
2015-09-09 17:15:51 +00:00
id = File . join ( @conf_dir , f )
2015-12-04 07:59:44 +00:00
files = find_files ( id , depth : 1 , type : 'file' )
2015-09-09 17:15:51 +00:00
2015-12-04 07:59:44 +00:00
includes . push ( files ) if files
2015-09-09 17:15:51 +00:00
end
2015-12-04 07:59:44 +00:00
# [].flatten! == nil
includes . flatten! || [ ]
2015-09-09 17:15:51 +00:00
end
2015-07-27 21:24:46 +00:00
def read_file ( path )
2015-10-26 03:04:18 +00:00
@files_contents [ path ] || = inspec . file ( path ) . content
2015-07-27 21:24:46 +00:00
end
2015-10-12 11:01:58 +00:00
def to_s
" Apache Config #{ @conf_path } "
end
2015-07-27 21:24:46 +00:00
end