class Nexpose::AssetGroup

Asset group configuration object containing Device details.

Attributes

assets[RW]

Array of devices associated with this asset group.

description[RW]
devices[RW]

Array of devices associated with this asset group.

devices=[RW]

Array of devices associated with this asset group.

id[RW]
name[RW]
tags[RW]

Public Class Methods

load(connection, id) click to toggle source

Load an existing configuration from a Nexpose instance.

@param [Connection] connection Connection to console where asset group

is configured.

@param [Fixnum] id Asset group ID of an existing group. @return [AssetGroup] Asset group configuration loaded from a Nexpose

console.
# File lib/nexpose/group.rb, line 160
def self.load(connection, id)
  xml = %(<AssetGroupConfigRequest session-id="#{connection.session_id}" group-id="#{id}"/>)
  r   = APIRequest.execute(connection.url, xml, '1.1', { timeout: connection.timeout, open_timeout: connection.open_timeout })
  parse(r.res)
end
new(name, desc, id = -1, risk = 0.0) click to toggle source
# File lib/nexpose/group.rb, line 81
def initialize(name, desc, id = -1, risk = 0.0)
  @name        = name
  @description = desc
  @id          = id
  @risk_score  = risk
  @assets      = []
  @tags        = []
end
parse(xml) click to toggle source
# File lib/nexpose/group.rb, line 166
def self.parse(xml)
  return nil unless xml
  group = REXML::XPath.first(xml, 'AssetGroupConfigResponse/AssetGroup')
  asset_group = new(group.attributes['name'],
                    group.attributes['description'],
                    group.attributes['id'].to_i,
                    group.attributes['riskscore'].to_f)

  group.elements.each('Description') do |desc|
    asset_group.description = desc.text
  end

  group.elements.each('Devices/device') do |dev|
    asset_group.assets << Device.new(dev.attributes['id'].to_i,
                                     dev.attributes['address'],
                                     dev.attributes['site-id'].to_i,
                                     dev.attributes['riskfactor'].to_f,
                                     dev.attributes['riskscore'].to_f)
  end
  group.elements.each('Tags/Tag') do |tag|
    asset_group.tags << TagSummary.parse_xml(tag)
  end
  asset_group
end

Public Instance Methods

as_xml() click to toggle source

Generate an XML representation of this group configuration

@return [String] XML valid for submission as part of other requests.

# File lib/nexpose/group.rb, line 102
def as_xml
  xml = REXML::Element.new('AssetGroup')
  xml.attributes['id']          = @id
  xml.attributes['name']        = @name
  xml.attributes['description'] = @description

  if @description && !@description.empty?
    elem = REXML::Element.new('Description')
    elem.add_text(@description)
    xml.add_element(elem)
  end

  elem = REXML::Element.new('Devices')
  @assets.each { |a| elem.add_element('device', { 'id' => a.id }) }
  xml.add_element(elem)

  unless tags.empty?
    tag_xml = xml.add_element(REXML::Element.new('Tags'))
    @tags.each { |tag| tag_xml.add_element(tag.as_xml) }
  end

  xml
end
rescan_assets(connection) click to toggle source

Launch ad hoc scans against each group of assets per site.

@param [Connection] connection Connection to console where asset group

is configured.

@return [Hash] Hash of site ID to Scan launch information for each scan.

# File lib/nexpose/group.rb, line 142
def rescan_assets(connection)
  scans     = {}
  sites_ids = @assets.map(&:site_id).uniq
  sites_ids.each do |site_id|
    to_scan = @assets.select { |d| d.site_id == site_id }
    scans[site_id] = connection.scan_devices(to_scan)
  end
  scans
end
save(connection) click to toggle source
# File lib/nexpose/group.rb, line 90
def save(connection)
  xml = "<AssetGroupSaveRequest session-id='#{connection.session_id}'>"
  xml << to_xml
  xml << '</AssetGroupSaveRequest>'
  res = connection.execute(xml)
  @id = res.attributes['group-id'].to_i if res.success && @id < 1
end
to_xml() click to toggle source

Get an XML representation of the group that is valid for a save request. Note that only name, description, and asset ID information is accepted by a save request.

@return [String] XML representation of the asset group.

# File lib/nexpose/group.rb, line 132
def to_xml
  as_xml.to_s
end