Class: Nexpose::Tag

Inherits:
TagSummary show all
Defined in:
lib/nexpose/tag.rb,
lib/nexpose/tag/criteria.rb

Overview

Tag object containing tag details

Defined Under Namespace

Modules: Type Classes: Criteria, Criterion

Instance Attribute Summary collapse

Attributes inherited from TagSummary

#id, #name, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TagSummary

#as_xml, parse_xml

Constructor Details

#initialize(name, type, id = -1)) ⇒ Tag

Returns a new instance of Tag.



209
210
211
212
213
214
215
# File 'lib/nexpose/tag.rb', line 209

def initialize(name, type, id = -1)
  @name   = name
  @type   = type
  @id     = id
  @source = 'nexpose-client'
  @color  = @type == Type::Generic::CUSTOM ? Type::Color::DEFAULT : nil
end

Instance Attribute Details

#asset_group_idsObject Also known as: group_ids

Array containing Asset Group IDs to be associated with tag



202
203
204
# File 'lib/nexpose/tag.rb', line 202

def asset_group_ids
  @asset_group_ids
end

#asset_idsObject

Array containing Asset IDs to be associated with tag



196
197
198
# File 'lib/nexpose/tag.rb', line 196

def asset_ids
  @asset_ids
end

#associated_asset_idsObject

Array containing Asset IDs directly associated with the tag



199
200
201
# File 'lib/nexpose/tag.rb', line 199

def associated_asset_ids
  @associated_asset_ids
end

#colorObject

HEX color code of tag



187
188
189
# File 'lib/nexpose/tag.rb', line 187

def color
  @color
end

#risk_modifierObject

Risk modifier



190
191
192
# File 'lib/nexpose/tag.rb', line 190

def risk_modifier
  @risk_modifier
end

#search_criteriaObject

A TagCriteria



207
208
209
# File 'lib/nexpose/tag.rb', line 207

def search_criteria
  @search_criteria
end

#site_idsObject

Array containing Site IDs to be associated with tag



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

def site_ids
  @site_ids
end

#sourceObject

Creation source



184
185
186
# File 'lib/nexpose/tag.rb', line 184

def source
  @source
end

Class Method Details

.create(hash) ⇒ Object

Create tag object from hash



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/nexpose/tag.rb', line 238

def self.create(hash)
  attributes = hash[:attributes]
  color      = attributes.find { |attr| attr[:tag_attribute_name] == 'COLOR' }
  color      = color[:tag_attribute_value] if color
  source     = attributes.find { |attr| attr[:tag_attribute_name] == 'SOURCE' }
  source     = source[:tag_attribute_value] if source
  tag        = Tag.new(hash[:tag_name], hash[:tag_type], hash[:tag_id])
  tag.color  = color
  tag.source = source
  tag
end

.load(connection, tag_id) ⇒ Tag

Retrieve detailed description of a single tag

Parameters:

  • connection (Connection)

    Nexpose connection

  • tag_id (Fixnum)

    ID of tag to retrieve

Returns:

  • (Tag)

    requested tag



284
285
286
287
# File 'lib/nexpose/tag.rb', line 284

def self.load(connection, tag_id)
  json = JSON.parse(AJAX.get(connection, "/api/2.0/tags/#{tag_id}"))
  Tag.parse(json)
end

.load_tags(tags) ⇒ Object

Create list of tag objects from hash



228
229
230
231
232
233
234
235
# File 'lib/nexpose/tag.rb', line 228

def self.load_tags(tags)
  unless tags.nil?
    tags = tags.map do |hash|
      self.create(hash)
    end
  end
  tags
end

.parse(json) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/nexpose/tag.rb', line 313

def self.parse(json)
  color         = json['attributes'].find { |attr| attr['tag_attribute_name'] == 'COLOR' }
  color         = color['tag_attribute_value'] if color
  source        = json['attributes'].find { |attr| attr['tag_attribute_name'] == 'SOURCE' }
  source        = source['tag_attribute_value'] if source
  tag           = Tag.new(json['tag_name'], json['tag_type'], json['tag_id'])
  tag.color     = color
  tag.source    = source
  tag.asset_ids = json['asset_ids']
  if json['tag_config']
    tag.site_ids             = json['tag_config']['site_ids']
    tag.associated_asset_ids = json['tag_config']['tag_associated_asset_ids']
    tag.asset_group_ids      = json['tag_config']['asset_group_ids']
    criteria                 = json['tag_config']['search_criteria']
    tag.search_criteria      = criteria ? Criteria.parse(criteria) : nil
  end
  modifier = json['attributes'].find { |attr| attr['tag_attribute_name'] == 'RISK_MODIFIER' }
  tag.risk_modifier = modifier['tag_attribute_value'].to_i if modifier
  tag
