2015-04-09 20:01:23 +00:00
# encoding: utf-8
2015-07-15 13:15:18 +00:00
# copyright: 2015, Vulcano Security GmbH
2015-04-09 20:01:23 +00:00
2015-09-30 10:19:55 +00:00
require 'utils/simpleconfig'
2015-08-03 03:11:01 +00:00
require 'utils/find_files'
2018-03-22 12:25:45 +00:00
require 'utils/file_reader'
2015-09-10 09:26:04 +00:00
require 'utils/hash'
2015-08-01 07:31:57 +00:00
require 'resources/mysql'
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
module Inspec::Resources
class MysqlConfEntry
def initialize ( path , params )
@params = params
@path = path
end
2015-08-28 19:41:48 +00:00
2016-03-08 18:06:55 +00:00
def method_missing ( name , * _ )
k = name . to_s
res = @params [ k ]
return true if res . nil? && @params . key? ( k )
@params [ k ]
end
2015-08-28 19:41:48 +00:00
2016-03-08 18:06:55 +00:00
def to_s
" MySQL Config entry [ #{ @path . join ( ' ' ) } ] "
end
2015-06-22 13:51:44 +00:00
end
2016-03-08 18:06:55 +00:00
class MysqlConf < Inspec . resource ( 1 )
name 'mysql_conf'
2018-02-19 14:26:49 +00:00
supports platform : 'unix'
supports platform : 'windows'
2016-03-08 18:06:55 +00:00
desc 'Use the mysql_conf InSpec audit resource to test the contents of the configuration file for MySQL, typically located at /etc/mysql/my.cnf or /etc/my.cnf.'
example "
describe mysql_conf ( 'path' ) do
its ( 'setting' ) { should eq 'value' }
end
2017-09-13 12:16:35 +00:00
# Test a parameter set within the [mysqld] section
describe mysql_conf do
its ( 'mysqld.port' ) { should cmp 3306 }
end
# Test a parameter set within the [mariadb] section using array notation
describe mysql_conf do
its ( [ 'mariadb' , 'max-connections' ] ) { should_not be_nil }
end
2016-03-08 18:06:55 +00:00
"
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
include FindFiles
2018-03-22 12:25:45 +00:00
include FileReader
2015-09-10 09:19:29 +00:00
2016-03-08 18:06:55 +00:00
def initialize ( conf_path = nil )
@conf_path = conf_path || inspec . mysql . conf_path
@files_contents = { }
@content = nil
@params = nil
read_content
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def content
@content || = read_content
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def params ( * opts )
@params || read_content
res = @params
opts . each do | opt |
res = res [ opt ] unless res . nil?
end
MysqlConfEntry . new ( opts , res )
2015-04-09 20:01:23 +00:00
end
2015-06-22 13:51:44 +00:00
2016-03-08 18:06:55 +00:00
def method_missing ( name )
@params || read_content
@params [ name . to_s ]
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def read_content
@content = ''
@params = { }
2015-08-03 03:27:34 +00:00
2016-03-08 18:06:55 +00:00
to_read = [ @conf_path ]
until to_read . empty?
cur_file = to_read [ 0 ]
raw_conf = read_file ( cur_file )
@content += raw_conf
2015-06-22 13:51:44 +00:00
2016-03-08 18:06:55 +00:00
params = SimpleConfig . new ( raw_conf ) . params
@params = @params . deep_merge ( params )
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
to_read = to_read . drop ( 1 )
# see if there is more stuff to include
2015-09-09 17:15:51 +00:00
2016-03-09 09:39:07 +00:00
dir = File . dirname ( cur_file )
2016-03-08 18:06:55 +00:00
to_read += include_files ( dir , raw_conf ) . find_all do | fp |
not @files_contents . key? fp
end
2015-04-09 20:01:23 +00:00
end
2016-03-08 18:06:55 +00:00
#
@content
2015-04-09 20:01:23 +00:00
end
2016-03-08 18:06:55 +00:00
def include_files ( reldir , conf )
files = conf . scan ( / ^!include \ s+(.*) \ s* / ) . flatten . compact . map { | x | abs_path ( reldir , x ) }
dirs = conf . scan ( / ^!includedir \ s+(.*) \ s* / ) . flatten . compact . map { | x | abs_path ( reldir , x ) }
dirs . map do | dir |
# @TODO: non local glob
files += find_files ( dir , depth : 1 , type : 'file' )
end
files
2015-09-09 17:15:51 +00:00
end
2016-03-08 18:06:55 +00:00
def abs_path ( dir , f )
return f if f . start_with? '/'
2016-03-09 09:39:07 +00:00
File . join ( dir , f )
2016-03-08 18:06:55 +00:00
end
2015-09-10 09:19:29 +00:00
2016-03-08 18:06:55 +00:00
def read_file ( path )
2018-03-22 12:25:45 +00:00
@files_contents [ path ] || = read_file_content ( path )
2016-03-08 18:06:55 +00:00
end
2015-10-12 11:01:58 +00:00
2016-03-08 18:06:55 +00:00
def to_s
'MySQL Configuration'
end
2015-10-12 11:01:58 +00:00
end
2015-04-09 20:01:23 +00:00
end