class Nexpose::Tag
Tag
object containing tag details
Attributes
Array containing Asset
Group IDs to be associated with tag
Array containing Asset
IDs to be associated with tag
Array containing Asset
IDs directly associated with the tag
HEX color code of tag
Array containing Asset
Group IDs to be associated with tag
Array containing Asset
Group IDs to be associated with tag
Risk modifier
A TagCriteria
Array containing Site
IDs to be associated with tag
Creation source
Public Class Methods
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
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
# 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
# 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
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
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
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
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
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
# 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
# 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
# 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