end

Instance Method Details

#add_to_asset(connection, asset_id) ⇒ Fixnum

Adds a tag to an asset

Parameters:

  • connection (Connection)

    Nexpose connection

  • asset_id (Fixnum)

    of the asset to be tagged

Returns:

  • (Fixnum)

    ID of applied tag



340
341
342
343
344
345
# File 'lib/nexpose/tag.rb', line 340

def add_to_asset(connection, asset_id)
  params = to_json_for_add
  url    = "/api/2.0/assets/#{asset_id}/tags"
  uri    = AJAX.post(connection, url, params, AJAX::CONTENT_TYPE::JSON)
  @id    = uri.split('/').last.to_i
end

#add_to_group(connection, group_id) ⇒ Fixnum Also known as: add_to_asset_group

Adds a tag to an asset group

Parameters:

  • connection (Connection)

    Nexpose connection

  • group_id (Fixnum)

    id of the asset group to be tagged

Returns:

  • (Fixnum)

    ID of applied tag



366
367
368
369
370
371
# File 'lib/nexpose/tag.rb', line 366

def add_to_group(connection, group_id)
  params = to_json_for_add
  url    = "/api/2.0/asset_groups/#{group_id}/tags"
  uri    = AJAX.post(connection, url, params, AJAX::CONTENT_TYPE::JSON)
  @id    = uri.split('/').last.to_i
end

#add_to_site(connection, site_id) ⇒ Fixnum

Adds a tag to a site

Parameters:

  • connection (Connection)

    Nexpose connection

  • site_id (Fixnum)

    of the site to be tagged

Returns:

  • (Fixnum)

    ID of applied tag



353
354
355
356
357
358
# File 'lib/nexpose/tag.rb', line 353

def add_to_site(connection, site_id)
  params = to_json_for_add
  url    = "/api/2.0/sites/#{site_id}/tags"
  uri    = AJAX.post(connection, url, params, AJAX::CONTENT_TYPE::JSON)
  @id    = uri.split('/').last.to_i
end

#delete(connection) ⇒ Object

Delete this tag from Nexpose console

Parameters:



309
310
311
# File 'lib/nexpose/tag.rb', line 309

def delete(connection)
  connection.delete_tag(@id)
end

#save(connection) ⇒ Fixnum

Creates and saves a tag to Nexpose console

Parameters:

Returns:

  • (Fixnum)

    ID of saved tag



267
268
269
270
271
272
273
274
275
276
# File 'lib/nexpose/tag.rb', line 267

def save(connection)
  params = to_json
  if @id == -1
    uri = AJAX.post(connection, '/api/2.0/tags', params, AJAX::CONTENT_TYPE::JSON)
    @id = uri.split('/').last.to_i
  else
    AJAX.put(connection, "/api/2.0/tags/#{@id}", params, AJAX::CONTENT_TYPE::JSON)
  end
  @id
end

#to_hObject



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/nexpose/tag.rb', line 250

def to_h
  {
    tag_id: id,
    tag_name: name,
    tag_type: type,
    attributes: [
      { tag_attribute_name: 'COLOR', tag_attribute_value: color },
      { tag_attribute_name: 'SOURCE', tag_attribute_value: source }
    ]
  }
end

#to_jsonObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/nexpose/tag.rb', line 289

def to_json
  json = { 'tag_name' => @name,
           'tag_type' => @type,
           'tag_id' => @id,
           'attributes' => [{ 'tag_attribute_name' => 'SOURCE',
                              'tag_attribute_value' => @source }],
           'tag_config' => { 'site_ids' => @site_ids,
                             'tag_associated_asset_ids' => @associated_asset_ids,
                             'asset_group_ids' => @asset_group_ids,
                             'search_criteria' => @search_criteria ? @search_criteria.to_h : nil } }
  if @type == Type::Generic::CUSTOM
    json['attributes'] << { 'tag_attribute_name' => 'COLOR', 'tag_attribute_value' => @color }
  end
  JSON.generate(json)
end