2017-04-05 14:09:03 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'utils/erlang_parser'
|
2018-03-22 12:25:45 +00:00
|
|
|
require 'utils/file_reader'
|
2017-04-05 14:09:03 +00:00
|
|
|
|
|
|
|
module Inspec::Resources
|
|
|
|
class RabbitmqConf < Inspec.resource(1)
|
|
|
|
name 'rabbitmq_config'
|
2018-02-19 14:26:49 +00:00
|
|
|
supports platform: 'unix'
|
2017-04-05 14:09:03 +00:00
|
|
|
desc 'Use the rabbitmq_config InSpec resource to test configuration data '\
|
|
|
|
'for the RabbitMQ service located in /etc/rabbitmq/rabbitmq.config on '\
|
|
|
|
'Linux and UNIX platforms.'
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2017-04-05 14:09:03 +00:00
|
|
|
describe rabbitmq_config.params('rabbit', 'ssl_listeners') do
|
|
|
|
it { should cmp 5671 }
|
|
|
|
end
|
2019-03-19 14:17:32 +00:00
|
|
|
EXAMPLE
|
2017-04-05 14:09:03 +00:00
|
|
|
|
2018-03-22 12:25:45 +00:00
|
|
|
include FileReader
|
|
|
|
|
2017-04-05 14:09:03 +00:00
|
|
|
def initialize(conf_path = nil)
|
|
|
|
@conf_path = conf_path || '/etc/rabbitmq/rabbitmq.config'
|
2018-03-22 12:25:45 +00:00
|
|
|
@content = read_file_content(@conf_path, allow_empty: true)
|
2017-04-05 14:09:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def params(*opts)
|
|
|
|
opts.inject(read_params) do |res, nxt|
|
|
|
|
res.respond_to?(:key) ? res[nxt] : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"rabbitmq_config #{@conf_path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def read_content
|
|
|
|
return @content if defined?(@content)
|
2018-03-22 12:25:45 +00:00
|
|
|
@content = read_file_content(@conf_path, allow_empty: true)
|
2017-04-05 14:09:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_params
|
|
|
|
return @params if defined?(@params)
|
|
|
|
return @params = {} if read_content.nil?
|
|
|
|
@params = ErlangConfigFile.parse(read_content)
|
|
|
|
rescue Parslet::ParseFailed
|
|
|
|
raise "Cannot parse RabbitMQ config: \"#{read_content}\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|