Class: Nexpose::DiscoveryConnection

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

Defined Under Namespace

Modules: CollectionMethod, EventSource, Protocol, Type Classes: Criteria, Criterion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMLUtils

#make_xml, #parse_xml, success?

Methods inherited from APIObject

#object_from_hash

Constructor Details

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

Create a new discovery connection.

Parameters:

  • name (String) (defaults to: nil)

    Name to assign to this connection.

  • address (String) (defaults to: nil)

    IP or fully qualified domain name of the connection server.

  • user (String) (defaults to: nil)

    User name for credentials on this connection.

  • password (String) (defaults to: nil)

    Password for credentials on this connection.



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

def initialize(name = nil, address = nil, user = nil, password = nil)
  @name, @address, @user, @password = name, address, user, password
  @type = nil  # for backwards compatibilitly, at some point should set this to Type::VSPHERE
  @id = -1
  @port = 443
  @protocol = Protocol::HTTPS
end

Instance Attribute Details

#addressObject

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



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

def address
  @address
end

#collection_methodObject

The collection method (e.g. for DHCP discovery connections)



99
100
101
# File 'lib/nexpose/discovery.rb', line 99

def collection_method
  @collection_method
end

#engine_idObject

The engine ID to use for this connection.



75
76
77
# File 'lib/nexpose/discovery.rb', line 75

def engine_id
  @engine_id
end

#event_sourceObject

The event source (e.g. for DHCP discovery connections)



102
103
104
# File 'lib/nexpose/discovery.rb', line 102

def event_source
  @event_source
end

#exchange_hostnameObject

The hostname of the exchange server to connect for exchange powershell connections



90
91
92
# File 'lib/nexpose/discovery.rb', line 90

def exchange_hostname
  @exchange_hostname
end

#exchange_passwordObject

The exchange password to connect for exchange powershell connections



96
97
98
# File 'lib/nexpose/discovery.rb', line 96

def exchange_password
  @exchange_password
end

#exchange_usernameObject

The exchange username to connect for exchange powershell connections



93
94
95
# File 'lib/nexpose/discovery.rb', line 93

def exchange_username
  @exchange_username
end

#idObject

A unique identifier for this connection.



63
64
65
# File 'lib/nexpose/discovery.rb', line 63

def id
  @id
end

#nameObject

A unique name for this connection.



66
67
68
# File 'lib/nexpose/discovery.rb', line 66

def name
  @name
end

#passwordObject

The password to use when connecting with the defined user.



81
82
83
# File 'lib/nexpose/discovery.rb', line 81

def password
  @password
end

#portObject

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



87
88
89
# File 'lib/nexpose/discovery.rb', line 87

def port
  @port
end

#protocolObject

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



84
85
86
# File 'lib/nexpose/discovery.rb', line 84

def protocol
  @protocol
end

#statusObject

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



106
107
108
# File 'lib/nexpose/discovery.rb', line 106

def status
  @status
end

#typeObject

Type of discovery connection



69
70
71
# File 'lib/nexpose/discovery.rb', line 69

def type
  @type
end

#userObject

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



78
79
80
# File 'lib/nexpose/discovery.rb', line 78

def user
  @user
end

Class Method Details

.parse(xml) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/nexpose/discovery.rb', line 220

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

#==(other) ⇒ Object



243
244
245
# File 'lib/nexpose/discovery.rb', line 243

def ==(other)
  eql?(other)
end

#as_xmlObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/nexpose/discovery.rb', line 197

def as_xml
  xml = REXML::Element.new('DiscoveryConnection')
  xml.attributes['name']              = @name
  xml.attributes['address']           = @address
  xml.attributes['port']              = @port
  xml.attributes['protocol']          = @protocol
  xml.attributes['user-name']         = @user
  xml.attributes['password']          = @password
  xml.attributes['exchange-hostname'] = @exchange_hostname if @exchange_hostname
  xml.attributes['exchange-username'] = @exchange_username if @exchange_username
  xml.attributes['exchange-password'] = @exchange_password if @exchange_password
  xml.attributes['type']              = @type if @type
  xml.attributes['collectionmethod']  = @collection_method if @collection_method
  xml.attributes['eventsource']       = @event_source if @event_source
  xml.attributes['engine-id'] = @engine_id if @engine_id && @engine_id != -1
  xml.attributes['id'] = @id if @id && @id != -1
  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:



183
184
185
186
187
# File 'lib/nexpose/discovery.rb', line 183

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

#create(nsc) ⇒ Object

Save this discovery connection on a given Nexpose console.

Parameters:



128
129
130
131
132
133
134
135
136
137
# File 'lib/nexpose/discovery.rb', line 128

def create(nsc)
  xml = nsc.make_xml('DiscoveryConnectionCreateRequest')
  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
end

#delete(nsc) ⇒ Object

Delete this connection from the console.

Parameters:



193
194
195
# File 'lib/nexpose/discovery.rb', line 193

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.



169
170
171
172
173
174
175
176
# File 'lib/nexpose/discovery.rb', line 169

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
# File 'lib/nexpose/discovery.rb', line 247

def eql?(other)
  id.eql?(other.id) &&
  name.eql?(other.name) &&
  type.eql?(other.type)
  # TODO Add remaining instance fields, once it is introduced in resource object
end

#save(nsc) ⇒ Object

Save this discovery connection to a Nexpose console.

Parameters:



156
157
158
159
160
# File 'lib/nexpose/discovery.rb', line 156

def save(nsc)
  @id == -1 ? create(nsc) : update(nsc)

  @id
end

#to_hObject



235
236
237
238
239
240
241
# File 'lib/nexpose/discovery.rb', line 235

def to_h
  { id: id,
    name: name,
    type: type
    # TODO Add remaining instance fields, once it is introduced in resource object
  }
end

#to_jsonObject



231
232
233
# File 'lib/nexpose/discovery.rb', line 231

def to_json
  JSON.generate(to_h)
end

#to_xmlObject



216
217
218
# File 'lib/nexpose/discovery.rb', line 216

def to_xml
  as_xml.to_s
end

#update(nsc) ⇒ Boolean

Update this (existing) discovery connection on a given Nexpose console.

Parameters:

Returns:

  • (Boolean)

    whether the update request was successful



144
145
146
147
148
149
150
# File 'lib/nexpose/discovery.rb', line 144

def update(nsc)
  xml = nsc.make_xml('DiscoveryConnectionUpdateRequest')
  xml.add_element(as_xml)

  response = nsc.execute(xml, '1.2')
  response.success
end