Class: Nexpose::Ticket

Inherits:
TicketSummary show all
Defined in:
lib/nexpose/ticket.rb

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Attributes inherited from TicketSummary

#asset_id, #assigned_to, #author, #created_on, #id, #name, #priority, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TicketSummary

parse

Constructor Details

#initialize(name, id = nil) ⇒ Ticket

Returns a new instance of Ticket.



133
134
135
136
137
138
139
140
# File 'lib/nexpose/ticket.rb', line 133

def initialize(name, id = nil)
  @id              = id
  @name            = name
  @priority        = Priority::NORMAL
  @vulnerabilities = []
  @comments        = []
  @history         = []
end

Instance Attribute Details

#commentsObject

Array of comments about the ticket.



128
129
130
# File 'lib/nexpose/ticket.rb', line 128

def comments
  @comments
end

#historyObject

History of events on this ticket.



131
132
133
# File 'lib/nexpose/ticket.rb', line 131

def history
  @history
end

#vulnerabilitiesObject

List of vulnerabilities (by ID) this ticket addresses.



125
126
127
# File 'lib/nexpose/ticket.rb', line 125

def vulnerabilities
  @vulnerabilities
end

Class Method Details

.load(connection, id) ⇒ Ticket

Load existing ticket data.

Parameters:

  • connection (Connection)

    Connection to console where ticket exists.

  • id (Fixnum)

    Ticket ID of an existing ticket.

Returns:

  • (Ticket)

    Ticket populated with current state.



170
171
172
173
174
175
176
177
178
# File 'lib/nexpose/ticket.rb', line 170

def self.load(connection, id)
  # TODO: Load multiple tickets in a single request, as supported by API.
  xml = connection.make_xml('TicketDetailsRequest')
  xml.add_element('Ticket', { 'id' => id })
  response = connection.execute(xml, '1.2')
  response.res.elements.each('//TicketInfo') do |info|
    return parse_details(info)
  end
end

.parse_details(xml) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/nexpose/ticket.rb', line 206

def self.parse_details(xml)
  ticket = parse(xml)

  xml.elements.each('Vulnerabilities/Vulnerability') do |vuln|
    ticket.vulnerabilities << vuln.attributes['id']
  end

  xml.elements.each('TicketHistory/Entry') do |entry|
    ticket.history << Event.parse(entry)
  end

  ticket.comments = ticket.history.select { |h| h.description == 'Added comment' }.map(&:comment)

  ticket
end

Instance Method Details

#delete(connection) ⇒ Boolean

Delete this ticket from the system.

Parameters:

  • connection (Connection)

    Connection to console where ticket exists.

Returns:

  • (Boolean)

    Whether the ticket was successfully delete.



160
161
162
# File 'lib/nexpose/ticket.rb', line 160

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

#save(connection) ⇒ Fixnum

Save this ticket to a Nexpose console.

Parameters:

  • connection (Connection)

    Connection to console where ticket exists.

Returns:

  • (Fixnum)

    Unique ticket ID assigned to this ticket.



147
148
149
150
151
152
153
# File 'lib/nexpose/ticket.rb', line 147

def save(connection)
  xml = connection.make_xml('TicketCreateRequest')
  xml.add_element(to_xml)

  response = connection.execute(xml, '1.2')
  @id = response.attributes['id'].to_i if response.success
end

#to_xmlObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/nexpose/ticket.rb', line 180

def to_xml
  xml = REXML::Element.new('TicketCreate')
  xml.add_attributes({ 'name' => @name,
                       'priority' => @priority,
                       'device-id' => @asset_id,
                       'assigned-to' => @assigned_to })

  vuln_xml = REXML::Element.new('Vulnerabilities')
  @vulnerabilities.each do |vuln_id|
    vuln_xml.add_element('Vulnerability', { 'id' => vuln_id.downcase })
  end
  xml.add_element(vuln_xml)

  unless @comments.empty?
    comments_xml = REXML::Element.new('Comments')
    @comments.each do |comment|
      comment_xml = REXML::Element.new('Comment')
      comment_xml.add_text(comment)
      comments_xml.add_element(comment_xml)
    end
    xml.add_element(comments_xml)
  end

  xml
end