Class: Inspec::Resources::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, params = {}) ⇒ Host

Returns a new instance of Host.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/resources/host.rb', line 43

def initialize(hostname, params = {})
  @hostname = hostname
  @port = params[:port]

  if params[:proto]
    warn '[DEPRECATION] The `proto` parameter is deprecated. Use `protocol` instead.'
    @protocol = params[:proto]
  else
    @protocol = params.fetch(:protocol, 'icmp')
  end

  return skip_resource 'Invalid protocol: only `tcp` and `icmp` protocols are support for the `host` resource.' unless
    %w{icmp tcp}.include?(@protocol)

  @host_provider = nil
  if inspec.os.linux?
    @host_provider = LinuxHostProvider.new(inspec)
  elsif inspec.os.windows?
    @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?
    return skip_resource "The following requirements are not met for this resource: #{missing_requirements.join(', ')}"
  end
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



41
42
43
# File 'lib/resources/host.rb', line 41

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



41
42
43
# File 'lib/resources/host.rb', line 41

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



41
42
43
# File 'lib/resources/host.rb', line 41

def protocol
  @protocol
end

Instance Method Details

#connectionObject



98
99
100
# File 'lib/resources/host.rb', line 98

def connection
  ping[:connection]
end

#ipaddressObject

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



107
108
109
# File 'lib/resources/host.rb', line 107

def ipaddress
  resolve.nil? || resolve.empty? ? nil : resolve
end

#protoObject



74
75
76
77
# File 'lib/resources/host.rb', line 74

def proto
  warn '[DEPRECATION] The `proto` method is deprecated. Use `protocol` instead.'
  protocol
end

#reachable?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/resources/host.rb', line 85

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) ⇒ Boolean

if we get the IP address, the host is resolvable

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/resources/host.rb', line 80

def resolvable?(type = nil)
  warn "The `host` resource ignores #{type} parameters. Continue to resolve host." if !type.nil?
  resolve.nil? || resolve.empty? ? false : true
end

#socketObject



102
103
104
# File 'lib/resources/host.rb', line 102

def socket
  ping[:socket]
end

#to_sObject



111
112
113
# File 'lib/resources/host.rb', line 111

def to_s
  "Host #{hostname}"
end