inspec/lib/resources/ssh_conf.rb

70 lines
1.4 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2015-07-15 13:15:18 +00:00
# copyright: 2015, Vulcano Security GmbH
# license: All rights reserved
require 'utils/simpleconfig'
class SshConf < Vulcano.resource(1)
name 'ssh_config'
2015-09-03 18:43:58 +00:00
def initialize(conf_path = nil, type = nil)
@conf_path = conf_path || '/etc/ssh/ssh_config'
2015-09-05 14:07:54 +00:00
typename = (@conf_path.include?('sshd') ? 'Server' : 'Client')
@type = type || "SSH #{typename} configuration #{conf_path}"
read_content
end
def to_s
@type
end
def content
@conf.content
end
2015-09-03 18:43:58 +00:00
def params(*opts)
res = @params
opts.each do |opt|
res = res[opt] unless res.nil?
end
res
end
2015-09-03 18:43:58 +00:00
def method_missing(name)
2015-09-08 23:07:08 +00:00
return nil if @params.nil?
param = @params[name.to_s]
# extract first value if we have only one value in array
param = param[0] if !param.nil? && param.length == 1
param
end
private
def read_content
@conf = vulcano.file(@conf_path)
# read the file
if !@conf.file?
return skip_resource "Can't find file \"#{@conf_path}\""
end
if @conf.content.empty? && @conf.size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
end
# parse the file
@params = SimpleConfig.new(
@conf.content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s*$/,
multiple_values: true,
).params
end
end
class SshdConf < SshConf
name 'sshd_config'
def initialize(path = nil)
super(path || '/etc/ssh/sshd_config')
end
end