mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
use Inspec::Filter in xinetd resource
This commit is contained in:
parent
d86161c616
commit
652f10ad9a
2 changed files with 59 additions and 94 deletions
|
@ -3,9 +3,10 @@
|
|||
# author: Dominik Richter
|
||||
|
||||
require 'utils/parser'
|
||||
require 'utils/filter'
|
||||
|
||||
module Inspec::Resources
|
||||
class XinetdConf < Inspec.resource(1) # rubocop:disable Metrics/ClassLength
|
||||
class XinetdConf < Inspec.resource(1)
|
||||
name 'xinetd_conf'
|
||||
desc 'Xinetd services configuration.'
|
||||
example "
|
||||
|
@ -20,112 +21,45 @@ module Inspec::Resources
|
|||
|
||||
include XinetdParser
|
||||
|
||||
def initialize(conf_path = '/etc/xinetd.conf', opts = {})
|
||||
def initialize(conf_path = '/etc/xinetd.conf')
|
||||
@conf_path = conf_path
|
||||
@params = opts[:params] unless opts[:params].nil?
|
||||
@filters = opts[:filters] || ''
|
||||
@filters = ''
|
||||
@contents = {}
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Xinetd config #{@conf_path}"
|
||||
end
|
||||
|
||||
def services(condition = nil)
|
||||
condition.nil? ? params['services'].keys : filter(service: condition)
|
||||
end
|
||||
|
||||
def ids(condition = nil)
|
||||
condition.nil? ? services_field('id') : filter(id: condition)
|
||||
end
|
||||
|
||||
def socket_types(condition = nil)
|
||||
condition.nil? ? services_field('socket_type') : filter(socket_type: condition)
|
||||
end
|
||||
|
||||
def types(condition = nil)
|
||||
condition.nil? ? services_field('type') : filter(type: condition)
|
||||
end
|
||||
|
||||
def wait(condition = nil)
|
||||
condition.nil? ? services_field('wait') : filter(wait: condition)
|
||||
end
|
||||
|
||||
def disabled?
|
||||
filter(disable: 'no').services.empty?
|
||||
end
|
||||
|
||||
def enabled?
|
||||
filter(disable: 'yes').services.empty?
|
||||
"Xinetd config #{@conf_path}#{@filters}"
|
||||
end
|
||||
|
||||
def params
|
||||
return @params if defined?(@params)
|
||||
return @params = {} if read_content.nil?
|
||||
flat_params = parse_xinetd(read_content)
|
||||
@params = { 'services' => {} }
|
||||
flat_params.each do |k, v|
|
||||
name = k[/^service (.+)$/, 1]
|
||||
if name.nil?
|
||||
@params[k] = v
|
||||
else
|
||||
@params['services'][name] = v
|
||||
end
|
||||
end
|
||||
@params
|
||||
@params ||= read_params
|
||||
end
|
||||
|
||||
def filter(conditions = {})
|
||||
res = params.dup
|
||||
filters = ''
|
||||
conditions.each do |k, v|
|
||||
v = v.to_s if v.is_a? Integer
|
||||
filters += " #{k} = #{v.inspect}"
|
||||
res['services'] = filter_by(res['services'], k.to_s, v)
|
||||
end
|
||||
XinetdConf.new(@conf_path, params: res, filters: filters)
|
||||
extend Inspec::Filter
|
||||
add_filter 'service'
|
||||
add_filter 'id'
|
||||
add_filter 'socket_type'
|
||||
add_filter 'type'
|
||||
add_filter 'wait'
|
||||
|
||||
def disabled?
|
||||
where({ 'disable' => 'no' }).services.empty?
|
||||
end
|
||||
|
||||
def enabled?
|
||||
where({ 'disable' => 'yes' }).services.empty?
|
||||
end
|
||||
|
||||
def where(conditions = {})
|
||||
fields, filters = Inspec::Filter.where(service_lines, conditions)
|
||||
res = clone
|
||||
res.instance_variable_set(:@filters, @filters + filters)
|
||||
res.instance_variable_set(:@services, fields)
|
||||
res
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Retrieve the provided field from all configured services.
|
||||
#
|
||||
# @param [String] field name, e.g. `socket_type`
|
||||
# @return [Array[String]] all values of this field across services
|
||||
def services_field(field)
|
||||
params['services'].values.compact.flatten
|
||||
.map { |x| x.params[field] }.flatten.compact
|
||||
end
|
||||
|
||||
def match_condition(sth, condition)
|
||||
case sth
|
||||
# this does Regex-matching as well as string comparison
|
||||
when condition
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
# Filter services by a criteria. This allows for search queries for
|
||||
# certain values.
|
||||
#
|
||||
# @param [Hash] service collection
|
||||
# @param [String] search key you want to query
|
||||
# @param [Any] search value that the key should match
|
||||
# @return [Hash] filtered service collection
|
||||
def filter_by(services, k, v)
|
||||
if k == 'service'
|
||||
return Hash[services.find_all { |name, _| match_condition(v, name) }]
|
||||
end
|
||||
Hash[services.map { |name, service_arr|
|
||||
found = service_arr.find_all { |service|
|
||||
match_condition(service.params[k], v)
|
||||
}
|
||||
found.empty? ? nil : [name, found]
|
||||
}.compact]
|
||||
end
|
||||
|
||||
def read_content(path = @conf_path)
|
||||
return @contents[path] if @contents.key?(path)
|
||||
file = inspec.file(path)
|
||||
|
@ -140,5 +74,36 @@ module Inspec::Resources
|
|||
|
||||
@contents[path]
|
||||
end
|
||||
|
||||
def read_params
|
||||
return {} if read_content.nil?
|
||||
flat_params = parse_xinetd(read_content)
|
||||
params = { 'services' => {} }
|
||||
|
||||
# parse services that were defined:
|
||||
flat_params.each do |k, v|
|
||||
name = k[/^service (.+)$/, 1]
|
||||
if name.nil?
|
||||
params[k] = v
|
||||
else
|
||||
params['services'][name] = v
|
||||
# add the service identifier to its parameters
|
||||
v.each { |service| service.params['service'] = name }
|
||||
end
|
||||
end
|
||||
params
|
||||
end
|
||||
|
||||
def service_lines
|
||||
@services ||= params['services'].values.flatten.map(&:params)
|
||||
end
|
||||
|
||||
def get_fields(*fields)
|
||||
res = service_lines.map do |line|
|
||||
fields.map { |f| line[f] }
|
||||
end.flatten
|
||||
return res unless fields == ['service']
|
||||
res.uniq
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,7 +25,7 @@ describe 'Inspec::Resources::XinetdConf' do
|
|||
end
|
||||
|
||||
it 'can filter by name' do
|
||||
_(resource.services('not here').params['services']).must_be_empty
|
||||
_(resource.services('not here').services).must_be_empty
|
||||
end
|
||||
|
||||
it 'can chain filters' do
|
||||
|
|
Loading…
Reference in a new issue