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.



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

def initialize(name, type, id = -1)
  @name, @type, @id = name, type, 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



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

def asset_group_ids
  @asset_group_ids
end

#asset_idsObject

Array containing Asset IDs to be associated with tag



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

def asset_ids
  @asset_ids
end

#associated_asset_idsObject

Array containing Asset IDs directly associated with the tag



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

def associated_asset_ids
  @associated_asset_ids
end

#colorObject

HEX color code of tag



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

def color
  @color
end

#risk_modifierObject

Risk modifier



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

def risk_modifier
  @risk_modifier
end

#search_criteriaObject

A TagCriteria



205
206
207
# File 'lib/nexpose/tag.rb', line 205

def search_criteria
  @search_criteria
end

#site_idsObject

Array containing Site IDs to be associated with tag



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

def site_ids
  @site_ids
end

#sourceObject

Creation source



182
183
184
# File 'lib/nexpose/tag.rb', line 182

def source
  @source
end

Class Method Details

.create(hash) ⇒ Object

Create tag object from hash



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/nexpose/tag.rb', line 234

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



286
287
288
289
# File 'lib/nexpose/tag.rb', line 286

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



224
225
226
227
228
229
230
231
# File 'lib/nexpose/tag.rb', line 224

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

.parse(json) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/nexpose/tag.rb', line 318

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' }
  if modifier
    tag.risk_modifier = modifier['tag_attribute_value'].to_i
  end
  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



347
348
349
350
351
352
# File 'lib/nexpose/tag.rb', line 347

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



373
374
375
376
377
378
# File 'lib/nexpose/tag.rb', line 373

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



360
361
362
363
364
365
# File 'lib/nexpose/tag.rb', line 360

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:



314
315
316
# File 'lib/nexpose/tag.rb', line 314

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



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

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/nexpose/tag.rb', line 246

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



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/nexpose/tag.rb', line 291

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