derive xinetd protocol from socket_type when not defined in the config file

Signed-off-by: Alex Pop <apop@chef.io>
This commit is contained in:
Alex Pop 2017-02-01 11:15:52 +00:00
parent a060e65b23
commit 495185b581
2 changed files with 29 additions and 2 deletions

View file

@ -92,8 +92,23 @@ module Inspec::Resources
params params
end end
# Method used to derive the default protocol used from the socket_type
def default_protocol(type)
case type
when 'stream'
'tcp'
when 'dgram'
'udp'
else
'unknown'
end
end
def service_lines def service_lines
@services ||= params['services'].values.flatten.map(&:params) @services ||= params['services'].values.flatten.map { |service|
service.params['protocol'] ||= default_protocol(service.params['socket_type'])
service.params
}
end end
end end
end end

View file

@ -34,12 +34,24 @@ describe 'Inspec::Resources::XinetdConf' do
_(one.ids).must_equal %w{chargen-dgram} _(one.ids).must_equal %w{chargen-dgram}
end end
it 'get all protocols' do it 'get all protocols for echo' do
one = resource.services('echo') one = resource.services('echo')
_(one.protocols).must_equal %w{tcp udp} _(one.protocols).must_equal %w{tcp udp}
_(one.ids).must_equal %w{echo-stream echo-dgram} _(one.ids).must_equal %w{echo-stream echo-dgram}
end end
it 'get all protocols for chargen, including derived from socket_type' do
one = resource.services('chargen')
_(one.protocols).must_equal %w{tcp udp}
_(one.ids).must_equal %w{chargen-stream chargen-dgram}
end
it 'params has only the protocols parsed from the config files' do
one = resource.params['services']['chargen'].map{|x| x.params['protocol']}
# in this example(CentOS), protocol is not defined in the config
_(one).must_equal [nil, nil]
end
it 'can filter by protocols' do it 'can filter by protocols' do
one = resource.services('echo') one = resource.services('echo')
_(one.protocols(/tcp.*/).ids).must_equal %w{echo-stream} _(one.protocols(/tcp.*/).ids).must_equal %w{echo-stream}