class Inspec::Resources::Host

Attributes

hostname[R]
port[R]
protocol[R]

Public Class Methods

new(hostname, params = {}) click to toggle source
# File lib/inspec/resources/host.rb, line 47
def initialize(hostname, params = {})
  @hostname = hostname
  @port = params[:port]

  if params[:proto]
    Inspec.deprecate(:host_resource_proto_usage, "The `host` resource `proto` resource parameter is deprecated. Please use `protocol`.")
    @protocol = params[:proto]
  else
    @protocol = params.fetch(:protocol, "icmp")
  end

  @host_provider = nil
  if inspec.os.linux?
    @host_provider = LinuxHostProvider.new(inspec)
  elsif inspec.os.windows?
    return skip_resource "Invalid protocol: only `tcp` and `icmp` protocols are support for the `host` resource on your OS." unless
      %w{icmp tcp}.include?(@protocol)

    @host_provider = WindowsHostProvider.new(inspec)
  elsif inspec.os.darwin?
    @host_provider = DarwinHostProvider.new(inspec)
  else
    return skip_resource "The `host` resource is not supported on your OS yet."
  end

  missing_requirements = @host_provider.missing_requirements(protocol)
  unless missing_requirements.empty?
    skip_resource "The following requirements are not met for this resource: " \
      "#{missing_requirements.join(", ")}"
  end
end

Public Instance Methods

connection() click to toggle source
# File lib/inspec/resources/host.rb, line 103
def connection
  ping[:connection]
end
ipaddress() click to toggle source

returns all A records of the IP address, will return an array

# File lib/inspec/resources/host.rb, line 112
def ipaddress
  resolve.nil? || resolve.empty? ? nil : resolve
end
proto() click to toggle source
# File lib/inspec/resources/host.rb, line 79
def proto
  Inspec.deprecate(:host_resource_proto_usage, "The host resource `proto` method is deprecated. Please use `protocol`.")
  protocol
end
reachable?() click to toggle source
# File lib/inspec/resources/host.rb, line 90
def reachable?
  # ping checks do not require port or protocol
  return ping.fetch(:success, false) if protocol == "icmp"

  # if either port or protocol are specified but not both, we cannot proceed.
  if port.nil? || protocol.nil?
    raise "Protocol required with port. Use `host` resource with host('#{hostname}', port: 1234, proto: 'tcp') parameters." if port.nil? || protocol.nil?
  end

  # perform the protocol-specific reachability test
  ping.fetch(:success, false)
end
resolvable?(type = nil) click to toggle source

if we get the IP address, the host is resolvable

# File lib/inspec/resources/host.rb, line 85
def resolvable?(type = nil)
  warn "The `host` resource ignores #{type} parameters. Continue to resolve host." unless type.nil?
  resolve.nil? || resolve.empty? ? false : true
end
socket() click to toggle source
# File lib/inspec/resources/host.rb, line 107
def socket
  ping[:socket]
end
to_s() click to toggle source
# File lib/inspec/resources/host.rb, line 116
def to_s
  resource_name = "Host #{hostname}"
  resource_name += " port #{port} proto #{protocol}" if port

  resource_name
end

Private Instance Methods

ping() click to toggle source
# File lib/inspec/resources/host.rb, line 125
def ping
  return @ping_cache if defined?(@ping_cache)
  return {} if @host_provider.nil?

  @ping_cache = @host_provider.ping(hostname, port, protocol)
end
resolve() click to toggle source
# File lib/inspec/resources/host.rb, line 132
def resolve
  return @ip_cache if defined?(@ip_cache)

  @ip_cache = @host_provider.resolve(hostname) unless @host_provider.nil?
end