Class: Nexpose::AssetGroup

Inherits:
AssetGroupSummary show all
Includes:
Sanitize
Defined in:
lib/nexpose/group.rb

Overview

Asset group configuration object containing Device details.

Instance Attribute Summary collapse

Attributes inherited from AssetGroupSummary

#risk_score

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sanitize

#replace_entities

Methods inherited from AssetGroupSummary

#delete

Constructor Details

#initialize(name, desc, id = -1,, risk = 0.0) ⇒ AssetGroup

Returns a new instance of AssetGroup.



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

def initialize(name, desc, id = -1, risk = 0.0)
  @name, @description, @id, @risk_score = name, desc, id, risk
  @assets = []
end

Instance Attribute Details

#assetsObject Also known as: devices

Array of devices associated with this asset group.



67
68
69
# File 'lib/nexpose/group.rb', line 67

def assets
  @assets
end

#descriptionObject

Returns the value of attribute description.



64
65
66
# File 'lib/nexpose/group.rb', line 64

def description
  @description
end

#idObject

Returns the value of attribute id.



64
65
66
# File 'lib/nexpose/group.rb', line 64

def id
  @id
end

#nameObject

Returns the value of attribute name.



64
65
66
# File 'lib/nexpose/group.rb', line 64

def name
  @name
end

Class Method Details

.load(connection, id) ⇒ AssetGroup

Load an existing configuration from a Nexpose instance.

Parameters:

  • connection (Connection)

    Connection to console where asset group is configured.

  • id (Fixnum)

    Asset group ID of an existing group.

Returns:

  • (AssetGroup)

    Asset group configuration loaded from a Nexpose console.



126
127
128
129
130
# File 'lib/nexpose/group.rb', line 126

def self.load(connection, id)
  xml = %(<AssetGroupConfigRequest session-id="#{connection.session_id}" group-id="#{id}"/>)
  r = APIRequest.execute(connection.url, xml)
  parse(r.res)
end

.parse(xml) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nexpose/group.rb', line 132

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('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
  asset_group
end

Instance Method Details

#rescan_assets(connection) ⇒ Hash

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

Parameters:

  • connection (Connection)

    Connection to console where asset group is configured.

Returns:

  • (Hash)

    Hash of site ID to Scan launch information for each scan.



108
109
110
111
112
113
114
115
116
# File 'lib/nexpose/group.rb', line 108

def rescan_assets(connection)
  sites_ids = @assets.map { |d| d.site_id }.uniq
  scans = {}
  sites_ids.each do |id|
    to_scan = @assets.select { |d| d.site_id == id }
    scans[id] = connection.scan_assets(to_scan)
  end
  scans
end

#save(connection) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/nexpose/group.rb', line 76

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 and @id < 1
end

#to_xmlString

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.

Returns:

  • (String)

    XML representation of the asset group.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nexpose/group.rb', line 90

def to_xml
  xml = %(<AssetGroup id="#{@id}" name="#{replace_entities(@name)}")
  xml << %( description="#{replace_entities(@description)}") if @description
  xml << '>'
  xml << '<Devices>'
  @assets.each do |asset|
    xml << %(<device id="#{asset.id}"/>)
  end
  xml << '</Devices>'
  xml << '</AssetGroup>'
end