Class: Nexpose::DiscoveryConnection

Inherits:
Object
  • Object
show all
Includes:
XMLUtils
Defined in:
lib/nexpose/discovery.rb

Defined Under Namespace

Modules: Protocol

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMLUtils

#make_xml, #parse_xml

Constructor Details

#initialize(name, address, user, password = nil) ⇒ DiscoveryConnection

Create a new discovery connection.

Parameters:

  • name (String)

    Name to assign to this connection.

  • address (String)

    IP or fully qualified domain name of the connection server.

  • user (String)

    User name for credentials on this connection.

  • password (String) (defaults to: nil)

    Password for credentials on this connection.



71
72
73
74
75
76
# File 'lib/nexpose/discovery.rb', line 71

def initialize(name, address, user, password = nil)
  @name, @address, @user, @password = name, address, user, password
  @id = -1
  @port = 443
  @protocol = Protocol::HTTPS
end

Instance Attribute Details

#addressObject

The IP address or fully qualified domain name of the server.



45
46
47
# File 'lib/nexpose/discovery.rb', line 45

def address
  @address
end

#idObject

A unique identifier for this connection.



39
40
41
# File 'lib/nexpose/discovery.rb', line 39

def id
  @id
end

#nameObject

A unique name for this connection.



42
43
44
# File 'lib/nexpose/discovery.rb', line 42

def name
  @name
end

#passwordObject

The password to use when connecting with the defined user.



51
52
53
# File 'lib/nexpose/discovery.rb', line 51

def password
  @password
end

#portObject

The port used for connecting to the server. A valid port from 1 to 65535.



57
58
59
# File 'lib/nexpose/discovery.rb', line 57

def port
  @port
end

#protocolObject

The protocol used for conneting to the server. One of DiscoveryConnection::Protocol



54
55
56
# File 'lib/nexpose/discovery.rb', line 54

def protocol
  @protocol
end

#statusObject

Whether or not the connection is active. Discovery is only possible when the connection is active.



61
62
63
# File 'lib/nexpose/discovery.rb', line 61

def status
  @status
end

#userObject

A user name that can be used to log into the server.



48
49
50
# File 'lib/nexpose/discovery.rb', line 48

def user
  @user
end

Class Method Details

.parse(xml) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/nexpose/discovery.rb', line 147

def self.parse(xml)
  conn = new(xml.attributes['name'],
             xml.attributes['address'],
             xml.attributes['user-name'])
  conn.id = xml.attributes['id'].to_i
  conn.protocol = xml.attributes['protocol']
  conn.port = xml.attributes['port'].to_i
  conn.status = xml.attributes['connection-status']
  conn
end

Instance Method Details

#as_xmlObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/nexpose/discovery.rb', line 132

def as_xml
  xml = REXML::Element.new('DiscoveryConnection')
  xml.add_attributes({ 'name' => @name,
                       'address' => @address,
                       'port' => @port,
                       'protocol' => @protocol,
                       'user-name' => @user,
                       'password' => @password })
  xml
end

#connect(nsc) ⇒ Object

Initiates a connection to a target used for dynamic discovery of assets. As long as a connection is active, dynamic discovery is continuous.

Parameters:



118
119
120
121
122
# File 'lib/nexpose/discovery.rb', line 118

def connect(nsc)
  xml = nsc.make_xml('DiscoveryConnectionConnectRequest', { 'id' => id })
  response = nsc.execute(xml, '1.2')
  response.success
end

#delete(nsc) ⇒ Object

Delete this connection from the console.

Parameters:



128
129
130
# File 'lib/nexpose/discovery.rb', line 128

def delete(nsc)
  nsc.delete_discovery_connection(@id)
end

#discover(nsc, criteria = nil) ⇒ Array[DiscoveredAsset]

Perform dynamic discover of assets against this connection.

Parameters:

  • nsc (Connection)

    Connection to a console.

  • criteria (Criteria) (defaults to: nil)

    Criteria search object narrowing which assets to filter.

Returns:

  • (Array[DiscoveredAsset])

    All discovered assets matching the criteria.



104
105
106
107
108
109
110
111
# File 'lib/nexpose/discovery.rb', line 104

def discover(nsc, criteria = nil)
  parameters = { 'table-id' => 'assetdiscovery',
                 'sort' => 'assetDiscoveryName',
                 'searchCriteria' => criteria.nil? ? 'null' : criteria.to_json,
                 'configID' => @id }
  data = DataTable._get_json_table(nsc, '/data/discoveryAsset/discoverAssets', parameters)
  data.map { |a| DiscoveredAsset.parse(a) }
end

#save(nsc) ⇒ Object

Save this discovery connection to a Nexpose console.

Parameters:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/nexpose/discovery.rb', line 82

def save(nsc)
  if @id == -1
    xml = nsc.make_xml('DiscoveryConnectionCreateRequest')
  else
    xml = nsc.make_xml('DiscoveryConnectionUpdateRequest')
  end
  xml.add_element(as_xml)
  response = nsc.execute(xml, '1.2')
  if response.success
    ret = REXML::XPath.first(response.res, 'DiscoveryConnectionCreateResponse')
    @id = ret.attributes['id'].to_i unless ret.nil?
  end
  @id
end

#to_xmlObject



143
144
145
# File 'lib/nexpose/discovery.rb', line 143

def to_xml
  as_xml.to_s
end