class Inspec::Resources::SshConfig

Public Class Methods

new(conf_path = nil, type = nil) click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 22
def initialize(conf_path = nil, type = nil)
  @conf_path = conf_path || ssh_config_file("ssh_config")
  typename = (@conf_path.include?("sshd") ? "Server" : "Client")
  @type = type || "SSH #{typename} configuration #{conf_path}"
  read_content
end

Public Instance Methods

content() click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 29
def content
  read_content
end
convert_hash(hash) click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 39
def convert_hash(hash)
  new_hash = {}
  hash.each do |k, v|
    new_hash[k.downcase] ||= v
  end
  new_hash
end
method_missing(name) click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 47
def method_missing(name)
  param = read_params[name.to_s.downcase]
  return nil if param.nil?
  # extract first value if we have only one value in array
  return param[0] if param.length == 1

  param
end
params(*opts) click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 33
def params(*opts)
  opts.inject(read_params) do |res, nxt|
    res.respond_to?(:key) ? res[nxt] : nil
  end
end
to_s() click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 56
def to_s
  "SSH Configuration"
end

Private Instance Methods

read_content() click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 62
def read_content
  return @content if defined?(@content)

  @content = read_file_content(@conf_path)
end
read_params() click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 68
def read_params
  return @params if defined?(@params)
  return @params = {} if read_content.nil?

  conf = SimpleConfig.new(
    read_content,
    assignment_regex: /^\s*(\S+?)\s+(.*?)\s*$/,
    multiple_values: true
  )
  @params = convert_hash(conf.params)
end
ssh_config_file(type) click to toggle source
# File lib/inspec/resources/ssh_config.rb, line 80
def ssh_config_file(type)
  if inspec.os.windows?
    programdata = inspec.os_env("programdata").content
    return "#{programdata}\\ssh\\#{type}"
  end

  "/etc/ssh/#{type}"
end