class Nexpose::Tag

Tag object containing tag details

Attributes

asset_group_ids[RW]

Array containing Asset Group IDs to be associated with tag

asset_ids[RW]

Array containing Asset IDs to be associated with tag

associated_asset_ids[RW]

Array containing Asset IDs directly associated with the tag

color[R]

HEX color code of tag

group_ids[RW]

Array containing Asset Group IDs to be associated with tag

group_ids=[RW]

Array containing Asset Group IDs to be associated with tag

risk_modifier[RW]

Risk modifier

search_criteria[RW]

A TagCriteria

site_ids[RW]

Array containing Site IDs to be associated with tag

source[RW]

Creation source

Public Class Methods

create(hash) click to toggle source

Create tag object from hash

# 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) click to toggle source

Retrieve detailed description of a single tag

@param [Connection] connection Nexpose connection @param [Fixnum] tag_id ID of tag to retrieve @return [Tag] requested tag

# 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) click to toggle source

Create list of tag objects from hash

# 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
new(name, type, id = -1) click to toggle source
# 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
parse(json) click to toggle source
# 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

Public Instance Methods

add_to_asset(connection, asset_id) click to toggle source

Adds a tag to an asset

@param [Connection] connection Nexpose connection @param [Fixnum] asset_id of the asset to be tagged @return [Fixnum] ID of applied tag

# 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_asset_group(connection, group_id)
Alias for: add_to_group
add_to_group(connection, group_id) click to toggle source

Adds a tag to an asset group

@param [Connection] connection Nexpose connection @param [Fixnum] group_id id of the asset group to be tagged @return [Fixnum] ID of applied tag

# 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
Also aliased as: add_to_asset_group
add_to_site(connection, site_id) click to toggle source

Adds a tag to a site

@param [Connection] connection Nexpose connection @param [Fixnum] site_id of the site to be tagged @return [Fixnum] ID of applied tag

# 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
color=(hex) click to toggle source

Set the color but validate it

# File lib/nexpose/tag.rb, line 218
def color=(hex)
  valid_colors = Type::Color.constants.map { |c| Type::Color.const_get(c) }
  unless hex.nil? || valid_colors.include?(hex.to_s.downcase)
    raise ArgumentError, "Unable to set color to an invalid color.\nUse one of #{valid_colors}"
  end

  @color = hex
end
delete(connection) click to toggle source

Delete this tag from Nexpose console

@param [Connection] connection Nexpose connection

# File lib/nexpose/tag.rb, line 309
def delete(connection)
  connection.delete_tag(@id)
end
save(connection) click to toggle source

Creates and saves a tag to Nexpose console

@param [Connection] connection Nexpose connection @return [Fixnum] ID of saved tag

# 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_h() click to toggle source
# 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_json() click to toggle source
# 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

Private Instance Methods

to_json_for_add() click to toggle source
# File lib/nexpose/tag.rb, line 376
def to_json_for_add
  if @id == -1
    json = { 'tag_name' => @name,
             'tag_type' => @type,
             'attributes' => [{ 'tag_attribute_name' => 'SOURCE',
                                'tag_attribute_value' => @source }] }
    if @type == Tag::Type::Generic::CUSTOM
      json['attributes'] << { 'tag_attribute_name' => 'COLOR',
                              'tag_attribute_value' => @color }
    end
    params = JSON.generate(json)
  else
    params = JSON.generate('tag_id' => @id)
  end
  params
end