mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
5fee525be8
Removes skip_resource and raise .. if InSpec.os stuff from initialize as this is covered by platform support. Signed-off-by: Miah Johnson <miah@chia-pet.org>
82 lines
2.1 KiB
Ruby
82 lines
2.1 KiB
Ruby
# encoding: utf-8
|
|
|
|
require 'utils/parser'
|
|
|
|
class EtcHosts < Inspec.resource(1)
|
|
name 'etc_hosts'
|
|
supports platform: 'linux'
|
|
supports platform: 'bsd'
|
|
supports platform: 'windows'
|
|
desc 'Use the etc_hosts InSpec audit resource to find an
|
|
ip_address and its associated hosts'
|
|
example "
|
|
describe etc_hosts.where { ip_address == '127.0.0.1' } do
|
|
its('ip_address') { should cmp '127.0.0.1' }
|
|
its('primary_name') { should cmp 'localhost' }
|
|
its('all_host_names') { should eq [['localhost', 'localhost.localdomain', 'localhost4', 'localhost4.localdomain4']] }
|
|
end
|
|
"
|
|
|
|
attr_reader :params
|
|
|
|
include CommentParser
|
|
|
|
def initialize(hosts_path = nil)
|
|
@conf_path = hosts_path || default_hosts_file_path
|
|
@content = nil
|
|
@params = nil
|
|
read_content
|
|
end
|
|
|
|
filter = FilterTable.create
|
|
filter.add_accessor(:where)
|
|
.add_accessor(:entries)
|
|
.add(:ip_address, field: 'ip_address')
|
|
.add(:primary_name, field: 'primary_name')
|
|
.add(:all_host_names, field: 'all_host_names')
|
|
|
|
filter.connect(self, :params)
|
|
|
|
private
|
|
|
|
def default_hosts_file_path
|
|
inspec.os.windows? ? 'C:\windows\system32\drivers\etc\hosts' : '/etc/hosts'
|
|
end
|
|
|
|
def read_content
|
|
@content = ''
|
|
@params = {}
|
|
@content = read_file(@conf_path)
|
|
@params = parse_conf(@content)
|
|
end
|
|
|
|
def parse_conf(content)
|
|
content.map do |line|
|
|
data, = parse_comment_line(line, comment_char: '#', standalone_comments: false)
|
|
parse_line(data) unless data == ''
|
|
end.compact
|
|
end
|
|
|
|
def parse_line(line)
|
|
line_parts = line.split
|
|
return nil unless line_parts.length >= 2
|
|
{
|
|
'ip_address' => line_parts[0],
|
|
'primary_name' => line_parts[1],
|
|
'all_host_names' => line_parts[1..-1],
|
|
}
|
|
end
|
|
|
|
def read_file(conf_path = @conf_path)
|
|
file = inspec.file(conf_path)
|
|
if !file.file?
|
|
return skip_resource "Can't find file. \"#{@conf_path}\""
|
|
end
|
|
|
|
raw_conf = file.content
|
|
if raw_conf.empty? && !file.empty?
|
|
return skip_resource("Could not read file contents\" #{@conf_path}\"")
|
|
end
|
|
raw_conf.lines
|
|
end
|
|
end
|