-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathdiscovery.rb
391 lines (355 loc) · 13.5 KB
/
discovery.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
module Nexpose
class Connection
# Retrieve information about all available connections for dynamic
# discovery of assets, including whether or not connections are active.
#
def list_discovery_connections
xml = make_xml('DiscoveryConnectionListingRequest')
response = execute(xml, '1.2')
connections = []
response.res.elements.each('DiscoveryConnectionListingResponse/DiscoveryConnectionSummary') do |conn|
connections << DiscoveryConnection.parse(conn)
end
connections
end
alias discovery_connections list_discovery_connections
# Delete an existing connection to a target used for dynamic discovery of assets.
#
# @param [Fixnum] id ID of an existing discovery connection.
#
def delete_discovery_connection(id)
xml = make_xml('DiscoveryConnectionDeleteRequest', { 'id' => id })
response = execute(xml, '1.2')
response.success
end
end
class DiscoveryConnection < APIObject
include XMLUtils
module CollectionMethod
DIRECTORY_WATCHER = 'DIRECTORY_WATCHER'
SYSLOG = 'SYSLOG'
end
module EventSource
INFOBLOX_TRINZIC = 'INFOBLOX_TRINZIC'
MICROSOFT_DHCP = 'MICROSOFT_DHCP'
end
module Protocol
HTTP = 'HTTP'
HTTPS = 'HTTPS'
LDAP = 'LDAP'
LDAPS = 'LDAPS'
SERVICE_PROXY = 'SERVICE_PROXY'
TCP = 'TCP'
UDP = 'UDP'
end
module Type
VSPHERE = 'VSPHERE'
AWS = 'AWS'
ACTIVESYNC = 'ACTIVESYNC'
ACTIVESYNC_POWERSHELL = 'ACTIVESYNC_POWERSHELL'
ACTIVESYNC_OFFICE365 = 'ACTIVESYNC_OFFICE365'
DHCP_SERVICE = 'DHCP_SERVICE'
end
# A unique identifier for this connection.
attr_accessor :id
# A unique name for this connection.
attr_accessor :name
# Type of discovery connection
attr_accessor :type
# The IP address or fully qualified domain name of the server.
attr_accessor :address
# The engine ID to use for this connection.
attr_accessor :engine_id
# A user name that can be used to log into the server.
attr_accessor :user
# The password to use when connecting with the defined user.
attr_accessor :password
# The protocol used for connecting to the server. One of DiscoveryConnection::Protocol
attr_accessor :protocol
# The port used for connecting to the server. A valid port from 1 to 65535.
attr_accessor :port
# The hostname of the exchange server to connect for exchange powershell connections
attr_accessor :exchange_hostname
# The exchange username to connect for exchange powershell connections
attr_accessor :exchange_username
# The exchange password to connect for exchange powershell connections
attr_accessor :exchange_password
# The collection method (e.g. for DHCP discovery connections)
attr_accessor :collection_method
# The event source (e.g. for DHCP discovery connections)
attr_accessor :event_source
# Whether or not the connection is active.
# Discovery is only possible when the connection is active.
attr_accessor :status
# Create a new discovery connection.
#
# @param [String] name Name to assign to this connection.
# @param [String] address IP or fully qualified domain name of the
# connection server.
# @param [String] user User name for credentials on this connection.
# @param [String] password Password for credentials on this connection.
#
def initialize(name = nil, address = nil, user = nil, password = nil)
@name = name
@address = address
@user = user
@password = password
@type = nil # For backwards compatibilitly, at some point should set this to Type::VSPHERE
@id = -1
@port = 443
@protocol = Protocol::HTTPS
end
# Save this discovery connection on a given Nexpose console.
#
# @param [Connection] nsc Connection to a console.
#
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
# Update this (existing) discovery connection on a given Nexpose console.
#
# @param [Connection] nsc Connection to a console.
# @return [Boolean] whether the update request was successful
#
def update(nsc)
xml = nsc.make_xml('DiscoveryConnectionUpdateRequest')
xml.add_element(as_xml)
response = nsc.execute(xml, '1.2')
response.success
end
# Save this discovery connection to a Nexpose console.
#
# @param [Connection] nsc Connection to a console.
#
def save(nsc)
@id == -1 ? create(nsc) : update(nsc)
@id
end
# Perform dynamic discover of assets against this connection.
#
# @param [Connection] nsc Connection to a console.
# @param [Criteria] criteria Criteria search object narrowing which assets
# to filter.
# @return [Array[DiscoveredAsset]] All discovered assets matching the criteria.
#
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
# Initiates a connection to a target used for dynamic discovery of assets.
# As long as a connection is active, dynamic discovery is continuous.
#
# @param [Connection] nsc Connection to a console.
#
def connect(nsc)
xml = nsc.make_xml('DiscoveryConnectionConnectRequest', { 'id' => id })
response = nsc.execute(xml, '1.2')
response.success
end
# Delete this connection from the console.
#
# @param [Connection] nsc Connection to a console.
#
def delete(nsc)
nsc.delete_discovery_connection(@id)
end
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
def to_xml
as_xml.to_s
end
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.engine_id = xml.attributes['engine-id'].to_i
conn
end
def to_json
JSON.generate(to_h)
end
def to_h
{
id: id,
name: name,
type: type
# TODO: Add remaining instance fields, once it is introduced in resource object
}
end
def ==(other)
eql?(other)
end
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
# Override of filter criterion to account for proper JSON naming.
#
class Criterion < Nexpose::Criterion
# Convert to Hash, which can be converted to JSON for API calls.
def to_h
{ operator: operator,
values: Array(value),
field_name: field }
end
# Create a Criterion object from a JSON-derived Hash.
#
# @param [Hash] json JSON-derived Hash of a Criterion object.
# @return [Criterion] Parsed object.
#
def self.parseHash(hash)
Criterion.new(hash[:field_name],
hash[:operator],
hash[:values])
end
end
# Override of filter criteria to account for different parsing from JSON.
#
class Criteria < Nexpose::Criteria
# Create a Criteria object from a Hash.
#
# @param [Hash] Hash of a Criteria object.
# @return [Criteria] Parsed object.
#
def self.parseHash(hash)
# The call returns empty JSON, so default to 'AND' if not present.
operator = hash[:operator] || 'AND'
ret = Criteria.new([], operator)
hash[:criteria].each do |c|
ret.criteria << Criterion.parseHash(c)
end
ret
end
end
end
class DiscoveredAsset
attr_accessor :name
attr_accessor :ip
attr_accessor :host
attr_accessor :datacenter
attr_accessor :cluster
attr_accessor :pool
attr_accessor :os
attr_accessor :status
def initialize(&block)
instance_eval(&block) if block_given?
end
def on?
@status == 'On'
end
def self.parse(json)
new do |asset|
asset.ip = json['IPAddress']
asset.os = json['OSName']
asset.name = json['assetDiscoveryName']
asset.cluster = json['cluster']
asset.datacenter = json['datacenter']
asset.host = json['host']
asset.status = json['powerStatus']
asset.pool = json['resourcePool']
end
end
end
class MobileDiscoveryConnection < DiscoveryConnection
# Create a new Mobile discovery connection.
#
# @param [String] name Name to assign to this connection.
# @param [DiscoveryConnection::Protocol] protocol The protocol to use for discovery - LDAPS or LDAP
# @param [String] address IP or fully qualified domain name of the
# connection server.
# @param [String] user User name for credentials on this connection.
# @param [String] password Password for credentials on this connection.
#
def initialize(name, protocol, address, user, password = nil)
@name = name
@protocol = protocol
@address = address
@user = user
@password = password
@type = Type::ACTIVESYNC
@id = -1
@port = 443 # port not used for mobile connection
end
end
class MobilePowershellDiscoveryConnection < DiscoveryConnection
# Create a new Mobile Powershell discovery connection.
#
# @param [String] name Name to assign to this connection.
# @param [String] address IP or fully qualified domain name of the
# WinRM server.
# @param [String] user WinRM User name for credentials on this connection.
# @param [String] password WinRM password for credentials on this connection.
# @param [String] exchange_hostname fully qualified domain name of the exchange server
# @param [String] exchange_username Exchange User name for exchange credentials on this connection.
# @param [String] exchange_password Exchange password for exchange credentials on this connection.
#
def initialize(name, address, user, password, exchange_hostname, exchange_username, exchange_password)
@name = name
@address = address
@user = user
@password = password
@protocol = Protocol::HTTPS
@exchange_hostname = exchange_hostname
@exchange_username = exchange_username
@exchange_password = exchange_password
@type = Type::ACTIVESYNC_POWERSHELL
@id = -1
@port = 443 # Port not used for mobile connection
end
end
class MobileOffice365DiscoveryConnection < DiscoveryConnection
# Create a new Mobile Office365 discovery connection.
#
# @param [String] name Name to assign to this connection.
# @param [String] address IP or fully qualified domain name of the
# WinRM server.
# @param [String] user WinRM User name for credentials on this connection.
# @param [String] password WinRM password for credentials on this connection.
# @param [String] exchange_username Exchange User name for exchange credentials on this connection.
# @param [String] exchange_password Exchange password for exchange credentials on this connection.
#
def initialize(name, address, user, password, exchange_username, exchange_password)
@name = name
@address = address
@user = user
@password = password
@protocol = Protocol::HTTPS
@exchange_hostname = '' # Nexpose will set to office365 server
@exchange_username = exchange_username
@exchange_password = exchange_password
@type = Type::ACTIVESYNC_OFFICE365
@id = -1
@port = 443 # Port not used for mobile connection
end
end
end